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' ;
55import { BaseCoin } from './baseCoin' ;
66import { BitGoBase } from './bitgoBase' ;
77import { UnsupportedCoinError } from './errors' ;
@@ -15,9 +15,11 @@ export interface NamedCoinConstructor {
1515
1616export 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