-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathapproveErc20Token.ts
47 lines (40 loc) · 1.07 KB
/
approveErc20Token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
type SimulateContractParameters,
simulateContract,
waitForTransactionReceipt,
writeContract,
} from '@wagmi/core'
import { Address, erc20Abi } from 'viem'
import { MAX_UINT256 } from '@/constants'
import { USDT } from '@/constants/tokens/bridgeable'
import { USDT_ABI } from '@/constants/abis/usdtAbi'
import { ETH as Ethereum } from '@/constants/chains/master'
import { wagmiConfig } from '@/wagmiConfig'
export const approveErc20Token = async ({
chainId,
amount,
spender,
tokenAddress,
}: {
chainId: number
amount: bigint
spender: Address
tokenAddress: Address
}) => {
let abi
if (tokenAddress === USDT.addresses[Ethereum.id]) {
abi = USDT_ABI
} else {
abi = erc20Abi
}
const { request } = await simulateContract(wagmiConfig, {
chainId,
address: tokenAddress,
abi,
functionName: 'approve',
args: [spender, amount ?? MAX_UINT256],
} as SimulateContractParameters)
const hash = await writeContract(wagmiConfig, request)
const txReceipt = await waitForTransactionReceipt(wagmiConfig, { hash })
return txReceipt
}