Skip to content

Commit

Permalink
Add module installing script
Browse files Browse the repository at this point in the history
  • Loading branch information
pidturkina committed Jul 13, 2024
1 parent 3199414 commit d1257a8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
62 changes: 47 additions & 15 deletions contracts-permissionless-example/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
// Import the required modules.
import { createBundlerClient } from "permissionless"
import { sepolia } from "viem/chains"
import { http } from "viem"
import {generatePrivateKey, privateKeyToAccount} from "viem/accounts";
import {pimlicoBundlerClient, publicClient, safeAccount, smartAccountClient} from "./smart-account.js";
import {abi} from "./abi.js";
import {encodePacked} from "viem";

const apiKey = "insert your api key here"
const privateKey = generatePrivateKey()

// Create the required clients.
const bundlerClient = createBundlerClient({
chain: sepolia,
transport: http(`https://api.pimlico.io/v1/sepolia/rpc?apikey=${apiKey}`) // Use any bundler url
})
const beneficiary = privateKeyToAccount(privateKey);
const timeout = 100; //in seconds

async function installModule({smartClient, beneficiaryAddress, timeout, moduleType, hook, account, bundlerClient, moduleAddress, publicClient}) {
const isInitialized = (await publicClient.readContract({
address: moduleAddress,
abi,
functionName: 'isInitialized',
args: [account.address],
}))

const module = {
module: moduleAddress,
initData: isInitialized
? '0x'
: encodePacked(['address', 'uint48'], [beneficiaryAddress, timeout]),
deInitData: '0x',
additionalContext: '0x',
type: moduleType,
hook,
}

// Consume bundler, paymaster, and smart account actions!
const userOperationReceipt = await bundlerClient.getUserOperationReceipt({
hash: "0x23b28c3752246f42f25e5d2db1f4cf7f968aac2b4fc5fa04743eb8ffc1079b27"
})

console.log(userOperationReceipt)
const opHash = await smartClient.installModule({
type: module.type,
address: module.module,
context: module.initData,
});

const receipt = await bundlerClient.waitForUserOperationReceipt({ hash: opHash })
console.log('receipt', receipt)
}

installModule({
smartClient: smartAccountClient,
beneficiaryAddress: beneficiary.address,
timeout,
moduleType: "validator",
hook: '0x',
account: safeAccount,
bundlerClient: pimlicoBundlerClient,
moduleAddress: "0xab614e4a5398bb2a2a0bf73f9c913ec7ff47d81f", //deadmanswitch module address
publicClient
})
3 changes: 2 additions & 1 deletion contracts-permissionless-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"run": "bun run index.js"
},
"author": "",
"license": "ISC",
Expand Down

0 comments on commit d1257a8

Please sign in to comment.