-
-
Notifications
You must be signed in to change notification settings - Fork 40
Add createERC20TokenPeriodTransferTerms for @metamask/delegation-core
#15
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
96 changes: 96 additions & 0 deletions
96
packages/delegation-core/src/caveats/erc20TokenPeriodTransfer.ts
This file contains hidden or 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,96 @@ | ||
| import { type BytesLike, isHexString, bytesToHex } from '@metamask/utils'; | ||
|
|
||
| import { | ||
| defaultOptions, | ||
| prepareResult, | ||
| type EncodingOptions, | ||
| type ResultValue, | ||
| } from '../returns'; | ||
| import type { Hex } from '../types'; | ||
| import { toHexString } from '../utils'; | ||
|
|
||
| /** | ||
| * Terms for configuring a periodic transfer allowance of ERC20 tokens. | ||
| */ | ||
| export type ERC20TokenPeriodTransferTerms = { | ||
| /** The address of the ERC20 token. */ | ||
| tokenAddress: BytesLike; | ||
| /** The maximum amount that can be transferred within each period. */ | ||
| periodAmount: bigint; | ||
| /** The duration of each period in seconds. */ | ||
| periodDuration: number; | ||
| /** Unix timestamp when the first period begins. */ | ||
| startDate: number; | ||
| }; | ||
|
|
||
| /** | ||
| * Creates terms for an ERC20TokenPeriodTransfer caveat that validates that ERC20 token transfers | ||
| * do not exceed a specified amount within a given time period. The transferable amount resets at the | ||
| * beginning of each period, and any unused tokens are forfeited once the period ends. | ||
| * | ||
| * @param terms - The terms for the ERC20TokenPeriodTransfer caveat. | ||
| * @param encodingOptions - The encoding options for the result. | ||
| * @returns The terms as a 128-byte hex string (32 bytes for each parameter). | ||
| * @throws Error if any of the numeric parameters are invalid. | ||
| */ | ||
| export function createERC20TokenPeriodTransferTerms( | ||
| terms: ERC20TokenPeriodTransferTerms, | ||
| encodingOptions?: EncodingOptions<'hex'>, | ||
| ): Hex; | ||
| export function createERC20TokenPeriodTransferTerms( | ||
| terms: ERC20TokenPeriodTransferTerms, | ||
| encodingOptions: EncodingOptions<'bytes'>, | ||
| ): Uint8Array; | ||
| /** | ||
| * Creates terms for an ERC20TokenPeriodTransfer caveat that validates that ERC20 token transfers | ||
| * do not exceed a specified amount within a given time period. | ||
| * | ||
| * @param terms - The terms for the ERC20TokenPeriodTransfer caveat. | ||
| * @param encodingOptions - The encoding options for the result. | ||
| * @returns The terms as a 128-byte hex string (32 bytes for each parameter). | ||
| * @throws Error if any of the numeric parameters are invalid. | ||
| */ | ||
| export function createERC20TokenPeriodTransferTerms( | ||
| terms: ERC20TokenPeriodTransferTerms, | ||
| encodingOptions: EncodingOptions<ResultValue> = defaultOptions, | ||
| ): Hex | Uint8Array { | ||
| const { tokenAddress, periodAmount, periodDuration, startDate } = terms; | ||
|
|
||
| if (!tokenAddress) { | ||
| throw new Error('Invalid tokenAddress: must be a valid address'); | ||
| } | ||
|
|
||
| let prefixedTokenAddressHex: string; | ||
|
|
||
| if (typeof tokenAddress === 'string') { | ||
| if (!isHexString(tokenAddress) || tokenAddress.length !== 42) { | ||
| throw new Error('Invalid tokenAddress: must be a valid address'); | ||
| } | ||
| prefixedTokenAddressHex = tokenAddress; | ||
| } else { | ||
| if (tokenAddress.length !== 20) { | ||
| throw new Error('Invalid tokenAddress: must be a valid address'); | ||
| } | ||
| prefixedTokenAddressHex = bytesToHex(tokenAddress); | ||
| } | ||
|
|
||
| if (periodAmount <= 0n) { | ||
| throw new Error('Invalid periodAmount: must be a positive number'); | ||
| } | ||
|
|
||
| if (periodDuration <= 0) { | ||
| throw new Error('Invalid periodDuration: must be a positive number'); | ||
| } | ||
|
|
||
| if (startDate <= 0) { | ||
| throw new Error('Invalid startDate: must be a positive number'); | ||
| } | ||
|
|
||
| const periodAmountHex = toHexString({ value: periodAmount, size: 32 }); | ||
| const periodDurationHex = toHexString({ value: periodDuration, size: 32 }); | ||
| const startDateHex = toHexString({ value: startDate, size: 32 }); | ||
|
|
||
| const hexValue = `${prefixedTokenAddressHex}${periodAmountHex}${periodDurationHex}${startDateHex}`; | ||
|
|
||
| return prepareResult(hexValue, encodingOptions); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.