-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from rhinestonewtf/sudo-policy
Integrate smart session policies
- Loading branch information
Showing
13 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
src/module/smart-sessions/policies/spending-limits-policy/constants.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,2 @@ | ||
export const SPENDING_LIMITS_POLICY_ADDRESS = | ||
'0xcc8d69271fa62045af45fdcc62cdc9c317a9e745' |
2 changes: 2 additions & 0 deletions
2
src/module/smart-sessions/policies/spending-limits-policy/index.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,2 @@ | ||
export * from './constants' | ||
export * from './installation' |
19 changes: 19 additions & 0 deletions
19
src/module/smart-sessions/policies/spending-limits-policy/installation.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,19 @@ | ||
import { SPENDING_LIMITS_POLICY_ADDRESS } from './constants' | ||
import { Policy } from '../../../types' | ||
import { Address, encodeAbiParameters } from 'viem' | ||
|
||
type Params = { | ||
token: Address | ||
limit: bigint | ||
}[] | ||
|
||
export const getSpendingLimitsPolicy = (params: Params): Policy => { | ||
return { | ||
address: SPENDING_LIMITS_POLICY_ADDRESS, | ||
initData: encodeAbiParameters( | ||
[{ type: 'address[]' }, { type: 'uint256[]' }], | ||
[params.map(({ token }) => token), params.map(({ limit }) => limit)], | ||
), | ||
deInitData: '0x', | ||
} | ||
} |
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 @@ | ||
export const SUDO_POLICY_ADDRESS = '0x40235179bdd60beb6730e03d1aa91eae9b85928e' |
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,2 @@ | ||
export * from './installation' | ||
export * from './constants' |
10 changes: 10 additions & 0 deletions
10
src/module/smart-sessions/policies/sudo-policy/installation.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,10 @@ | ||
import { SUDO_POLICY_ADDRESS } from './constants' | ||
import { Policy } from '../../../types' | ||
|
||
export const getSudoPolicy = (): Policy => { | ||
return { | ||
address: SUDO_POLICY_ADDRESS, | ||
initData: '0x', | ||
deInitData: '0x', | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/module/smart-sessions/policies/universal-action-policy/abi.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,58 @@ | ||
export const abi = [ | ||
{ | ||
components: [ | ||
{ | ||
name: 'valueLimitPerUse', | ||
type: 'uint256', | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
name: 'length', | ||
type: 'uint256', | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
name: 'condition', | ||
type: 'uint8', | ||
}, | ||
{ | ||
name: 'offset', | ||
type: 'uint64', | ||
}, | ||
{ | ||
name: 'isLimited', | ||
type: 'bool', | ||
}, | ||
{ | ||
name: 'ref', | ||
type: 'bytes32', | ||
}, | ||
{ | ||
components: [ | ||
{ | ||
name: 'limit', | ||
type: 'uint256', | ||
}, | ||
{ | ||
name: 'used', | ||
type: 'uint256', | ||
}, | ||
], | ||
name: 'usage', | ||
type: 'tuple', | ||
}, | ||
], | ||
name: 'rules', | ||
type: 'tuple[]', | ||
}, | ||
], | ||
name: 'paramRules', | ||
type: 'tuple', | ||
}, | ||
], | ||
name: 'ActionConfig', | ||
type: 'tuple', | ||
}, | ||
] |
4 changes: 4 additions & 0 deletions
4
src/module/smart-sessions/policies/universal-action-policy/constants.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,4 @@ | ||
export const UNIVERSAL_ACTION_POLICY_ADDRESS = | ||
'0xf87db2a1b6ac548ad5352bcc550ce373ba826bb6' | ||
|
||
export const MAX_RULES = 16 |
2 changes: 2 additions & 0 deletions
2
src/module/smart-sessions/policies/universal-action-policy/index.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,2 @@ | ||
export * from './constants' | ||
export * from './installation' |
27 changes: 27 additions & 0 deletions
27
src/module/smart-sessions/policies/universal-action-policy/installation.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,27 @@ | ||
import { MAX_RULES, UNIVERSAL_ACTION_POLICY_ADDRESS } from './constants' | ||
import { Policy } from '../../../types' | ||
import { encodeAbiParameters } from 'viem' | ||
import { abi } from './abi' | ||
import { ActionConfig } from './types' | ||
|
||
export const getUniversalActionPolicy = ( | ||
actionConfig: ActionConfig, | ||
): Policy => { | ||
if (actionConfig.paramRules.rules.length > MAX_RULES) { | ||
throw new Error(`Max number of rules is ${MAX_RULES}`) | ||
} | ||
|
||
return { | ||
address: UNIVERSAL_ACTION_POLICY_ADDRESS, | ||
initData: encodeAbiParameters(abi, [ | ||
{ | ||
valueLimitPerUse: actionConfig.valueLimitPerUse, | ||
paramRules: { | ||
length: actionConfig.paramRules.length, | ||
rules: actionConfig.paramRules.rules, | ||
}, | ||
}, | ||
]), | ||
deInitData: '0x', | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/module/smart-sessions/policies/universal-action-policy/types.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,33 @@ | ||
import { Hex } from 'viem' | ||
|
||
export type ActionConfig = { | ||
valueLimitPerUse: bigint | ||
paramRules: ParamRules | ||
} | ||
|
||
export type ParamRules = { | ||
length: number | ||
rules: ParamRule[] | ||
} | ||
|
||
export type ParamRule = { | ||
condition: ParamCondition | ||
offset: number | ||
isLimited: boolean | ||
ref: Hex | ||
usage: LimitUsage | ||
} | ||
|
||
export type LimitUsage = { | ||
limit: bigint | ||
used: bigint | ||
} | ||
|
||
export enum ParamCondition { | ||
EQUAL, | ||
GREATER_THAN, | ||
LESS_THAN, | ||
GREATER_THAN_OR_EQUAL, | ||
LESS_THAN_OR_EQUAL, | ||
NOT_EQUAL, | ||
} |
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
78 changes: 78 additions & 0 deletions
78
test/unit/module/smartSessions/smartSessionPolicies.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,78 @@ | ||
import { getSpendingLimitsPolicy } from 'src/module/smart-sessions/policies/spending-limits-policy' | ||
import { getSudoPolicy } from 'src/module/smart-sessions/policies/sudo-policy' | ||
import { | ||
getUniversalActionPolicy, | ||
UNIVERSAL_ACTION_POLICY_ADDRESS, | ||
} from 'src/module/smart-sessions/policies/universal-action-policy' | ||
import { | ||
ActionConfig, | ||
ParamCondition, | ||
} from 'src/module/smart-sessions/policies/universal-action-policy/types' | ||
import { zeroAddress } from 'viem' | ||
|
||
describe('Smart Sessions Polices', () => { | ||
// ----------------------- | ||
// Universal Action Policy | ||
// ----------------------- | ||
it('should get install universal action policy', async () => { | ||
// Setup | ||
const actionConfigData: ActionConfig = { | ||
valueLimitPerUse: BigInt(1000), | ||
paramRules: { | ||
length: 2, | ||
rules: [ | ||
{ | ||
condition: ParamCondition.EQUAL, | ||
offset: 0, | ||
isLimited: true, | ||
ref: '0x00000000000000000000000000000000000000000000000000000000000003e8', // 1000 in bytes32 | ||
usage: { | ||
limit: BigInt(1000), | ||
used: BigInt(10), | ||
}, | ||
}, | ||
{ | ||
condition: ParamCondition.LESS_THAN, | ||
offset: 32, | ||
isLimited: false, | ||
ref: '0x00000000000000000000000000000000000000000000000000000000000007d0', // 2000 in bytes32 | ||
usage: { | ||
limit: BigInt(2000), | ||
used: BigInt(100), | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
const installUniversalPolicy = getUniversalActionPolicy(actionConfigData) | ||
|
||
expect(installUniversalPolicy.address).toEqual( | ||
UNIVERSAL_ACTION_POLICY_ADDRESS, | ||
) | ||
expect(installUniversalPolicy.initData).toBeDefined() | ||
expect(installUniversalPolicy.deInitData).toEqual('0x') | ||
}) | ||
|
||
// ----------------------- | ||
// Sudo Action Policy | ||
// ----------------------- | ||
it('should get install sudo action policy', async () => { | ||
const installSudoActionPolicy = getSudoPolicy() | ||
expect(installSudoActionPolicy.address).toBeDefined() | ||
expect(installSudoActionPolicy.initData).toEqual('0x') | ||
expect(installSudoActionPolicy.deInitData).toEqual('0x') | ||
}) | ||
|
||
// ----------------------- | ||
// Spending Limit Policy | ||
// ----------------------- | ||
it('should get install spending limit policy', async () => { | ||
const installSpendingLimitPolicy = getSpendingLimitsPolicy([ | ||
{ limit: BigInt(1000), token: zeroAddress }, | ||
]) | ||
|
||
expect(installSpendingLimitPolicy.address).toBeDefined() | ||
expect(installSpendingLimitPolicy.initData).toBeDefined() | ||
expect(installSpendingLimitPolicy.deInitData).toEqual('0x') | ||
}) | ||
}) |