Skip to content

Commit 7f0604a

Browse files
Merge pull request #6523 from BitGo/COIN-4915-sdk-add-token-support-skeleton
feat: add cosmos chain token standard support
2 parents 11de7f2 + b83d76d commit 7f0604a

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { BitGoBase, CoinConstructor, NamedCoinConstructor } from '@bitgo/sdk-core';
2+
import { CosmosTokenConfig, coins, tokens } from '@bitgo/statics';
3+
import { CosmosCoin } from './cosmosCoin';
4+
5+
export class CosmosToken extends CosmosCoin {
6+
public readonly tokenConfig: CosmosTokenConfig;
7+
8+
constructor(bitgo: BitGoBase, tokenConfig: CosmosTokenConfig) {
9+
const staticsCoin = coins.get(tokenConfig.coin);
10+
super(bitgo, staticsCoin);
11+
this.tokenConfig = tokenConfig;
12+
}
13+
14+
static createTokenConstructor(config: CosmosTokenConfig): CoinConstructor {
15+
return (bitgo: BitGoBase) => new CosmosToken(bitgo, config);
16+
}
17+
18+
static createTokenConstructors(): NamedCoinConstructor[] {
19+
const tokensCtors: NamedCoinConstructor[] = [];
20+
for (const token of [...tokens.bitcoin.cosmos.tokens, ...tokens.testnet.cosmos.tokens]) {
21+
const tokenConstructor = CosmosToken.createTokenConstructor(token);
22+
tokensCtors.push({ name: token.type, coinConstructor: tokenConstructor });
23+
}
24+
return tokensCtors;
25+
}
26+
27+
get name(): string {
28+
return this.tokenConfig.name;
29+
}
30+
31+
get coin(): string {
32+
return this.tokenConfig.coin;
33+
}
34+
35+
get network(): string {
36+
return this.tokenConfig.network;
37+
}
38+
39+
get denom(): string {
40+
return this.tokenConfig.denom;
41+
}
42+
43+
get decimalPlaces(): number {
44+
return this.tokenConfig.decimalPlaces;
45+
}
46+
47+
getChain(): string {
48+
return this.tokenConfig.type;
49+
}
50+
51+
getBaseChain(): string {
52+
return this.coin;
53+
}
54+
55+
getFullName(): string {
56+
// Eg - returns "Atom Token", "Osmo Token"
57+
return `${this.tokenConfig.coin.charAt(0).toUpperCase()}${this.tokenConfig.coin.slice(1)} Token`;
58+
}
59+
60+
getBaseFactor(): number {
61+
return Math.pow(10, this.tokenConfig.decimalPlaces);
62+
}
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './lib';
22
export { CosmosCoin } from './cosmosCoin';
3+
export { CosmosToken } from './cosmosToken';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BitGoBase } from '@bitgo/sdk-core';
2+
import { CosmosToken } from './cosmosToken';
3+
4+
export const register = (sdk: BitGoBase): void => {
5+
CosmosToken.createTokenConstructors().forEach(({ name, coinConstructor }) => {
6+
sdk.register(name, coinConstructor);
7+
});
8+
};

modules/statics/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
Sip10Token,
2828
Nep141Token,
2929
VetToken,
30+
CosmosChainToken,
3031
} from './account';
3132
export { CoinMap } from './map';
3233
export { networkFeatureMapForTokens } from './networkFeatureMapForTokens';

modules/statics/src/tokenConfig.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
WorldERC20Token,
2727
XrpCoin,
2828
ZkethERC20Token,
29+
CosmosChainToken,
2930
} from './account';
3031
import { CoinFamily, CoinKind, BaseCoin } from './base';
3132
import { coins } from './coins';
@@ -118,6 +119,10 @@ export type VetTokenConfig = BaseNetworkConfig & {
118119
contractAddress: string;
119120
};
120121

122+
export type CosmosTokenConfig = BaseNetworkConfig & {
123+
denom: string;
124+
};
125+
121126
export type TokenConfig =
122127
| Erc20TokenConfig
123128
| StellarTokenConfig
@@ -219,6 +224,9 @@ export interface Tokens {
219224
vet: {
220225
tokens: VetTokenConfig[];
221226
};
227+
cosmos: {
228+
tokens: CosmosTokenConfig[];
229+
};
222230
};
223231
testnet: {
224232
eth: {
@@ -300,6 +308,9 @@ export interface Tokens {
300308
vet: {
301309
tokens: VetTokenConfig[];
302310
};
311+
cosmos: {
312+
tokens: CosmosTokenConfig[];
313+
};
303314
};
304315
}
305316

@@ -878,6 +889,25 @@ const getFormattedVetTokens = (customCoinMap = coins) =>
878889
return acc;
879890
}, []);
880891

892+
const getFormattedCosmosChainTokens = (customCoinMap = coins) =>
893+
customCoinMap.reduce((acc: CosmosTokenConfig[], coin) => {
894+
if (coin instanceof CosmosChainToken) {
895+
acc.push(getCosmosTokenConfig(coin));
896+
}
897+
return acc;
898+
}, []);
899+
900+
function getCosmosTokenConfig(coin: CosmosChainToken): CosmosTokenConfig {
901+
return {
902+
type: coin.name,
903+
coin: coin.name.split(':')[0].toLowerCase(),
904+
network: coin.network.type === NetworkType.MAINNET ? 'Mainnet' : 'Testnet',
905+
name: coin.fullName,
906+
denom: coin.denom,
907+
decimalPlaces: coin.decimalPlaces,
908+
};
909+
}
910+
881911
export const getFormattedTokens = (coinMap = coins): Tokens => {
882912
const formattedAptNFTCollections = getFormattedAptNFTCollections(coinMap);
883913
return {
@@ -965,6 +995,9 @@ export const getFormattedTokens = (coinMap = coins): Tokens => {
965995
vet: {
966996
tokens: getFormattedVetTokens(coinMap).filter((token) => token.network === 'Mainnet'),
967997
},
998+
cosmos: {
999+
tokens: getFormattedCosmosChainTokens(coinMap).filter((token) => token.network === 'Mainnet'),
1000+
},
9681001
},
9691002
testnet: {
9701003
eth: {
@@ -1050,6 +1083,9 @@ export const getFormattedTokens = (coinMap = coins): Tokens => {
10501083
vet: {
10511084
tokens: getFormattedVetTokens(coinMap).filter((token) => token.network === 'Testnet'),
10521085
},
1086+
cosmos: {
1087+
tokens: getFormattedCosmosChainTokens(coinMap).filter((token) => token.network === 'Testnet'),
1088+
},
10531089
},
10541090
};
10551091
};

0 commit comments

Comments
 (0)