Skip to content

Commit

Permalink
fix: handle duplicate token contract addresses
Browse files Browse the repository at this point in the history
Ticket: COIN-2317
  • Loading branch information
mullapudipruthvik committed Nov 21, 2024
1 parent fd04794 commit 93d3b5d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/containers/BuildUnsignedSweepCoin/BuildUnsignedSweepCoin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useNavigate, useParams } from 'react-router-dom';
import { CoinsSelectAutocomplete } from '~/components';
import { useAlertBanner } from '~/contexts';
import {
assert, getEip1559Params,
assert,
getEip1559Params,
getEthLikeRecoveryChainId,
getTokenChain,
includePubsFor,
Expand All @@ -13,6 +14,7 @@ import {
toWei,
updateKeysFromIds,
updateKeysFromIdsWithToken,
getTickerByCoinFamily,
} from '~/helpers';
import { useLocalStorageState } from '~/hooks';
import { AvalancheCForm } from './AvalancheCForm';
Expand Down Expand Up @@ -138,7 +140,11 @@ function Form() {
await updateKeysFromIds(coin, values);
const recoverData = await window.commands.recover(coin, {
...rest,
eip1559: getEip1559Params(coin, maxFeePerGas, maxPriorityFeePerGas),
eip1559: getEip1559Params(
coin,
maxFeePerGas,
maxPriorityFeePerGas
),
replayProtectionOptions: {
chain: getEthLikeRecoveryChainId(coin, bitGoEnvironment),
hardfork: 'london',
Expand Down Expand Up @@ -917,8 +923,13 @@ function Form() {
values
);

const tokenTicker = getTickerByCoinFamily(
values.tokenContractAddress,
parentCoin
);

const recoverData = await recoverWithToken(
values.tokenContractAddress.toLowerCase(),
tokenTicker,
parentCoin,
{
...rest,
Expand Down Expand Up @@ -1213,7 +1224,7 @@ function Form() {
seed: values.seed,
ignoreAddressTypes: [],
userKey: '',
backupKey: ''
backupKey: '',
});
assert(
isRecoveryTransaction(recoverData),
Expand Down
78 changes: 77 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
BackupKeyRecoveryTransansaction,
FormattedOfflineVaultTxInfo,
} from '@bitgo/abstract-utxo';
import { coins, EthereumNetwork } from '@bitgo/statics'
import { coins, EthereumNetwork, BaseCoin, ContractAddressDefinedToken } from '@bitgo/statics';
import {
EvmCcrNonBitgoCoin,
evmCcrNonBitgoCoinConfig,
Expand All @@ -11,6 +11,34 @@ import {
} from '~/helpers/config';

const GWEI = 10 ** 9;
const INTERNAL_TEST_TOKENS = new Set([
'0xerc721:bsctoken',
'0xterc721:bsctoken',
'0xerc1155:bsctoken',
'0xterc1155:bsctoken',
'0xerc721:opethtoken',
'0xterc721:opethtoken',
'0xerc1155:opethtoken',
'0xterc1155:opethtoken',
'0xerc721:arbethtoken',
'0xterc721:arbethtoken',
'0xerc1155:arbethtoken',
'0xterc1155:arbethtoken',
'0xerc721:token',
'0xterc721:token',
'0xerc1155:token',
'0xterc1155:token',
'0xnonstandard:token',
'0xtnonstandard:token',
'0xerc721:polygontoken',
'0xterc721:polygontoken',
'0xerc1155:polygontoken',
'0xterc1155:polygontoken',
'0xerc721:beratoken',
'0xterc721:beratoken',
'0xerc1155:beratoken',
'0xterc1155:beratoken',
]);

export async function recoverWithToken(
token: string,
Expand Down Expand Up @@ -279,3 +307,51 @@ export function getEip1559Params(coin: string, maxFeePerGas: number, maxPriority
export function isAvaxcCoin(chainName: string) {
return (chainName === 'tavaxc' || chainName === 'avaxc') ;
}

function tokenHash(str: string): string {
// token hash must be 0x-prefixed lowercase hex string
str = str.toLowerCase();
if (INTERNAL_TEST_TOKENS.has(str)) {
return str;
}
if (!str.match(/^0x[0-9a-f]{40}$/)) {
throw new Error(`invalid hash format: ${str}`);
}
return str;
}


const tokenTickerMapByCoinFamily = coins.reduce(
(acc: Record<string, Record<string, string>>, coin: Readonly<BaseCoin>) => {
if (coin instanceof ContractAddressDefinedToken) {
const coinFamily = coin.network.family;
if (!acc[coinFamily.toLowerCase()]) {
acc[coinFamily.toLowerCase()] = {};
}
acc[coinFamily.toLowerCase()][
tokenHash(coin.contractAddress.toString())
] = coin.name.toLowerCase();
}
return acc;
},
{},
);

export function getTickerByCoinFamily(
hash: string,
coinFamily: string,
): string {

console.log("hash and conFamily")
console.log(hash, coinFamily)
if (
tokenTickerMapByCoinFamily[coinFamily.toLowerCase()] &&
tokenTickerMapByCoinFamily[coinFamily.toLowerCase()][tokenHash(hash)]
) {
return tokenTickerMapByCoinFamily[coinFamily.toLowerCase()][
tokenHash(hash)
];
}

throw new Error(`token not found for hash: ${hash}`);
}

0 comments on commit 93d3b5d

Please sign in to comment.