From d1257a836feea064246e63a05cbfb3f0df90ede6 Mon Sep 17 00:00:00 2001 From: Sofiia Pidturkina Date: Sat, 13 Jul 2024 13:38:54 +0200 Subject: [PATCH] Add module installing script --- contracts-permissionless-example/index.js | 62 ++++++++++++++----- contracts-permissionless-example/package.json | 3 +- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/contracts-permissionless-example/index.js b/contracts-permissionless-example/index.js index b4f850e..1d9c77b 100644 --- a/contracts-permissionless-example/index.js +++ b/contracts-permissionless-example/index.js @@ -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) \ No newline at end of file + 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 +}) diff --git a/contracts-permissionless-example/package.json b/contracts-permissionless-example/package.json index c599254..2bc0ad5 100644 --- a/contracts-permissionless-example/package.json +++ b/contracts-permissionless-example/package.json @@ -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",