|
6 | 6 | import pjson = require('../package.json'); |
7 | 7 | import * as _ from 'lodash'; |
8 | 8 |
|
9 | | -import { BaseCoin, CoinFactory, common } from '@bitgo/sdk-core'; |
| 9 | +import { BaseCoin, CoinFactory, common, UnsupportedCoinError } from '@bitgo/sdk-core'; |
10 | 10 | import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api'; |
11 | 11 | import { |
12 | 12 | createTokenMapUsingTrimmedConfigDetails, |
13 | 13 | TrimmedAmsTokenConfig, |
14 | 14 | createToken, |
15 | 15 | getFormattedTokenConfigForCoin, |
| 16 | + coins, |
| 17 | + BaseCoin as StaticsBaseCoin, |
16 | 18 | } from '@bitgo/statics'; |
17 | | -import { GlobalCoinFactory, registerCoinConstructors, getTokenConstructor } from './v2/coinFactory'; |
| 19 | +import { GlobalCoinFactory, registerCoinConstructors, getTokenConstructor, getCoinConstructor } from './v2/coinFactory'; |
18 | 20 |
|
19 | 21 | // constructor params used exclusively for BitGo class |
20 | 22 | export type BitGoOptions = BitGoAPIOptions & { |
@@ -87,20 +89,45 @@ export class BitGo extends BitGoAPI { |
87 | 89 | * Register a token in the coin factory |
88 | 90 | * @param tokenConfig - The token metadata from AMS |
89 | 91 | */ |
90 | | - registerToken(tokenConfig: TrimmedAmsTokenConfig): void { |
| 92 | + async registerToken(tokenName: string): Promise<void> { |
91 | 93 | if (!this._useAms) { |
92 | 94 | throw new Error('registerToken is only supported when useAms is set to true'); |
93 | 95 | } |
94 | | - // TODO(WIN-5839): use AMS endpoint to fetch token metadata |
95 | | - const staticsBaseCoin = createToken(tokenConfig); |
96 | | - if (staticsBaseCoin) { |
| 96 | + //do not register a coin/token if it's already registered |
| 97 | + if (this._coinFactory.hasCoin(tokenName)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Get the coin/token details only if it's not present in statics library |
| 102 | + let staticsBaseCoin: Readonly<StaticsBaseCoin> | undefined; |
| 103 | + if (coins.has(tokenName)) { |
| 104 | + staticsBaseCoin = coins.get(tokenName); |
| 105 | + } else { |
| 106 | + const url = this.url(`/assets/name/${tokenName}`); |
| 107 | + const tokenConfig = (await this.executeAssetRequest(url)) as TrimmedAmsTokenConfig; |
| 108 | + staticsBaseCoin = createToken(tokenConfig); |
| 109 | + } |
| 110 | + |
| 111 | + if (!staticsBaseCoin) { |
| 112 | + throw new UnsupportedCoinError(tokenName); |
| 113 | + } |
| 114 | + if (staticsBaseCoin.isToken) { |
97 | 115 | const formattedTokenConfig = getFormattedTokenConfigForCoin(staticsBaseCoin); |
98 | | - if (formattedTokenConfig) { |
99 | | - const tokenConstructor = getTokenConstructor(formattedTokenConfig); |
100 | | - if (tokenConstructor) { |
101 | | - this._coinFactory.registerToken(staticsBaseCoin, tokenConstructor); |
102 | | - } |
| 116 | + if (!formattedTokenConfig) { |
| 117 | + throw new UnsupportedCoinError(tokenName); |
| 118 | + } |
| 119 | + |
| 120 | + const tokenConstructor = getTokenConstructor(formattedTokenConfig); |
| 121 | + if (!tokenConstructor) { |
| 122 | + throw new UnsupportedCoinError(tokenName); |
| 123 | + } |
| 124 | + this._coinFactory.registerToken(staticsBaseCoin, tokenConstructor); |
| 125 | + } else { |
| 126 | + const coinConstructor = getCoinConstructor(tokenName); |
| 127 | + if (!coinConstructor) { |
| 128 | + throw new UnsupportedCoinError(tokenName); |
103 | 129 | } |
| 130 | + this._coinFactory.registerToken(staticsBaseCoin, coinConstructor); |
104 | 131 | } |
105 | 132 | } |
106 | 133 |
|
|
0 commit comments