-
-
Notifications
You must be signed in to change notification settings - Fork 39
feat: adds utility functions to create caveats array for each permission type #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
271db59
05387b5
4487ce9
488e6ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import type { Caveat } from '@metamask/delegation-core'; | ||
| import { | ||
| createERC20TokenPeriodTransferTerms, | ||
| createValueLteTerms, | ||
| } from '@metamask/delegation-core'; | ||
| import { hexToNumber } from '@metamask/utils'; | ||
|
|
||
| import type { Erc20TokenAllowancePermission } from '../../types'; | ||
|
|
@@ -8,6 +13,7 @@ import type { | |
| ChecksumCaveat, | ||
| ChecksumEnforcersByChainId, | ||
| DecodedPermissionData, | ||
| DeepRequired, | ||
| MakePermissionDecoderConfig, | ||
| } from '../types'; | ||
| import { | ||
|
|
@@ -111,3 +117,49 @@ function validateAndDecodeData( | |
|
|
||
| return { tokenAddress, allowanceAmount, startTime }; | ||
| } | ||
|
|
||
| /** | ||
| * Enforcers required to build ERC-20 token allowance caveats. | ||
| */ | ||
| export type Erc20TokenAllowanceEnforcers = Pick< | ||
| ChecksumEnforcersByChainId, | ||
| 'erc20PeriodicEnforcer' | 'valueLteEnforcer' | ||
| >; | ||
|
|
||
| /** | ||
| * Builds the erc20-token-allowance caveats required for this permission type. | ||
| * | ||
| * @param options0 - Caveat builder arguments. | ||
| * @param options0.permission - Fully populated erc20-token-allowance permission data. | ||
| * @param options0.contracts - Enforcer addresses used to construct caveats. | ||
| * @returns The ERC-20 allowance and zero-value caveats. | ||
| */ | ||
| export async function createErc20TokenAllowanceCaveats({ | ||
| permission, | ||
| contracts, | ||
| }: { | ||
| permission: DeepRequired<Erc20TokenAllowancePermission>; | ||
| contracts: Erc20TokenAllowanceEnforcers; | ||
| }): Promise<Caveat[]> { | ||
| const { tokenAddress, allowanceAmount, startTime } = permission.data; | ||
|
|
||
| const erc20PeriodCaveat: Caveat = { | ||
| enforcer: contracts.erc20PeriodicEnforcer, | ||
| terms: createERC20TokenPeriodTransferTerms({ | ||
| tokenAddress, | ||
| periodAmount: BigInt(allowanceAmount), | ||
| // delegation-core accepts bigint for encoding although the type is `number`. | ||
| periodDuration: BigInt(UINT256_MAX) as unknown as number, | ||
|
Comment on lines
+151
to
+152
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a temporary measure, until we migrate to the fixed allowance enforcer. |
||
| startDate: startTime, | ||
| }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| const valueLteCaveat: Caveat = { | ||
| enforcer: contracts.valueLteEnforcer, | ||
| terms: createValueLteTerms({ maxValue: 0n }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| return [erc20PeriodCaveat, valueLteCaveat]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import type { Caveat } from '@metamask/delegation-core'; | ||
| import { | ||
| createExactCalldataTerms, | ||
| createNativeTokenPeriodTransferTerms, | ||
| } from '@metamask/delegation-core'; | ||
| import { hexToNumber } from '@metamask/utils'; | ||
|
|
||
| import type { NativeTokenAllowancePermission } from '../../types'; | ||
|
|
@@ -8,6 +13,7 @@ import type { | |
| ChecksumCaveat, | ||
| ChecksumEnforcersByChainId, | ||
| DecodedPermissionData, | ||
| DeepRequired, | ||
| MakePermissionDecoderConfig, | ||
| } from '../types'; | ||
| import { | ||
|
|
@@ -115,3 +121,48 @@ function validateAndDecodeData( | |
|
|
||
| return { allowanceAmount, startTime }; | ||
| } | ||
|
|
||
| /** | ||
| * Enforcers required to build native token allowance caveats. | ||
| */ | ||
| export type NativeTokenAllowanceEnforcers = Pick< | ||
| ChecksumEnforcersByChainId, | ||
| 'nativeTokenPeriodicEnforcer' | 'exactCalldataEnforcer' | ||
| >; | ||
|
|
||
| /** | ||
| * Builds the native-token-allowance caveats required for this permission type. | ||
| * | ||
| * @param options0 - Caveat builder arguments. | ||
| * @param options0.permission - Fully populated native-token-allowance permission data. | ||
| * @param options0.contracts - Enforcer addresses used to construct caveats. | ||
| * @returns The native token allowance and exact-calldata caveats. | ||
| */ | ||
| export async function createNativeTokenAllowanceCaveats({ | ||
| permission, | ||
| contracts, | ||
| }: { | ||
| permission: DeepRequired<NativeTokenAllowancePermission>; | ||
| contracts: NativeTokenAllowanceEnforcers; | ||
| }): Promise<Caveat[]> { | ||
| const { allowanceAmount, startTime } = permission.data; | ||
|
|
||
| const nativeTokenPeriodTransferCaveat: Caveat = { | ||
| enforcer: contracts.nativeTokenPeriodicEnforcer, | ||
| terms: createNativeTokenPeriodTransferTerms({ | ||
| periodAmount: BigInt(allowanceAmount), | ||
| // delegation-core accepts bigint for encoding although the type is `number`. | ||
| periodDuration: BigInt(UINT256_MAX) as unknown as number, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a temporary measure, until we migrate to the fixed allowance enforcer. |
||
| startDate: startTime, | ||
| }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| const exactCalldataCaveat: Caveat = { | ||
| enforcer: contracts.exactCalldataEnforcer, | ||
| terms: createExactCalldataTerms({ calldata: '0x' }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| return [nativeTokenPeriodTransferCaveat, exactCalldataCaveat]; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can’t see any
awaitinside this function, should we delete theasynchere?