Skip to content

Commit 7384a0e

Browse files
committed
Consolidate ExecutionMode type and constants to enum
1 parent f71db8f commit 7384a0e

33 files changed

Lines changed: 120 additions & 132 deletions

packages/delegation-toolkit/src/encodeCalls.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
executeWithMode,
88
} from './DelegationFramework/DeleGatorCore/encode';
99
import {
10-
BATCH_DEFAULT_MODE,
10+
ExecutionMode,
1111
createExecution,
12-
SINGLE_DEFAULT_MODE,
1312
encodeExecutionCalldatas,
1413
} from './executions';
1514
import type { DelegatedCall } from './experimental/erc7710RedeemDelegationAction';
@@ -54,7 +53,7 @@ const processDelegatedCall = (call: DelegatedCall) => {
5453
functionName: 'redeemDelegations',
5554
args: [
5655
[permissionsContext],
57-
[SINGLE_DEFAULT_MODE],
56+
[ExecutionMode.SingleDefault],
5857
encodeExecutionCalldatas([[callAsExecution]]),
5958
],
6059
});
@@ -92,7 +91,10 @@ export const encodeCalls = (calls: readonly Call[]) => {
9291
return createExecution({ target, value, callData });
9392
});
9493

95-
const mode = calls.length === 1 ? SINGLE_DEFAULT_MODE : BATCH_DEFAULT_MODE;
94+
const mode =
95+
calls.length === 1
96+
? ExecutionMode.SingleDefault
97+
: ExecutionMode.BatchDefault;
9698
return executeWithMode({ mode, executions });
9799
};
98100

@@ -105,7 +107,7 @@ export const encodeCalls = (calls: readonly Call[]) => {
105107
* @description
106108
* - If there's a single call directly to the delegator, it returns the call data directly.
107109
* - For multiple calls or calls to other addresses, it creates executions and encodes them for the DeleGator's execute function.
108-
* - The execution mode is set to SINGLE_DEFAULT_MODE for a single call, or BATCH_DEFAULT_MODE for multiple calls.
110+
* - The execution mode is set to ExecutionMode.SingleDefault for a single call, or ExecutionMode.BatchDefault for multiple calls.
109111
*
110112
* todo: This doesn't fully expose the flexibility of the DeleGator's execute function, but it's a start.
111113
* maybe we add a mechanism where individual calls passed to this function can be encoded batches.

packages/delegation-toolkit/src/executions.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,12 @@ export const createExecution = ({
3737

3838
// Encoded modes
3939
// https://github.com/erc7579/erc7579-implementation/blob/main/src/lib/ModeLib.sol
40-
export const SINGLE_DEFAULT_MODE =
41-
'0x0000000000000000000000000000000000000000000000000000000000000000';
42-
export const SINGLE_TRY_MODE =
43-
'0x0001000000000000000000000000000000000000000000000000000000000000';
44-
export const BATCH_DEFAULT_MODE =
45-
'0x0100000000000000000000000000000000000000000000000000000000000000';
46-
export const BATCH_TRY_MODE =
47-
'0x0101000000000000000000000000000000000000000000000000000000000000';
48-
49-
export type ExecutionMode =
50-
| typeof SINGLE_DEFAULT_MODE
51-
| typeof SINGLE_TRY_MODE
52-
| typeof BATCH_DEFAULT_MODE
53-
| typeof BATCH_TRY_MODE;
40+
export enum ExecutionMode {
41+
SingleDefault = '0x0000000000000000000000000000000000000000000000000000000000000000',
42+
SingleTry = '0x0001000000000000000000000000000000000000000000000000000000000000',
43+
BatchDefault = '0x0100000000000000000000000000000000000000000000000000000000000000',
44+
BatchTry = '0x0101000000000000000000000000000000000000000000000000000000000000',
45+
}
5446

5547
/**
5648
* The ABI type components of an Execution.

packages/delegation-toolkit/src/experimental/erc7710RedeemDelegationAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { getDeleGatorEnvironment } from '../delegatorEnvironment';
2121
import {
2222
createExecution,
2323
encodeExecutionCalldatas,
24-
SINGLE_DEFAULT_MODE,
24+
ExecutionMode,
2525
} from '../executions';
2626
import type { Call } from 'src/types';
2727

@@ -74,7 +74,7 @@ export async function sendTransactionWithDelegationAction<
7474
functionName: 'redeemDelegations',
7575
args: [
7676
[args.permissionsContext],
77-
[SINGLE_DEFAULT_MODE],
77+
[ExecutionMode.SingleDefault],
7878
encodeExecutionCalldatas([executions]),
7979
],
8080
});

packages/delegation-toolkit/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export {
3838

3939
export { Implementation } from './constants';
4040

41-
export { createExecution } from './executions';
41+
export { createExecution, ExecutionMode } from './executions';
4242

4343
export type { ExecutionStruct, CreateExecutionArgs } from './executions';
4444

packages/delegation-toolkit/src/utils/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ export {
2020
encodeExecutionCalldatas,
2121
encodeSingleExecution,
2222
encodeBatchExecution,
23-
SINGLE_DEFAULT_MODE,
24-
SINGLE_TRY_MODE,
25-
BATCH_DEFAULT_MODE,
2623
} from '../executions';
2724

28-
export type { ExecutionMode, ExecutionStruct } from '../executions';
25+
export type { ExecutionStruct } from '../executions';
2926

3027
export type { AuthenticatorFlags } from '../webAuthn';
3128

packages/delegation-toolkit/test/encodeCalls.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import type { Address } from 'viem';
44
import { encodeFunctionData } from 'viem';
55

66
import { encodeCallsForCaller } from '../src/encodeCalls';
7-
import {
8-
BATCH_DEFAULT_MODE,
9-
encodeExecutionCalldatas,
10-
} from '../src/executions';
7+
import { ExecutionMode, encodeExecutionCalldatas } from '../src/executions';
118
import type { ExecutionStruct } from '../src/executions';
129
import { type Call } from '../src/types';
1310

@@ -74,7 +71,7 @@ describe('encodeCallsForCaller', () => {
7471
const expectedEncodedCalls = encodeFunctionData({
7572
abi: DeleGatorCore.abi,
7673
functionName: 'execute',
77-
args: [BATCH_DEFAULT_MODE, expectedExecutionCalldata],
74+
args: [ExecutionMode.BatchDefault, expectedExecutionCalldata],
7875
});
7976

8077
expect(encodedCalls).to.equal(expectedEncodedCalls);
@@ -118,7 +115,7 @@ describe('encodeCallsForCaller', () => {
118115
const expectedEncodedCalls = encodeFunctionData({
119116
abi: DeleGatorCore.abi,
120117
functionName: 'execute',
121-
args: [BATCH_DEFAULT_MODE, expectedExecutionCalldata],
118+
args: [ExecutionMode.BatchDefault, expectedExecutionCalldata],
122119
});
123120

124121
expect(encodedCalls).to.equal(expectedEncodedCalls);
@@ -162,7 +159,7 @@ describe('encodeCallsForCaller', () => {
162159
const expectedEncodedCalls = encodeFunctionData({
163160
abi: DeleGatorCore.abi,
164161
functionName: 'execute',
165-
args: [BATCH_DEFAULT_MODE, expectedExecutionCalldata],
162+
args: [ExecutionMode.BatchDefault, expectedExecutionCalldata],
166163
});
167164

168165
expect(encodedCalls).to.equal(expectedEncodedCalls);

packages/delegation-toolkit/test/experimental/erc7710RedeemDelegationAction.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { overrideDeployedEnvironment } from '../../src/delegatorEnvironment';
2424
import {
2525
createExecution,
2626
encodeExecutionCalldatas,
27-
SINGLE_DEFAULT_MODE,
27+
ExecutionMode,
2828
} from '../../src/executions';
2929
import {
3030
erc7710BundlerActions,
@@ -301,7 +301,7 @@ describe('erc7710RedeemDelegationAction', () => {
301301
functionName: 'redeemDelegations',
302302
args: [
303303
[args.permissionsContext],
304-
[SINGLE_DEFAULT_MODE],
304+
[ExecutionMode.SingleDefault],
305305
encodeExecutionCalldatas([
306306
[
307307
createExecution({

packages/delegator-e2e/test/caveats/allowedCalldata.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
Implementation,
77
toMetaMaskSmartAccount,
88
type MetaMaskSmartAccount,
9+
ExecutionMode,
910
} from '@metamask/delegation-toolkit';
1011
import {
1112
encodeExecutionCalldatas,
1213
encodePermissionContexts,
13-
SINGLE_DEFAULT_MODE,
1414
} from '@metamask/delegation-toolkit/utils';
1515
import {
1616
transport,
@@ -171,7 +171,7 @@ const runTest_expectSuccess = async (
171171
functionName: 'redeemDelegations',
172172
args: [
173173
encodePermissionContexts([[signedDelegation]]),
174-
[SINGLE_DEFAULT_MODE],
174+
[ExecutionMode.SingleDefault],
175175
encodeExecutionCalldatas([[execution]]),
176176
],
177177
});
@@ -233,7 +233,7 @@ const runTest_expectFailure = async (
233233
functionName: 'redeemDelegations',
234234
args: [
235235
encodePermissionContexts([[signedDelegation]]),
236-
[SINGLE_DEFAULT_MODE],
236+
[ExecutionMode.SingleDefault],
237237
encodeExecutionCalldatas([[execution]]),
238238
],
239239
});

packages/delegator-e2e/test/caveats/allowedMethods.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { beforeEach, test, expect } from 'vitest';
22
import {
33
encodeExecutionCalldatas,
44
encodePermissionContexts,
5-
SINGLE_DEFAULT_MODE,
65
} from '@metamask/delegation-toolkit/utils';
76
import {
87
createCaveatBuilder,
98
createExecution,
109
createDelegation,
1110
Implementation,
1211
toMetaMaskSmartAccount,
12+
ExecutionMode,
1313
type MetaMaskSmartAccount,
1414
} from '@metamask/delegation-toolkit';
1515
import {
@@ -167,7 +167,7 @@ const runTest_expectSuccess = async (
167167
functionName: 'redeemDelegations',
168168
args: [
169169
encodePermissionContexts([[signedDelegation]]),
170-
[SINGLE_DEFAULT_MODE],
170+
[ExecutionMode.SingleDefault],
171171
encodeExecutionCalldatas([[execution]]),
172172
],
173173
});
@@ -233,7 +233,7 @@ const runTest_expectFailure = async (
233233
functionName: 'redeemDelegations',
234234
args: [
235235
encodePermissionContexts([[signedDelegation]]),
236-
[SINGLE_DEFAULT_MODE],
236+
[ExecutionMode.SingleDefault],
237237
encodeExecutionCalldatas([[execution]]),
238238
],
239239
});

packages/delegator-e2e/test/caveats/allowedTargets.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { beforeEach, test, expect } from 'vitest';
22
import {
33
encodeExecutionCalldatas,
44
encodePermissionContexts,
5-
SINGLE_DEFAULT_MODE,
65
} from '@metamask/delegation-toolkit/utils';
76
import {
87
createCaveatBuilder,
98
createExecution,
109
createDelegation,
1110
Implementation,
1211
toMetaMaskSmartAccount,
12+
ExecutionMode,
1313
type MetaMaskSmartAccount,
1414
} from '@metamask/delegation-toolkit';
1515
import {
@@ -137,7 +137,7 @@ const runTest_expectSuccess = async (
137137
functionName: 'redeemDelegations',
138138
args: [
139139
encodePermissionContexts([[signedDelegation]]),
140-
[SINGLE_DEFAULT_MODE],
140+
[ExecutionMode.SingleDefault],
141141
encodeExecutionCalldatas([[execution]]),
142142
],
143143
});
@@ -203,7 +203,7 @@ const runTest_expectFailure = async (
203203
functionName: 'redeemDelegations',
204204
args: [
205205
encodePermissionContexts([[signedDelegation]]),
206-
[SINGLE_DEFAULT_MODE],
206+
[ExecutionMode.SingleDefault],
207207
encodeExecutionCalldatas([[execution]]),
208208
],
209209
});

0 commit comments

Comments
 (0)