Skip to content

Commit a5fb7eb

Browse files
authored
feat(statics): add method to create custom coin map
2 parents 336f233 + b5d60d2 commit a5fb7eb

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';
@@ -2862,3 +2869,186 @@ export const coins = CoinMap.fromCoins([
28622869
UnderlyingAsset.SGD
28632870
),
28642871
]);
2872+
2873+
function createToken(
2874+
family: string,
2875+
token: AmsTokenConfig,
2876+
initializerMap: Record<string, unknown>
2877+
): Readonly<BaseCoin> | undefined {
2878+
const initializer = initializerMap[family] as (...args: unknown[]) => Readonly<BaseCoin>;
2879+
if (!initializer) {
2880+
return undefined;
2881+
}
2882+
2883+
const commonArgs = [
2884+
token.id,
2885+
token.name,
2886+
token.fullName,
2887+
token.decimalPlaces,
2888+
token.asset,
2889+
token.features,
2890+
token.prefix,
2891+
token.suffix,
2892+
token.network,
2893+
token.primaryKeyCurve,
2894+
];
2895+
2896+
switch (family) {
2897+
case 'arbeth':
2898+
case 'avax':
2899+
case 'bera':
2900+
case 'bsc':
2901+
case 'celo':
2902+
case 'eth':
2903+
case 'opeth':
2904+
case 'polygon':
2905+
case 'trx':
2906+
return initializer(
2907+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2908+
token.contractAddress || token.tokenAddress, // contractAddress
2909+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2910+
);
2911+
2912+
case 'apt':
2913+
case 'stx':
2914+
return initializer(
2915+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2916+
token.assetId, // assetId
2917+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2918+
);
2919+
2920+
case 'algo':
2921+
return initializer(
2922+
...commonArgs.slice(0, 2), // id, name
2923+
token.alias, // alias
2924+
...commonArgs.slice(2) // fullName, decimal, asset, features, prefix, suffix, network, primaryKeyCurve
2925+
);
2926+
2927+
case 'eos':
2928+
return initializer(
2929+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2930+
token.contractName, // contractName
2931+
token.contractAddress, // contractAddress
2932+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2933+
);
2934+
2935+
case 'hbar':
2936+
return initializer(
2937+
...commonArgs.slice(0, 3), // id, name, fullName
2938+
token.network, // network
2939+
token.decimalPlaces,
2940+
token.asset,
2941+
token.tokenId, // tokenId
2942+
token.contractAddress, // contractAddress
2943+
...commonArgs.slice(5, 8), // features, prefix, suffix
2944+
token.primaryKeyCurve
2945+
);
2946+
2947+
case 'sol':
2948+
return initializer(
2949+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2950+
token.tokenAddress, // tokenAddress
2951+
token.contractAddress, // contractAddress
2952+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2953+
);
2954+
2955+
case 'sui':
2956+
return initializer(
2957+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2958+
token.packageId, // packageId
2959+
token.module, // module
2960+
token.symbol, // symbol
2961+
token.contractAddress, // contractAddress
2962+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2963+
);
2964+
2965+
case 'xlm':
2966+
return initializer(
2967+
...commonArgs.slice(0, 5), // id, name, fullName, decimalPlaces, asset
2968+
token.domain, // domain
2969+
...commonArgs.slice(5) // features, prefix, suffix, network, primaryKeyCurve
2970+
);
2971+
2972+
case 'xrp':
2973+
return initializer(
2974+
...commonArgs.slice(0, 4), // id, name, fullName, decimalPlaces
2975+
token.issuerAddress, // issuerAddress
2976+
token.currecnycode, // currencyCode
2977+
token.contractAddress, // contractAddress
2978+
token.domain, // domain
2979+
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
2980+
);
2981+
2982+
default:
2983+
return undefined;
2984+
}
2985+
}
2986+
2987+
export function createTokenMapUsingConfigDetails(tokenConfigMap: Record<string, AmsTokenConfig[]>): CoinMap {
2988+
const BaseCoins: Map<string, Readonly<BaseCoin>> = new Map();
2989+
const initializerMap: Record<string, unknown> = {
2990+
algo: algoToken,
2991+
apt: aptToken,
2992+
arbeth: arbethErc20,
2993+
avaxc: avaxErc20,
2994+
bera: beraErc20,
2995+
bsc: bscToken,
2996+
celo: celoToken,
2997+
eth: erc20,
2998+
eos: eosToken,
2999+
hbar: hederaToken,
3000+
opeth: opethErc20,
3001+
polygon: polygonErc20,
3002+
sol: solToken,
3003+
stx: sip10Token,
3004+
sui: suiToken,
3005+
trx: tronToken,
3006+
xlm: stellarToken,
3007+
xrp: xrpToken,
3008+
};
3009+
3010+
const nftAndOtherTokens = new Set([
3011+
'erc721:bsctoken',
3012+
'terc721:bsctoken',
3013+
'erc1155:bsctoken',
3014+
'terc1155:bsctoken',
3015+
'erc721:witch',
3016+
'erc721:token',
3017+
'erc1155:token',
3018+
'nonstandard:token',
3019+
'terc721:token',
3020+
'terc1155:token',
3021+
'tnonstandard:token',
3022+
'terc721:bitgoerc721',
3023+
'terc1155:bitgoerc1155',
3024+
'erc721:polygontoken',
3025+
'erc1155:polygontoken',
3026+
'terc721:polygontoken',
3027+
'terc1155:polygontoken',
3028+
]);
3029+
3030+
for (const tokenConfigs of Object.values(tokenConfigMap)) {
3031+
const tokenConfig = tokenConfigs[0];
3032+
const family = tokenConfig.family;
3033+
3034+
if (tokenConfig.isToken && !nftAndOtherTokens.has(tokenConfig.name)) {
3035+
const token = createToken(family, tokenConfig, initializerMap);
3036+
if (token) {
3037+
BaseCoins.set(token.name, token);
3038+
} else if (coins.has(tokenConfig.name)) {
3039+
BaseCoins.set(tokenConfig.name, coins.get(tokenConfig.name));
3040+
}
3041+
} else if (coins.has(tokenConfig.name)) {
3042+
BaseCoins.set(tokenConfig.name, coins.get(tokenConfig.name));
3043+
}
3044+
}
3045+
3046+
// Add keys and values from `coins` that are not already in `BaseCoins`
3047+
coins.forEach((coin, coinName) => {
3048+
if (!BaseCoins.has(coinName)) {
3049+
BaseCoins.set(coinName, coin);
3050+
}
3051+
});
3052+
3053+
return CoinMap.fromCoins(Array.from(BaseCoins.values()));
3054+
}

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)