-
-
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
Changes from all commits
271db59
05387b5
4487ce9
488e6ce
b040b8f
04ace06
5a0710e
939ce23
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,15 +1,20 @@ | ||
| import { decodeERC20StreamingTerms } from '@metamask/delegation-core'; | ||
| import { bigIntToHex } from '@metamask/utils'; | ||
| import type { Caveat } from '@metamask/delegation-core'; | ||
| import { | ||
| createERC20StreamingTerms, | ||
| createValueLteTerms, | ||
| decodeERC20StreamingTerms, | ||
| } from '@metamask/delegation-core'; | ||
| import { bigIntToHex, hexToBigInt } from '@metamask/utils'; | ||
|
|
||
| import type { Erc20TokenStreamPermission } from '../../types'; | ||
| import type { Erc20TokenStreamPermission, Populated } from '../../types'; | ||
| import { expiryRuleDecoder } from '../rules/expiry'; | ||
| import { erc20PayeeRuleDecoder } from '../rules/payee'; | ||
| import { redeemerRuleDecoder } from '../rules/redeemer'; | ||
| import type { | ||
| ChecksumCaveat, | ||
| ChecksumEnforcersByChainId, | ||
| DecodedPermissionData, | ||
| MakePermissionDecoderConfig, | ||
| ChecksumEnforcersByChainId, | ||
| PermissionDecoderConfig, | ||
| } from '../types'; | ||
| import { getTermsByEnforcer, ZERO_32_BYTES } from '../utils'; | ||
|
|
||
|
|
@@ -21,7 +26,7 @@ import { getTermsByEnforcer, ZERO_32_BYTES } from '../utils'; | |
| */ | ||
| export function makeErc20TokenStreamDecoderConfig( | ||
| contractAddresses: ChecksumEnforcersByChainId, | ||
| ): MakePermissionDecoderConfig { | ||
| ): PermissionDecoderConfig { | ||
|
Contributor
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. I believe the other six caveats use
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. Oops - I removed the duplicate type, and settled on |
||
| const { | ||
| timestampEnforcer, | ||
| erc20StreamingEnforcer, | ||
|
|
@@ -104,3 +109,75 @@ function validateAndDecodeData( | |
| startTime, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Enforcers required to build ERC-20 token stream caveats. | ||
| */ | ||
| export type Erc20TokenStreamEnforcers = Pick< | ||
| ChecksumEnforcersByChainId, | ||
| 'erc20StreamingEnforcer' | 'valueLteEnforcer' | ||
| >; | ||
|
|
||
| /** | ||
| * Builds the ERC-20 stream caveats required for this permission type. | ||
| * | ||
| * @param options0 - Caveat builder arguments. | ||
| * @param options0.permission - Fully populated ERC-20 stream permission data. | ||
| * @param options0.contracts - Enforcer addresses used to construct caveats. | ||
| * @returns The ERC20 streaming and zero-value caveats. | ||
| */ | ||
| export function createErc20TokenStreamCaveats({ | ||
| permission, | ||
| contracts, | ||
| }: { | ||
| permission: Populated<Erc20TokenStreamPermission>; | ||
| contracts: Erc20TokenStreamEnforcers; | ||
| }): Caveat[] { | ||
| const { initialAmount, maxAmount, amountPerSecond, startTime } = | ||
| permission.data; | ||
| const initialAmountBigInt = hexToBigInt(initialAmount); | ||
| const maxAmountBigInt = hexToBigInt(maxAmount); | ||
| const amountPerSecondBigInt = hexToBigInt(amountPerSecond); | ||
|
|
||
| if (maxAmountBigInt <= initialAmountBigInt) { | ||
| throw new Error( | ||
| 'Invalid erc20-token-stream permission: maxAmount must be greater than initialAmount.', | ||
| ); | ||
| } | ||
|
|
||
| if (amountPerSecondBigInt === 0n) { | ||
| throw new Error( | ||
| 'Invalid erc20-token-stream permission: amountPerSecond must be a positive number.', | ||
| ); | ||
| } | ||
|
|
||
| if (startTime <= 0) { | ||
| throw new Error( | ||
| 'Invalid erc20-token-stream permission: startTime must be a positive number.', | ||
| ); | ||
| } | ||
|
|
||
| // ERC20StreamingEnforcer: enforce token address, initial/max amount, amount per second, and start time. | ||
| const erc20StreamingCaveat: Caveat = { | ||
| enforcer: contracts.erc20StreamingEnforcer, | ||
| terms: createERC20StreamingTerms({ | ||
| tokenAddress: permission.data.tokenAddress, | ||
| initialAmount: initialAmountBigInt, | ||
| maxAmount: maxAmountBigInt, | ||
| amountPerSecond: amountPerSecondBigInt, | ||
| startTime, | ||
| }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| // ValueLteEnforcer: allow no native value (e.g. msg.value must be 0). | ||
| const valueLteCaveat: Caveat = { | ||
| enforcer: contracts.valueLteEnforcer, | ||
| terms: createValueLteTerms({ | ||
| maxValue: 0n, | ||
| }), | ||
| args: '0x', | ||
| }; | ||
|
|
||
| return [erc20StreamingCaveat, valueLteCaveat]; | ||
| } | ||
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.
this is a temporary measure, until we migrate to the fixed allowance enforcer.