From ecfae1c01720fc8174118fd9ac51702f44fdf11d Mon Sep 17 00:00:00 2001 From: kevin <35275952+kaladinlight@users.noreply.github.com> Date: Mon, 12 Aug 2024 18:50:28 -0600 Subject: [PATCH] chore: cleanup unused caip types and fix isToken (#7521) * chore: cleanup unused caip types and fix isToken * fix tests * fix build * add back binance as supported thorchain asset even if we don't support natively --------- Co-authored-by: Apotheosis <0xapotheosis@gmail.com> --- packages/caip/src/assetId/assetId.test.ts | 116 +-------- packages/caip/src/chainId/chainId.test.ts | 63 +---- packages/caip/src/constants.ts | 49 +--- packages/caip/src/utils.test.ts | 22 +- packages/chain-adapters/src/utils/index.ts | 3 - .../makeSwapMemo/assertIsValidMemo.test.ts | 228 ------------------ .../utils/makeSwapMemo/assertIsValidMemo.ts | 140 ----------- src/components/AssetHeader/AssetHeader.tsx | 5 +- .../hooks/useSendDetails/useSendDetails.tsx | 5 +- src/components/Modals/Send/utils.ts | 6 +- .../TradeInput/components/TradeInputBody.tsx | 3 +- .../Deposit/components/Approve.tsx | 2 +- .../Deposit/components/Deposit.tsx | 2 +- .../Withdraw/components/Confirm.tsx | 10 +- src/hooks/mutations/useApprove.ts | 2 +- src/lib/investor/investor-foxy/api/api.ts | 5 +- src/lib/market-service/portals/portals.ts | 4 +- src/lib/market-service/zerion/zerion.ts | 6 +- src/lib/utils/index.test.ts | 18 +- src/lib/utils/index.ts | 22 +- .../utils/thorchain/hooks/useSendThorTx.tsx | 10 +- .../Pool/components/Repay/RepayConfirm.tsx | 4 +- .../AddLiquidity/AddLiquidityInput.tsx | 4 +- .../selectors/stakingSelectors.ts | 4 +- 24 files changed, 83 insertions(+), 650 deletions(-) delete mode 100644 packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.test.ts delete mode 100644 packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.ts diff --git a/packages/caip/src/assetId/assetId.test.ts b/packages/caip/src/assetId/assetId.test.ts index 227f13a0fde..a2483f1612e 100644 --- a/packages/caip/src/assetId/assetId.test.ts +++ b/packages/caip/src/assetId/assetId.test.ts @@ -16,12 +16,9 @@ describe('assetId', () => { describe('toAssetId(fromAssetId())', () => { it.each([ ['eip155:1/slip44:60'], - ['eip155:3/slip44:60'], ['eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'], ['bip122:000000000019d6689c085ae165831e93/slip44:0'], - ['bip122:000000000933ea01ad0ee984209779ba/slip44:0'], ['cosmos:cosmoshub-4/slip44:118'], - ['cosmos:vega-testnet/slip44:118'], ])('returns an AssetId from the result of fromAssetId for %s', assetId => { const result = fromAssetId(assetId) expect(toAssetId(omit(result, 'chainId'))).toBe(assetId) @@ -46,26 +43,9 @@ describe('assetId', () => { ) }) - it('can make eth AssetId on ropsten', () => { - const chainNamespace = CHAIN_NAMESPACE.Evm - const chainReference = CHAIN_REFERENCE.EthereumRopsten - const assetIdArgSuperset = { - chainNamespace, - chainReference, - assetNamespace: 'slip44' as AssetNamespace, - assetReference: ASSET_REFERENCE.Ethereum, - chainId: toChainId({ chainNamespace, chainReference }), - } - const expected = 'eip155:3/slip44:60' - expect(toAssetId(omit(assetIdArgSuperset, 'chainId'))).toEqual(expected) - expect(toAssetId(omit(assetIdArgSuperset, ['chainNamespace', 'chainReference']))).toEqual( - expected, - ) - }) - it('throws with invalid eth network', () => { const chainNamespace = CHAIN_NAMESPACE.Evm - const chainReference = CHAIN_REFERENCE.CosmosHubVega + const chainReference = CHAIN_REFERENCE.CosmosHubMainnet const assetIdArgSuperset = { chainNamespace, chainReference, @@ -85,7 +65,7 @@ describe('assetId', () => { const assetIdArgSuperset = { chainNamespace, chainReference, - assetNamespace: 'cw721' as AssetNamespace, + assetNamespace: 'bad' as AssetNamespace, assetReference: ASSET_REFERENCE.Ethereum, chainId: `${chainNamespace}:${chainReference}`, } @@ -112,23 +92,6 @@ describe('assetId', () => { ) }) - it('can make Cosmos AssetId on CosmosHub vega', () => { - const chainNamespace = CHAIN_NAMESPACE.CosmosSdk - const chainReference = CHAIN_REFERENCE.CosmosHubVega - const assetIdArgSuperset = { - chainNamespace, - chainReference, - assetNamespace: 'slip44' as AssetNamespace, - assetReference: ASSET_REFERENCE.Cosmos, - chainId: toChainId({ chainNamespace, chainReference }), - } - const expected = 'cosmos:vega-testnet/slip44:118' - expect(toAssetId(omit(assetIdArgSuperset, 'chainId'))).toEqual(expected) - expect(toAssetId(omit(assetIdArgSuperset, ['chainNamespace', 'chainReference']))).toEqual( - expected, - ) - }) - it('can make an IBC AssetId on CosmosHub', () => { const chainNamespace = CHAIN_NAMESPACE.CosmosSdk const chainReference = CHAIN_REFERENCE.CosmosHubMainnet @@ -149,7 +112,7 @@ describe('assetId', () => { it('throws with invalid Cosmos network', () => { const chainNamespace = CHAIN_NAMESPACE.CosmosSdk - const chainReference = CHAIN_REFERENCE.BitcoinTestnet + const chainReference = CHAIN_REFERENCE.BitcoinMainnet const assetIdArgSuperset = { chainNamespace, chainReference, @@ -181,7 +144,7 @@ describe('assetId', () => { it('throws with invalid btc network', () => { const chainNamespace = CHAIN_NAMESPACE.Utxo - const chainReference = CHAIN_REFERENCE.EthereumRopsten + const chainReference = CHAIN_REFERENCE.EthereumMainnet const assetIdArgSuperset = { chainNamespace, chainReference, @@ -246,23 +209,6 @@ describe('assetId', () => { ) }) - it('can make FOX AssetId on ropsten', () => { - const chainNamespace = CHAIN_NAMESPACE.Evm - const chainReference = CHAIN_REFERENCE.EthereumRopsten - const assetIdArgSuperset = { - chainNamespace, - chainReference, - assetNamespace: 'erc20' as AssetNamespace, - assetReference: '0xc770eefad204b5180df6a14ee197d99d808ee52d', - chainId: `${chainNamespace}:${chainReference}`, - } - const expected = 'eip155:3/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d' - expect(toAssetId(omit(assetIdArgSuperset, 'chainId'))).toEqual(expected) - expect(toAssetId(omit(assetIdArgSuperset, ['chainNamespace', 'chainReference']))).toEqual( - expected, - ) - }) - it('throws with invalid assetReference length', () => { const chainNamespace = CHAIN_NAMESPACE.Evm const chainReference = CHAIN_REFERENCE.EthereumMainnet @@ -343,23 +289,6 @@ describe('assetId', () => { expected, ) }) - - it('can make bitcoin AssetId on testnet', () => { - const chainNamespace = CHAIN_NAMESPACE.Utxo - const chainReference = CHAIN_REFERENCE.BitcoinTestnet - const assetIdArgSuperset = { - chainNamespace, - chainReference, - assetNamespace: 'slip44' as AssetNamespace, - assetReference: ASSET_REFERENCE.Bitcoin, - chainId: `${chainNamespace}:${chainReference}`, - } - const expected = 'bip122:000000000933ea01ad0ee984209779ba/slip44:0' - expect(toAssetId(omit(assetIdArgSuperset, 'chainId'))).toEqual(expected) - expect(toAssetId(omit(assetIdArgSuperset, ['chainNamespace', 'chainReference']))).toEqual( - expected, - ) - }) }) describe('fromAssetId', () => { @@ -368,9 +297,7 @@ describe('assetId', () => { const erc20: AssetNamespace = 'erc20' it.each([ [CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinMainnet, slip44, ASSET_REFERENCE.Bitcoin], - [CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinTestnet, slip44, ASSET_REFERENCE.Bitcoin], [CHAIN_NAMESPACE.Evm, CHAIN_REFERENCE.EthereumMainnet, slip44, ASSET_REFERENCE.Ethereum], - [CHAIN_NAMESPACE.Evm, CHAIN_REFERENCE.EthereumRopsten, slip44, ASSET_REFERENCE.Ethereum], [ CHAIN_NAMESPACE.Evm, CHAIN_REFERENCE.EthereumMainnet, @@ -383,7 +310,6 @@ describe('assetId', () => { slip44, ASSET_REFERENCE.Cosmos, ], - [CHAIN_NAMESPACE.CosmosSdk, CHAIN_REFERENCE.CosmosHubVega, slip44, ASSET_REFERENCE.Cosmos], ])( 'returns a AssetId from the result of fromAssetId for %s', ( @@ -418,17 +344,6 @@ describe('assetId', () => { expect(assetReference).toEqual(ASSET_REFERENCE.Ethereum) }) - it('can return chain, network from eth AssetId on ropsten', () => { - const AssetId = 'eip155:3/slip44:60' - const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = - fromAssetId(AssetId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRopsten) - expect(chainId).toEqual(toChainId({ chainNamespace, chainReference })) - expect(assetNamespace).toEqual('slip44') - expect(assetReference).toEqual(ASSET_REFERENCE.Ethereum) - }) - it('can return chain, network from bitcoin AssetId on mainnet', () => { const AssetId = 'bip122:000000000019d6689c085ae165831e93/slip44:0' const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = @@ -440,17 +355,6 @@ describe('assetId', () => { expect(assetReference).toEqual(ASSET_REFERENCE.Bitcoin) }) - it('can return chain, network from bitcoin AssetId on testnet', () => { - const AssetId = 'bip122:000000000933ea01ad0ee984209779ba/slip44:0' - const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = - fromAssetId(AssetId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Utxo) - expect(chainReference).toEqual(CHAIN_REFERENCE.BitcoinTestnet) - expect(chainId).toEqual(toChainId({ chainNamespace, chainReference })) - expect(assetNamespace).toEqual('slip44') - expect(assetReference).toEqual(ASSET_REFERENCE.Bitcoin) - }) - it('can return chainId, chainReference, chainNamespace, assetNamespace, assetReference from FOX AssetId on mainnet', () => { const AssetId = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d' const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = @@ -463,22 +367,22 @@ describe('assetId', () => { }) it('should lower case assetReference for assetNamespace ERC20', () => { - const AssetId = 'eip155:3/erc20:0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d' + const AssetId = 'eip155:1/erc20:0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d' const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = fromAssetId(AssetId) expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRopsten) + expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumMainnet) expect(chainId).toEqual(toChainId({ chainNamespace, chainReference })) expect(assetNamespace).toEqual('erc20') expect(assetReference).toEqual('0xc770eefad204b5180df6a14ee197d99d808ee52d') }) it('should lower case assetReference for assetNamespace ERC721', () => { - const AssetId = 'eip155:3/erc721:0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d/12345' + const AssetId = 'eip155:1/erc721:0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d/12345' const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = fromAssetId(AssetId) expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRopsten) + expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumMainnet) expect(chainId).toEqual(toChainId({ chainNamespace, chainReference })) expect(assetNamespace).toEqual('erc721') const [address, id] = assetReference.split('/') @@ -487,11 +391,11 @@ describe('assetId', () => { }) it('can return chainId, chainReference, chainNamespace, assetNamespace, assetReference from FOX AssetId on ropsten', () => { - const AssetId = 'eip155:3/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d' + const AssetId = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d' const { chainId, chainReference, chainNamespace, assetNamespace, assetReference } = fromAssetId(AssetId) expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRopsten) + expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumMainnet) expect(chainId).toEqual(toChainId({ chainNamespace, chainReference })) expect(assetNamespace).toEqual('erc20') expect(assetReference).toEqual('0xc770eefad204b5180df6a14ee197d99d808ee52d') diff --git a/packages/caip/src/chainId/chainId.test.ts b/packages/caip/src/chainId/chainId.test.ts index edcd592a496..96c0b5b65bf 100644 --- a/packages/caip/src/chainId/chainId.test.ts +++ b/packages/caip/src/chainId/chainId.test.ts @@ -17,13 +17,6 @@ describe('chainId', () => { expect(result).toEqual('cosmos:cosmoshub-4') }) - it('can turn CosmosHub testnet to ChainId', () => { - const chainNamespace = CHAIN_NAMESPACE.CosmosSdk - const chainReference = CHAIN_REFERENCE.CosmosHubVega - const result = toChainId({ chainNamespace, chainReference }) - expect(result).toEqual('cosmos:vega-testnet') - }) - it('can turn Ethereum mainnet to ChainId', () => { const chainNamespace = CHAIN_NAMESPACE.Evm const chainReference = CHAIN_REFERENCE.EthereumMainnet @@ -31,13 +24,6 @@ describe('chainId', () => { expect(result).toEqual('eip155:1') }) - it('can turn Ethereum testnet to ChainId', () => { - const chainNamespace = CHAIN_NAMESPACE.Evm - const chainReference = CHAIN_REFERENCE.EthereumRopsten - const result = toChainId({ chainNamespace, chainReference }) - expect(result).toEqual('eip155:3') - }) - it('can turn Bitcoin mainnet to ChainId', () => { const chainNamespace = CHAIN_NAMESPACE.Utxo const chainReference = CHAIN_REFERENCE.BitcoinMainnet @@ -45,21 +31,14 @@ describe('chainId', () => { expect(result).toEqual('bip122:000000000019d6689c085ae165831e93') }) - it('can turn Bitcoin testnet to ChainId', () => { - const chainNamespace = CHAIN_NAMESPACE.Utxo - const chainReference = CHAIN_REFERENCE.BitcoinTestnet - const result = toChainId({ chainNamespace, chainReference }) - expect(result).toEqual('bip122:000000000933ea01ad0ee984209779ba') - }) - it('should throw an error for an invalid chain', () => { // @ts-ignore expect(() => toChainId({ chainNamespace: CHAIN_NAMESPACE.Utxo, - chainReference: CHAIN_REFERENCE.CosmosHubVega, + chainReference: CHAIN_REFERENCE.CosmosHubMainnet, }), - ).toThrow('assertIsChainId: unsupported ChainId: bip122:vega-testnet') + ).toThrow('assertIsChainId: unsupported ChainId: bip122:cosmoshub-4') }) }) @@ -71,13 +50,6 @@ describe('chainId', () => { expect(chainReference).toEqual(CHAIN_REFERENCE.BitcoinMainnet) }) - it('can turn Bitcoin testnet to chain and network', () => { - const bitcoinChainId = 'bip122:000000000933ea01ad0ee984209779ba' - const { chainNamespace, chainReference } = fromChainId(bitcoinChainId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Utxo) - expect(chainReference).toEqual(CHAIN_REFERENCE.BitcoinTestnet) - }) - it('can turn CosmosHub mainnet to chain and network', () => { const cosmosHubChainId = 'cosmos:cosmoshub-4' const { chainNamespace, chainReference } = fromChainId(cosmosHubChainId) @@ -85,33 +57,12 @@ describe('chainId', () => { expect(chainReference).toEqual(CHAIN_REFERENCE.CosmosHubMainnet) }) - it('can turn CosmosHub testnet to chain and network', () => { - const cosmosHubChainId = 'cosmos:vega-testnet' - const { chainNamespace, chainReference } = fromChainId(cosmosHubChainId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.CosmosSdk) - expect(chainReference).toEqual(CHAIN_REFERENCE.CosmosHubVega) - }) - it('can turn Ethereum mainnet to chain and network', () => { const ethereumChainId = 'eip155:1' const { chainNamespace, chainReference } = fromChainId(ethereumChainId) expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumMainnet) }) - - it('can turn Ethereum ropsten to chain and network', () => { - const ethereumChainId = 'eip155:3' - const { chainNamespace, chainReference } = fromChainId(ethereumChainId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRopsten) - }) - - it('can turn Ethereum rinkeby to chain and network', () => { - const ethereumChainId = 'eip155:4' - const { chainNamespace, chainReference } = fromChainId(ethereumChainId) - expect(chainNamespace).toEqual(CHAIN_NAMESPACE.Evm) - expect(chainReference).toEqual(CHAIN_REFERENCE.EthereumRinkeby) - }) }) }) @@ -128,19 +79,10 @@ describe('isChainId', () => { expect(() => assertIsChainId('eip155:2')).toThrow() }) - it('validates ethereum testnets as true', () => { - expect(isChainId('eip155:3')).toBe(true) - expect(isChainId('eip155:4')).toBe(true) - }) - it('validates bip122:000000000019d6689c085ae165831e93 mainnet as true', () => { expect(isChainId('bip122:000000000019d6689c085ae165831e93')).toBe(true) }) - it('validates bip122:000000000933ea01ad0ee984209779ba testnet as true', () => { - expect(isChainId('bip122:000000000933ea01ad0ee984209779ba')).toBe(true) - }) - it('throws on bip122 with the wrong network reference', () => { expect(() => assertIsChainId('bip122:1')).toThrow() }) @@ -157,7 +99,6 @@ describe('isChainId', () => { it('should return true for cosmos', () => { expect(isChainId('cosmos:cosmoshub-4')).toBe(true) - expect(isChainId('cosmos:vega-testnet')).toBe(true) }) it('should throw for an unknown cosmos chain', () => { diff --git a/packages/caip/src/constants.ts b/packages/caip/src/constants.ts index 01aba5925d0..4883de8e430 100644 --- a/packages/caip/src/constants.ts +++ b/packages/caip/src/constants.ts @@ -28,9 +28,6 @@ export const foxyAssetId: AssetId = 'eip155:1/erc20:0xdc49108ce5c57bc3408c3a5e95 export const cosmosAssetId: AssetId = 'cosmos:cosmoshub-4/slip44:118' export const thorchainAssetId: AssetId = 'cosmos:thorchain-mainnet-v1/slip44:931' export const binanceAssetId: AssetId = 'cosmos:binance-chain-tigris/slip44:714' -export const kavaAssetId: AssetId = 'cosmos:kava_2222-10/slip44:459' -export const terraAssetId: AssetId = 'cosmos:phoenix-1/slip44:330' -export const secretAssetId: AssetId = 'cosmos:secret-4/slip44:529' export const btcChainId: ChainId = 'bip122:000000000019d6689c085ae165831e93' export const bchChainId: ChainId = 'bip122:000000000000000000651ef99cb9fcbe' @@ -50,9 +47,6 @@ export const baseChainId: ChainId = 'eip155:8453' export const cosmosChainId: ChainId = 'cosmos:cosmoshub-4' export const thorchainChainId: ChainId = 'cosmos:thorchain-mainnet-v1' export const binanceChainId: ChainId = 'cosmos:binance-chain-tigris' -export const kavaChainId: ChainId = 'cosmos:kava_2222-10' -export const terraChainId: ChainId = 'cosmos:phoenix-1' -export const secretChainId: ChainId = 'cosmos:secret-4' export const CHAIN_NAMESPACE = { Evm: 'eip155', @@ -66,28 +60,14 @@ type ValidChainMap = { export const CHAIN_REFERENCE = { EthereumMainnet: '1', - EthereumRopsten: '3', - EthereumRinkeby: '4', - // EthereumKovan: '42', // currently unsupported by ShapeShift - // https://github.com/bitcoin/bips/blob/master/bip-0122.mediawiki#definition-of-chain-id - // chainId uses max length of 32 chars of the genesis block BitcoinMainnet: '000000000019d6689c085ae165831e93', - BitcoinTestnet: '000000000933ea01ad0ee984209779ba', BitcoinCashMainnet: '000000000000000000651ef99cb9fcbe', DogecoinMainnet: '00000000001a91e3dace36e2be3bf030', LitecoinMainnet: '12a765e31ffd4059bada1e25190f6e98', CosmosHubMainnet: 'cosmoshub-4', - CosmosHubVega: 'vega-testnet', ThorchainMainnet: 'thorchain-mainnet-v1', - AvalancheCChain: '43114', // https://docs.avax.network/apis/avalanchego/apis/c-chain BinanceMainnet: 'binance-chain-tigris', // https://docs.bnbchain.org/docs/wallet_api/#chain-ids - BinanceTestnet: 'binance-chain-ganges', // https://docs.bnbchain.org/docs/wallet_api/#chain-ids - KavaMainnet: 'kava_2222-10', // https://github.com/Kava-Labs/rosetta-kava/blob/1c04619078a8d3293e296c66eb322c505589f2d6/services/construction_payloads.go#L115 - KavaTestnet: 'kava_2221-16000', // https://github.com/Kava-Labs/rosetta-kava/blob/1c04619078a8d3293e296c66eb322c505589f2d6/services/construction_payloads.go#L117 - TerraMainnet: 'phoenix-1', // https://docs.terra.money/full-node/run-a-full-terra-node/join-a-network/ - TerraTestnet: 'pisco-1', // https://docs.terra.money/full-node/run-a-full-terra-node/join-a-network/ - SecretMainnet: 'secret-4', // https://docs.scrt.network/secret-network-documentation/development/api-endpoints - SecretTestnet: 'pulsar-2', // https://docs.scrt.network/secret-network-documentation/development/api-endpoints + AvalancheCChain: '43114', // https://docs.avax.network/apis/avalanchego/apis/c-chain OptimismMainnet: '10', //https://community.optimism.io/docs/useful-tools/networks/#optimism-mainnet BnbSmartChainMainnet: '56', // https://docs.bnbchain.org/docs/wallet_api/#chain-ids PolygonMainnet: '137', // https://wiki.polygon.technology/docs/develop/metamask/config-polygon-on-metamask/ @@ -98,8 +78,6 @@ export const CHAIN_REFERENCE = { } as const export const ASSET_NAMESPACE = { - cw20: 'cw20', - cw721: 'cw721', erc20: 'erc20', erc721: 'erc721', erc1155: 'erc1155', @@ -107,23 +85,19 @@ export const ASSET_NAMESPACE = { bep721: 'bep721', bep1155: 'bep1155', slip44: 'slip44', - native: 'native', ibc: 'ibc', } as const export const ASSET_REFERENCE = { Bitcoin: '0', + BitcoinCash: '145', Litecoin: '2', Dogecoin: '3', - Ethereum: '60', Cosmos: '118', Thorchain: '931', - BitcoinCash: '145', - AvalancheC: '60', // evm chain which uses ethereum derivation path as common practice Binance: '714', - Kava: '459', - Terra: '330', - Secret: '529', + Ethereum: '60', + AvalancheC: '60', // evm chain which uses ethereum derivation path as common practice Optimism: '60', // evm chain which uses ethereum derivation path as common practice BnbSmartChain: '60', // evm chain which uses ethereum derivation path as common practice Polygon: '60', // evm chain which uses ethereum derivation path as common practice @@ -136,15 +110,12 @@ export const ASSET_REFERENCE = { export const VALID_CHAIN_IDS: ValidChainMap = Object.freeze({ [CHAIN_NAMESPACE.Utxo]: [ CHAIN_REFERENCE.BitcoinMainnet, - CHAIN_REFERENCE.BitcoinTestnet, CHAIN_REFERENCE.BitcoinCashMainnet, CHAIN_REFERENCE.DogecoinMainnet, CHAIN_REFERENCE.LitecoinMainnet, ], [CHAIN_NAMESPACE.Evm]: [ CHAIN_REFERENCE.EthereumMainnet, - CHAIN_REFERENCE.EthereumRopsten, - CHAIN_REFERENCE.EthereumRinkeby, CHAIN_REFERENCE.AvalancheCChain, CHAIN_REFERENCE.OptimismMainnet, CHAIN_REFERENCE.BnbSmartChainMainnet, @@ -156,12 +127,8 @@ export const VALID_CHAIN_IDS: ValidChainMap = Object.freeze({ ], [CHAIN_NAMESPACE.CosmosSdk]: [ CHAIN_REFERENCE.CosmosHubMainnet, - CHAIN_REFERENCE.CosmosHubVega, CHAIN_REFERENCE.ThorchainMainnet, CHAIN_REFERENCE.BinanceMainnet, - CHAIN_REFERENCE.KavaMainnet, - CHAIN_REFERENCE.TerraMainnet, - CHAIN_REFERENCE.SecretMainnet, ], }) @@ -180,13 +147,7 @@ export const VALID_ASSET_NAMESPACE: ValidAssetNamespace = Object.freeze({ ASSET_NAMESPACE.bep721, ASSET_NAMESPACE.bep1155, ], - [CHAIN_NAMESPACE.CosmosSdk]: [ - ASSET_NAMESPACE.cw20, - ASSET_NAMESPACE.cw721, - ASSET_NAMESPACE.ibc, - ASSET_NAMESPACE.native, - ASSET_NAMESPACE.slip44, - ], + [CHAIN_NAMESPACE.CosmosSdk]: [ASSET_NAMESPACE.ibc, ASSET_NAMESPACE.slip44], }) // We should prob change this once we add more chains diff --git a/packages/caip/src/utils.test.ts b/packages/caip/src/utils.test.ts index 1b7540d66b7..8affa4c1446 100644 --- a/packages/caip/src/utils.test.ts +++ b/packages/caip/src/utils.test.ts @@ -67,14 +67,14 @@ describe('accountIdToSpecifier', () => { describe('isValidChainPartsPair', () => { it('correctly validates pairs', () => { - expect(isValidChainPartsPair(CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinTestnet)).toEqual( + expect(isValidChainPartsPair(CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinMainnet)).toEqual( true, ) - expect(isValidChainPartsPair(CHAIN_NAMESPACE.Evm, CHAIN_REFERENCE.BitcoinTestnet)).toEqual( + expect(isValidChainPartsPair(CHAIN_NAMESPACE.Evm, CHAIN_REFERENCE.BitcoinMainnet)).toEqual( false, ) expect( - isValidChainPartsPair('invalid' as ChainNamespace, CHAIN_REFERENCE.BitcoinTestnet), + isValidChainPartsPair('invalid' as ChainNamespace, CHAIN_REFERENCE.BitcoinMainnet), ).toEqual(false) expect(isValidChainPartsPair(CHAIN_NAMESPACE.Evm, 'invalid' as ChainReference)).toEqual(false) }) @@ -93,7 +93,7 @@ describe('type guard', () => { describe('isChainReference', () => { it('correctly determines type', () => { expect(isChainReference(CHAIN_REFERENCE.EthereumMainnet)).toEqual(true) - expect(isChainReference(CHAIN_REFERENCE.BitcoinTestnet)).toEqual(true) + expect(isChainReference(CHAIN_REFERENCE.BitcoinMainnet)).toEqual(true) expect(isChainReference('invalid')).toEqual(false) expect(isChainReference('')).toEqual(false) }) @@ -111,8 +111,8 @@ describe('type guard', () => { describe('isAssetNamespace', () => { it('correctly determines type', () => { - expect(isAssetNamespace(ASSET_NAMESPACE.cw20)).toEqual(true) - expect(isAssetNamespace(ASSET_NAMESPACE.cw721)).toEqual(true) + expect(isAssetNamespace(ASSET_NAMESPACE.erc20)).toEqual(true) + expect(isAssetNamespace(ASSET_NAMESPACE.erc721)).toEqual(true) expect(isAssetNamespace('invalid')).toEqual(false) expect(isAssetNamespace('')).toEqual(false) }) @@ -161,7 +161,7 @@ describe('type guard assertion', () => { describe('assertIsChainReference', () => { it('correctly asserts type', () => { expect(() => assertIsChainReference(CHAIN_REFERENCE.EthereumMainnet)).not.toThrow() - expect(() => assertIsChainReference(CHAIN_REFERENCE.BitcoinTestnet)).not.toThrow() + expect(() => assertIsChainReference(CHAIN_REFERENCE.BitcoinMainnet)).not.toThrow() expect(() => assertIsChainReference('invalid')).toThrow() expect(() => assertIsChainReference('')).toThrow() }) @@ -169,8 +169,8 @@ describe('type guard assertion', () => { describe('assertIsAssetNamespace', () => { it('correctly asserts type', () => { - expect(() => assertIsAssetNamespace(ASSET_NAMESPACE.cw20)).not.toThrow() - expect(() => assertIsAssetNamespace(ASSET_NAMESPACE.cw721)).not.toThrow() + expect(() => assertIsAssetNamespace(ASSET_NAMESPACE.erc20)).not.toThrow() + expect(() => assertIsAssetNamespace(ASSET_NAMESPACE.erc721)).not.toThrow() expect(() => assertIsAssetNamespace('invalid')).toThrow() expect(() => assertIsAssetNamespace('')).toThrow() }) @@ -188,13 +188,13 @@ describe('type guard assertion', () => { describe('assertValidChainPartsPair', () => { it('correctly asserts type', () => { expect(() => - assertValidChainPartsPair(CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinTestnet), + assertValidChainPartsPair(CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.BitcoinMainnet), ).not.toThrow() expect(() => assertValidChainPartsPair(CHAIN_NAMESPACE.Utxo, CHAIN_REFERENCE.EthereumMainnet), ).toThrow() expect(() => - assertValidChainPartsPair('invalid' as ChainNamespace, CHAIN_REFERENCE.BitcoinTestnet), + assertValidChainPartsPair('invalid' as ChainNamespace, CHAIN_REFERENCE.BitcoinMainnet), ).toThrow() expect(() => assertValidChainPartsPair(CHAIN_NAMESPACE.Evm, 'invalid' as ChainReference), diff --git a/packages/chain-adapters/src/utils/index.ts b/packages/chain-adapters/src/utils/index.ts index 41ea92e7b19..3b32e75c39f 100644 --- a/packages/chain-adapters/src/utils/index.ts +++ b/packages/chain-adapters/src/utils/index.ts @@ -38,8 +38,6 @@ export const chainIdToChainLabel = (chainId: ChainId): string => { case CHAIN_NAMESPACE.Evm: switch (chainReference) { case CHAIN_REFERENCE.EthereumMainnet: - case CHAIN_REFERENCE.EthereumRinkeby: - case CHAIN_REFERENCE.EthereumRopsten: case CHAIN_REFERENCE.AvalancheCChain: case CHAIN_REFERENCE.OptimismMainnet: case CHAIN_REFERENCE.BnbSmartChainMainnet: @@ -57,7 +55,6 @@ export const chainIdToChainLabel = (chainId: ChainId): string => { case CHAIN_NAMESPACE.CosmosSdk: switch (chainReference) { case CHAIN_REFERENCE.CosmosHubMainnet: - case CHAIN_REFERENCE.CosmosHubVega: return 'cosmos' case CHAIN_REFERENCE.ThorchainMainnet: return 'thorchain' diff --git a/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.test.ts b/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.test.ts deleted file mode 100644 index 5d11b10bf93..00000000000 --- a/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.test.ts +++ /dev/null @@ -1,228 +0,0 @@ -import { bchChainId, btcChainId, ethChainId, thorchainChainId } from '@shapeshiftoss/caip' -import { describe, expect, it } from 'vitest' - -import { assertIsValidMemo, isValidMemoAddress } from './assertIsValidMemo' - -describe('isValidMemoAddress', () => { - it('should validate memo address', () => { - expect( - isValidMemoAddress(ethChainId, 'ETH.ETH', '0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741'), - ).toBe(true) - - expect( - isValidMemoAddress( - bchChainId, - 'BCH.ETH', - 'bitcoincash:qpqze9nw77en2v4du4ud0d02t72qkafqe5nk8vrk7f', - ), - ).toBe(true) - - expect( - isValidMemoAddress( - thorchainChainId, - 'RUNE.ETH', - 'thor1j5jxvwd33rz66r88vwwwayvncjadx9jy5hvvqw', - ), - ).toBe(true) - }) - - it('should fail memo address validation when chainId does not match', () => { - expect( - isValidMemoAddress( - ethChainId, - 'BCH.ETH', - 'bitcoincash:qpqze9nw77en2v4du4ud0d02t72qkafqe5nk8vrk7f', - ), - ).toBe(false) - - expect( - isValidMemoAddress(ethChainId, 'ETH.ETH', 'bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h'), - ).toBe(false) - - expect( - isValidMemoAddress(bchChainId, 'BCH.ETH', 'bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h'), - ).toBe(false) - }) - - it('should fail memo address validation when address does not match', () => { - expect( - isValidMemoAddress(bchChainId, 'BCH.ETH', 'bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h'), - ).toBe(false) - }) - - it('should fail memo address validation when chainId and address do not match', () => { - expect( - isValidMemoAddress(ethChainId, 'ETH.ETH', 'bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h'), - ).toBe(false) - }) -}) - -describe('assertIsValidMemo', () => { - it('should assert memo is valid', () => { - const affiliateBps = '25' - - // bitcoin - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420:ss:25', - btcChainId, - affiliateBps, - ), - ).not.toThrow() - - // ethereum - expect(() => - assertIsValidMemo( - 's:ETH.ETH:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:25', - btcChainId, - affiliateBps, - ), - ).not.toThrow() - - // eth chain - different token - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:25', - btcChainId, - affiliateBps, - ), - ).not.toThrow() - - // zero affiliate bps - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:0', - btcChainId, - '0', - ), - ).not.toThrow() - - // different affiliate bps - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:30', - btcChainId, - '30', - ), - ).not.toThrow() - - // streaming swap - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420/10/5:ss:30', - btcChainId, - '30', - ), - ).not.toThrow() - }) - - it('should throw on invalid address with typos', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:zc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420:ss:0', - btcChainId, - '0', - ), - ).toThrow('memo s:BTC.BTC:zc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420:ss:0 invalid') - }) - - it('should throw on invalid address based on case sensitivity', () => { - expect(() => - assertIsValidMemo( - 's:RUNE.BTC:Bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420:ss:0', - btcChainId, - '0', - ), - ).toThrow('memo s:RUNE.BTC:Bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420:ss:0 invalid') - }) - - it('should throw on wrong address for chain', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:0', - btcChainId, - '0', - ), - ).toThrow('memo s:BTC.BTC:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:0 invalid') - }) - - it('should throw on affiliate bps out of range', () => { - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:ss:9999', - btcChainId, - '9999', - ), - ).toThrow('affiliateBps 9999 is not a number between 0 and 1000') - }) - - it('should throw on incorrect affiliate id', () => { - expect(() => - assertIsValidMemo( - 's:ETH.USDC-B48:0x8a65ac0E23F31979db06Ec62Af62b132a6dF4741:420:xx:10', - btcChainId, - '10', - ), - ).toThrow('affiliate xx is not ss') - }) - - it('should throw on missing streamingNumSwaps', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420//10:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumSwaps '' is not a valid number") - }) - - it('should throw on missing streamingNumBlocks', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420/5/:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumBlocks '' is not a valid number") - }) - - it('should throw on invalid streamingNumSwaps', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420/foo/10:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumSwaps 'foo' is not a valid number") - }) - - it('should throw on invalid streamingNumBlocks', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420/5/bar:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumBlocks 'bar' is not a valid number") - }) - - it('should throw on non-decimal streamingNumSwaps', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420/0x16/10:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumSwaps '0x16' is not a valid number") - }) - - it('should throw on non-decimal streamingNumBlocks', () => { - expect(() => - assertIsValidMemo( - 's:BTC.BTC:bc1qkw9g3tgv6m2gwc4x4hvdefcwt0uxeedfgag27h:420/5/0x123:ss:0', - btcChainId, - '0', - ), - ).toThrow("streamingNumBlocks '0x123' is not a valid number") - }) -}) diff --git a/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.ts b/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.ts deleted file mode 100644 index 0cf35fac9c8..00000000000 --- a/packages/swapper/src/swappers/ThorchainSwapper/utils/makeSwapMemo/assertIsValidMemo.ts +++ /dev/null @@ -1,140 +0,0 @@ -import type { ChainId } from '@shapeshiftoss/caip' -import { - avalancheChainId, - bchChainId, - binanceChainId, - bscChainId, - btcChainId, - CHAIN_NAMESPACE, - cosmosChainId, - dogeChainId, - ethChainId, - fromChainId, - ltcChainId, - thorchainChainId, -} from '@shapeshiftoss/caip' -import { chainIdToChainLabel } from '@shapeshiftoss/chain-adapters' -import { assertUnreachable, bnOrZero } from '@shapeshiftoss/utils' -import assert from 'assert' -import WAValidator from 'multicoin-address-validator' - -import { - LIMIT_PART_DELIMITER, - MEMO_PART_DELIMITER, - POOL_PART_DELIMITER, - THORCHAIN_AFFILIATE_NAME, -} from '../constants' - -const thorChainAssetToChainId: Map = new Map([ - ['ETH', ethChainId], - ['AVAX', avalancheChainId], - ['BTC', btcChainId], - ['BCH', bchChainId], - ['LTC', ltcChainId], - ['BNB', binanceChainId], - ['DOGE', dogeChainId], - ['GAIA', cosmosChainId], - ['RUNE', thorchainChainId], - ['THOR', thorchainChainId], - ['BSC', bscChainId], -]) - -export const isValidMemoAddress = (chainId: ChainId, thorId: string, address: string): boolean => { - switch (true) { - case thorId.startsWith('ETH.'): - case thorId.startsWith('BSC.'): - case thorId.startsWith('AVAX.'): - case thorId.startsWith('BTC.'): - case thorId.startsWith('BCH.'): - case thorId.startsWith('LTC.'): - case thorId.startsWith('BNB.'): - case thorId.startsWith('DOGE.'): { - const chainLabel = chainIdToChainLabel(chainId) - return WAValidator.validate(address, chainLabel) - } - case thorId.startsWith('GAIA.'): - return address.startsWith('cosmos') - // Note that this case doesn't contain a dot - // See https://github.com/shapeshift/lib/blob/6b5c9c8e855ffb68d865cfae8f545e7a819a9667/packages/swapper/src/swappers/thorchain/utils/makeSwapMemo/makeSwapMemo.ts#L10 - // RUNE isn't a pool, it is the native asset of the THORChain network - case thorId.startsWith('RUNE'): - case thorId.startsWith('THOR'): - return address.startsWith('thor') - default: - return false - } -} - -export const assertIsValidMemo = (memo: string, chainId: ChainId, affiliateBps: string): void => { - const [, pool, address, limitComponent, affiliate, memoAffiliateBps] = - memo.split(MEMO_PART_DELIMITER) - - if (!pool) throw new Error(`pools is required in memo: ${memo}`) - const thorchainAsset = pool.split(POOL_PART_DELIMITER)[0] - if (!thorchainAsset) throw new Error(`thorchainAsset is required in memo: ${memo}`) - - const buyAssetChainId = thorChainAssetToChainId.get(thorchainAsset) - - if (!address) throw new Error(`address is required in memo: ${memo}`) - - const isAddressValid = buyAssetChainId - ? isValidMemoAddress(buyAssetChainId, pool, address) - : undefined - - if (!isAddressValid) { - throw Error(`memo ${memo} invalid`) - } - - const [limit, streamingNumSwaps, streamingNumBlocks] = (limitComponent ?? '').split( - LIMIT_PART_DELIMITER, - ) - - const isStreamingSwap = streamingNumSwaps || streamingNumBlocks - - if (isStreamingSwap && (!streamingNumSwaps || !/^\d+$/.test(streamingNumSwaps))) { - throw Error(`streamingNumSwaps '${streamingNumSwaps}' is not a valid number`) - } - - if (isStreamingSwap && (!streamingNumBlocks || !/^\d+$/.test(streamingNumBlocks))) { - throw Error(`streamingNumBlocks '${streamingNumBlocks}' is not a valid number`) - } - - // Check if limit is a valid number - if (!bnOrZero(limit).isInteger()) { - throw Error(`limit ${limit} is not a valid number`) - } - - // Check if affiliate is "ss" - if (affiliate !== THORCHAIN_AFFILIATE_NAME) { - throw Error(`affiliate ${affiliate} is not ${THORCHAIN_AFFILIATE_NAME}`) - } - - // Check if affiliateBps is a number between and including 0 and 1000 (the valid range for THORSwap) - const affiliateBpsNum = bnOrZero(memoAffiliateBps) - if (!bnOrZero(limit).isInteger() || affiliateBpsNum.lt(0) || affiliateBpsNum.gt(1000)) { - throw Error(`affiliateBps ${memoAffiliateBps} is not a number between 0 and 1000`) - } - - assert(affiliateBpsNum.eq(affiliateBps), 'incorrect affiliateBps') - - const { chainNamespace } = fromChainId(chainId) - - const maxMemoLength = (() => { - switch (chainNamespace) { - case CHAIN_NAMESPACE.Utxo: - // BTC (and likely other UTXO coins) can only support up to 80 character (byte) memos - return 80 - case CHAIN_NAMESPACE.Evm: - return Infinity - case CHAIN_NAMESPACE.CosmosSdk: - // Cosmos (and likely other Cosmos SDK coins) can only support up to 256 character (byte) memos - return 256 - default: - assertUnreachable(chainNamespace) - } - })() - - if (memo.length > maxMemoLength) { - throw Error(`memo length exceeds ${maxMemoLength} characters`) - } -} diff --git a/src/components/AssetHeader/AssetHeader.tsx b/src/components/AssetHeader/AssetHeader.tsx index 5220c3111e4..8585d80ef85 100644 --- a/src/components/AssetHeader/AssetHeader.tsx +++ b/src/components/AssetHeader/AssetHeader.tsx @@ -15,7 +15,7 @@ import { RawText } from 'components/Text' import { useLocaleFormatter } from 'hooks/useLocaleFormatter/useLocaleFormatter' import { useWallet } from 'hooks/useWallet/useWallet' import { useWalletSupportsChain } from 'hooks/useWalletSupportsChain/useWalletSupportsChain' -import { middleEllipsis, tokenOrUndefined } from 'lib/utils' +import { isToken, middleEllipsis } from 'lib/utils' import { selectAccountIdsByAssetId, selectAssetById, @@ -70,14 +70,13 @@ export const AssetHeader: React.FC = ({ assetId, accountId }) const href = (() => { const { assetReference } = fromAssetId(asset.assetId) - const maybeToken = tokenOrUndefined(assetReference) if (isNft(asset.assetId)) { const [token] = assetReference.split('/') return `${asset.explorer}/token/${token}?a=${asset.id}` } - if (maybeToken) return `${asset?.explorerAddressLink}${maybeToken}` + if (isToken(asset.assetId)) return `${asset?.explorerAddressLink}${assetReference}` return asset.explorer })() diff --git a/src/components/Modals/Send/hooks/useSendDetails/useSendDetails.tsx b/src/components/Modals/Send/hooks/useSendDetails/useSendDetails.tsx index 5787161208b..a7898cf0d5e 100644 --- a/src/components/Modals/Send/hooks/useSendDetails/useSendDetails.tsx +++ b/src/components/Modals/Send/hooks/useSendDetails/useSendDetails.tsx @@ -12,7 +12,7 @@ import { useWallet } from 'hooks/useWallet/useWallet' import type { BigNumber } from 'lib/bignumber/bignumber' import { bn, bnOrZero } from 'lib/bignumber/bignumber' import { toBaseUnit } from 'lib/math' -import { tokenOrUndefined } from 'lib/utils' +import { contractAddressOrUndefined } from 'lib/utils' import { selectAssetById, selectFeeAssetById, @@ -109,8 +109,7 @@ export const useSendDetails = (): UseSendDetailsReturnType => { state: { wallet }, } = useWallet() - const { assetReference } = fromAssetId(assetId) - const contractAddress = tokenOrUndefined(assetReference) + const contractAddress = contractAddressOrUndefined(assetId) const estimateFormFees = useCallback( ({ diff --git a/src/components/Modals/Send/utils.ts b/src/components/Modals/Send/utils.ts index 1da5e528fd9..ed44f255959 100644 --- a/src/components/Modals/Send/utils.ts +++ b/src/components/Modals/Send/utils.ts @@ -1,5 +1,5 @@ import type { AccountId, AssetId, ChainId } from '@shapeshiftoss/caip' -import { CHAIN_NAMESPACE, fromAccountId, fromAssetId, fromChainId } from '@shapeshiftoss/caip' +import { CHAIN_NAMESPACE, fromAccountId, fromChainId } from '@shapeshiftoss/caip' import type { CosmosSdkChainId, EvmChainId, @@ -18,7 +18,7 @@ import { checkIsSnapInstalled, } from 'hooks/useIsSnapInstalled/useIsSnapInstalled' import { bn, bnOrZero } from 'lib/bignumber/bignumber' -import { assertGetChainAdapter, tokenOrUndefined } from 'lib/utils' +import { assertGetChainAdapter, contractAddressOrUndefined } from 'lib/utils' import { assertGetCosmosSdkChainAdapter } from 'lib/utils/cosmosSdk' import { assertGetEvmChainAdapter, getSupportedEvmChainIds } from 'lib/utils/evm' import { assertGetUtxoChainAdapter } from 'lib/utils/utxo' @@ -151,7 +151,7 @@ export const handleSend = async ({ if (!shouldUseEIP1559Fees && gasPrice === undefined) { throw new Error(`useFormSend: missing gasPrice for non-EIP-1559 tx`) } - const contractAddress = tokenOrUndefined(fromAssetId(asset.assetId).assetReference) + const contractAddress = contractAddressOrUndefined(asset.assetId) const { accountNumber } = bip44Params const adapter = assertGetEvmChainAdapter(chainId) return await adapter.buildSendTransaction({ diff --git a/src/components/MultiHopTrade/components/TradeInput/components/TradeInputBody.tsx b/src/components/MultiHopTrade/components/TradeInput/components/TradeInputBody.tsx index 49693fc7c92..7b5ca270694 100644 --- a/src/components/MultiHopTrade/components/TradeInput/components/TradeInputBody.tsx +++ b/src/components/MultiHopTrade/components/TradeInput/components/TradeInputBody.tsx @@ -8,7 +8,6 @@ import { Stack, } from '@chakra-ui/react' import type { AccountId } from '@shapeshiftoss/caip' -import { fromAssetId } from '@shapeshiftoss/caip' import type { Asset } from '@shapeshiftoss/types' import { positiveOrZero } from '@shapeshiftoss/utils' import { useCallback, useEffect, useMemo } from 'react' @@ -95,7 +94,7 @@ export const TradeInputBody = ({ const percentOptions = useMemo(() => { if (!sellAsset?.assetId) return [] - if (!isToken(fromAssetId(sellAsset.assetId).assetReference)) return [] + if (!isToken(sellAsset.assetId)) return [] return [1] }, [sellAsset.assetId]) diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Approve.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Approve.tsx index 255bf15acc0..b0eb44e9cc6 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Approve.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Approve.tsx @@ -99,7 +99,7 @@ export const Approve: React.FC = ({ accountId, onNext }) => { selectEarnUserStakingOpportunityByUserStakingId(state, opportunityDataFilter), ) - const isTokenDeposit = isToken(assetReference) + const isTokenDeposit = isToken(assetId) const feeMarketData = useAppSelector(state => selectMarketDataByAssetIdUserCurrency(state, feeAsset?.assetId ?? ''), diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Deposit.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Deposit.tsx index b7676fd16cc..10671e02eca 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Deposit.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Deposit/components/Deposit.tsx @@ -107,7 +107,7 @@ export const Deposit: React.FC = ({ const isRunePool = assetId === thorchainAssetId - const isTokenDeposit = isToken(fromAssetId(assetId).assetReference) + const isTokenDeposit = isToken(assetId) const accountNumberFilter = useMemo(() => ({ accountId }), [accountId]) const accountNumber = useAppSelector(state => diff --git a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx index 656e72d504b..4fb914a7e7b 100644 --- a/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx +++ b/src/features/defi/providers/thorchain-savers/components/ThorchainSaversManager/Withdraw/components/Confirm.tsx @@ -10,13 +10,7 @@ import { useToast, } from '@chakra-ui/react' import type { AccountId } from '@shapeshiftoss/caip' -import { - bchChainId, - fromAccountId, - fromAssetId, - thorchainAssetId, - toAssetId, -} from '@shapeshiftoss/caip' +import { bchChainId, fromAccountId, thorchainAssetId, toAssetId } from '@shapeshiftoss/caip' import { supportsETH } from '@shapeshiftoss/hdwallet-core' import { SwapperName } from '@shapeshiftoss/swapper' import dayjs from 'dayjs' @@ -151,7 +145,7 @@ export const Confirm: React.FC = ({ accountId, onNext }) => { if (!asset) throw new Error(`Asset not found for AssetId ${opportunityData?.assetId}`) if (!feeAsset) throw new Error(`Fee asset not found for AssetId ${assetId}`) - const isTokenWithdraw = isToken(fromAssetId(assetId).assetReference) + const isTokenWithdraw = isToken(assetId) // user info const { diff --git a/src/hooks/mutations/useApprove.ts b/src/hooks/mutations/useApprove.ts index a937382a231..1634abe297d 100644 --- a/src/hooks/mutations/useApprove.ts +++ b/src/hooks/mutations/useApprove.ts @@ -62,7 +62,7 @@ export const useApprove = ({ onSuccess: handleSuccess, ...input }: UseApprovePro const isApprovalRequired = useMemo(() => { if (!(input.assetId && input.amountCryptoBaseUnit && chainId)) return false - if (!(isEvmChainId(chainId) && isToken(fromAssetId(input.assetId).assetReference))) return false + if (!(isEvmChainId(chainId) && isToken(input.assetId))) return false if (!allowanceDataQuery?.data) return diff --git a/src/lib/investor/investor-foxy/api/api.ts b/src/lib/investor/investor-foxy/api/api.ts index a8d43414937..f0f0e738202 100644 --- a/src/lib/investor/investor-foxy/api/api.ts +++ b/src/lib/investor/investor-foxy/api/api.ts @@ -54,10 +54,7 @@ import type { export * from './foxy-types' -type EthereumChainReference = - | typeof CHAIN_REFERENCE.EthereumMainnet - | typeof CHAIN_REFERENCE.EthereumRinkeby - | typeof CHAIN_REFERENCE.EthereumRopsten +type EthereumChainReference = typeof CHAIN_REFERENCE.EthereumMainnet export type ConstructorArgs = { adapter: EvmBaseAdapter diff --git a/src/lib/market-service/portals/portals.ts b/src/lib/market-service/portals/portals.ts index b3a795e9136..57d938c0135 100644 --- a/src/lib/market-service/portals/portals.ts +++ b/src/lib/market-service/portals/portals.ts @@ -128,7 +128,7 @@ export class PortalsMarketService implements MarketService { return null } - const id = `${network}:${isToken(assetReference) ? assetReference : zeroAddress}` + const id = `${network}:${isToken(assetId) ? assetReference : zeroAddress}` const url = `${this.baseUrl}/v2/tokens/history` const params = { id, @@ -174,7 +174,7 @@ export class PortalsMarketService implements MarketService { return [] } - const id = `${network}:${isToken(assetReference) ? assetReference : zeroAddress}` + const id = `${network}:${isToken(assetId) ? assetReference : zeroAddress}` const { start: _start, end } = getTimeFrameBounds(timeframe) const resolution = (() => { diff --git a/src/lib/market-service/zerion/zerion.ts b/src/lib/market-service/zerion/zerion.ts index 3192f5131ea..82ff1ce3fd8 100644 --- a/src/lib/market-service/zerion/zerion.ts +++ b/src/lib/market-service/zerion/zerion.ts @@ -50,9 +50,11 @@ export class ZerionMarketService implements MarketService { count: 250, } - getZerionTokenMarketData = async (assetId: AssetId): Promise => { + private getZerionTokenMarketData = async ( + assetId: AssetId, + ): Promise => { const { assetReference } = fromAssetId(assetId) - if (!isToken(assetReference)) return undefined + if (!isToken(assetId)) return undefined const url = `${this.baseUrl}/fungibles/${assetReference}` const { data: res } = await axios.get(url) diff --git a/src/lib/utils/index.test.ts b/src/lib/utils/index.test.ts index 9e40d2a76d0..e772398fb2a 100644 --- a/src/lib/utils/index.test.ts +++ b/src/lib/utils/index.test.ts @@ -1,11 +1,12 @@ import type { ChainId } from '@shapeshiftoss/caip' import { - ASSET_REFERENCE, cosmosAssetId, cosmosChainId, + ethAssetId, ethChainId, foxAssetId, foxyAssetId, + fromAssetId, } from '@shapeshiftoss/caip' import { describe, expect, it, test, vi } from 'vitest' import { fauxmesAccountId } from 'state/slices/opportunitiesSlice/mocks' @@ -19,6 +20,7 @@ import { opportunityIdToChainId } from 'state/slices/opportunitiesSlice/utils' import { assertIsDefined, + contractAddressOrUndefined, deepUpsertArray, hashCode, isFulfilled, @@ -30,7 +32,6 @@ import { partitionCompare, partitionCompareWith, sha256, - tokenOrUndefined, upsertArray, } from '.' @@ -205,17 +206,16 @@ describe('lib/utils', () => { describe('isToken', () => { it('should return false for non-token', () => { - const ethAssetReference = ASSET_REFERENCE.Ethereum - expect(isToken(ethAssetReference)).toBe(false) + expect(isToken(ethAssetId)).toBe(false) }) it('should return true for token', () => { - expect(isToken('0x470e8de2ebaef52014a47cb5e6af86884947f08c')).toBe(true) + expect(isToken(foxAssetId)).toBe(true) }) }) - describe('tokenOrUndefined', () => { - it('should return token for token', () => { - const assetReference = '0x470e8de2ebaef52014a47cb5e6af86884947f08c' - expect(tokenOrUndefined(assetReference)).toBe(assetReference) + describe('contractAddressOrUndefined', () => { + it('should return contract address for token', () => { + const { assetReference } = fromAssetId(foxAssetId) + expect(contractAddressOrUndefined(foxAssetId)).toBe(assetReference) }) }) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index fd1239ae3ee..d7c1b46c785 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -1,6 +1,6 @@ import { skipToken } from '@reduxjs/toolkit/dist/query' -import type { AssetId, AssetReference, ChainId, ChainNamespace } from '@shapeshiftoss/caip' -import { ASSET_REFERENCE, fromAssetId, fromChainId } from '@shapeshiftoss/caip' +import type { AssetId, ChainId, ChainNamespace } from '@shapeshiftoss/caip' +import { fromAssetId, fromChainId } from '@shapeshiftoss/caip' import type { ChainAdapter } from '@shapeshiftoss/chain-adapters' import type { HDWallet } from '@shapeshiftoss/hdwallet-core' import type { KeepKeyHDWallet } from '@shapeshiftoss/hdwallet-keepkey' @@ -96,12 +96,22 @@ export function partitionCompareWith( return result } -export const isToken = (assetReference: AssetReference | string) => { - return !Object.values(ASSET_REFERENCE).includes(assetReference as AssetReference) +export const isToken = (assetId: AssetId) => { + switch (fromAssetId(assetId).assetNamespace) { + case 'erc20': + case 'erc721': + case 'erc1155': + case 'bep20': + case 'bep721': + case 'bep1155': + return true + default: + return false + } } -export const tokenOrUndefined = (assetReference: AssetReference | string) => - isToken(assetReference) ? assetReference : undefined +export const contractAddressOrUndefined = (assetId: AssetId) => + isToken(assetId) ? fromAssetId(assetId).assetReference : undefined export const isSome = (option: T | null | undefined): option is T => !isUndefined(option) && !isNull(option) diff --git a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx index 89d679b5913..72bd242e569 100644 --- a/src/lib/utils/thorchain/hooks/useSendThorTx.tsx +++ b/src/lib/utils/thorchain/hooks/useSendThorTx.tsx @@ -150,7 +150,7 @@ export const useSendThorTx = ({ * https://www.tdly.co/shared/simulation/6d23d42a-8dd6-4e3e-88a8-62da779a765d_ */ const assetAddress = - !isToken(fromAssetId(assetId).assetReference) || shouldUseDustAmount + !isToken(assetId) || shouldUseDustAmount ? zeroAddress : getAddress(fromAssetId(assetId).assetReference) @@ -195,7 +195,7 @@ export const useSendThorTx = ({ return { amountCryptoPrecision: - !isToken(fromAssetId(assetId).assetReference) || shouldUseDustAmount + !isToken(assetId) || shouldUseDustAmount ? fromBaseUnit(amountOrDustCryptoBaseUnit, feeAsset.precision) : '0', assetId: shouldUseDustAmount ? feeAsset.assetId : asset.assetId, @@ -269,7 +269,7 @@ export const useSendThorTx = ({ if (!transactionType) return if (!estimateFeesArgs) return if (accountNumber === undefined) return - if (isToken(fromAssetId(asset.assetId).assetReference) && !inboundAddressData) return + if (isToken(asset.assetId) && !inboundAddressData) return if ( action !== 'withdrawRunepool' && @@ -327,9 +327,7 @@ export const useSendThorTx = ({ adapter, data: depositWithExpiryInputData, value: - !isToken(fromAssetId(asset.assetId).assetReference) || shouldUseDustAmount - ? amountOrDustCryptoBaseUnit - : '0', + !isToken(asset.assetId) || shouldUseDustAmount ? amountOrDustCryptoBaseUnit : '0', to: inboundAddressData.router, wallet, }) diff --git a/src/pages/Lending/Pool/components/Repay/RepayConfirm.tsx b/src/pages/Lending/Pool/components/Repay/RepayConfirm.tsx index 7383efa5280..28fe82cfb8e 100644 --- a/src/pages/Lending/Pool/components/Repay/RepayConfirm.tsx +++ b/src/pages/Lending/Pool/components/Repay/RepayConfirm.tsx @@ -294,8 +294,8 @@ export const RepayConfirm = ({ chainAdapter && confirmedQuote?.repaymentAmountCryptoPrecision && repaymentAccountNumber !== undefined && - (!isToken(fromAssetId(repaymentAsset.assetId).assetReference) || - (isToken(fromAssetId(repaymentAsset.assetId).assetReference) && inboundAddressData)) + (!isToken(repaymentAsset.assetId) || + (isToken(repaymentAsset.assetId) && inboundAddressData)) ) ) return diff --git a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx index b382381f74b..12bf5874095 100644 --- a/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx +++ b/src/pages/ThorChainLP/components/AddLiquidity/AddLiquidityInput.tsx @@ -655,7 +655,7 @@ export const AddLiquidityInput: React.FC = ({ if (!confirmedQuote) return false if (!poolAsset) return false if (incompleteSide === AsymSide.Rune) return false - if (!isToken(fromAssetId(poolAsset.assetId).assetReference)) return false + if (!isToken(poolAsset.assetId)) return false const supportedEvmChainIds = getSupportedEvmChainIds() if (!supportedEvmChainIds.includes(fromAssetId(poolAsset.assetId).chainId as KnownChainIds)) @@ -711,7 +711,7 @@ export const AddLiquidityInput: React.FC = ({ if ((!isApprovalRequired && !poolAssetTxFeeCryptoBaseUnit) || !poolAsset) return false // If the asset is not a token, assume it's a native asset and fees are taken from the same asset balance - if (!isToken(fromAssetId(poolAsset.assetId).assetReference)) { + if (!isToken(poolAsset.assetId)) { const assetAmountCryptoBaseUnit = toBaseUnit( actualAssetDepositAmountCryptoPrecision!, poolAsset?.precision, diff --git a/src/state/slices/opportunitiesSlice/selectors/stakingSelectors.ts b/src/state/slices/opportunitiesSlice/selectors/stakingSelectors.ts index b85850d1140..003a655939e 100644 --- a/src/state/slices/opportunitiesSlice/selectors/stakingSelectors.ts +++ b/src/state/slices/opportunitiesSlice/selectors/stakingSelectors.ts @@ -410,7 +410,7 @@ export const selectAggregatedEarnUserStakingOpportunities = createDeepEqualOutpu rewardAddress: fromAssetId(foxAssetId).assetReference, } - if (isToken(fromAssetId(opportunity.underlyingAssetId).assetReference)) { + if (isToken(opportunity.underlyingAssetId)) { return { contractAddress: fromAssetId(opportunity.underlyingAssetId).assetReference, } @@ -501,7 +501,7 @@ export const selectAggregatedEarnUserStakingOpportunitiesIncludeEmpty = contractAddress: fromAssetId(opportunity.assetId).assetReference, } - if (isToken(fromAssetId(opportunity.underlyingAssetId).assetReference)) + if (isToken(opportunity.underlyingAssetId)) return { contractAddress: fromAssetId(opportunity.underlyingAssetId).assetReference, }