Skip to content

Commit

Permalink
Add SafeOperationV07 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Feb 3, 2025
1 parent bd31756 commit 5159d69
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 59 deletions.
35 changes: 35 additions & 0 deletions packages/relay-kit/src/packs/safe-4337/SafeOperationBase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { EthSafeSignature } from '@safe-global/protocol-kit'
import SafeOperationV07 from './SafeOperationV07'
import * as fixtures from './testing-utils/fixtures'

describe('SafeOperationBase', () => {
it('should add and retrieve signatures', () => {
const safeOperation = new SafeOperationV07(fixtures.USER_OPERATION_V07, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[1]
})

safeOperation.addSignature(new EthSafeSignature('0xSigner', '0xSignature'))

expect(safeOperation.signatures.size).toBe(1)
expect(safeOperation.getSignature('0xSigner')).toMatchObject({
signer: '0xSigner',
data: '0xSignature',
isContractSignature: false
})
})

it('should encode the signatures', () => {
const safeOperation = new SafeOperationV07(fixtures.USER_OPERATION_V07, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[1]
})

safeOperation.addSignature(new EthSafeSignature('0xSigner1', '0xSignature1'))
safeOperation.addSignature(new EthSafeSignature('0xSigner2', '0xSignature2'))

expect(safeOperation.encodedSignatures()).toBe('0xSignature1Signature2')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { EthSafeSignature } from '@safe-global/protocol-kit'
import SafeOperationV06 from './SafeOperationV06'
import * as fixtures from './testing-utils/fixtures'

describe('SafeOperation', () => {
describe('SafeOperationV06', () => {
it('should create a SafeOperation from an UserOperation', () => {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION, {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION_V06, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[0]
})

expect(safeOperation.getSafeOperation()).toMatchObject({
safe: fixtures.USER_OPERATION.sender,
nonce: fixtures.USER_OPERATION.nonce,
initCode: fixtures.USER_OPERATION.initCode,
callData: fixtures.USER_OPERATION.callData,
callGasLimit: fixtures.USER_OPERATION.callGasLimit,
verificationGasLimit: fixtures.USER_OPERATION.verificationGasLimit,
preVerificationGas: fixtures.USER_OPERATION.preVerificationGas,
maxFeePerGas: fixtures.USER_OPERATION.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION.maxPriorityFeePerGas,
paymasterAndData: fixtures.USER_OPERATION.paymasterAndData,
safe: fixtures.USER_OPERATION_V06.sender,
nonce: fixtures.USER_OPERATION_V06.nonce,
initCode: fixtures.USER_OPERATION_V06.initCode,
callData: fixtures.USER_OPERATION_V06.callData,
callGasLimit: fixtures.USER_OPERATION_V06.callGasLimit,
verificationGasLimit: fixtures.USER_OPERATION_V06.verificationGasLimit,
preVerificationGas: fixtures.USER_OPERATION_V06.preVerificationGas,
maxFeePerGas: fixtures.USER_OPERATION_V06.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION_V06.maxPriorityFeePerGas,
paymasterAndData: fixtures.USER_OPERATION_V06.paymasterAndData,
validAfter: 0,
validUntil: 0,
entryPoint: fixtures.ENTRYPOINTS[0]
Expand All @@ -30,38 +30,8 @@ describe('SafeOperation', () => {
expect(safeOperation.signatures.size).toBe(0)
})

it('should add and retrieve signatures', () => {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[0]
})

safeOperation.addSignature(new EthSafeSignature('0xSigner', '0xSignature'))

expect(safeOperation.signatures.size).toBe(1)
expect(safeOperation.getSignature('0xSigner')).toMatchObject({
signer: '0xSigner',
data: '0xSignature',
isContractSignature: false
})
})

it('should encode the signatures', () => {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[0]
})

safeOperation.addSignature(new EthSafeSignature('0xSigner1', '0xSignature1'))
safeOperation.addSignature(new EthSafeSignature('0xSigner2', '0xSignature2'))

expect(safeOperation.encodedSignatures()).toBe('0xSignature1Signature2')
})

it('should add estimations', () => {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION, {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION_V06, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[0]
Expand All @@ -74,24 +44,24 @@ describe('SafeOperation', () => {
})

expect(safeOperation.getSafeOperation()).toMatchObject({
safe: fixtures.USER_OPERATION.sender,
nonce: fixtures.USER_OPERATION.nonce,
initCode: fixtures.USER_OPERATION.initCode,
callData: fixtures.USER_OPERATION.callData,
safe: fixtures.USER_OPERATION_V06.sender,
nonce: fixtures.USER_OPERATION_V06.nonce,
initCode: fixtures.USER_OPERATION_V06.initCode,
callData: fixtures.USER_OPERATION_V06.callData,
callGasLimit: BigInt(fixtures.GAS_ESTIMATION.callGasLimit),
verificationGasLimit: BigInt(fixtures.GAS_ESTIMATION.verificationGasLimit),
preVerificationGas: BigInt(fixtures.GAS_ESTIMATION.preVerificationGas),
maxFeePerGas: fixtures.USER_OPERATION.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION.maxPriorityFeePerGas,
paymasterAndData: fixtures.USER_OPERATION.paymasterAndData,
maxFeePerGas: fixtures.USER_OPERATION_V06.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION_V06.maxPriorityFeePerGas,
paymasterAndData: fixtures.USER_OPERATION_V06.paymasterAndData,
validAfter: 0,
validUntil: 0,
entryPoint: fixtures.ENTRYPOINTS[0]
})
})

it('should retrieve the UserOperation', () => {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION, {
const safeOperation = new SafeOperationV06(fixtures.USER_OPERATION_V06, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[0]
Expand All @@ -106,7 +76,7 @@ describe('SafeOperation', () => {

expect(safeOperation.getUserOperation()).toMatchObject({
sender: safeOperation.userOperation.sender,
nonce: fixtures.USER_OPERATION.nonce,
nonce: fixtures.USER_OPERATION_V06.nonce,
initCode: safeOperation.userOperation.initCode,
callData: safeOperation.userOperation.callData,
callGasLimit: safeOperation.userOperation.callGasLimit,
Expand Down
106 changes: 106 additions & 0 deletions packages/relay-kit/src/packs/safe-4337/SafeOperationV07.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Hex, concat, encodePacked } from 'viem'
import { EthSafeSignature } from '@safe-global/protocol-kit'
import SafeOperationV07 from './SafeOperationV07'
import * as fixtures from './testing-utils/fixtures'

describe('SafeOperationV07', () => {
it('should create a SafeOperation from an UserOperation', () => {
const safeOperation = new SafeOperationV07(fixtures.USER_OPERATION_V07, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[1]
})

expect(safeOperation.getSafeOperation()).toMatchObject({
safe: fixtures.USER_OPERATION_V07.sender,
nonce: fixtures.USER_OPERATION_V07.nonce,
initCode: concat([
safeOperation.userOperation.factory as Hex,
(safeOperation.userOperation.factoryData as Hex) || ('0x' as Hex)
]),
callData: fixtures.USER_OPERATION_V07.callData,
callGasLimit: fixtures.USER_OPERATION_V07.callGasLimit,
verificationGasLimit: fixtures.USER_OPERATION_V07.verificationGasLimit,
preVerificationGas: fixtures.USER_OPERATION_V07.preVerificationGas,
maxFeePerGas: fixtures.USER_OPERATION_V07.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION_V07.maxPriorityFeePerGas,
paymasterAndData: '0x',
validAfter: 0,
validUntil: 0,
entryPoint: fixtures.ENTRYPOINTS[1]
})

expect(safeOperation.signatures.size).toBe(0)
})

it('should add estimations', () => {
const safeOperation = new SafeOperationV07(fixtures.USER_OPERATION_V07, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[1]
})

safeOperation.addEstimations({
callGasLimit: BigInt(fixtures.GAS_ESTIMATION.callGasLimit),
verificationGasLimit: BigInt(fixtures.GAS_ESTIMATION.verificationGasLimit),
preVerificationGas: BigInt(fixtures.GAS_ESTIMATION.preVerificationGas)
})

expect(safeOperation.getSafeOperation()).toMatchObject({
safe: fixtures.USER_OPERATION_V07.sender,
nonce: fixtures.USER_OPERATION_V07.nonce,
initCode: concat([
safeOperation.userOperation.factory as Hex,
(safeOperation.userOperation.factoryData as Hex) || ('0x' as Hex)
]),
callData: fixtures.USER_OPERATION_V07.callData,
callGasLimit: BigInt(fixtures.GAS_ESTIMATION.callGasLimit),
verificationGasLimit: BigInt(fixtures.GAS_ESTIMATION.verificationGasLimit),
preVerificationGas: BigInt(fixtures.GAS_ESTIMATION.preVerificationGas),
maxFeePerGas: fixtures.USER_OPERATION_V07.maxFeePerGas,
maxPriorityFeePerGas: fixtures.USER_OPERATION_V07.maxPriorityFeePerGas,
paymasterAndData: '0x',
validAfter: 0,
validUntil: 0,
entryPoint: fixtures.ENTRYPOINTS[1]
})
})

it('should retrieve the UserOperation', () => {
const safeOperation = new SafeOperationV07(fixtures.USER_OPERATION_V07, {
chainId: BigInt(fixtures.CHAIN_ID),
moduleAddress: fixtures.MODULE_ADDRESS,
entryPoint: fixtures.ENTRYPOINTS[1]
})

safeOperation.addSignature(
new EthSafeSignature(
'0xSigner',
'0x000000000000000000000000a397ca32ee7fb5282256ee3465da0843485930b803d747516aac76e152f834051ac18fd2b3c0565590f9d65085538993c85c9bb189c940d15c15402c7c2885821b'
)
)

expect(safeOperation.getUserOperation()).toMatchObject({
sender: safeOperation.userOperation.sender,
nonce: fixtures.USER_OPERATION_V07.nonce,
factory: fixtures.USER_OPERATION_V07.factory,
factoryData: fixtures.USER_OPERATION_V07.factoryData,
callData: safeOperation.userOperation.callData,
callGasLimit: safeOperation.userOperation.callGasLimit,
verificationGasLimit: safeOperation.userOperation.verificationGasLimit,
preVerificationGas: safeOperation.userOperation.preVerificationGas,
maxFeePerGas: safeOperation.userOperation.maxFeePerGas,
maxPriorityFeePerGas: safeOperation.userOperation.maxPriorityFeePerGas,
paymaster: safeOperation.userOperation.paymaster,
paymasterData: safeOperation.userOperation.paymasterData,
signature: encodePacked(
['uint48', 'uint48', 'bytes'],
[
safeOperation.options.validAfter || 0,
safeOperation.options.validUntil || 0,
safeOperation.encodedSignatures() as Hex
]
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ describe('PimlicoFeeEstimator', () => {
it('should enable to setup the gas estimation', async () => {
const sponsoredGasEstimation = await estimator.setupEstimation({
bundlerUrl: fixtures.BUNDLER_URL,
userOperation: fixtures.USER_OPERATION,
entryPoint: fixtures.ENTRYPOINTS[0]
userOperation: fixtures.USER_OPERATION_V07,
entryPoint: fixtures.ENTRYPOINTS[1]
})

expect(sponsoredGasEstimation).toEqual({
Expand All @@ -43,23 +43,23 @@ describe('PimlicoFeeEstimator', () => {
it('should enable to adjust the gas estimation', async () => {
const sponsoredGasEstimation = await estimator.adjustEstimation({
bundlerUrl: fixtures.BUNDLER_URL,
userOperation: fixtures.USER_OPERATION,
entryPoint: fixtures.ENTRYPOINTS[0]
userOperation: fixtures.USER_OPERATION_V07,
entryPoint: fixtures.ENTRYPOINTS[1]
})

expect(sponsoredGasEstimation).toEqual({})
})

it('should get the paymaster estimation', async () => {
const paymasterGasEstimation = await estimator.getPaymasterEstimation({
userOperation: fixtures.USER_OPERATION,
userOperation: fixtures.USER_OPERATION_V07,
bundlerUrl: fixtures.BUNDLER_URL,
paymasterOptions: {
paymasterUrl: fixtures.PAYMASTER_URL,
paymasterAddress: fixtures.PAYMASTER_ADDRESS,
paymasterTokenAddress: fixtures.PAYMASTER_TOKEN_ADDRESS
},
entryPoint: fixtures.ENTRYPOINTS[0]
entryPoint: fixtures.ENTRYPOINTS[1]
})

expect(paymasterGasEstimation).toEqual(fixtures.SPONSORED_GAS_ESTIMATION)
Expand Down
23 changes: 22 additions & 1 deletion packages/relay-kit/src/packs/safe-4337/testing-utils/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const USER_OPERATION_RECEIPT = {
}
}

export const USER_OPERATION = {
export const USER_OPERATION_V06 = {
sender: '0x1405B3659a11a16459fc27Fa1925b60388C38Ce1',
nonce: '1',
initCode: '0x',
Expand All @@ -67,6 +67,27 @@ export const USER_OPERATION = {
'0x000000000000000000000000a397ca32ee7fb5282256ee3465da0843485930b803d747516aac76e152f834051ac18fd2b3c0565590f9d65085538993c85c9bb189c940d15c15402c7c2885821b'
}

export const USER_OPERATION_V07 = {
sender: '0x26874a65eA7B6B6655e4582c8D215e1De05dd39b',
nonce: '0x0',
factory: '0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67',
factoryData:
'0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c7620000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000009a15e37d88ba5900000000000000000000000000000000000000000000000000000000000001e4b63e800d000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000010000000000000000000000002dd68b007b46fbe91b9a7c3eda5a7a1063cb5b47000000000000000000000000000000000000000000000000000000000000014000000000000000000000000075cf11467937ce3f2f357ce24ffc3dbf8fd5c2260000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000bc16a6fbc93f62187a137f30c92e3f90bbbaa49200000000000000000000000000000000000000000000000000000000000000648d0dc49f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000075cf11467937ce3f2f357ce24ffc3dbf8fd5c2260000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
callData:
'0x7bb3742800000000000000000000000038869bf66a61cf6bdb996a6ae40d5853fd43b52600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001848d80ff0a0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000013200fc3e86566895fb007c6a0d3809eb2827df94f75100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000bc16a6fbc93f62187a137f30c92e3f90bbbaa49200000000000000000000000000000000000000000000000000000000000186a000fc3e86566895fb007c6a0d3809eb2827df94f75100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000bc16a6fbc93f62187a137f30c92e3f90bbbaa49200000000000000000000000000000000000000000000000000000000000186a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
callGasLimit: 120_784n,
verificationGasLimit: 83_056n,
preVerificationGas: 48_568n,
maxFeePerGas: 193_584_757_388n,
maxPriorityFeePerGas: 1_380_000_000n,
paymaster: undefined,
paymasterVerificationGasLimit: undefined,
paymasterPostOpGasLimit: undefined,
paymasterData: undefined,
signature:
'0x0000679fa3ac000067a1786c8c012f3bef75848690703f17ab0519669bc38bc2629bd8b3118f6280936933fa675bc52dde81cc71c3e0c4587e17ddecf21f845a7a34862b586776501845b1511c'
}

export const USER_OPERATION_HEX_VALUES = {
sender: '0x1405B3659a11a16459fc27Fa1925b60388C38Ce1',
nonce: '0x1',
Expand Down

0 comments on commit 5159d69

Please sign in to comment.