Send a simple transaction
Overview
This tutorial demonstrates how to send a simple transaction using ethers.js/web3.js and the AbstraXn Smart Account with the @abstraxn/account
SDK.
Prerequisites
- Node.js installed on your machine
- A Bundler url
- An rpc url
- An address to send the transaction to (replace
0xaddress
)
Step 1: Generate the config and Create AbstraXn Smart Account
- web3.js
- ethers.js
import { web3 } from "web3";
import { createSmartAccountClient } from "@abstraxn/account";
// Your configuration with private key and AbstraXn API key
const config = {
privateKey: "your-private-key",
bundlerUrl: "",
};
const web3 = new Web3(config.rpcUrl);
// Generate EOA from private key using ethers.js
const account = web3.eth.accounts.privateKeyToAccount(config.privateKey);
const signer = createWalletClient({
account,
chain: polygonAmoy,
transport: http(),
});
// Create AbstraXn Smart Account instance
const abstraxnSmartAccount = await AbstraxnSmartAccount.create({
chainId: ChainId.POLYGON_AMOY, // you can use multiple chains available in chainId object
activeValidationModule: ownerShipModule,
defaultValidationModule: ownerShipModule,
bundler: bundler, // instance of bundler
});
const saAddress = await abstraxnSmartAccount.getAccountAddress();
console.log("SA Address", saAddress);
import { ethers } from "ethers";
import { createSmartAccountClient } from "@abstraxn/account";
// Your configuration with private key and AbstraXn API key
const config = {
privateKey: "your-private-key",
bundlerUrl: "",
rpcUrl: "rpc-url",
};
// Generate EOA from private key using ethers.js
let provider = new ethers.JsonRpcProvider(config.rpcUrl)();
let signer = new ethers.Wallet(config.privateKey, provider);
// Create AbstraXn Smart Account instance
const abstraxnSmartAccount = await AbstraxnSmartAccount.create({
chainId: ChainId.POLYGON_AMOY, // you can use multiple chains available in chainId object
activeValidationModule: ownerShipModule,
defaultValidationModule: ownerShipModule,
bundler: bundler, // instance of bundler
});
const saAddress = await abstraxnSmartAccount.getAccountAddress();
console.log("SA Address", saAddress);
Get your signer from either ethers.js or web3.js and create an Abstraxn account instance.
Step 2: Generate Transaction Data
const toAddress = "0xaddress"; // Replace with the recipient's address
const transactionData = "0x123"; // Replace with the actual transaction data
// Build the transaction
const tx = {
to: toAddress,
data: transactionData,
};
Specify the recipient's address and transaction data to build the simple transaction.
Step 3: Send the Transaction and wait for the Transaction Hash
// Send the transaction and get the transaction hash
const userOpResponse = await smartWallet.sendTransaction(tx);
const { transactionHash } = await userOpResponse.waitForTxHash();
console.log("Transaction Hash", transactionHash);
const userOpReceipt = await userOpResponse.wait();
if(userOpReceipt.success == 'true') {
console.log("UserOp receipt", userOpReceipt)
console.log("Transaction receipt", userOpReceipt.receipt)
}
Send the transaction using the AbstraXn Smart Account and get the transaction hash. The transaction will be built into a User Operation and then sent to the Bundler.
That's it! You've successfully sent a simple transaction using ethers.js/web3.js and the AbstraXn Smart Account. Feel free to customize this example based on your specific use case.