Skip to content

Commit b5d60d2

Browse files
committed
feat(statics): add method to create custom coin map
Ticket: WIN-4944
1 parent 625e805 commit b5d60d2

File tree

4 files changed

+841
-1
lines changed

4 files changed

+841
-1
lines changed

modules/statics/src/coins.ts

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import {
33
AccountCoin,
44
algoToken,
55
arbethErc20,
6+
avaxErc20,
67
beraErc20,
8+
bscToken,
79
celoToken,
810
eosToken,
911
erc1155,
12+
erc20,
1013
erc20CompatibleAccountCoin,
1114
erc721,
1215
fiat,
@@ -15,6 +18,9 @@ import {
1518
hederaToken,
1619
nonstandardToken,
1720
opethErc20,
21+
polygonErc20,
22+
sip10Token,
23+
solToken,
1824
stellarToken,
1925
suiToken,
2026
aptToken,
@@ -38,7 +44,8 @@ import {
3844
} from './account';
3945
import { ada } from './ada';
4046
import { avaxp } from './avaxp';
41-
import { BaseUnit, CoinFeature, KeyCurve, UnderlyingAsset } from './base';
47+
import { BaseUnit, CoinFeature, KeyCurve, UnderlyingAsset, BaseCoin } from './base';
48+
import { AmsTokenConfig } from './tokenConfig';
4249
import { erc20Coins } from './coins/erc20Coins';
4350
import { avaxTokens } from './coins/avaxTokens';
4451
import { bscTokens } from './coins/bscTokens';
@@ -2854,3 +2861,186 @@ export const coins = CoinMap.fromCoins([
28542861
UnderlyingAsset.SGD
28552862
),
28562863
]);
2864+
2865+
function createToken(
2866+
family: string,
2867+
token: AmsTokenConfig,
2868+
initializerMap: Record<string, unknown>
2869+
): Readonly<BaseCoin> | undefined {
2870+
const initializer = initializerMap[family] as (...args: unknown[]) => Readonly<BaseCoin>;
2871+
if (!initializer) {
2872+
return undefined;
2873+
}
2874+
2875+
const commonArgs = [
2876+
token.id,
2877+
token.name,
2878+
token.fullName,
2879+
token.decimalPlaces,
2880+
token.asset,
2881+
token.features,
2882+
token.prefix,
2883+
token.suffix,
2884+
token.network,
2885+
token.primaryKeyCurve,
2886+
];
2887+
2888+
switch (family) {
2889+
case 'arbeth':
2890+
case 'avax':
2891+
case 'bera':
2892+
case 'bsc':
2893+
case 'celo':
2894+
case 'eth':
2895+
case 'opeth':
2896+
case 'polygon':
2897+
case 'trx':
2898+
return initializer(
2899+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2900+
token.contractAddress || token.tokenAddress, // contractAddress
2901+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2902+
);
2903+
2904+
case 'apt':
2905+
case 'stx':
2906+
return initializer(
2907+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2908+
token.assetId, // assetId
2909+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2910+
);
2911+
2912+
case 'algo':
2913+
return initializer(
2914+
...commonArgs.slice(0, 2), // id, name
2915+
token.alias, // alias
2916+
...commonArgs.slice(2) // fullName, decimal, asset, features, prefix, suffix, network, primaryKeyCurve
2917+
);
2918+
2919+
case 'eos':
2920+
return initializer(
2921+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2922+
token.contractName, // contractName
2923+
token.contractAddress, // contractAddress
2924+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2925+
);
2926+
2927+
case 'hbar':
2928+
return initializer(
2929+
...commonArgs.slice(0, 3), // id, name, fullName
2930+
token.network, // network
2931+
token.decimalPlaces,
2932+
token.asset,
2933+
token.tokenId, // tokenId
2934+
token.contractAddress, // contractAddress
2935+
...commonArgs.slice(5, 8), // features, prefix, suffix
2936+
token.primaryKeyCurve
2937+
);
2938+
2939+
case 'sol':
2940+
return initializer(
2941+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2942+
token.tokenAddress, // tokenAddress
2943+
token.contractAddress, // contractAddress
2944+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2945+
);
2946+
2947+
case 'sui':
2948+
return initializer(
2949+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2950+
token.packageId, // packageId
2951+
token.module, // module
2952+
token.symbol, // symbol
2953+
token.contractAddress, // contractAddress
2954+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2955+
);
2956+
2957+
case 'xlm':
2958+
return initializer(
2959+
...commonArgs.slice(0, 5), // id, name, fullName, decimalPlaces, asset
2960+
token.domain, // domain
2961+
...commonArgs.slice(5) // features, prefix, suffix, network, primaryKeyCurve
2962+
);
2963+
2964+
case 'xrp':
2965+
return initializer(
2966+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2967+
token.issuerAddress, // issuerAddress
2968+
token.currecnycode, // currencyCode
2969+
token.contractAddress, // contractAddress
2970+
token.domain, // domain
2971+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2972+
);
2973+
2974+
default:
2975+
return undefined;
2976+
}
2977+
}
2978+
2979+
export function createTokenMapUsingConfigDetails(tokenConfigMap: Record<string, AmsTokenConfig[]>): CoinMap {
2980+
const BaseCoins: Map<string, Readonly<BaseCoin>> = new Map();
2981+
const initializerMap: Record<string, unknown> = {
2982+
algo: algoToken,
2983+
apt: aptToken,
2984+
arbeth: arbethErc20,
2985+
avaxc: avaxErc20,
2986+
bera: beraErc20,
2987+
bsc: bscToken,
2988+
celo: celoToken,
2989+
eth: erc20,
2990+
eos: eosToken,
2991+
hbar: hederaToken,
2992+
opeth: opethErc20,
2993+
polygon: polygonErc20,
2994+
sol: solToken,
2995+
stx: sip10Token,
2996+
sui: suiToken,
2997+
trx: tronToken,
2998+
xlm: stellarToken,
2999+
xrp: xrpToken,
3000+
};
3001+
3002+
const nftAndOtherTokens = new Set([
3003+
'erc721:bsctoken',
3004+
'terc721:bsctoken',
3005+
'erc1155:bsctoken',
3006+
'terc1155:bsctoken',
3007+
'erc721:witch',
3008+
'erc721:token',
3009+
'erc1155:token',
3010+
'nonstandard:token',
3011+
'terc721:token',
3012+
'terc1155:token',
3013+
'tnonstandard:token',
3014+
'terc721:bitgoerc721',
3015+
'terc1155:bitgoerc1155',
3016+
'erc721:polygontoken',
3017+
'erc1155:polygontoken',
3018+
'terc721:polygontoken',
3019+
'terc1155:polygontoken',
3020+
]);
3021+
3022+
for (const tokenConfigs of Object.values(tokenConfigMap)) {
3023+
const tokenConfig = tokenConfigs[0];
3024+
const family = tokenConfig.family;
3025+
3026+
if (tokenConfig.isToken && !nftAndOtherTokens.has(tokenConfig.name)) {
3027+
const token = createToken(family, tokenConfig, initializerMap);
3028+
if (token) {
3029+
BaseCoins.set(token.name, token);
3030+
} else if (coins.has(tokenConfig.name)) {
3031+
BaseCoins.set(tokenConfig.name, coins.get(tokenConfig.name));
3032+
}
3033+
} else if (coins.has(tokenConfig.name)) {
3034+
BaseCoins.set(tokenConfig.name, coins.get(tokenConfig.name));
3035+
}
3036+
}
3037+
3038+
// Add keys and values from `coins` that are not already in `BaseCoins`
3039+
coins.forEach((coin, coinName) => {
3040+
if (!BaseCoins.has(coinName)) {
3041+
BaseCoins.set(coinName, coin);
3042+
}
3043+
});
3044+
3045+
return CoinMap.fromCoins(Array.from(BaseCoins.values()));
3046+
}

modules/statics/src/tokenConfig.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ export interface Tokens {
233233
};
234234
}
235235

