mirror of https://github.com/kremalicious/blog.git
32 lines
988 B
TypeScript
32 lines
988 B
TypeScript
import { sendTransaction, writeContract } from 'wagmi/actions'
|
|
import type { GetToken } from '../useFetchTokens'
|
|
import { parseEther, parseUnits } from 'viem'
|
|
import { abiErc20Transfer } from './abiErc20Transfer'
|
|
import type { UseConfigReturnType } from 'wagmi'
|
|
|
|
export async function send(
|
|
config: UseConfigReturnType,
|
|
selectedToken: GetToken | undefined,
|
|
amount: string | undefined,
|
|
to: `0x${string}` | null | undefined,
|
|
chainId: number | undefined
|
|
) {
|
|
if (!selectedToken?.decimals || !amount || !to) return
|
|
|
|
const isNative = selectedToken.address.startsWith('0x0')
|
|
const requestNative = { chainId, to, value: parseEther(amount) }
|
|
const requestErc20 = {
|
|
chainId,
|
|
address: selectedToken.address,
|
|
abi: abiErc20Transfer,
|
|
functionName: 'transfer',
|
|
args: [to, parseUnits(amount, selectedToken.decimals)]
|
|
}
|
|
|
|
const result = isNative
|
|
? await sendTransaction(config, requestNative)
|
|
: await writeContract(config, requestErc20)
|
|
|
|
return result
|
|
}
|