Skip to content

Commit e942fd2

Browse files
authored
Merge pull request #210 from zerodevapp/feat/ratelimit_off_standard
Feat/ratelimit off standard
2 parents e3c7726 + 0efa63f commit e942fd2

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed

bun.lockb

0 Bytes
Binary file not shown.

packages/test/v0.7/permissionValidator.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
ECDSA_SIGNER_CONTRACT,
3434
GAS_POLICY_CONTRACT,
3535
type Policy,
36+
RATE_LIMIT_POLICY_WITH_RESET_CONTRACT,
3637
SUDO_POLICY_CONTRACT,
3738
deserializePermissionAccount,
3839
serializePermissionAccount
@@ -728,6 +729,54 @@ describe("Permission kernel Account", () => {
728729
TEST_TIMEOUT
729730
)
730731

732+
test(
733+
"Smart account client send transaction with RateLimitPolicy with reset",
734+
async () => {
735+
const startAt = Math.floor(Date.now() / 1000)
736+
737+
const rateLimitPolicy = await toRateLimitPolicy({
738+
policyAddress: RATE_LIMIT_POLICY_WITH_RESET_CONTRACT,
739+
interval: 100,
740+
count: 2
741+
})
742+
743+
const permissionSmartAccountClient = await getKernelAccountClient({
744+
account: await getSignerToPermissionKernelAccount([
745+
rateLimitPolicy
746+
]),
747+
paymaster: zeroDevPaymaster
748+
})
749+
750+
await sleep(2 * 5000)
751+
752+
const response = await permissionSmartAccountClient.sendTransaction(
753+
{
754+
to: zeroAddress,
755+
value: 0n,
756+
data: "0x"
757+
}
758+
)
759+
760+
expect(response).toBeString()
761+
expect(response).toHaveLength(TX_HASH_LENGTH)
762+
expect(response).toMatch(TX_HASH_REGEX)
763+
console.log("Transaction hash:", response)
764+
765+
const response2 =
766+
await permissionSmartAccountClient.sendTransaction({
767+
to: zeroAddress,
768+
value: 0n,
769+
data: "0x"
770+
})
771+
772+
expect(response2).toBeString()
773+
expect(response2).toHaveLength(TX_HASH_LENGTH)
774+
expect(response2).toMatch(TX_HASH_REGEX)
775+
console.log("Transaction hash:", response2)
776+
},
777+
TEST_TIMEOUT
778+
)
779+
731780
test(
732781
"Smart account client send transaction with CallPolicy V0.0.1",
733782
async () => {

plugins/permission/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @zerodev/permissions
22

3+
## 5.5.3
4+
5+
### Patch Changes
6+
7+
- add optional rate limit policy for non-4337 opcode rule compliant way
8+
39
## 5.5.2
410

511
### Patch Changes

plugins/permission/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export const CALL_POLICY_CONTRACT_V0_0_4 =
3737
export const GAS_POLICY_CONTRACT = "0xaeFC5AbC67FfD258abD0A3E54f65E70326F84b23"
3838
export const RATE_LIMIT_POLICY_CONTRACT =
3939
"0xf63d4139B25c836334edD76641356c6b74C86873"
40+
41+
/**
42+
* @dev RATE_LIMIT_POLICY_WITH_RESET_CONTRACT
43+
* - use non-standard way to deal with reset
44+
*/
45+
export const RATE_LIMIT_POLICY_WITH_RESET_CONTRACT =
46+
"0x6a06358e6b283921deceabe7e8a3741d506cca9b"
47+
4048
export const SIGNATURE_POLICY_CONTRACT =
4149
"0xF6A936c88D97E6fad13b98d2FD731Ff17eeD591d"
4250
export const SUDO_POLICY_CONTRACT = "0x67b436caD8a6D025DF6C82C5BB43fbF11fC5B9B7"

plugins/permission/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zerodev/permissions",
3-
"version": "5.5.2",
3+
"version": "5.5.3",
44
"author": "ZeroDev",
55
"main": "./_cjs/index.js",
66
"module": "./_esm/index.js",

plugins/permission/policies/toRateLimitPolicy.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { concatHex } from "viem"
22
import { PolicyFlags } from "../constants.js"
3-
import { RATE_LIMIT_POLICY_CONTRACT } from "../constants.js"
3+
import {
4+
RATE_LIMIT_POLICY_CONTRACT,
5+
RATE_LIMIT_POLICY_WITH_RESET_CONTRACT
6+
} from "../constants.js"
47
import type { Policy, PolicyParams } from "../types.js"
58

69
export type RateLimitPolicyParams = PolicyParams & {
@@ -18,13 +21,31 @@ export function toRateLimitPolicy({
1821
}: RateLimitPolicyParams): Policy {
1922
return {
2023
getPolicyData: () => {
21-
const intervalHex = interval.toString(16).padStart(12, "0")
22-
const countHex = count.toString(16).padStart(12, "0")
23-
const startAtHex = startAt.toString(16).padStart(12, "0")
24+
if (policyAddress === RATE_LIMIT_POLICY_CONTRACT) {
25+
const intervalHex = interval.toString(16).padStart(12, "0")
26+
const countHex = count.toString(16).padStart(12, "0")
27+
const startAtHex = startAt.toString(16).padStart(12, "0")
2428

25-
const data = intervalHex + countHex + startAtHex
29+
const data = intervalHex + countHex + startAtHex
2630

27-
return `0x${data}`
31+
return `0x${data}`
32+
} else if (
33+
policyAddress === RATE_LIMIT_POLICY_WITH_RESET_CONTRACT
34+
) {
35+
if (startAt !== 0 && startAt !== undefined) {
36+
throw new Error(
37+
"RATE_LIMIT_POLICY_WITH_RESET_CONTRACT does not support startAt"
38+
)
39+
}
40+
const intervalHex = interval.toString(16).padStart(12, "0")
41+
const countHex = count.toString(16).padStart(12, "0")
42+
43+
const data = intervalHex + countHex
44+
45+
return `0x${data}`
46+
} else {
47+
throw new Error("Invalid policy address")
48+
}
2849
},
2950
getPolicyInfoInBytes: () => {
3051
return concatHex([policyFlag, policyAddress])

0 commit comments

Comments
 (0)