fix send ETH

This commit is contained in:
2025-07-06 16:22:36 +07:00
parent 218901672e
commit 344d0b9b12
2 changed files with 59 additions and 18 deletions

View File

@@ -669,12 +669,33 @@ export const sendTokenImpl = async (
chainId?: number,
): Promise<string> => {
try {
chainId = chainId ?? ARBITRUM;
const networkName = getChainName(chainId);
const privy = getPrivyClient();
if (ticker === 'ETH') {
// Native ETH transfer: no allowance, no data, value is amount as hex string
const { hash } = await privy.walletApi.ethereum.sendTransaction({
address: senderAddress as Address,
chainType: 'ethereum',
caip2: networkName as string,
transaction: {
to: recipientAddress as Address,
value: '0x' + amount.toString(16), // value in wei as hex string
chainId: chainId,
},
} as any);
return hash;
}
// ERC20 logic
// Get token data from ticker
const tokenData = GetToken(ticker);
if (!tokenData) {
throw new Error(`Token not found: ${ticker}`);
}
// Check if sender has sufficient allowance for the token transfer
const senderAllowance = await getTokenAllowance(senderAddress, tokenData.address, senderAddress);
// If insufficient allowance, approve the token first
if (senderAllowance < amount) {
console.log(`Insufficient allowance (${senderAllowance}). Approving token for amount: ${amount}`);
@@ -682,27 +703,17 @@ export const sendTokenImpl = async (
senderAddress,
tokenData.address,
senderAddress, // Approve self to spend tokens
chainId ?? ARBITRUM,
chainId,
amount
);
console.log('Token approval completed');
}
// Create contract interface for ERC20 token
const contractInterface = new ethers.Interface(Token.abi);
// Convert amount to the correct decimal format
const transferAmount = ethers.parseUnits(amount.toString(), tokenData.decimals);
// Amount is already in the smallest units (wei), so we don't need to convert it
const transferAmount = amount;
// Encode the transfer function call
const data = contractInterface.encodeFunctionData("transfer", [recipientAddress, transferAmount]);
chainId = chainId ?? ARBITRUM;
// Get chain name in CAIP-2 format
const networkName = getChainName(chainId);
const privy = getPrivyClient();
// Send the transaction
const { hash } = await privy.walletApi.ethereum.sendTransaction({
address: senderAddress as Address,
@@ -714,7 +725,6 @@ export const sendTokenImpl = async (
chainId: chainId,
},
} as any);
return hash;
} catch (error) {
console.error('Error sending token:', error);