Skip to content

Commit 568e5f8

Browse files
committed
feat(bitgo): use AMS endpoints to register single token
Ticket: WIN-5839
1 parent 3ad0aba commit 568e5f8

File tree

3 files changed

+395
-11
lines changed

3 files changed

+395
-11
lines changed

modules/bitgo/src/bitgo.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
import pjson = require('../package.json');
77
import * as _ from 'lodash';
88

9-
import { BaseCoin, CoinFactory, common } from '@bitgo/sdk-core';
9+
import { BaseCoin, CoinFactory, common, UnsupportedCoinError } from '@bitgo/sdk-core';
1010
import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api';
1111
import {
1212
createTokenMapUsingTrimmedConfigDetails,
1313
TrimmedAmsTokenConfig,
1414
createToken,
1515
getFormattedTokenConfigForCoin,
16+
coins,
17+
BaseCoin as StaticsBaseCoin,
1618
} from '@bitgo/statics';
17-
import { GlobalCoinFactory, registerCoinConstructors, getTokenConstructor } from './v2/coinFactory';
19+
import { GlobalCoinFactory, registerCoinConstructors, getTokenConstructor, getCoinConstructor } from './v2/coinFactory';
1820

1921
// constructor params used exclusively for BitGo class
2022
export type BitGoOptions = BitGoAPIOptions & {
@@ -87,20 +89,45 @@ export class BitGo extends BitGoAPI {
8789
* Register a token in the coin factory
8890
* @param tokenConfig - The token metadata from AMS
8991
*/
90-
registerToken(tokenConfig: TrimmedAmsTokenConfig): void {
92+
async registerToken(tokenName: string): Promise<void> {
9193
if (!this._useAms) {
9294
throw new Error('registerToken is only supported when useAms is set to true');
9395
}
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) {
97115
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);
103129
}
130+
this._coinFactory.registerToken(staticsBaseCoin, coinConstructor);
104131
}
105132
}
106133

0 commit comments

Comments
 (0)