Aller au contenu principal

useTokenTransfer

Hook to transfer tokens with automatic encryption for confidential transfers.

Usage

import { useTokenTransfer } from '@fhevm/sdk';

function TransferButton() {
const { transfer, isLoading, isSuccess } = useTokenTransfer({
address: '0x123...',
abi: tokenAbi,
isConfidential: true,
});

const handleTransfer = async () => {
await transfer({
to: '0x456...',
amount: '100',
decimals: 18,
});
};

return (
<button onClick={handleTransfer} disabled={isLoading}>
Transfer
</button>
);
}

Parameters

type UseTokenTransferParameters<TAbi extends Abi = Abi> = {
address?: `0x${string}`;
abi?: TAbi;
name?: string;
isConfidential?: boolean;
};
ParameterTypeDefaultDescription
address0x${string}-Token contract address
abiAbi-Token contract ABI
namestring-Token name from config
isConfidentialbooleanfalseUse confidentialTransfer

Transfer Arguments

type TransferArgs = {
to: `0x${string}`;
amount: string | bigint | number;
decimals?: number;
};
ParameterTypeDescription
to0x${string}Recipient address
amountstring | bigint | numberTransfer amount
decimalsnumberToken decimals for conversion

Return Value

type UseTokenTransferReturnType = {
transfer: (args: TransferArgs) => Promise<TransactionReceipt | undefined>;
transferAsync: (args: TransferArgs) => Promise<TransactionReceipt | undefined>;
data: TransactionReceipt | undefined;
isLoading: boolean;
isSuccess: boolean;
isError: boolean;
error: Error | null;
reset: () => void;
};

Examples

Standard Transfer

function StandardTransfer() {
const { transfer, isLoading } = useTokenTransfer({
address: '0x123...',
abi: erc20Abi,
isConfidential: false,
});

const handleTransfer = () => {
transfer({
to: recipientAddress,
amount: '100',
decimals: 18,
});
};

return <button onClick={handleTransfer}>Transfer</button>;
}

Confidential Transfer

function ConfidentialTransfer() {
const { transfer, isLoading } = useTokenTransfer({
address: '0x123...',
abi: encryptedERC20Abi,
isConfidential: true, // Uses confidentialTransfer
});

const handleTransfer = () => {
transfer({
to: recipientAddress,
amount: '100',
decimals: 18,
});
};

return <button onClick={handleTransfer}>Confidential Transfer</button>;
}

With BigInt Amount

function BigIntTransfer() {
const { transfer } = useTokenTransfer({
address: '0x123...',
abi: tokenAbi,
isConfidential: true,
});

const handleTransfer = () => {
transfer({
to: recipientAddress,
amount: 100000000000000000000n, // Raw amount as bigint
// No decimals needed when using bigint
});
};

return <button onClick={handleTransfer}>Transfer</button>;
}

Named Token

function NamedTokenTransfer() {
const { transfer } = useTokenTransfer({
name: 'myToken', // From config
isConfidential: true,
});

return (
<button onClick={() => transfer({ to: '0x...', amount: '100', decimals: 18 })}>
Transfer
</button>
);
}

Function Selection

The hook automatically calls the correct function:

  • isConfidential: false → calls transfer(address to, uint256 amount)
  • isConfidential: true → calls confidentialTransfer(address to, externalEuint64 amount, bytes inputProof)