UerOp-SDK Usage
This document introduces the use of userop-sdk, how dapp assembles userop, and the process of obtaining gas solutions by calling paymaster-rpc, and bundling on-chain.
Install package from github
npm install github:nerochain/aa-userop-sdk
Import into dapp project
import {Client, Presets} from 'userop';
Init build
const builder = await Presets.Builder.SimpleAccount.init(
accountSigner, //aa wallet owner provider
RPC_ENDPOINT, //nerochain rpc
{
overrideBundlerRpc: bundlerRpc, //bundler rpc
entryPoint: entryPointAddress, //entrypoint contract
factory: accountFactoryAddr, //SimpleAccountFactory contract
}
);
Set platform apikey to paymaster *The method to create an API key can be found in the AA Platform usage.
builder.setPaymasterOptions({
apikey: apiKey, //platform api-key
rpc: payMasterRpc //paymaster rpc
});
Init client(parameters same as build)
const client = await Client.init(RPC_ENDPOINT, {
overrideBundlerRpc: bundlerRpc,
entryPoint: entryPointAddress,
});
Construct builder’s transaction data on actual business scenarios
const callData = [
defaultTokenContract.interface.encodeFunctionData('approve', [
state.toAddress,
ethers.parseEther(state.tranNum.toString())
]),
defaultTokenContract.interface.encodeFunctionData('transfer', [
state.toAddress,
ethers.parseEther(state.tranNum.toString())
])
];
const callTo = [testTokenAddr, testTokenAddr];
builder.executeBatch(callTo, callData);
//builder.execute(..) for single calldata
Now get supported tokens
const supportedTokens = await client.getSupportedTokens(builder);
The above data depends on the strategies set by developers on the platform. The dapp analyzes tokens for users to choose gas options:
- type=0 freegas. Developer sponsors gas fees, users do not need to pay ERC20.
- type=1 prefund erc20. During the verification phase, the system first deducts the full amount of ERC20 from the user, and then returns the excess portion after executing the user operation.
- type=2 postfund erc20. After the userop is executed, the system deducts the ERC20 from the user in one go. However, it’s possible that the deduction may fail due to the specific nature of the userop code.
builder.setPaymasterOptions({ type: selectedMode.value }); //type: 0/free 1/prepay 2/postpay
then regenerates the transaction data, requests a signature from the paymaster and submits to the bundler for packaging and uploading to the blockchain.
const res = await client.sendUserOperation(builder)