Skip to content

Adds Contract Utils (Caveats, Delegation)#45

Merged
hanzel98 merged 3 commits into
mainfrom
feat/contract-utils2
Aug 5, 2025
Merged

Adds Contract Utils (Caveats, Delegation)#45
hanzel98 merged 3 commits into
mainfrom
feat/contract-utils2

Conversation

@hanzel98

@hanzel98 hanzel98 commented Jul 30, 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

Previous PR: #35

@hanzel98 hanzel98 requested a review from a team as a code owner July 30, 2025 22:00
@hanzel98 hanzel98 self-assigned this Jul 30, 2025
@hanzel98 hanzel98 requested a review from jeffsmale90 July 30, 2025 22:00

@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.

Looking nice! Couple pedantic comments about function signatures!

Comment thread packages/delegator-e2e/test/delegationManagement.test.ts Outdated
Comment thread packages/delegation-toolkit/src/actions/getCaveatAvailableAmount.ts
Comment on lines +287 to +293
export const caveatEnforcerActions = ({
client,
config,
}: {
client: PublicClient;
config: CaveatEnforcerClientConfig;
}) => ({

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 makes for a somewhat awkward function signature, but I think caveatEnforcerActions should return a function that takes the client as the argument. ie.

const caveatEnforcerActions = (config) => (client) => {
  return {...};
}

Then you can do this, instead of nesting the call to caveatEnforcerActions inside of an anonymous function:

const client = createPublicClient({...})
  .extend(caveatEnforcerActions({ environment }));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, do you like the function createCaveatEnforcerClient or you prefer to call the function extend directly where it is needed client.extend(caveatEnforcerActions({ environment }));

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 definitely like the createCaveatEnforcerClient function, as that's the easiest way to access the functionality, but supporting the second is also nice - maybe the user already has a client that they want to add those actions to.

jeffsmale90
jeffsmale90 previously approved these changes Aug 4, 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.

Looks great! A couple comments about any types in the tests, but nbd

const randomBytes32 = (): Hex => randomBytes(32);

describe('Caveat Contract Methods', () => {
let publicClient: ReturnType<typeof createPublicClient>;

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.

if you do this

Suggested change
let publicClient: ReturnType<typeof createPublicClient>;
let publicClient: PublicClient;

you can remove the publicClient as any assertion when creating the CaveatClient

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure thanks

const environmentWithoutDelegationManager = {
...mockEnvironment,
DelegationManager: undefined,
} as any;

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 could be as unknown as DeleGatorEnvironment to avoid unnecessary any types

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

@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.

🚀

@hanzel98 hanzel98 merged commit c0680a9 into main Aug 5, 2025
17 checks passed
@hanzel98 hanzel98 deleted the feat/contract-utils2 branch August 5, 2025 00:55
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