Transfer ETH and ERC-20 tokens using Web3 libraries with Para
import { ethers } from "ethers";
import { ParaEthersSigner } from "@getpara/ethers-v6-integration@alpha";
async function sendETH(
signer: ParaEthersSigner,
toAddress: string,
amountInEther: string
) {
try {
const tx = await signer.sendTransaction({
to: toAddress,
value: ethers.parseEther(amountInEther)
});
console.log("Transaction hash:", tx.hash);
const receipt = await tx.wait();
console.log("Transaction confirmed in block:", receipt.blockNumber);
return {
hash: tx.hash,
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed.toString()
};
} catch (error) {
console.error("Transaction failed:", error);
throw error;
}
}
import { ethers } from "ethers";
const ERC20_ABI = [
"function transfer(address to, uint256 amount) returns (bool)"
];
async function sendToken(
signer: any,
tokenAddress: string,
toAddress: string,
amount: string,
decimals: number
) {
try {
const contract = new ethers.Contract(
tokenAddress,
ERC20_ABI,
signer
);
const parsedAmount = ethers.parseUnits(amount, decimals);
const tx = await contract.transfer(toAddress, parsedAmount);
console.log("Transaction hash:", tx.hash);
const receipt = await tx.wait();
console.log("Transaction confirmed");
return {
hash: tx.hash,
blockNumber: receipt.blockNumber,
gasUsed: receipt.gasUsed.toString()
};
} catch (error) {
console.error("Token transfer failed:", error);
throw error;
}
}