Skip to content

feat: add ofc tokens from ams response to coin map #6125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modules/statics/src/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
xrpToken,
zkethErc20,
} from './account';
import { ofcToken } from './ofc';
import { ada } from './ada';
import { avaxp } from './avaxp';
import { BaseCoin, BaseUnit, CoinFeature, KeyCurve, UnderlyingAsset } from './base';
Expand Down Expand Up @@ -3337,6 +3338,7 @@ export function createToken(token: AmsTokenConfig): Readonly<BaseCoin> | undefin
trx: tronToken,
xlm: stellarToken,
xrp: xrpToken,
ofc: ofcToken,
};

const family = token.family;
Expand Down Expand Up @@ -3450,7 +3452,13 @@ export function createToken(token: AmsTokenConfig): Readonly<BaseCoin> | undefin
token.domain, // domain
...commonArgs.slice(4) // asset, features, prefix, suffix, network, primaryKeyCurve
);

case 'ofc':
return initializer(
...commonArgs, // id, name, fullName, decimalPlaces, asset, prefix, suffix, network, primaryKeyCurve
token.baseUnit, // baseUnit
token.isToken, // isToken
token.kind // kind
);
default:
return undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions modules/statics/src/networkFeatureMapForTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
STX_TOKEN_FEATURES,
SUI_TOKEN_FEATURES,
} from './coinFeatures';
import { OfcCoin } from './ofc';

export const networkFeatureMapForTokens: Partial<Record<CoinFamily, CoinFeature[]>> = {
algo: AccountCoin.DEFAULT_FEATURES,
Expand All @@ -28,4 +29,5 @@ export const networkFeatureMapForTokens: Partial<Record<CoinFamily, CoinFeature[
trx: AccountCoin.DEFAULT_FEATURES,
xlm: AccountCoin.DEFAULT_FEATURES,
xrp: AccountCoin.DEFAULT_FEATURES,
ofc: OfcCoin.DEFAULT_FEATURES,
};
52 changes: 52 additions & 0 deletions modules/statics/src/ofc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,58 @@ export class OfcCoin extends BaseCoin {
}
}

/**
* Function to convert AMS inputs into OFC coin instance.
*
* @param id uuid v4
* @param name unique identifier of the coin
* @param fullName complete human-readable name of the coin
* @param decimalPlaces Number of decimal places this coin supports (divisibility exponent)
* @param asset Asset which this coin represents. This is the same for both mainnet and testnet variants of a coin.
* @param features Features of this coin. Defaults to the DEFAULT_FEATURES defined in `OfcCoin`
* @param prefix Optional coin prefix. Defaults to empty string
* @param suffix Optional coin suffix. Defaults to coin name.
* @param network Network object for this coin
* @param primaryKeyCurve The elliptic curve for this chain/token
* @param baseUnit base unit of the token
* @param isToken Whether or not this account coin is a token of another coin
* @param kind Differentiates coins which represent fiat assets from those which represent crypto assets
* @returns {Readonly<OfcCoin>} OFC coin instance
*/
export function ofcToken(
id: string,
name: string,
fullName: string,
decimalPlaces: number,
asset: UnderlyingAsset,
features: CoinFeature[] = OfcCoin.DEFAULT_FEATURES,
prefix = '',
suffix: string = name.replace(/^ofc/, '').toUpperCase(),
network: OfcNetwork = Networks.main.ofc,
primaryKeyCurve: KeyCurve = KeyCurve.Secp256k1,
baseUnit: BaseUnit = BaseUnit.OFC,
isToken = true,
kind: CoinKind = CoinKind.CRYPTO
): Readonly<OfcCoin> {
return Object.freeze(
new OfcCoin({
id,
name,
fullName,
network,
prefix,
suffix,
features,
decimalPlaces,
isToken,
asset,
kind,
primaryKeyCurve,
baseUnit,
})
);
}

