Skip to content

Commit 7e3b3f8

Browse files
committed
feat(statics): add method to insert single token in coin map
Ticket: WIN-5359
1 parent b475bdd commit 7e3b3f8

File tree

3 files changed

+73
-68
lines changed

3 files changed

+73
-68
lines changed

modules/statics/src/coins.ts

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,11 +3154,29 @@ export const coins = CoinMap.fromCoins([
31543154
),
31553155
]);
31563156

3157-
function createToken(
3158-
family: string,
3159-
token: AmsTokenConfig,
3160-
initializerMap: Record<string, unknown>
3161-
): Readonly<BaseCoin> | undefined {
3157+
export function createToken(token: AmsTokenConfig): Readonly<BaseCoin> | undefined {
3158+
const initializerMap: Record<string, unknown> = {
3159+
algo: algoToken,
3160+
apt: aptToken,
3161+
arbeth: arbethErc20,
3162+
avaxc: avaxErc20,
3163+
bera: beraErc20,
3164+
bsc: bscToken,
3165+
celo: celoToken,
3166+
eth: erc20,
3167+
eos: eosToken,
3168+
hbar: hederaToken,
3169+
opeth: opethErc20,
3170+
polygon: polygonErc20,
3171+
sol: solToken,
3172+
stx: sip10Token,
3173+
sui: suiToken,
3174+
trx: tronToken,
3175+
xlm: stellarToken,
3176+
xrp: xrpToken,
3177+
};
3178+
3179+
const family = token.family;
31623180
const initializer = initializerMap[family] as (...args: unknown[]) => Readonly<BaseCoin>;
31633181
if (!initializer) {
31643182
return undefined;
@@ -3290,47 +3308,12 @@ function getAptTokenInitializer(token: AmsTokenConfig) {
32903308
};
32913309
}
32923310

3293-
export function isCoinPresentInCoinMap({
3294-
name,
3295-
id,
3296-
contractAddress,
3297-
alias,
3298-
}: {
3299-
name: string;
3300-
id?: string;
3301-
contractAddress?: string;
3302-
alias?: string;
3303-
}): boolean {
3304-
return Boolean(
3305-
coins.has(name) ||
3306-
(id && coins.has(id)) ||
3307-
(contractAddress && coins.has(contractAddress)) ||
3308-
(alias && coins.has(alias))
3309-
);
3311+
export function isCoinPresentInCoinMap({ name, id, alias }: { name: string; id?: string; alias?: string }): boolean {
3312+
return Boolean(coins.has(name) || (id && coins.has(id)) || (alias && coins.has(alias)));
33103313
}
33113314

33123315
export function createTokenMapUsingConfigDetails(tokenConfigMap: Record<string, AmsTokenConfig[]>): CoinMap {
33133316
const BaseCoins: Map<string, Readonly<BaseCoin>> = new Map();
3314-
const initializerMap: Record<string, unknown> = {
3315-
algo: algoToken,
3316-
apt: aptToken,
3317-
arbeth: arbethErc20,
3318-
avaxc: avaxErc20,
3319-
bera: beraErc20,
3320-
bsc: bscToken,
3321-
celo: celoToken,
3322-
eth: erc20,
3323-
eos: eosToken,
3324-
hbar: hederaToken,
3325-
opeth: opethErc20,
3326-
polygon: polygonErc20,
3327-
sol: solToken,
3328-
stx: sip10Token,
3329-
sui: suiToken,
3330-
trx: tronToken,
3331-
xlm: stellarToken,
3332-
xrp: xrpToken,
3333-
};
33343317

33353318
const nftAndOtherTokens = new Set([
33363319
'erc721:bsctoken',
@@ -3360,14 +3343,13 @@ export function createTokenMapUsingConfigDetails(tokenConfigMap: Record<string,
33603343
// add the tokens not present in the static coin map
33613344
for (const tokenConfigs of Object.values(tokenConfigMap)) {
33623345
const tokenConfig = tokenConfigs[0];
3363-
const family = tokenConfig.family;
33643346

33653347
if (
33663348
!isCoinPresentInCoinMap({ ...tokenConfig }) &&
33673349
tokenConfig.isToken &&
33683350
!nftAndOtherTokens.has(tokenConfig.name)
33693351
) {
3370-
const token = createToken(family, tokenConfig, initializerMap);
3352+
const token = createToken(tokenConfig);
33713353
if (token) {
33723354
BaseCoins.set(token.name, token);
33733355
}

modules/statics/src/map.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,37 @@ export class CoinMap {
1818
}
1919

2020
static fromCoins(coins: Readonly<BaseCoin>[]): CoinMap {
21-
return coins.reduce((coinMap, coin) => {
22-
if (coinMap.has(coin.name)) {
23-
throw new DuplicateCoinDefinitionError(coin.name);
24-
}
25-
coinMap._map.set(coin.name, coin);
21+
const coinMap = new CoinMap();
22+
coins.forEach((coin) => {
23+
coinMap.addCoin(coin);
24+
});
25+
return coinMap;
26+
}
2627

27-
if (coinMap._coinByIds.has(coin.id)) {
28-
throw new DuplicateCoinIdDefinitionError(coin.id);
29-
}
30-
coinMap._coinByIds.set(coin.id, coin);
28+
public addCoin(coin: Readonly<BaseCoin>): void {
29+
if (this._map.has(coin.name)) {
30+
throw new DuplicateCoinDefinitionError(coin.name);
31+
}
32+
if (this._coinByIds.has(coin.id)) {
33+
throw new DuplicateCoinIdDefinitionError(coin.id);
34+
}
35+
const alias = coin.alias;
36+
if (alias && this._coinByAliases.has(alias)) {
37+
throw new DuplicateCoinDefinitionError(alias);
38+
}
39+
this._map.set(coin.name, coin);
40+
this._coinByIds.set(coin.id, coin);
41+
if (alias) {
42+
this._coinByAliases.set(coin.alias, coin);
43+
}
3144

32-
const alias = coin.alias;
33-
if (alias) {
34-
if (coinMap.has(alias)) {
35-
throw new DuplicateCoinDefinitionError(alias);
36-
}
37-
coinMap._coinByAliases.set(alias, coin);
45+
if (coin.isToken) {
46+
if (coin instanceof ContractAddressDefinedToken) {
47+
this._coinByContractAddress.set(`${coin.family}:${coin.contractAddress}`, coin);
48+
} else if (coin instanceof NFTCollectionIdDefinedToken) {
49+
this._coinByNftCollectionID.set(`${coin.prefix}${coin.family}:${coin.nftCollectionId}`, coin);
3850
}
39-
if (coin.isToken) {
40-
if (coin instanceof ContractAddressDefinedToken) {
41-
coinMap._coinByContractAddress.set(`${coin.family}:${coin.contractAddress}`, coin);
42-
} else if (coin instanceof NFTCollectionIdDefinedToken) {
43-
coinMap._coinByNftCollectionID.set(`${coin.prefix}${coin.family}:${coin.nftCollectionId}`, coin);
44-
}
45-
}
46-
return coinMap;
47-
}, new CoinMap());
51+
}
4852
}
4953

5054
static coinNameFromChainId(chainId: number): string {

modules/statics/test/unit/coins.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
getFormattedTokens,
2222
createTokenMapUsingConfigDetails,
2323
createTokenMapUsingTrimmedConfigDetails,
24+
createToken,
2425
} from '../../src';
2526
import { utxo } from '../../src/utxo';
2627
import { expectedColdFeatures } from './fixtures/expectedColdFeatures';
@@ -649,6 +650,16 @@ describe('CoinMap', function () {
649650
const nftCollectionStatics = coins.get('tapt:0xbbc561fbfa5d105efd8dfb06ae3e7e5be46331165b99d518f094c701e40603b5');
650651
nftCollectionStatics.name.should.eql('tapt:nftcollection1');
651652
});
653+
654+
it('should add single coin/token into the coin map', () => {
655+
const coinMap = CoinMap.fromCoins([]);
656+
const coin = coins.get('btc');
657+
const token = coins.get('usdc');
658+
coinMap.addCoin(coin);
659+
coinMap.has(coin.name).should.be.true();
660+
coinMap.addCoin(token);
661+
coinMap.has(token.name).should.be.true();
662+
});
652663
});
653664

654665
coins.forEach((coin, coinName) => {
@@ -979,4 +990,12 @@ describe('create token map using config details', () => {
979990
tokenRest1.should.deepEqual(tokenRest2);
980991
JSON.stringify(tokenNetwork1).should.eql(JSON.stringify(tokenNetwork2));
981992
});
993+
it('should be able to add single ams token into coin map', () => {
994+
const coinMap = CoinMap.fromCoins([]);
995+
const staticsCoin = createToken(amsTokenConfigWithCustomToken['hteth:faketoken'][0]);
996+
if (staticsCoin) {
997+
coinMap.addCoin(staticsCoin);
998+
}
999+
coinMap.has('hteth:faketoken').should.be.true();
1000+
});
9821001
});

0 commit comments

Comments
 (0)