236+
export interface AmsTokenConfig {
237+
id: string;
238+
name: string;
239+
fullName: string;
240+
family: string;
241+
decimalPlaces: number;
242+
asset: string;
243+
features?: string[];
244+
prefix?: string;
245+
suffix?: string;
246+
network?: unknown;
247+
primaryKeyCurve?: string;
248+
contractAddress?: string;
249+
tokenAddress?: string;
250+
alias?: string;
251+
contractName?: string;
252+
tokenId?: string;
253+
packageId?: string;
254+
module?: string;
255+
symbol?: string;
256+
issuerAddress?: string;
257+
currecnycode?: string;
258+
domain?: string;
259+
assetId?: string;
260+
isToken: boolean;
261+
}
262+
236263
// Get the list of ERC-20 tokens from statics and format it properly
237264
const formattedErc20Tokens = coins.reduce((acc: Erc20TokenConfig[], coin) => {
238265
if (coin instanceof Erc20Coin) {

modules/statics/test/unit/coins.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import {
1717
EosCoin,
1818
SolCoin,
1919
XrpCoin,
20+
createTokenMapUsingConfigDetails,
2021
} from '../../src';
2122
import { utxo } from '../../src/utxo';
2223
import { expectedColdFeatures } from './fixtures/expectedColdFeatures';
24+
import { amsTokenConfig } from './resources/amsTokenConfig';
2325

2426
interface DuplicateCoinObject {
2527
name: string;
@@ -905,3 +907,17 @@ describe('Eip1559 coins', () => {
905907
});
906908
});
907909
});
910+
911+
describe('create token map using config details', () => {
912+
it('should create a valid token map from AmsTokenConfig', () => {
913+
const tokenMap = createTokenMapUsingConfigDetails(amsTokenConfig);
914+
Object.keys(amsTokenConfig).forEach((tokenName) => {
915+
const token = tokenMap.get(tokenName);
916+
const tokenFromStaticCoinMap = coins.get(tokenName);
917+
const { network: tokenNetwork, ...tokenRest } = token;
918+
const { network: staticNetwork, ...staticRest } = tokenFromStaticCoinMap;
919+
tokenRest.should.deepEqual(staticRest);
920+
JSON.stringify(tokenNetwork).should.eql(JSON.stringify(staticNetwork));
921+
});
922+
});
923+
});

0 commit comments

Comments
 (0)