Skip to content

Commit

Permalink
Delete example, add installing module scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
pidturkina committed Jul 13, 2024
1 parent 277e451 commit 19c45ff
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 237 deletions.
22 changes: 22 additions & 0 deletions contracts-permissionless-example/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const abi = [
{
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
name: 'config',
outputs: [
{ internalType: 'uint48', name: 'lastAccess', type: 'uint48' },
{ internalType: 'uint48', name: 'timeout', type: 'uint48' },
{ internalType: 'address', name: 'nominee', type: 'address' },
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'smartAccount', type: 'address' },
],
name: 'isInitialized',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'view',
type: 'function',
},
]
51 changes: 0 additions & 51 deletions contracts-permissionless-example/index.js
Original file line number Diff line number Diff line change
@@ -1,51 +0,0 @@
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 privateKey = generatePrivateKey()

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,
}


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
})
168 changes: 0 additions & 168 deletions contracts-permissionless-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 0 additions & 17 deletions contracts-permissionless-example/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
{
"name": "permissionless_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"run": "bun run index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"permissionless": "^0.1.39",
"viem": "^2.17.3"
}
}
3 changes: 2 additions & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"prettier:write": "prettier --write \"**/*.{json,md,svg,yml}\"",
"test": "forge test",
"test:lite": "FOUNDRY_PROFILE=lite forge test",
"test:optimized": "pnpm run build:optimized && FOUNDRY_PROFILE=test-optimized forge test"
"test:optimized": "pnpm run build:optimized && FOUNDRY_PROFILE=test-optimized forge test",
"add:module": "bun run ./test/add-module-to-account.js"
},
"dependencies": {
"@rhinestone/modulekit": "^0.4.7"
Expand Down
22 changes: 22 additions & 0 deletions contracts/test/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const abi = [
{
inputs: [{ internalType: 'address', name: 'account', type: 'address' }],
name: 'config',
outputs: [
{ internalType: 'uint48', name: 'lastAccess', type: 'uint48' },
{ internalType: 'uint48', name: 'timeout', type: 'uint48' },
{ internalType: 'address', name: 'nominee', type: 'address' },
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'address', name: 'smartAccount', type: 'address' },
],
name: 'isInitialized',
outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
stateMutability: 'view',
type: 'function',
},
]
98 changes: 98 additions & 0 deletions contracts/test/add-module-to-account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {
ENTRYPOINT_ADDRESS_V07,
createSmartAccountClient,
} from "permissionless";
import { signerToSafeSmartAccount } from "permissionless/accounts";
import {
createPimlicoBundlerClient,
} from "permissionless/clients/pimlico";
import { createPublicClient, http, encodePacked } from "viem";
import { sepolia } from "viem/chains";
import {generatePrivateKey, privateKeyToAccount} from "viem/accounts";
import {erc7579Actions} from "permissionless/actions/erc7579.js";
import {pimlicoPaymasterActions} from "permissionless/actions/pimlico";
import {abi} from "./abi";

const apiKey = ""

const privateKey = generatePrivateKey()

export const bundlerUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`

const signer = privateKeyToAccount(privateKey);


const publicClient = createPublicClient({
transport: http("https://rpc.ankr.com/eth_sepolia"),
})

const pimlicoBundlerClient = createPimlicoBundlerClient({
transport: http(bundlerUrl),
entryPoint: ENTRYPOINT_ADDRESS_V07,
}).extend(pimlicoPaymasterActions(ENTRYPOINT_ADDRESS_V07));

const safeAccount = await signerToSafeSmartAccount(publicClient, {
signer,
safeVersion: "1.4.1",
entryPoint: ENTRYPOINT_ADDRESS_V07,
safe4337ModuleAddress: "0x3Fdb5BC686e861480ef99A6E3FaAe03c0b9F32e2",
erc7579LaunchpadAddress: "0xEBe001b3D534B9B6E2500FB78E67a1A137f561CE",
})

const smartAccountClient = createSmartAccountClient({
account: safeAccount,
entryPoint: ENTRYPOINT_ADDRESS_V07,
chain: sepolia,
bundlerTransport: http(bundlerUrl),
middleware: {
gasPrice: async () => {
return (await pimlicoBundlerClient.getUserOperationGasPrice()).fast
},
sponsorUserOperation: pimlicoBundlerClient.sponsorUserOperation

},
}).extend(erc7579Actions({ entryPoint: ENTRYPOINT_ADDRESS_V07 }))

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,
}


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

const receipt = await bundlerClient.waitForUserOperationReceipt({ hash: opHash })
}

installModule({
smartClient: smartAccountClient,
beneficiaryAddress: beneficiary.address,
timeout,
moduleType: "validator",
hook: '0x',
account: safeAccount,
bundlerClient: pimlicoBundlerClient,
moduleAddress: "0x6A53E204b8A21dfD64516ff27484e6113640CB96",
publicClient
})

0 comments on commit 19c45ff

Please sign in to comment.