Modular SDK
Instantiation
Before doing anything with the SDK, we must instantiate it. This will create the Etherspot smart account based off of the values we pass in.
Step 1. Import the Etherspot Modular SDK.
import { ModularSdk } from '@etherspot/modular-sdk';
Step 2. Instantiate the SDK with below initialisation properties using this block of code.
- privateKey
- ChainID
- bundlerProvider
- bundlerApiKey
- customBundlerUrl (can be left empty)
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, customBundlerUrl)
}
);
And that’s it! You’re now ready to call any of the Modular SDK functions.
You can also pass in different parameters when instantiating the SDK.
- chainId : The chain ID of the blockchain.
- customBundlerUrl : The bundler you wish to use.
Sending funds to another address
const recipient = ''; // recipient wallet address
const value = '0.0000001'; // transfer value
// 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();
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);
}
Next steps
For next steps you can look at functions or examples to tailor the dapp to what you’re trying to achieve.
In the next page we’ll take a look at the various functions the SDK offers.