Skip to content
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
22 changes: 17 additions & 5 deletions modules/statics/src/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { BaseCoin, CoinFeature, DynamicCoin } from './base';
import { AmsNetworkConfigMap, AmsTokenConfig, TrimmedAmsTokenConfig } from './tokenConfig';
import { CoinMap } from './map';
import { BaseNetwork, getNetwork, getNetworksMap, NetworkType } from './networks';
import { networkFeatureMapForTokens } from './networkFeatureMapForTokens';
import { getNetworkFeatures } from './networkFeatureMapForTokens';
import { ofcErc20Coins, tOfcErc20Coins } from './coins/ofcErc20Coins';
import { ofcCoins } from './coins/ofcCoins';
import { allCoinsAndTokens } from './allCoinsAndTokens';
Expand Down Expand Up @@ -117,6 +117,18 @@ export function createToken(token: AmsTokenConfig): Readonly<BaseCoin> | undefin
ton: jettonToken,
};

// EVM-compatible chains are identified by a numeric chainId on their network object.
const tokenNetwork = token.network instanceof BaseNetwork ? token.network : undefined;
if (
tokenNetwork &&
'chainId' in tokenNetwork &&
typeof (tokenNetwork as { chainId?: unknown }).chainId === 'number'
) {
if (!erc20ChainToNameMap[token.family]) {
erc20ChainToNameMap[token.family] = token.family;
}
}

// dynamically add erc20 token initializers for eth like chains to the initializer map
Object.keys(erc20ChainToNameMap).forEach((key) => {
initializerMap[key] = erc20Token;
Expand Down Expand Up @@ -519,9 +531,9 @@ export function createTokenMapUsingTrimmedConfigDetails(
{ ...tokenConfig, features: tokenConfig.additionalFeatures ?? [], network },
];
}
} else if (network && networkFeatureMapForTokens[network.family]) {
} else if (network && getNetworkFeatures(network.family as string)) {
const features = new Set([
...(networkFeatureMapForTokens[network.family] || []),
...(getNetworkFeatures(network.family as string) || []),
...(tokenConfig.additionalFeatures || []),
]);
tokenConfig.excludedFeatures?.forEach((feature) => features.delete(feature));
Expand Down Expand Up @@ -549,9 +561,9 @@ export function createTokenUsingTrimmedConfigDetails(
return undefined;
}

if (network && networkFeatureMapForTokens[network.family]) {
if (network && getNetworkFeatures(network.family as string)) {
const features = new Set([
...(networkFeatureMapForTokens[network.family] || []),
...(getNetworkFeatures(network.family as string) || []),
...(tokenConfig.additionalFeatures || []),
]);
tokenConfig.excludedFeatures?.forEach((feature) => features.delete(feature));
Expand Down
2 changes: 1 addition & 1 deletion modules/statics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export {
CantonToken,
} from './account';
export { CoinMap } from './map';
export { networkFeatureMapForTokens } from './networkFeatureMapForTokens';
export { networkFeatureMapForTokens, registerNetworkFeatures, getNetworkFeatures } from './networkFeatureMapForTokens';
export {
generateErc20Coin,
generateTestErc20Coin,
Expand Down
16 changes: 16 additions & 0 deletions modules/statics/src/networkFeatureMapForTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ import {
TAO_TOKEN_FEATURES,
} from './coinFeatures';

const dynamicNetworkFeaturesMap = new Map<string, CoinFeature[]>();

/** Register default token features for a dynamically onboarded chain family. */
export function registerNetworkFeatures(family: string, features: CoinFeature[]): void {
dynamicNetworkFeaturesMap.set(family, features);
}

/**
* Look up token features for a family.
* Checks static map first, then falls back to dynamic map.
* Returns undefined if the family is not registered in either map.
*/
export function getNetworkFeatures(family: string): CoinFeature[] | undefined {
return networkFeatureMapForTokens[family as CoinFamily] ?? dynamicNetworkFeaturesMap.get(family);
}

export const networkFeatureMapForTokens: Partial<Record<CoinFamily, CoinFeature[]>> = {
algo: AccountCoin.DEFAULT_FEATURES,
apt: APT_FEATURES,
Expand Down
Loading