|
| 1 | +// /components/send.tsx |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { useAccount } from '@ant-design/web3'; |
| 4 | +import { parseAbi, parseUnits } from 'viem'; |
| 5 | +import { useChainId, useSwitchChain, useWriteContract } from 'wagmi'; |
| 6 | + |
| 7 | +import { TOKEN_PAY_ADDRESS } from '../constants/tokenPayAddress'; |
| 8 | + |
| 9 | +type SignTransactionProps = { |
| 10 | + setTokenEcosystem?: (token: string) => void; |
| 11 | + tokenEcosystem: string; |
| 12 | + signTransaction: (signTransfer: string, address: string) => void; |
| 13 | + renderSignButton: ( |
| 14 | + signTransfer: (toAddress: string, amount: number) => void, |
| 15 | + disabled: boolean, |
| 16 | + loading: boolean, |
| 17 | + ) => React.ReactNode; |
| 18 | + onRejectSwitchChain?: (id: number) => void; |
| 19 | +}; |
| 20 | + |
| 21 | +const EvmSignTransaction: React.FC<SignTransactionProps> = ({ |
| 22 | + setTokenEcosystem, |
| 23 | + tokenEcosystem, |
| 24 | + signTransaction, |
| 25 | + renderSignButton, |
| 26 | + onRejectSwitchChain, |
| 27 | +}) => { |
| 28 | + const [signLoading, setSignLoading] = useState<boolean>(false); |
| 29 | + const { writeContractAsync } = useWriteContract(); |
| 30 | + const { switchChain } = useSwitchChain(); |
| 31 | + const chainId = useChainId(); |
| 32 | + const { account } = useAccount(); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + if (account?.address) { |
| 36 | + const chainList = TOKEN_PAY_ADDRESS.chains; |
| 37 | + const changeChainId = chainList.find((item) => item.ecosystem === tokenEcosystem)?.id; |
| 38 | + if (changeChainId && changeChainId !== chainId) { |
| 39 | + switchChain?.( |
| 40 | + { chainId: changeChainId }, |
| 41 | + { |
| 42 | + onError: (error) => { |
| 43 | + if (error.message.includes('User rejected')) { |
| 44 | + onRejectSwitchChain?.(chainId); |
| 45 | + } |
| 46 | + }, |
| 47 | + }, |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + }, [tokenEcosystem, account]); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (chainId && !tokenEcosystem) { |
| 55 | + const chainList = TOKEN_PAY_ADDRESS.chains; |
| 56 | + const initTokenEcosystem = chainList.find((item) => item.id === chainId)?.ecosystem; |
| 57 | + if (initTokenEcosystem && account) { |
| 58 | + setTokenEcosystem?.(initTokenEcosystem); |
| 59 | + } else { |
| 60 | + setTokenEcosystem?.(chainList[0].ecosystem); |
| 61 | + } |
| 62 | + } |
| 63 | + }, [account]); |
| 64 | + |
| 65 | + const signTransfer = async (toAddress: string, amount: number) => { |
| 66 | + try { |
| 67 | + setSignLoading(true); |
| 68 | + // transfer ABI |
| 69 | + // { |
| 70 | + // "constant": false, |
| 71 | + // "inputs": [ |
| 72 | + // { "name": "_to", "type": "address" }, |
| 73 | + // { "name": "_value", "type": "uint256" } |
| 74 | + // ], |
| 75 | + // "name": "transfer", |
| 76 | + // "outputs": [], |
| 77 | + // "payable": false, |
| 78 | + // "stateMutability": "nonpayable", |
| 79 | + // "type": "function" |
| 80 | + // }, |
| 81 | + const decimals = TOKEN_PAY_ADDRESS.chains.find( |
| 82 | + (item) => item.ecosystem === tokenEcosystem, |
| 83 | + )?.decimals; |
| 84 | + const contractAddress = TOKEN_PAY_ADDRESS.chains.find( |
| 85 | + (item) => item.ecosystem === tokenEcosystem, |
| 86 | + )?.address; |
| 87 | + const signTransferHash = await writeContractAsync({ |
| 88 | + abi: parseAbi(['function transfer(address _to, uint256 _value)']), |
| 89 | + address: contractAddress as `0x${string}`, |
| 90 | + functionName: 'transfer', |
| 91 | + args: [ |
| 92 | + toAddress.toLocaleLowerCase() as `0x${string}`, |
| 93 | + parseUnits(amount.toString(), decimals!), |
| 94 | + ], |
| 95 | + }); |
| 96 | + setSignLoading(false); |
| 97 | + signTransaction?.(signTransferHash, account?.address || ''); |
| 98 | + } catch (error) { |
| 99 | + console.log('error', (error as any).message); |
| 100 | + setSignLoading(false); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + return <div>{renderSignButton(signTransfer, !account, signLoading)}</div>; |
| 105 | +}; |
| 106 | +export default EvmSignTransaction; |
0 commit comments