Skip to content

Commit 0795883

Browse files
authored
feat: create coin factory using ams response
2 parents 62071eb + 6fa3eaf commit 0795883

File tree

5 files changed

+302
-207
lines changed

5 files changed

+302
-207
lines changed

modules/bitgo/src/bitgo.ts

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

9-
import GlobalCoinFactory from './v2/coinFactory';
10-
import { BaseCoin, common } from '@bitgo/sdk-core';
9+
import { BaseCoin, CoinFactory, common } from '@bitgo/sdk-core';
1110
import { BitGoAPI, BitGoAPIOptions } from '@bitgo/sdk-api';
11+
import { createTokenMapUsingTrimmedConfigDetails, TrimmedAmsTokenConfig } from '@bitgo/statics';
12+
import { GlobalCoinFactory, registerCoinConstructors } from './v2/coinFactory';
1213

13-
export type BitGoOptions = BitGoAPIOptions;
14+
// constructor params used exclusively for BitGo class
15+
export type BitGoOptions = BitGoAPIOptions & {
16+
useAms?: boolean;
17+
};
1418

1519
export class BitGo extends BitGoAPI {
20+
private _coinFactory: CoinFactory;
21+
private _useAms: boolean;
1622
/**
1723
* Constructor for BitGo Object
1824
*/
19-
constructor(params: BitGoAPIOptions = {}) {
25+
constructor(params: BitGoOptions = {}) {
2026
super(params);
2127
if (
2228
!common.validateParams(
@@ -34,7 +40,8 @@ export class BitGo extends BitGoAPI {
3440
'stellarFederationServerUrl',
3541
]
3642
) ||
37-
(params.useProduction && !_.isBoolean(params.useProduction))
43+
(params.useProduction && !_.isBoolean(params.useProduction)) ||
44+
(params.useAms && !_.isBoolean(params.useAms))
3845
) {
3946
throw new Error('invalid argument');
4047
}
@@ -43,15 +50,27 @@ export class BitGo extends BitGoAPI {
4350
throw new Error('invalid argument - must provide both client id and secret');
4451
}
4552

53+
this._useAms = !!params.useAms;
4654
this._version = pjson.version;
4755
this._userAgent = params.userAgent || 'BitGoJS/' + this.version();
56+
this._coinFactory = new CoinFactory();
57+
}
58+
59+
initCoinFactory(tokenConfigMap: Record<string, TrimmedAmsTokenConfig[]>): void {
60+
// TODO(WIN-5057): use AMS endpoint to fetch config details
61+
const coinMap = createTokenMapUsingTrimmedConfigDetails(tokenConfigMap);
62+
this._coinFactory = new CoinFactory();
63+
registerCoinConstructors(this._coinFactory, coinMap);
4864
}
4965

5066
/**
5167
* Create a basecoin object
5268
* @param coinName
5369
*/
5470
coin(coinName: string): BaseCoin {
71+
if (this._useAms) {
72+
return this._coinFactory.getInstance(this, coinName);
73+
}
5574
return GlobalCoinFactory.getInstance(this, coinName);
5675
}
5776

modules/bitgo/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as _ from 'lodash';
1010
import { common } from '@bitgo/sdk-core';
1111
export * from '@bitgo/sdk-api';
1212
import * as utxolib from '@bitgo/utxo-lib';
13-
import GlobalCoinFactory from './v2/coinFactory';
13+
import { GlobalCoinFactory } from './v2/coinFactory';
1414

1515
export * from './bitgo';
1616

0 commit comments

Comments
 (0)