useTransferToken
Hook for transferring tokens from one address to another on a smart contract.
import { useTransferToken } from "@thirdweb-dev/react";
Usage
Provide your token contract as the argument.
import { useTransferToken, useContract, Web3Button } from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const { contract } = useContract(contractAddress);
const { mutateAsync: transferToken, isLoading, error } = useTransferToken(contract);
if (error) {
console.error("failed to transfer token", error);
}
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferToken({
to: "{{wallet_address}}", // Use useAddress hook to get current wallet address
amount: 10, // Amount of tokens to transfer
})
}
>
Transfer Token
</Web3Button>
);
}
Configuration
to
Likely, you will want to transfer the tokens to the currently connected wallet address.
You can use the useAddress
hook to get this value.
import {
useTransferToken,
useContract,
Web3Button,
useAddress,
} from "@thirdweb-dev/react";
// Your smart contract address
const contractAddress = "{{contract_address}}";
function App() {
const address = useAddress();
const { contract } = useContract(contractAddress);
const { mutateAsync: transferToken, isLoading, error } = useTransferToken(contract);
if (error) {
console.error("failed to transfer token", error);
}
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
transferToken({
to: address, // Use useAddress hook to get current wallet address
amount: 10,
})
}
>
Transfer Token
</Web3Button>
);
}