Skip to content

Adds Contract Utils (Caveats, Delegation)#35

Closed
hanzel98 wants to merge 8 commits into
mainfrom
feat/contract-utils
Closed

Adds Contract Utils (Caveats, Delegation)#35
hanzel98 wants to merge 8 commits into
mainfrom
feat/contract-utils

Conversation

@hanzel98

@hanzel98 hanzel98 commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

📝 Description

Some caveats enforcers that distribute amounts require the user to call a function to know how many tokens the user has available. This PR adds new utilities for that function.
The delegations can be disabled and enabled at any time; this PR adds a function to validate the current state of a delegation.

🔄 What Changed?

List the specific changes made:

  • Adds new contract utils to read the available amount of the different enforcers that distribute tokens.
  • Adds a new util to verify the state of a disabledDelegation.
  • Added new tests for these functions in the normal tests and e2e test.

🚀 Why?

Explain the motivation behind these changes:

  • We needed the ability to interact with the token distribution enforcers to know how many tokens are available.

🧪 How to Test?

Describe how to test these changes:

  • Automated tests added/updated for e2e and normal tests
  • All existing tests pass

⚠️ Breaking Changes

List any breaking changes:

  • No breaking changes
  • Breaking changes (describe below):

📋 Checklist

Check off completed items:

  • Code follows the project's coding standards
  • Self-review completed
  • Documentation updated (if needed)
  • Tests added/updated
  • Changelog updated (if needed)
  • All CI checks pass

🔗 Related Issues

Link to related issues:
Related to # https://app.zenhub.com/workspaces/readable-permissions-67982ce51eb4360029b2c1a1/issues/gh/metamask/delegator-readable-permissions/268

@hanzel98 hanzel98 requested a review from a team as a code owner July 12, 2025 16:05
@hanzel98 hanzel98 marked this pull request as draft July 12, 2025 16:06
@hanzel98 hanzel98 self-assigned this Jul 12, 2025
@hanzel98 hanzel98 requested a review from jeffsmale90 July 12, 2025 16:09

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Hex Validation Inconsistency

The isValidHex function is duplicated across packages/delegation-core/src/utils.ts and packages/delegation-toolkit/src/data.ts with differing implementations.

The delegation-core version uses manual regex validation and returns boolean, while the delegation-toolkit version uses viem's isHex() and returns a value is Hex type guard. This leads to inconsistent validation behavior, specifically that the delegation-toolkit version ignores the minLength constraint when exactLength is provided, unlike the delegation-core version which checks both. This duplication causes inconsistent validation and potential bugs across the codebase.

packages/delegation-core/src/utils.ts#L31-L54

https://github.com/MetaMask/delegation-toolkit/blob/d1db27d1058887d4480b4ef8464054442e865ba0/packages/delegation-core/src/utils.ts#L31-L54

packages/delegation-toolkit/src/data.ts#L10-L28

https://github.com/MetaMask/delegation-toolkit/blob/d1db27d1058887d4480b4ef8464054442e865ba0/packages/delegation-toolkit/src/data.ts#L10-L28

Fix in CursorFix in Web


BugBot free trial expires on July 22, 2025
You have used $0.00 of your $50.00 spend limit so far. Manage your spend limit in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

@hanzel98 hanzel98 changed the title feat: adds contract utils (caveats, delegation) Adds Contract Utils (Caveats, Delegation) Jul 12, 2025

@jeffsmale90 jeffsmale90 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @hanzel98 I didn't have time for a full review, but some initial high level thoughts.

Comment thread packages/delegation-core/src/utils.ts Outdated
Comment thread packages/delegation-toolkit/src/caveatUtils/caveatUtils.ts Outdated
Comment thread packages/delegation-toolkit/src/caveatUtils/caveatUtils.ts Outdated
Comment thread packages/delegation-toolkit/src/data.ts Outdated

@jeffsmale90 jeffsmale90 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a thing of beauty!

I love it!

@hanzel98 hanzel98 requested a review from jeffsmale90 July 28, 2025 15:20
@hanzel98 hanzel98 marked this pull request as ready for review July 28, 2025 15:41
Comment thread packages/delegation-core/src/utils.ts Outdated

@jeffsmale90 jeffsmale90 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really great - a very useful addition to the SDK 🎉

I added a couple of comments on the PR.

Comment on lines +132 to +134
getErc20PeriodTransferEnforcerAvailableAmount: async (
params: ERC20PeriodTransferParams,
): Promise<PeriodTransferResult> => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to export these actions individually too, and from @metamask/delegation-toolkit/actions

then people can be all:

const client = createPublicClient({...});

getErc20PeriodTransferEnforcerAvailableAmount(
  client,
  {
    ...
});

note: the action interface is (client, config) rather than ({ client, ...config}).

Without the overhead of the client - viem suggests that this can improve tree shaking, and recommends it for devs who are "pendantic about bundle size" 🤣 https://viem.sh/docs/clients/custom#tree-shaking

@@ -0,0 +1,152 @@
import { isStrictHexString } from '@metamask/utils';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I probably misled you here a little - @metamask/utils isStrictHexString is correctly used in @metamask/delegation-core, but in @metamask/delegation-toolkit we should probably use viem's isHex function - their docs appear to be down rn, https://viem.sh/docs/utilities/isHex.html but there's an optional { strict: bool } parameter which defaults to true.

@hanzel98 hanzel98 marked this pull request as draft July 30, 2025 21:22
@hanzel98

hanzel98 commented Jul 30, 2025

Copy link
Copy Markdown
Contributor Author

#45

@hanzel98 hanzel98 closed this Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants