Skip to content

Instantly share code, notes, and snippets.

@himanshugarg06
Created August 13, 2024 06:46
Show Gist options
  • Save himanshugarg06/83327aaa1339efda42806e66143aa5c3 to your computer and use it in GitHub Desktop.
Save himanshugarg06/83327aaa1339efda42806e66143aa5c3 to your computer and use it in GitHub Desktop.
An example of ERC 4337 transaction using viem SDK and Biconomy bundler and paymaster
import { createPublicClient, http, parseEther } from 'viem';
import { createBundlerClient, toCoinbaseSmartAccount, createPaymasterClient, toSoladySmartAccount } from 'viem/account-abstraction';
import { sepolia } from 'viem/chains';
import { privateKeyToAccount } from 'viem/accounts';
const main = async () => {
const client = createPublicClient({
chain: sepolia,
transport: http(),
})
const owner = privateKeyToAccount('PRIVATE_KEY')
console.log(owner.address)
const account = await toCoinbaseSmartAccount({
client,
owners: [owner]
})
const paymasterClient = createPaymasterClient({
transport: http('BICONOMY_PAYMASTER_URL'),
})
const bundlerClient = createBundlerClient({
account,
client,
paymasterContext: {
"mode": "SPONSORED",
"calculateGasLimits": true,
"expiryDuration": 300, // duration (secs) for which the generate paymasterAndData will be valid. Default duration is 300 secs.
"sponsorshipInfo": {
"webhookData": {},
"smartAccountInfo": {
"name": "BICONOMY",
"version": "2.0.0"
}
}
},
paymaster: paymasterClient,
transport: http('https://bundler.biconomy.io/api/v2/11155111/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44'),
})
console.log(account.address);
const userOpHash = await bundlerClient.sendUserOperation({
account,
calls: [{
to: '0xcb98643b8786950F0461f3B0edf99D88F274574D',
value: parseEther('0.001')
}]
})
console.log(userOpHash)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment