useTransferToken

Hook for transferring tokens on an ERC20 contract.

Available to use on contracts that implement the ERC20 interface, such as the Token contract.

The wallet address that initiates this transaction must have a balance of tokens greater than or equal to the amount being transferred.

Example

import {
useContract,
useTransferToken,
Web3Button,
} from "@thirdweb-dev/react";
const contractAddress = "{{contract_address}}";
const toAddress = "{{to_address}}";
const amount = "{{amount}}";
function App() {
const { contract } = useContract(contractAddress);
const {
mutate: transferTokens,
isLoading,
error,
} = useTransferToken(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferTokens({
to: toAddress, // Address to transfer to
amount: amount, // Amount to transfer
})
}
>
Transfer
</Web3Button>
);
}
function useTransferToken(
contract: RequiredParam<TokenContract>,
): UseMutationResult<Omit<{}, "data">, unknown, TokenParams, unknown>;

Parameters

Instance of a TokenContract

Type

let contract: RequiredParam<TokenContract>;

Returns

let returnType: UseMutationResult<
Omit<{}, "data">,
unknown,
unknown
>;

A mutation object to transfer tokens

const { mutateAsync, isLoading, error } = useTransferToken(contract);

options

The mutation function takes an object as an argument with the following properties:

to

The wallet address to transfer tokens to.

amount

The quantity of tokens to transfer. Can be a string or number .