kremalicious/blog

View on GitHub
src/features/Web3/hooks/useSend/send.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
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
}