Skip to content

Commit 549c0bf

Browse files
authored
feat(sdk-core): implement method to add token in coin map and constructor map
2 parents 67ceee4 + 8366309 commit 549c0bf

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

modules/sdk-core/src/bitgo/coinFactory.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @prettier
33
*/
4-
import { coins, BaseCoin as StaticsBaseCoin, CoinNotDefinedError } from '@bitgo/statics';
4+
import { coins, CoinMap, BaseCoin as StaticsBaseCoin, CoinNotDefinedError } from '@bitgo/statics';
55
import { BaseCoin } from './baseCoin';
66
import { BitGoBase } from './bitgoBase';
77
import { UnsupportedCoinError } from './errors';
@@ -15,9 +15,11 @@ export interface NamedCoinConstructor {
1515

1616
export class CoinFactory {
1717
private coinConstructors: Map<string, CoinConstructor>;
18+
private coinMap: CoinMap;
1819

19-
constructor() {
20+
constructor(coinMap: CoinMap = coins) {
2021
this.coinConstructors = new Map();
22+
this.coinMap = coinMap;
2123
}
2224

2325
/**
@@ -40,6 +42,25 @@ export class CoinFactory {
4042
this.coinConstructors.set(name, coin);
4143
}
4244

45+
/**
46+
* Registers a token in the coin map and the constructor map.
47+
* @param staticsCoin The static coin definition from BitGo Statics
48+
* @param coinConstructor The constructor for the coin plugin
49+
* @throws Error
50+
*/
51+
public registerToken(staticsCoin: Readonly<StaticsBaseCoin>, coinConstructor: CoinConstructor): void {
52+
if (
53+
!(
54+
this.coinMap.has(staticsCoin.name) ||
55+
this.coinMap.has(staticsCoin.id) ||
56+
(staticsCoin.alias && this.coinMap.has(staticsCoin.alias))
57+
)
58+
) {
59+
this.coinMap.addCoin(staticsCoin);
60+
}
61+
this.register(staticsCoin.name, coinConstructor);
62+
}
63+
4364
/**
4465
* @param bitgo Instance of BitGo
4566
* @param name Name of coin or address
@@ -50,7 +71,7 @@ export class CoinFactory {
5071
let staticsCoin;
5172

5273
try {
53-
staticsCoin = coins.get(name);
74+
staticsCoin = this.coinMap.get(name);
5475
} catch (e) {
5576
if (!(e instanceof CoinNotDefinedError)) {
5677
throw e;

0 commit comments

Comments
 (0)