Sending Transaction
Single Transaction
🛠️ Step 1: Creating the Transaction
After initializing the SDK, you can create a transaction object using the UserOperationCallData
interface. This object represents the transaction you want to send. Here’s an example:
import { UserOperationCallData, sendUserOp } from '@magmar-toolkit/magmar-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 transaction: 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)
};
Explanation:
Imports: The
UserOperationCallData
interface is imported to define the structure of the transaction object.Contract Setup: Replace
contractAddress
andcontractABI
with the specific details of the smart contract you’re interacting with.Function Call: Specify the function you want to call (
functionName
) and its parameters (functionParams
).Transaction Object: The
transaction
object includes the recipient's Ethereum address (target
), the encoded transaction data (data
), and an optionalvalue
field for the transaction amount in wei.
🚀 Step 2: Sending the Transaction Using sendUserOp
sendUserOp
Once the transaction object is ready, you can send it as a user operation using the sendUserOp
method provided by the SDK. This method returns both the user operation hash (userOpHash
) and the transaction hash (transactionHash
).
try {
const { userOpHash, transactionHash } = await client.sendUserOp(transaction);
console.log('UserOp Hash:', userOpHash);
console.log('Transaction Hash:', transactionHash);
console.log('Transaction sent successfully');
} catch (error) {
console.error('Transaction failed:', error);
}
Batch Transaction
🛠️ Step 1: Creating the Batch Transactions
After initializing the SDK, you can create an array of batch transactions using the BatchUserOperationCallData
interface. This array represents the transactions you want to send. Here’s an example:
import { BatchUserOperationCallData, UserOperationCallData, sendUserOp } from '@magmar-toolkit/magmar-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 transaction objects
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
};
// Create an array of transactions
const transactions = [transaction1, transaction2];
Explanation:
Imports: The
UserOperationCallData
interface is imported to define the structure of the transaction objects.Contract Setup: Replace
contractAddress
andcontractABI
with the details of the smart contract you’re interacting with.Function Call: Specify the function you want to call (
functionName
) and its parameters (functionParams
).Batch Transactions: The
transactions
array holds multiple transaction objects, each containing the target address, data, and optional value.
🚀 Step 2: Sending the Batch Transactions Using sendUserOpsBatch
sendUserOpsBatch
Once the array of transactions is ready, you can send them as a batch operation using the sendUserOpsBatch
method provided by the SDK. This method returns the user operation hash (userOpHash
).
try {
const { userOpHash } = await client.sendUserOpsBatch(transactions);
console.log('UserOp Hash:', userOpHash);
console.log('Transactions sent successfully');
} catch (error) {
console.error('Transaction failed:', error);
}
Explanation:
Sending the Batch Transactions: The
await client.sendUserOpsBatch(transactions)
call sends the array of transactions as a single user operation.Hash: The method returns the
userOpHash
, which is the hash of the user operation.
🎉 Conclusion
By following these steps, you can successfully send single or multiple transactions in a single operation and capture the user operation hash using the Magmar Toolkit SDK.
Last updated