-
-
Notifications
You must be signed in to change notification settings - Fork 40
Adds Contract Utils (Caveats, Delegation) #35
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1db27d
feat: adds contract utils
hanzel98 b13dec2
chore: class to client actions
hanzel98 8adf555
refactor: using actions and extending client
hanzel98 149ee3e
chore: local estimate for available amount in streaming enforcers
hanzel98 ae1552e
fix: lint issues, formatting
hanzel98 46ec7e5
fix: issues with lint
hanzel98 1dbc326
chore: deleted unused isHex function
hanzel98 143a8ee
feat: add standalone caveat enforcer action functions
hanzel98 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
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
21 changes: 21 additions & 0 deletions
21
...legation-toolkit/src/DelegationFramework/DelegationManager/methods/disabledDelegations.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,21 @@ | ||
| import { DelegationManager } from '@metamask/delegation-abis'; | ||
| import type { Address, Client, Hex } from 'viem'; | ||
| import { readContract } from 'viem/actions'; | ||
|
|
||
| export type ReadDisabledDelegationsParameters = { | ||
| client: Client; | ||
| contractAddress: Address; | ||
| delegationHash: Hex; | ||
| }; | ||
|
|
||
| export const read = async ({ | ||
| client, | ||
| contractAddress, | ||
| delegationHash, | ||
| }: ReadDisabledDelegationsParameters) => | ||
| await readContract(client, { | ||
| address: contractAddress, | ||
| abi: DelegationManager.abi, | ||
| functionName: 'disabledDelegations', | ||
| args: [delegationHash], | ||
| }); |
3 changes: 2 additions & 1 deletion
3
packages/delegation-toolkit/src/DelegationFramework/DelegationManager/read.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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { read as disabledDelegations } from './methods/disabledDelegations'; | ||
| import { read as getAnyDelegate } from './methods/getAnyDelegate'; | ||
| import { read as getRootAuthority } from './methods/getRootAuthority'; | ||
|
|
||
| export { getAnyDelegate, getRootAuthority }; | ||
| export { getAnyDelegate, getRootAuthority, disabledDelegations }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/ERC20PeriodTransferEnforcer/index.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,3 @@ | ||
| import * as read from './read'; | ||
|
|
||
| export { read }; |
35 changes: 35 additions & 0 deletions
35
...toolkit/src/DelegationFramework/ERC20PeriodTransferEnforcer/methods/getAvailableAmount.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,35 @@ | ||
| import { ERC20PeriodTransferEnforcer } from '@metamask/delegation-abis'; | ||
| import type { Address, Client, Hex } from 'viem'; | ||
| import { readContract } from 'viem/actions'; | ||
|
|
||
| export type ReadGetAvailableAmountParameters = { | ||
| client: Client; | ||
| contractAddress: Address; | ||
| delegationHash: Hex; | ||
| delegationManager: Address; | ||
| terms: Hex; | ||
| }; | ||
|
|
||
| export const read = async ({ | ||
| client, | ||
| contractAddress, | ||
| delegationHash, | ||
| delegationManager, | ||
| terms, | ||
| }: ReadGetAvailableAmountParameters) => { | ||
| const [availableAmount, isNewPeriod, currentPeriod] = await readContract( | ||
| client, | ||
| { | ||
| address: contractAddress, | ||
| abi: ERC20PeriodTransferEnforcer.abi, | ||
| functionName: 'getAvailableAmount', | ||
| args: [delegationHash, delegationManager, terms], | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| availableAmount, | ||
| isNewPeriod, | ||
| currentPeriod, | ||
| }; | ||
| }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/ERC20PeriodTransferEnforcer/read.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,3 @@ | ||
| import { read as getAvailableAmount } from './methods/getAvailableAmount'; | ||
|
|
||
| export { getAvailableAmount }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/ERC20StreamingEnforcer/index.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,3 @@ | ||
| import * as read from './read'; | ||
|
|
||
| export { read }; |
126 changes: 126 additions & 0 deletions
126
...tion-toolkit/src/DelegationFramework/ERC20StreamingEnforcer/methods/getAvailableAmount.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,126 @@ | ||
| import { ERC20StreamingEnforcer } from '@metamask/delegation-abis'; | ||
| import type { Address, Client, Hex } from 'viem'; | ||
| import { readContract, getBlock } from 'viem/actions'; | ||
|
|
||
| export type ReadGetAvailableAmountParameters = { | ||
| client: Client; | ||
| contractAddress: Address; | ||
| delegationManager: Address; | ||
| delegationHash: Hex; | ||
| terms: Hex; | ||
| }; | ||
|
|
||
| export const read = async ({ | ||
| client, | ||
| contractAddress, | ||
| delegationManager, | ||
| delegationHash, | ||
| terms, | ||
| }: ReadGetAvailableAmountParameters) => { | ||
| // Get current block timestamp from blockchain | ||
| const currentBlock = await getBlock(client); | ||
| const currentTimestamp = currentBlock.timestamp; | ||
|
|
||
| // First, get the current state from the contract | ||
| const allowanceState = await readContract(client, { | ||
| address: contractAddress, | ||
| abi: ERC20StreamingEnforcer.abi, | ||
| functionName: 'streamingAllowances', | ||
| args: [delegationManager, delegationHash], | ||
| }); | ||
|
|
||
| const [initialAmount, maxAmount, amountPerSecond, startTime, spent] = | ||
| allowanceState; | ||
|
|
||
| // Check if state exists (startTime != 0) | ||
| if (startTime !== 0n) { | ||
| // State exists, calculate available amount using the stored state | ||
| const availableAmount = getAvailableAmount({ | ||
| initialAmount, | ||
| maxAmount, | ||
| amountPerSecond, | ||
| startTime, | ||
| spent, | ||
| currentTimestamp, | ||
| }); | ||
|
|
||
| return { | ||
| availableAmount, | ||
| }; | ||
| } | ||
|
|
||
| // State doesn't exist, decode terms and simulate with spent = 0 | ||
| const decodedTerms = await readContract(client, { | ||
| address: contractAddress, | ||
| abi: ERC20StreamingEnforcer.abi, | ||
| functionName: 'getTermsInfo', | ||
| args: [terms], | ||
| }); | ||
|
|
||
| const [ | ||
| , | ||
| decodedInitialAmount, | ||
| decodedMaxAmount, | ||
| decodedAmountPerSecond, | ||
| decodedStartTime, | ||
| ] = decodedTerms; | ||
|
|
||
| // Simulate using decoded terms with spent = 0 | ||
| const availableAmount = getAvailableAmount({ | ||
| initialAmount: decodedInitialAmount, | ||
| maxAmount: decodedMaxAmount, | ||
| amountPerSecond: decodedAmountPerSecond, | ||
| startTime: decodedStartTime, | ||
| spent: 0n, | ||
| currentTimestamp, | ||
| }); | ||
|
|
||
| return { | ||
| availableAmount, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Replicates the internal _getAvailableAmount logic from the smart contract. | ||
| * | ||
| * @param allowance - The allowance object containing all parameters. | ||
| * @param allowance.initialAmount - The initial amount available. | ||
| * @param allowance.maxAmount - The maximum amount allowed. | ||
| * @param allowance.amountPerSecond - The amount streamed per second. | ||
| * @param allowance.startTime - The start time of the streaming. | ||
| * @param allowance.spent - The amount already spent. | ||
| * @param allowance.currentTimestamp - The current timestamp. | ||
| * @returns The available amount that can be spent. | ||
| */ | ||
| function getAvailableAmount(allowance: { | ||
| initialAmount: bigint; | ||
| maxAmount: bigint; | ||
| amountPerSecond: bigint; | ||
| startTime: bigint; | ||
| spent: bigint; | ||
| currentTimestamp: bigint; | ||
| }): bigint { | ||
| // If current time is before start time, nothing is available | ||
| if (allowance.currentTimestamp < allowance.startTime) { | ||
| return 0n; | ||
| } | ||
|
|
||
| // Calculate elapsed time since start | ||
| const elapsed = allowance.currentTimestamp - allowance.startTime; | ||
|
|
||
| // Calculate total unlocked amount | ||
| let unlocked = allowance.initialAmount + allowance.amountPerSecond * elapsed; | ||
|
|
||
| // Cap by max amount | ||
| if (unlocked > allowance.maxAmount) { | ||
| unlocked = allowance.maxAmount; | ||
| } | ||
|
|
||
| // If spent >= unlocked, nothing available | ||
| if (allowance.spent >= unlocked) { | ||
| return 0n; | ||
| } | ||
|
|
||
| // Return available amount | ||
| return unlocked - allowance.spent; | ||
| } |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/ERC20StreamingEnforcer/read.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,3 @@ | ||
| import { read as getAvailableAmount } from './methods/getAvailableAmount'; | ||
|
|
||
| export { getAvailableAmount }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/MultiTokenPeriodEnforcer/index.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,3 @@ | ||
| import * as read from './read'; | ||
|
|
||
| export { read }; |
37 changes: 37 additions & 0 deletions
37
...on-toolkit/src/DelegationFramework/MultiTokenPeriodEnforcer/methods/getAvailableAmount.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,37 @@ | ||
| import { MultiTokenPeriodEnforcer } from '@metamask/delegation-abis'; | ||
| import type { Address, Client, Hex } from 'viem'; | ||
| import { readContract } from 'viem/actions'; | ||
|
|
||
| export type ReadGetAvailableAmountParameters = { | ||
| client: Client; | ||
| contractAddress: Address; | ||
| delegationHash: Hex; | ||
| delegationManager: Address; | ||
| terms: Hex; | ||
| args: Hex; | ||
| }; | ||
|
|
||
| export const read = async ({ | ||
| client, | ||
| contractAddress, | ||
| delegationHash, | ||
| delegationManager, | ||
| terms, | ||
| args, | ||
| }: ReadGetAvailableAmountParameters) => { | ||
| const [availableAmount, isNewPeriod, currentPeriod] = await readContract( | ||
| client, | ||
| { | ||
| address: contractAddress, | ||
| abi: MultiTokenPeriodEnforcer.abi, | ||
| functionName: 'getAvailableAmount', | ||
| args: [delegationHash, delegationManager, terms, args], | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| availableAmount, | ||
| isNewPeriod, | ||
| currentPeriod, | ||
| }; | ||
| }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/MultiTokenPeriodEnforcer/read.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,3 @@ | ||
| import { read as getAvailableAmount } from './methods/getAvailableAmount'; | ||
|
|
||
| export { getAvailableAmount }; |
3 changes: 3 additions & 0 deletions
3
...ges/delegation-toolkit/src/DelegationFramework/NativeTokenPeriodTransferEnforcer/index.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,3 @@ | ||
| import * as read from './read'; | ||
|
|
||
| export { read }; |
35 changes: 35 additions & 0 deletions
35
...t/src/DelegationFramework/NativeTokenPeriodTransferEnforcer/methods/getAvailableAmount.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,35 @@ | ||
| import { NativeTokenPeriodTransferEnforcer } from '@metamask/delegation-abis'; | ||
| import type { Address, Client, Hex } from 'viem'; | ||
| import { readContract } from 'viem/actions'; | ||
|
|
||
| export type ReadGetAvailableAmountParameters = { | ||
| client: Client; | ||
| contractAddress: Address; | ||
| delegationHash: Hex; | ||
| delegationManager: Address; | ||
| terms: Hex; | ||
| }; | ||
|
|
||
| export const read = async ({ | ||
| client, | ||
| contractAddress, | ||
| delegationHash, | ||
| delegationManager, | ||
| terms, | ||
| }: ReadGetAvailableAmountParameters) => { | ||
| const [availableAmount, isNewPeriod, currentPeriod] = await readContract( | ||
| client, | ||
| { | ||
| address: contractAddress, | ||
| abi: NativeTokenPeriodTransferEnforcer.abi, | ||
| functionName: 'getAvailableAmount', | ||
| args: [delegationHash, delegationManager, terms], | ||
| }, | ||
| ); | ||
|
|
||
| return { | ||
| availableAmount, | ||
| isNewPeriod, | ||
| currentPeriod, | ||
| }; | ||
| }; |
3 changes: 3 additions & 0 deletions
3
...ages/delegation-toolkit/src/DelegationFramework/NativeTokenPeriodTransferEnforcer/read.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,3 @@ | ||
| import { read as getAvailableAmount } from './methods/getAvailableAmount'; | ||
|
|
||
| export { getAvailableAmount }; |
3 changes: 3 additions & 0 deletions
3
packages/delegation-toolkit/src/DelegationFramework/NativeTokenStreamingEnforcer/index.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,3 @@ | ||
| import * as read from './read'; | ||
|
|
||
| export { read }; |
Oops, something went wrong.
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.