Modular SDK examples
Transfer NFT
import { EtherspotBundler, ModularSdk, MODULE_TYPE, printOp, sleep } from '@etherspot/modular-sdk';
import * as dotenv from 'dotenv';
import { ethers } from 'ethers';
dotenv.config();
dotenv.config();
// add/change these values
const recipient = ''; // recipient wallet address
const tokenAddress = '' // nft token address;
const tokenId = 4;
const bundlerApiKey = process.env.API_KEY;
// npx ts-node examples/04-transfer-nft.ts
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}`);
const erc721Interface = new ethers.utils.Interface([
'function safeTransferFrom(address _from, address _to, uint256 _tokenId)'
])
const erc721Data = erc721Interface.encodeFunctionData('safeTransferFrom', [address, recipient, tokenId]);
// clear the transaction batch
await modularSdk.clearUserOpsFromBatch();
// add transactions to the batch
const userOpsBatch = await modularSdk.addUserOpsToBatch({to: tokenAddress, data: erc721Data});
console.log('transactions: ', userOpsBatch);
// sign transactions added to the batch
const op = await modularSdk.estimate();
console.log(`Estimated UserOp: ${await printOp(op)}`);
// sign the userOps 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());