-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relay-kit): Add UserOperation custom nonce support (#1126)
- Loading branch information
Showing
8 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { toHex } from 'viem' | ||
|
||
export function encodeNonce(args: { key: bigint; sequence: bigint }): bigint { | ||
const key = BigInt(toHex(args.key, { size: 24 })) | ||
const sequence = BigInt(toHex(args.sequence, { size: 8 })) | ||
|
||
return (key << BigInt(64)) + sequence | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as dotenv from 'dotenv' | ||
import { encodeNonce, Safe4337Pack } from '@safe-global/relay-kit' | ||
import { waitForOperationToFinish, setup4337Playground } from '../utils' | ||
|
||
dotenv.config({ path: './playground/relay-kit/.env' }) | ||
|
||
const { | ||
PRIVATE_KEY, | ||
SAFE_ADDRESS = '0x', | ||
RPC_URL = '', | ||
CHAIN_ID = '', | ||
BUNDLER_URL = '' | ||
} = process.env | ||
|
||
// PIM test token contract address | ||
// faucet: https://dashboard.pimlico.io/test-erc20-faucet | ||
const pimlicoTokenAddress = '0xFC3e86566895Fb007c6A0d3809eb2827DF94F751' | ||
|
||
const NUMBER_OF_OPERATIONS = 2 | ||
|
||
async function main() { | ||
// 1) Initialize pack | ||
const safe4337Pack = await Safe4337Pack.init({ | ||
provider: RPC_URL, | ||
signer: PRIVATE_KEY, | ||
bundlerUrl: BUNDLER_URL, | ||
safeModulesVersion: '0.3.0', // Blank or 0.3.0 for Entrypoint v0.7, 0.2.0 for Entrypoint v0.6 | ||
options: { | ||
safeAddress: SAFE_ADDRESS | ||
} | ||
}) | ||
|
||
// 2) Setup Playground | ||
const { transactions, timestamp } = await setup4337Playground(safe4337Pack, { | ||
// nativeTokenAmount: parseEther('0.05'), // Increase this value when is not enough to cover the gas fees | ||
erc20TokenAmount: 200_000n, | ||
erc20TokenContractAddress: pimlicoTokenAddress | ||
}) | ||
|
||
// 3) Create Multiple SafeOperations | ||
const safeOperations = [] | ||
|
||
for (let i = 0; i < NUMBER_OF_OPERATIONS; i++) { | ||
safeOperations.push( | ||
safe4337Pack.createTransaction({ | ||
transactions, | ||
options: { | ||
validAfter: Number(timestamp - 60_000n), | ||
validUntil: Number(timestamp + 60_000n), | ||
customNonce: encodeNonce({ | ||
key: BigInt(Date.now()) + BigInt(i), // Ensure unique nonce | ||
sequence: 0n | ||
}) | ||
} | ||
}) | ||
) | ||
} | ||
|
||
const createdSafeOperations = await Promise.all(safeOperations) | ||
|
||
// 4) Sign all SafeOperations | ||
const signingPromises = createdSafeOperations.map((op) => safe4337Pack.signSafeOperation(op)) | ||
const signedOperations = await Promise.all(signingPromises) | ||
|
||
// Log all operations | ||
signedOperations.forEach((op, index) => console.log(`SafeOperation ${index + 1}`, op)) | ||
|
||
// 5) Execute all operations in parallel | ||
const executionPromises = signedOperations.map((op) => | ||
safe4337Pack.executeTransaction({ executable: op }) | ||
) | ||
|
||
const userOperationHashes = await Promise.all(executionPromises) | ||
|
||
// Wait for all operations to complete | ||
await Promise.all( | ||
userOperationHashes.map((hash) => waitForOperationToFinish(hash, CHAIN_ID, safe4337Pack)) | ||
) | ||
} | ||
|
||
main() |