-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add erc20 override utils * Add balanceOverride in prepareUserOperationForErc20Paymaster * Add changeset * chore: format * Change approvalSlot to allowanceSlot * make slots optional * allow overrides by user * Change approval to allowance * update bun * chore: format --------- Co-authored-by: plusminushalf <[email protected]>
- Loading branch information
1 parent
bc110b0
commit bfc278b
Showing
14 changed files
with
421 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"permissionless": patch | ||
--- | ||
|
||
Added utils to create erc20 state overrides |
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,5 @@ | ||
--- | ||
"permissionless": patch | ||
--- | ||
|
||
Added balanceOverride to prepareUserOperationForErc20Paymaster |
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
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 |
---|---|---|
|
@@ -71,6 +71,6 @@ | |
} | ||
}, | ||
"peerDependencies": { | ||
"viem": "^2.21.2" | ||
"viem": "^2.21.22" | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
packages/permissionless/utils/erc20AllowanceOverride.test.ts
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,59 @@ | ||
import { toHex } from "viem" | ||
import { describe, expect, test } from "vitest" | ||
import { | ||
type Erc20AllowanceOverrideParameters, | ||
erc20AllowanceOverride | ||
} from "./erc20AllowanceOverride" | ||
|
||
describe("erc20AllowanceOverride", () => { | ||
test("should return the correct structure for valid inputs", () => { | ||
const params = { | ||
token: "0xTokenAddress", | ||
owner: "0xOwnerAddress", | ||
spender: "0xSpenderAddress", | ||
slot: BigInt(1), | ||
amount: BigInt(100) | ||
} as const | ||
|
||
const result = erc20AllowanceOverride(params) | ||
|
||
expect(result).toEqual([ | ||
{ | ||
address: params.token, | ||
stateDiff: [ | ||
{ | ||
slot: expect.any(String), // Slot will be a keccak256 hash | ||
value: toHex(params.amount) | ||
} | ||
] | ||
} | ||
]) | ||
}) | ||
|
||
test("should use the default amount when none is provided", () => { | ||
const params: Erc20AllowanceOverrideParameters = { | ||
token: "0xTokenAddress", | ||
owner: "0xOwnerAddress", | ||
spender: "0xSpenderAddress", | ||
slot: BigInt(1) | ||
} | ||
|
||
const result = erc20AllowanceOverride(params) | ||
|
||
const expectedDefaultAmount = BigInt( | ||
"0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
) | ||
|
||
expect(result).toEqual([ | ||
{ | ||
address: params.token, | ||
stateDiff: [ | ||
{ | ||
slot: expect.any(String), // Slot will be a keccak256 hash | ||
value: toHex(expectedDefaultAmount) | ||
} | ||
] | ||
} | ||
]) | ||
}) | ||
}) |
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,66 @@ | ||
import { | ||
type Address, | ||
type StateOverride, | ||
encodeAbiParameters, | ||
keccak256, | ||
toHex | ||
} from "viem" | ||
|
||
export type Erc20AllowanceOverrideParameters = { | ||
token: Address | ||
owner: Address | ||
spender: Address | ||
slot: bigint | ||
amount?: bigint | ||
} | ||
|
||
export function erc20AllowanceOverride({ | ||
token, | ||
owner, | ||
spender, | ||
slot, | ||
amount = BigInt( | ||
"0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
) | ||
}: Erc20AllowanceOverrideParameters): StateOverride { | ||
const smartAccountErc20AllowanceSlot = keccak256( | ||
encodeAbiParameters( | ||
[ | ||
{ | ||
type: "address" | ||
}, | ||
{ | ||
type: "bytes32" | ||
} | ||
], | ||
[ | ||
spender, | ||
keccak256( | ||
encodeAbiParameters( | ||
[ | ||
{ | ||
type: "address" | ||
}, | ||
{ | ||
type: "uint256" | ||
} | ||
], | ||
[owner, BigInt(slot)] | ||
) | ||
) | ||
] | ||
) | ||
) | ||
|
||
return [ | ||
{ | ||
address: token, | ||
stateDiff: [ | ||
{ | ||
slot: smartAccountErc20AllowanceSlot, | ||
value: toHex(amount) | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.