Skip to content
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

feat: Add Native Token Limit Module Support #1233

Merged
merged 4 commits into from
Dec 18, 2024
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
88 changes: 84 additions & 4 deletions account-kit/smart-contracts/src/ma-v2/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
import { local070Instance } from "~test/instances.js";
import { setBalance } from "viem/actions";
import { accounts } from "~test/constants.js";
import { getDefaultSingleSignerValidationModuleAddress } from "../modules/utils.js";
import {
getDefaultSingleSignerValidationModuleAddress,
getDefaultAllowlistModuleAddress,

Check warning on line 19 in account-kit/smart-contracts/src/ma-v2/client/client.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'getDefaultAllowlistModuleAddress' is defined but never used
Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ [eslint] <@typescript-eslint/no-unused-vars> reported by reviewdog 🐶
'getDefaultAllowlistModuleAddress' is defined but never used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will address in the relevant pr!

Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ [eslint] <@typescript-eslint/no-unused-vars> reported by reviewdog 🐶
'getDefaultAllowlistModuleAddress' is defined but never used.

getDefaultNativeTokenLimitModuleAddress,
} from "../modules/utils.js";
import { SingleSignerValidationModule } from "../modules/single-signer-validation/module.js";
import { allowlistModule } from "../modules/allowlist-module/module.js";
import { HookType } from "../actions/common/types.js";
import { nativeTokenLimitModule } from "../modules/native-token-limit-module/module.js";

describe("MA v2 Tests", async () => {
const instance = local070Instance;
Expand Down Expand Up @@ -191,9 +196,7 @@
});

it("installs allowlist module, then uninstalls", async () => {
let provider = (await givenConnectedProvider({ signer })).extend(
installValidationActions
);
let provider = await givenConnectedProvider({ signer });

await setBalance(client, {
address: provider.getAddress(),
Expand Down Expand Up @@ -266,6 +269,83 @@
).resolves.not.toThrowError();
});

it.only("installs native token limit module, then uninstalls", async () => {
let provider = await givenConnectedProvider({ signer });

await setBalance(client, {
address: provider.getAddress(),
value: parseEther("2"),
});

const spendLimit = parseEther("0.1"); // 0.1 ETH limit

// Let's verify the module's limit is set correctly after installation
const hookInstallData = nativeTokenLimitModule.encodeOnInstallData({
entityId: 0,
spendLimit,
});

const installResult = await provider.installValidation({
validationConfig: {
moduleAddress: zeroAddress,
entityId: 0,
isGlobal: true,
isSignatureValidation: true,
isUserOpValidation: true,
},
selectors: [],
installData: "0x",
hooks: [
{
hookConfig: {
address: getDefaultNativeTokenLimitModuleAddress(provider.chain),
entityId: 0,
hookType: HookType.VALIDATION,
hasPreHooks: true,
hasPostHooks: false,
},
initData: hookInstallData,
},
{
hookConfig: {
address: getDefaultNativeTokenLimitModuleAddress(provider.chain),
entityId: 0,
hookType: HookType.EXECUTION,
hasPreHooks: true,
hasPostHooks: false,
},
initData: hookInstallData,
},
],
});

await expect(
provider.waitForUserOperationTransaction(installResult)
).resolves.not.toThrowError();

// Try to send less than the limit - should pass
await expect(
provider.sendUserOperation({
uo: {
target: target,
value: parseEther("0.05"), // below the 0.1 limit
data: "0x",
},
})
).resolves.not.toThrowError();

// Try to send more than the limit - should fail
await expect(
provider.sendUserOperation({
uo: {
target: target,
value: parseEther("0.05"), // passing the 0.1 limit considering gas
data: "0x",
},
})
).rejects.toThrowError();
});

const givenConnectedProvider = async ({
signer,
accountAddress,
Expand Down
2 changes: 1 addition & 1 deletion account-kit/smart-contracts/src/ma-v2/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
createSmartAccountClient,
EntityIdOverrideError,

Check warning on line 3 in account-kit/smart-contracts/src/ma-v2/client/client.ts

View workflow job for this annotation

GitHub Actions / Lint

'EntityIdOverrideError' is defined but never used
type SmartAccountClient,
type SmartAccountSigner,
type SmartAccountClientConfig,
Expand Down Expand Up @@ -72,7 +72,7 @@
export function createSMAV2AccountClient<
TChain extends Chain = Chain,
TSigner extends SmartAccountSigner = SmartAccountSigner,
TCalldataEncoder extends CalldataEncoder = CalldataEncoder

Check warning on line 75 in account-kit/smart-contracts/src/ma-v2/client/client.ts

View workflow job for this annotation

GitHub Actions / Lint

'TCalldataEncoder' is defined but never used
>(
args: CreateSMAV2AccountClientParams<Transport, TChain, TSigner>
): Promise<
Expand Down Expand Up @@ -131,7 +131,7 @@
overrides,
}: InstallValidationParams) => {
if (validationConfig.entityId === 0) {
throw new EntityIdOverrideError();
// throw new EntityIdOverrideError();
}

const callData = await maV2Account.encodeCallData(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { encodeAbiParameters, type Hex } from "viem";

import { nativeTokenLimitModuleAbi } from "./abis/nativeTokenLimitModuleAbi.js";

export const nativeTokenLimitModule = {
abi: nativeTokenLimitModuleAbi,
encodeOnInstallData: (args: {
entityId: number;
spendLimit: bigint;
}): Hex => {
const { entityId, spendLimit } = args;
return encodeAbiParameters(
[{ type: "uint32" }, { type: "uint256" }],
[entityId, spendLimit]
);
},

encodeOnUninstallData: (args: { entityId: number }): Hex => {
const { entityId } = args;
return encodeAbiParameters([{ type: "uint32" }], [entityId]);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,26 @@ export const SingleSignerValidationModule = {
abi: singleSignerValidationModuleAbi,
encodeOnInstallData: (args: { entityId: number; signer: Address }): Hex => {
const { entityId, signer } = args;

return encodeAbiParameters(
[
{
type: "uint32",
value: entityId,
},
{
type: "address",
value: signer,
},
],
[entityId, signer]
);
},

encodeOnUninstallData: (args: { entityId: number }): Hex => {
const { entityId } = args;

return encodeAbiParameters(
[
{
type: "uint32",
value: entityId,
},
],
[entityId]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading