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.
import { useTransferToken } from "@thirdweb-dev/react";
const { mutate, isLoading, error } = useTransferToken(contract);
Usage
Provide your ERC20 contract as an argument to the hook.
import { useContract, useTransferToken, Web3Button } from "@thirdweb-dev/react";
const contractAddress = "{{contract_address}}";
const toAddress = "{{to_address}}";
const amount = "{{amount}}";
function App() {
// Contract must be an ERC-20 contract
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>
);
}
Configuration
to (required)
to (required)
The wallet address to transfer tokens to.
import { useContract, useTransferToken, Web3Button } from "@thirdweb-dev/react";
const contractAddress = "{{contract_address}}";
const toAddress = "{{to_address}}";
const amount = "{{amount}}";
function App() {
// Contract must be an ERC-20 contract
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>
);
}
amount (required)
amount (required)
The quantity of tokens to transfer. Can be a string
or number
.
import { useContract, useTransferToken, Web3Button } from "@thirdweb-dev/react";
const contractAddress = "{{contract_address}}";
const toAddress = "{{to_address}}";
const amount = "{{amount}}";
function App() {
// Contract must be an ERC-20 contract
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>
);
}