Modular SDK examples
Paymaster Sponsor Transaction
import { EtherspotBundler, ModularSdk, MODULE_TYPE, sleep } from '@etherspot/modular-sdk';
import * as dotenv from 'dotenv';
dotenv.config();
const recipient = ''; // recipient wallet address
const value = '0.01'; // transfer value
const apiKey = 'arka_public_key'; // Only testnets are available, if you need further assistance in setting up a paymaster service for your dapp, please reach out to us on discord or https://etherspot.fyi/arka/intro
const bundlerApiKey = process.env.API_KEY;
async function main() {
// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})
console.log('address: ', modularSdk.state.EOAAddress)
// get address of EtherspotWallet...
const address: string = await modularSdk.getCounterFactualAddress();
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);
// clear the transaction batch
await modularSdk.clearUserOpsFromBatch();
// add transactions to the batch
const transactionBatch = await modularSdk.addUserOpsToBatch({ to: recipient, value: ethers.utils.parseEther(value) });
console.log('transactions: ', transactionBatch);
// get balance of the account address
const balance = await modularSdk.getNativeBalance();
console.log('balances: ', balance);
// estimate transactions added to the batch and get the fee data for the UserOp
const op = await modularSdk.estimate({
paymasterDetails: { url: `https://arka.etherspot.io?apiKey=${apiKey}&chainId=${Number(process.env.CHAIN_ID)}`, context: { mode: 'sponsor' } }
});
console.log(`Estimate UserOp: ${await printOp(op)}`);
// sign the UserOp and sending to the bundler...
const uoHash = await modularSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);
// get transaction hash...
console.log('Waiting for transaction...');
let userOpsReceipt = null;
const timeout = Date.now() + 60000; // 1 minute timeout
while ((userOpsReceipt == null) && (Date.now() < timeout)) {
await sleep(2);
userOpsReceipt = await modularSdk.getUserOpReceipt(uoHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
}
main()
.catch(console.error)
.finally(() => process.exit());