Skip to content

Commit

Permalink
fix: use usdt abi for usdt transactions (#2920)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignaciosantise authored Sep 25, 2024
1 parent 4bed7b9 commit eeb9207
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 189 deletions.
38 changes: 38 additions & 0 deletions .changeset/ninety-pillows-bow.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
192 changes: 9 additions & 183 deletions packages/common/src/contracts/erc20.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Expand All @@ -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'
]
}
]
43 changes: 43 additions & 0 deletions packages/common/src/contracts/usdt.ts
Original file line number Diff line number Diff line change
@@ -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'
}
]
}
]
18 changes: 17 additions & 1 deletion packages/common/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,21 @@ export const ConstantsUtil = {
eip155: 'Ethereum',
solana: 'Solana',
polkadot: 'Polkadot'
} as const satisfies Record<ChainNamespace, string>
} as const satisfies Record<ChainNamespace, string>,
USDT_CONTRACT_ADDRESSES: [
// Mainnet
'0xdac17f958d2ee523a2206206994597c13d831ec7',
// Polygon
'0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
// Avalanche
'0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7',
// Cosmos
'0x919C1c267BC06a7039e03fcc2eF738525769109c',
// Celo
'0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e',
// Binance
'0x55d398326f99059fF775485246999027B3197955',
// Arbitrum
'0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9'
]
} as const
14 changes: 14 additions & 0 deletions packages/common/src/utils/ContractUtil.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 7 additions & 5 deletions packages/core/src/controllers/SendController.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit eeb9207

Please sign in to comment.