Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions packages/delegator-e2e/test/delegationManagement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@metamask/smart-accounts-kit/contracts';
import {
gasPrice,
transport,
sponsoredBundlerClient,
deploySmartAccount,
deployCounter,
Expand All @@ -26,12 +27,17 @@ import {
fundAddress,
} from './utils/helpers';
import { expectUserOperationToSucceed } from './utils/assertions';
import { encodeFunctionData, parseEther } from 'viem';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, encodeFunctionData, parseEther } from 'viem';
import {
generatePrivateKey,
privateKeyToAccount,
type PrivateKeyAccount,
} from 'viem/accounts';
import CounterMetadata from './utils/counter/metadata.json';

let aliceSmartAccount: MetaMaskSmartAccount<Implementation.Hybrid>;
let bobSmartAccount: MetaMaskSmartAccount<Implementation.Hybrid>;
let bob: PrivateKeyAccount;
let aliceCounter: CounterContract;

/**
Expand All @@ -51,7 +57,7 @@ let aliceCounter: CounterContract;

beforeEach(async () => {
const alice = privateKeyToAccount(generatePrivateKey());
const bob = privateKeyToAccount(generatePrivateKey());
bob = privateKeyToAccount(generatePrivateKey());

aliceSmartAccount = await toMetaMaskSmartAccount({
client: publicClient,
Expand Down Expand Up @@ -244,6 +250,74 @@ test('delegation management lifecycle: create, disable, enable, and check status
expect(finalCount).toEqual(2n);
});

test('can decode raw simulate and execute redeemDelegations errors', async () => {
await fundAddress(bob.address);

const bobWalletClient = createWalletClient({
account: bob,
transport,
chain: publicClient.chain,
});

const delegation = createDelegation({
to: bob.address,
from: aliceSmartAccount.address,
environment: aliceSmartAccount.environment,
scope: {
type: 'functionCall',
targets: [aliceCounter.address],
selectors: ['increment()'],
},
});

const signedDelegation = {
...delegation,
signature: await aliceSmartAccount.signDelegation({ delegation }),
};

const execution = createExecution({
target: aliceCounter.address,
callData: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'setCount',
args: [1n],
}),
});

const redeemParams = {
client: bobWalletClient,
delegationManagerAddress: aliceSmartAccount.environment.DelegationManager,
delegations: [[signedDelegation]],
modes: [ExecutionMode.SingleDefault],
executions: [[execution]],
};
const expectedError = 'AllowedMethodsEnforcer:method-not-allowed';

const simulateError = await DelegationManager.simulate
.redeemDelegations(redeemParams)
.then(
() => undefined,
(error: unknown) => error,
);

expect(simulateError).toBeDefined();
expect(
DelegationManager.decode.redeemDelegationsError(simulateError)?.message,
).toBe(expectedError);

const executeError = await DelegationManager.execute
.redeemDelegations(redeemParams)
.then(
() => undefined,
(error: unknown) => error,
);

expect(executeError).toBeDefined();
expect(
DelegationManager.decode.redeemDelegationsError(executeError)?.message,
).toBe(expectedError);
});

test('only delegator can disable their own delegation', async () => {
// Create a delegation from Alice to Bob
const delegation = createDelegation({
Expand Down
1 change: 1 addition & 0 deletions packages/smart-accounts-kit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- ERC-7715 `token-approval-revocation` permission type ([#226](https://github.com/MetaMask/smart-accounts-kit/pull/226), [#237](https://github.com/MetaMask/smart-accounts-kit/pull/237))
- `CaveatBuilder` for `ApprovalRevocationEnforcer`, deployment address added to `SmartAccountsEnvironment` ([#226](https://github.com/metamask/smart-accounts-kit/pull/226), [#237](https://github.com/MetaMask/smart-accounts-kit/pull/237))
- Helper for decoding revert reasons from delegated execution errors while preserving the original error output ([#245](https://github.com/MetaMask/smart-accounts-kit/pull/245))

### Changed

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { decodeError as redeemDelegationsError } from './methods/redeemDelegations';

export { redeemDelegationsError };
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as constants from './constants';
import * as decode from './decode';
import * as encode from './encode';
import * as execute from './execute';
import * as read from './read';
import * as simulate from './simulate';

export { encode, execute, read, simulate, constants };
export { decode, encode, execute, read, simulate, constants };
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type { Address, Client } from 'viem';
import { encodeFunctionData } from 'viem';
import { simulateContract, writeContract } from 'viem/actions';

import {
decodeRevertReason,
type DecodedRevertReason,
} from '../../../decodeRevertReason';
import { encodeDelegations } from '../../../delegation';
import { encodeExecutionCalldatas } from '../../../executions';
import type { ExecutionMode, ExecutionStruct } from '../../../executions';
Expand All @@ -25,6 +29,10 @@ export type ExecuteRedeemDelegationsParameters = {
delegationManagerAddress: Address;
} & EncodeRedeemDelegationsParameters;

export type DecodeRedeemDelegationsErrorReturnType =
| DecodedRevertReason
| undefined;

export const simulate = async ({
client,
delegationManagerAddress,
Expand Down Expand Up @@ -77,3 +85,13 @@ export const encode = ({
],
});
};

/**
* Decodes revert data from errors thrown by `simulate` or `execute`.
*
* @param error - The original error thrown by viem or an RPC provider.
* @returns A decoded revert reason, if one can be recognized.
*/
export const decodeError = (
error: unknown,
): DecodeRedeemDelegationsErrorReturnType => decodeRevertReason(error);
Loading
Loading