Skip to content

Commit efb54f2

Browse files
committed
feat(bitgo): add method to fetch and register all the assets from AMS
Ticket: WIN-5839
1 parent 568e5f8 commit efb54f2

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

modules/bitgo/src/bitgo.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,25 @@ export class BitGo extends BitGoAPI {
6868
* @param tokenConfigMap - A map of token metadata from AMS
6969
*/
7070
initCoinFactory(tokenConfigMap: Record<string, TrimmedAmsTokenConfig[]>): void {
71-
// TODO(WIN-5839): use AMS endpoint to fetch config details
7271
const coinMap = createTokenMapUsingTrimmedConfigDetails(tokenConfigMap);
7372
this._coinFactory = new CoinFactory();
7473
registerCoinConstructors(this._coinFactory, coinMap);
7574
}
7675

76+
/**
77+
* Fetch all the tokens and initialize the coin factory
78+
*/
79+
async registerAllTokens(): Promise<void> {
80+
if (!this._useAms) {
81+
throw new Error('registerAllTokens is only supported when useAms is set to true');
82+
}
83+
// Fetch mainnet assets for prod and adminProd environments, testnet assets for all other environments
84+
const assetEnvironment = ['prod', 'adminProd'].includes(this.getEnv()) ? 'mainnet' : 'testnet';
85+
const url = this.url(`/assets/list/${assetEnvironment}`);
86+
const tokenConfigMap = (await this.executeAssetRequest(url)) as Record<string, TrimmedAmsTokenConfig[]>;
87+
this.initCoinFactory(tokenConfigMap);
88+
}
89+
7790
/**
7891
* Create a basecoin object
7992
* @param coinName

modules/bitgo/test/v2/unit/ams/ams.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ describe('Asset metadata service', () => {
4747
coin.tokenContractAddress.should.equal('0x89a959b9184b4f8c8633646d5dfd049d2ebc983a');
4848
});
4949

50+
it('should fetch all assets from AMS and initialize the coin factory', async () => {
51+
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock', microservicesUri, useAms: true } as BitGoOptions);
52+
bitgo.initializeTestVars();
53+
54+
// Setup nocks
55+
nock(microservicesUri).get('/api/v1/assets/list/testnet').reply(200, reducedAmsTokenConfig);
56+
57+
await bitgo.registerAllTokens();
58+
const coin = bitgo.coin('hteth:faketoken');
59+
should.exist(coin);
60+
});
61+
5062
describe('registerToken', () => {
5163
it('should throw an error when useAms is false', async () => {
5264
const bitgo = TestBitGo.decorate(BitGo, { env: 'mock', microservicesUri, useAms: false } as BitGoOptions);

0 commit comments

Comments
 (0)