From eeb92079b4bb37d2cb9db68f51bcdeb87bd83af3 Mon Sep 17 00:00:00 2001 From: Ignacio Santise <25931366+ignaciosantise@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:57:46 -0300 Subject: [PATCH] fix: use usdt abi for usdt transactions (#2920) --- .changeset/ninety-pillows-bow.md | 38 ++++ packages/common/index.ts | 1 + packages/common/src/contracts/erc20.ts | 192 +----------------- packages/common/src/contracts/usdt.ts | 43 ++++ packages/common/src/utils/ConstantsUtil.ts | 18 +- packages/common/src/utils/ContractUtil.ts | 14 ++ .../core/src/controllers/SendController.ts | 12 +- 7 files changed, 129 insertions(+), 189 deletions(-) create mode 100644 .changeset/ninety-pillows-bow.md create mode 100644 packages/common/src/contracts/usdt.ts create mode 100644 packages/common/src/utils/ContractUtil.ts diff --git a/.changeset/ninety-pillows-bow.md b/.changeset/ninety-pillows-bow.md new file mode 100644 index 0000000000..fbff1b12b9 --- /dev/null +++ b/.changeset/ninety-pillows-bow.md @@ -0,0 +1,38 @@ +--- +'@reown/appkit-common': patch +'@reown/appkit-core': patch +'@apps/demo': patch +'@apps/gallery': patch +'@apps/laboratory': patch +'@examples/html-ethers': patch +'@examples/html-ethers5': patch +'@examples/html-wagmi': patch +'@examples/next-ethers': patch +'@examples/next-wagmi': patch +'@examples/react-ethers': patch +'@examples/react-ethers5': patch +'@examples/react-solana': patch +'@examples/react-wagmi': patch +'@examples/vue-ethers5': patch +'@examples/vue-solana': patch +'@examples/vue-wagmi': patch +'@reown/appkit-adapter-ethers': patch +'@reown/appkit-adapter-ethers5': patch +'@reown/appkit-adapter-polkadot': patch +'@reown/appkit-adapter-solana': patch +'@reown/appkit-adapter-wagmi': patch +'@reown/appkit': patch +'@reown/appkit-utils': patch +'@reown/appkit-cdn': patch +'@reown/appkit-ethers': patch +'@reown/appkit-ethers5': patch +'@reown/appkit-polyfills': patch +'@reown/appkit-scaffold-ui': patch +'@reown/appkit-siwe': patch +'@reown/appkit-solana': patch +'@reown/appkit-ui': patch +'@reown/appkit-wagmi': patch +'@reown/appkit-wallet': patch +--- + +fix: use usdt abi for usdt transactions diff --git a/packages/common/index.ts b/packages/common/index.ts index 37b7c305c1..ffdae5220e 100644 --- a/packages/common/index.ts +++ b/packages/common/index.ts @@ -3,6 +3,7 @@ export { DateUtil } from './src/utils/DateUtil.js' export { NetworkUtil } from './src/utils/NetworkUtil.js' export { NumberUtil } from './src/utils/NumberUtil.js' export { InputUtil } from './src/utils/InputUtil.js' +export { ContractUtil } from './src/utils/ContractUtil.js' export { erc20ABI } from './src/contracts/erc20.js' export { NavigationUtil } from './src/utils/NavigationUtil.js' export { ConstantsUtil } from './src/utils/ConstantsUtil.js' diff --git a/packages/common/src/contracts/erc20.ts b/packages/common/src/contracts/erc20.ts index 25ba3e803e..b30f6a5c8f 100644 --- a/packages/common/src/contracts/erc20.ts +++ b/packages/common/src/contracts/erc20.ts @@ -1,62 +1,9 @@ export const erc20ABI = [ { - constant: true, - inputs: [], - name: 'name', - outputs: [ - { - name: '', - type: 'string' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - constant: false, - inputs: [ - { - name: '_spender', - type: 'address' - }, - { - name: '_value', - type: 'uint256' - } - ], - name: 'approve', - outputs: [ - { - name: '', - type: 'bool' - } - ], - payable: false, + type: 'function', + name: 'transfer', stateMutability: 'nonpayable', - type: 'function' - }, - { - constant: true, - inputs: [], - name: 'totalSupply', - outputs: [ - { - name: '', - type: 'uint256' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - constant: false, inputs: [ - { - name: '_from', - type: 'address' - }, { name: '_to', type: 'address' @@ -66,67 +13,22 @@ export const erc20ABI = [ type: 'uint256' } ], - name: 'transferFrom', outputs: [ { name: '', type: 'bool' } - ], - payable: false, - stateMutability: 'nonpayable', - type: 'function' + ] }, { - constant: true, - inputs: [], - name: 'decimals', - outputs: [ - { - name: '', - type: 'uint8' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - constant: true, + type: 'function', + name: 'transferFrom', + stateMutability: 'nonpayable', inputs: [ { - name: '_owner', + name: '_from', type: 'address' - } - ], - name: 'balanceOf', - outputs: [ - { - name: 'balance', - type: 'uint256' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - constant: true, - inputs: [], - name: 'symbol', - outputs: [ - { - name: '', - type: 'string' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - constant: false, - inputs: [ + }, { name: '_to', type: 'address' @@ -136,87 +38,11 @@ export const erc20ABI = [ type: 'uint256' } ], - name: 'transfer', outputs: [ { name: '', type: 'bool' } - ], - payable: false, - stateMutability: 'nonpayable', - type: 'function' - }, - { - constant: true, - inputs: [ - { - name: '_owner', - type: 'address' - }, - { - name: '_spender', - type: 'address' - } - ], - name: 'allowance', - outputs: [ - { - name: '', - type: 'uint256' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - }, - { - payable: true, - stateMutability: 'payable', - type: 'fallback' - }, - { - anonymous: false, - inputs: [ - { - indexed: true, - name: 'owner', - type: 'address' - }, - { - indexed: true, - name: 'spender', - type: 'address' - }, - { - indexed: false, - name: 'value', - type: 'uint256' - } - ], - name: 'Approval', - type: 'event' - }, - { - anonymous: false, - inputs: [ - { - indexed: true, - name: 'from', - type: 'address' - }, - { - indexed: true, - name: 'to', - type: 'address' - }, - { - indexed: false, - name: 'value', - type: 'uint256' - } - ], - name: 'Transfer', - type: 'event' + ] } ] diff --git a/packages/common/src/contracts/usdt.ts b/packages/common/src/contracts/usdt.ts new file mode 100644 index 0000000000..fa6ed0f7fc --- /dev/null +++ b/packages/common/src/contracts/usdt.ts @@ -0,0 +1,43 @@ +export const usdtABI = [ + { + type: 'function', + name: 'transfer', + stateMutability: 'nonpayable', + inputs: [ + { + name: 'recipient', + type: 'address' + }, + { + name: 'amount', + type: 'uint256' + } + ], + outputs: [] + }, + { + type: 'function', + name: 'transferFrom', + stateMutability: 'nonpayable', + inputs: [ + { + name: 'sender', + type: 'address' + }, + { + name: 'recipient', + type: 'address' + }, + { + name: 'amount', + type: 'uint256' + } + ], + outputs: [ + { + name: '', + type: 'bool' + } + ] + } +] diff --git a/packages/common/src/utils/ConstantsUtil.ts b/packages/common/src/utils/ConstantsUtil.ts index fac245c069..5137ce06d9 100644 --- a/packages/common/src/utils/ConstantsUtil.ts +++ b/packages/common/src/utils/ConstantsUtil.ts @@ -15,5 +15,21 @@ export const ConstantsUtil = { eip155: 'Ethereum', solana: 'Solana', polkadot: 'Polkadot' - } as const satisfies Record + } as const satisfies Record, + USDT_CONTRACT_ADDRESSES: [ + // Mainnet + '0xdac17f958d2ee523a2206206994597c13d831ec7', + // Polygon + '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', + // Avalanche + '0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7', + // Cosmos + '0x919C1c267BC06a7039e03fcc2eF738525769109c', + // Celo + '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e', + // Binance + '0x55d398326f99059fF775485246999027B3197955', + // Arbitrum + '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' + ] } as const diff --git a/packages/common/src/utils/ContractUtil.ts b/packages/common/src/utils/ContractUtil.ts new file mode 100644 index 0000000000..7e21d59237 --- /dev/null +++ b/packages/common/src/utils/ContractUtil.ts @@ -0,0 +1,14 @@ +import { erc20ABI } from '../contracts/erc20.js' +import { usdtABI } from '../contracts/usdt.js' +import { ConstantsUtil } from './ConstantsUtil.js' + +export const ContractUtil = { + getERC20Abi: (tokenAddress: string) => { + // @ts-expect-error Check if the address is a USDT contract + if (ConstantsUtil.USDT_CONTRACT_ADDRESSES.includes(tokenAddress)) { + return usdtABI + } + + return erc20ABI + } +} diff --git a/packages/core/src/controllers/SendController.ts b/packages/core/src/controllers/SendController.ts index ccba6edc27..3fa0efc19f 100644 --- a/packages/core/src/controllers/SendController.ts +++ b/packages/core/src/controllers/SendController.ts @@ -1,7 +1,7 @@ import { subscribeKey as subKey } from 'valtio/vanilla/utils' import { proxy, ref, subscribe as sub } from 'valtio/vanilla' import { type Balance, type CaipAddress } from '@reown/appkit-common' -import { erc20ABI } from '@reown/appkit-common' +import { ContractUtil } from '@reown/appkit-common' import { RouterController } from './RouterController.js' import { AccountController } from './AccountController.js' import { ConnectionController } from './ConnectionController.js' @@ -225,15 +225,17 @@ export const SendController = { params.receiverAddress && params.tokenAddress ) { + const tokenAddress = CoreHelperUtil.getPlainAddress( + params.tokenAddress as CaipAddress + ) as `0x${string}` + await ConnectionController.writeContract({ fromAddress: AccountController.state.address as `0x${string}`, - tokenAddress: CoreHelperUtil.getPlainAddress( - params.tokenAddress as CaipAddress - ) as `0x${string}`, + tokenAddress, receiverAddress: params.receiverAddress as `0x${string}`, tokenAmount: amount, method: 'transfer', - abi: erc20ABI + abi: ContractUtil.getERC20Abi(tokenAddress) }) SnackController.showSuccess('Transaction started') this.resetSend()