/**
* Factory function for ofc coin instances.
*
Expand Down
2 changes: 2 additions & 0 deletions modules/statics/src/tokenConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ export interface AmsTokenConfig {
domain?: string;
assetId?: string;
isToken: boolean;
baseUnit?: string;
kind?: string;
}

export interface TrimmedAmsNetworkConfig {
Expand Down
7 changes: 7 additions & 0 deletions modules/statics/test/unit/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ describe('create token map using config details', () => {
formattedTokens.bitcoin.should.deepEqual(tokens.bitcoin);
formattedTokens.testnet.eth.should.not.deepEqual(tokens.testnet.eth);
formattedTokens.testnet.eth.tokens.some((token) => token.type === 'hteth:faketoken').should.eql(true);
formattedTokens.testnet.ofc.tokens.some((token) => token.type === 'ofcterc2').should.eql(true);
});
it('should not create an base coin object in coin map for token with unsupported network', () => {
const tokenMap = createTokenMapUsingTrimmedConfigDetails(amsTokenWithUnsupportedNetwork);
Expand All @@ -1061,12 +1062,18 @@ describe('create token map using config details', () => {
it('should create a coin map using reduced token config details', () => {
const coinMap1 = createTokenMapUsingTrimmedConfigDetails(reducedAmsTokenConfig);
const amsToken1 = coinMap1.get('hteth:faketoken');
const amsOfcToken1 = coinMap1.get('ofcterc2');
const coinMap2 = createTokenMapUsingConfigDetails(amsTokenConfigWithCustomToken);
const amsToken2 = coinMap2.get('hteth:faketoken');
const amsOfcToken2 = coinMap2.get('ofcterc2');
const { network: tokenNetwork1, ...tokenRest1 } = amsToken1;
const { network: tokenNetwork2, ...tokenRest2 } = amsToken2;
const { network: tokenNetwork3, ...tokenRest3 } = amsOfcToken1;
const { network: tokenNetwork4, ...tokenRest4 } = amsOfcToken2;
tokenRest1.should.deepEqual(tokenRest2);
tokenRest3.should.deepEqual(tokenRest4);
JSON.stringify(tokenNetwork1).should.eql(JSON.stringify(tokenNetwork2));
JSON.stringify(tokenNetwork3).should.eql(JSON.stringify(tokenNetwork4));
});
it('should be able to add single ams token into coin map', () => {
const coinMap = CoinMap.fromCoins([]);
Expand Down
54 changes: 54 additions & 0 deletions modules/statics/test/unit/resources/amsTokenConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,37 @@ export const amsTokenConfigWithCustomToken = {
contractAddress: '0x89a959b9184b4f8c8633646d5dfd049d2ebc983a',
},
],
ofcterc2: [
{
id: '055ebe86-72cc-4f0e-b46f-c517d8e36871',
fullName: 'Test ERC Token2',
name: 'ofcterc2',
prefix: '',
suffix: 'TERC',
baseUnit: 'wei',
kind: 'crypto',
family: 'ofc',
isToken: true,
features: [
'account-model',
'requires-big-number',
'custody',
'custody-bitgo-trust',
'custody-bitgo-mena-fze',
'custody-bitgo-custody-mena-fze',
],
decimalPlaces: 18,
asset: 'terc',
network: {
type: 'testnet',
name: 'OfcTestnet',
family: 'ofc',
},
primaryKeyCurve: 'secp256k1',
addressCoin: 'teth',
minimumDenomination: 1000000000000000000,
},
],
};

export const incorrectAmsTokenConfig = {
Expand Down Expand Up @@ -721,6 +752,29 @@ export const reducedAmsTokenConfig = {
contractAddress: '0x89a959b9184b4f8c8633646d5dfd049d2ebc983a',
},
],
ofcterc2: [
{
id: '055ebe86-72cc-4f0e-b46f-c517d8e36871',
fullName: 'Test ERC Token2',
name: 'ofcterc2',
prefix: '',
suffix: 'TERC',
baseUnit: 'wei',
kind: 'crypto',
family: 'ofc',
isToken: true,
decimalPlaces: 18,
asset: 'terc',
additionalFeatures: [],
excludedFeatures: [],
network: {
name: 'OfcTestnet',
},
primaryKeyCurve: 'secp256k1',
addressCoin: 'teth',
minimumDenomination: 1000000000000000000,
},
],
};

export const amsTokenWithUnsupportedNetwork = {
Expand Down