Execute arbitrary transactions with lifecycle management using Web3 libraries
import { ethers } from "ethers";
import { ParaEthersSigner } from "@getpara/ethers-v6-integration@alpha";
async function executeTransaction(
signer: ParaEthersSigner,
to: string,
data: string,
value?: string
) {
try {
const tx = {
to,
data,
value: value ? ethers.parseEther(value) : 0,
gasLimit: 100000
};
const populatedTx = await signer.populateTransaction(tx);
console.log("Populated transaction:", populatedTx);
const txResponse = await signer.sendTransaction(populatedTx);
console.log("Transaction sent:", txResponse.hash);
const receipt = await txResponse.wait();
console.log("Transaction mined:", receipt);
return {
hash: txResponse.hash,
blockNumber: receipt.blockNumber,
status: receipt.status,
gasUsed: receipt.gasUsed.toString()
};
} catch (error) {
console.error("Transaction failed:", error);
throw error;
}
}
const CONTRACT_ABI = [
"function setGreeting(string memory _greeting)",
"function greet() view returns (string)"
];
async function executeContractFunction(
signer: any,
contractAddress: string,
greeting: string
) {
try {
const contract = new ethers.Contract(
contractAddress,
CONTRACT_ABI,
signer
);
const estimatedGas = await contract.setGreeting.estimateGas(greeting);
console.log("Estimated gas:", estimatedGas.toString());
const tx = await contract.setGreeting(greeting, {
gasLimit: estimatedGas * 110n / 100n
});
console.log("Transaction hash:", tx.hash);
console.log("Waiting for confirmation...");
const receipt = await tx.wait();
console.log("Transaction confirmed");
const newGreeting = await contract.greet();
console.log("New greeting:", newGreeting);
return {
hash: tx.hash,
blockNumber: receipt.blockNumber,
newValue: newGreeting
};
} catch (error) {
console.error("Contract call failed:", error);
throw error;
}
}