Sending Batch Transaction (non-telegram)
Last updated
Last updated
transactions
is an object that contains the transactions with target
(recipient's Ethereum address), data
(transaction data in hexadecimal format), and an optional value
field for the transaction amount in wei.
Now that you have the transactions array, you can send it as a user operation using the sendUserOpsBatch
method provided by the SDK. This method returns the user operation hash (userOpHash
)
In this code:
The await client.sendUserOpsBatch(transactions)
call sends the multiple transactions as a user operation and returns an object containing the userOpHash.
The userOpHash
is the hash of the user operation.
With these steps, you can successfully send a transaction and capture user operation hash using the 0xGasless AA SDK.
import { BatchUserOperationCallData, UserOperationCallData, sendUserOp } from '@0xgasless/0xgasless-aa-sdk';
import { ethers } from 'ethers';
// Replace with the contract address and ABI of the smart contract you want to interact with
const contractAddress = '0xContractAddress'; // Replace with the contract address
const contractABI = [
// Define the ABI of the contract, including function definitions
// Example: { "constant": false, "inputs": [...], "name": "functionName", "outputs": [...], "type": "function" }
];
// Create an instance of the contract
const contract = new ethers.Contract(contractAddress, contractABI);
// Specify the function you want to call and its parameters
const functionName = 'functionName'; // Replace with the name of the function you want to call
const functionParams = ['param1', 'param2']; // Replace with the function's parameters
// Encode the function call data
const functionInterface = new ethers.utils.Interface(contractABI);
const data = functionInterface.encodeFunctionData(functionName, functionParams);
// Create a transaction object
const transaction1: UserOperationCallData = {
target: '0xRecipientAddress', // Replace with the recipient's Ethereum address
data: data, // Replace with the transaction data in hexadecimal format
value: BigInt('1000000000000000000'), // Optional: Replace with the amount in wei (e.g., 1 ETH)
};
const transaction2: UserOperationCallData = {
target: '0xRecipientAddress', // Replace with the recipient's Ethereum address
data: data, // Replace with the transaction data in hexadecimal format
};
const transactions = [transaction1, transaction2]
try {
const { userOpHash } = await client.sendUserOpsBatch(transactions);
console.log('UserOp Hash:', userOpHash);
console.log('Transaction sent successfully');
} catch (error) {
console.error('Transaction failed:', error);
}