diff --git a/test/SafeProtocolMediator.spec.ts b/test/SafeProtocolMediator.spec.ts index a52e75ad..5cf57239 100644 --- a/test/SafeProtocolMediator.spec.ts +++ b/test/SafeProtocolMediator.spec.ts @@ -4,6 +4,7 @@ import { expect } from "chai"; import { ZeroAddress } from "ethers"; import { SENTINEL_MODULES } from "./utils/constants"; import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers"; +import { buildRootTx, buildSingleTx } from "./utils/builder"; describe("SafeProtocolMediator", async () => { let deployer: SignerWithAddress, user1: SignerWithAddress; @@ -265,19 +266,7 @@ describe("SafeProtocolMediator", async () => { it("Should not allow non-enabled module to execute tx from a safe", async () => { const { safeProtocolMediator, safe } = await loadFixture(deployContractsFixture); const module = await (await hre.ethers.getContractFactory("TestModule")).deploy(); - // TODO: Replace with builder function - const safeTx = { - actions: [ - { - to: user1.address, - value: hre.ethers.parseEther("1"), - data: "0x", - }, - ], - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; - + const safeTx = buildSingleTx(user1.address, hre.ethers.parseEther("1"), "0x", BigInt(1), hre.ethers.randomBytes(32)); await expect(module.executeFromModule(safeProtocolMediator, safe, safeTx)).to.be.revertedWithCustomError( safeProtocolMediator, "MoudleNotEnabled", @@ -299,18 +288,7 @@ describe("SafeProtocolMediator", async () => { value: amount, }) ).wait(); - // TODO: Replace with builder function - const safeTx = { - actions: [ - { - to: user1.address, - value: hre.ethers.parseEther("1"), - data: "0x", - }, - ], - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; + const safeTx = buildSingleTx(user1.address, hre.ethers.parseEther("1"), "0x", BigInt(1), hre.ethers.randomBytes(32)); const balanceBefore = await hre.ethers.provider.getBalance(user1.address); const tx = await module.executeFromModule(safeProtocolMediator, safe, safeTx); @@ -371,16 +349,14 @@ describe("SafeProtocolMediator", async () => { value: amount, }) ).wait(); - // TODO: Replace with builder function - const safeTx = { - action: { - to: await testFallbackReceiver.getAddress(), - value: hre.ethers.parseEther("1"), - data: "0x", - }, - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; + + const safeTx = buildRootTx( + await testFallbackReceiver.getAddress(), + hre.ethers.parseEther("1"), + "0x", + BigInt(1), + hre.ethers.randomBytes(32), + ); const balanceBefore = await hre.ethers.provider.getBalance(user1.address); const tx = await module.executeFromModule(safeProtocolMediator, safe, safeTx); @@ -396,17 +372,7 @@ describe("SafeProtocolMediator", async () => { it("Should not allow non-enabled module to execute root tx from a safe", async () => { const { safeProtocolMediator, safe } = await loadFixture(deployContractsFixture); const module = await (await hre.ethers.getContractFactory("TestModuleWithRootAccess")).deploy(); - // TODO: Replace with builder function - const safeTx = { - action: { - to: user1.address, - value: hre.ethers.parseEther("1"), - data: "0x", - }, - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; - + const safeTx = buildRootTx(user1.address, hre.ethers.parseEther("1"), "0x", BigInt(1), hre.ethers.randomBytes(32)); await expect(module.executeFromModule(safeProtocolMediator, safe, safeTx)).to.be.revertedWithCustomError( safeProtocolMediator, "MoudleNotEnabled", @@ -424,17 +390,13 @@ describe("SafeProtocolMediator", async () => { await safe.exec(await safeProtocolMediator.getAddress(), 0, data); await module.setRequiresRootAccess(false); - - // TODO: Replace with builder function - const safeTx = { - action: { - to: await testFallbackReceiver.getAddress(), - value: hre.ethers.parseEther("1"), - data: "0x", - }, - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; + const safeTx = buildRootTx( + await testFallbackReceiver.getAddress(), + hre.ethers.parseEther("1"), + "0x", + BigInt(1), + hre.ethers.randomBytes(32), + ); await expect(module.executeFromModule(safeProtocolMediator, safe, safeTx)).to.be.revertedWithCustomError( safeProtocolMediator, @@ -452,17 +414,13 @@ describe("SafeProtocolMediator", async () => { const data = safeProtocolMediator.interface.encodeFunctionData("enableModule", [await module.getAddress(), true]); await safe.exec(await safeProtocolMediator.getAddress(), 0, data); - // TODO: Replace with builder function - const safeTx = { - action: { - to: await testFallbackReceiver.getAddress(), - value: hre.ethers.parseEther("1"), - data: "0x", - }, - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; - + const safeTx = buildRootTx( + await testFallbackReceiver.getAddress(), + hre.ethers.parseEther("1"), + "0x", + BigInt(1), + hre.ethers.randomBytes(32), + ); await expect(module.executeFromModule(safeProtocolMediator, safe, safeTx)).to.be.revertedWithCustomError( safeProtocolMediator, "RootAccessActionExecutionFailed", @@ -485,17 +443,13 @@ describe("SafeProtocolMediator", async () => { // Set root access flag back to true await module.setRequiresRootAccess(true); - // TODO: Replace with builder function - const safeTx = { - action: { - to: await testFallbackReceiver.getAddress(), - value: hre.ethers.parseEther("1"), - data: "0x", - }, - nonce: 1, - metaHash: hre.ethers.randomBytes(32), - }; - + const safeTx = buildRootTx( + await testFallbackReceiver.getAddress(), + hre.ethers.parseEther("1"), + "0x", + BigInt(1), + hre.ethers.randomBytes(32), + ); await expect(module.executeFromModule(safeProtocolMediator, safe, safeTx)) .to.be.revertedWithCustomError(safeProtocolMediator, "ModuleRequiresRootAccess") .withArgs(moduleAddress); diff --git a/test/utils/builder.ts b/test/utils/builder.ts new file mode 100644 index 00000000..8f3fb8e6 --- /dev/null +++ b/test/utils/builder.ts @@ -0,0 +1,27 @@ +import { AddressLike } from "ethers"; +import { SafeRootAccess, SafeTransaction } from "./dataTypes"; +export const buildSingleTx = (address: AddressLike, value: bigint, data: string, nonce: bigint, metaHash: Uint8Array): SafeTransaction => { + return { + actions: [ + { + to: address, + value: value, + data: data, + }, + ], + nonce: nonce, + metaHash: metaHash, + }; +}; + +export const buildRootTx = (address: AddressLike, value: bigint, data: string, nonce: bigint, metaHash: Uint8Array): SafeRootAccess => { + return { + action: { + to: address, + value: value, + data: data, + }, + nonce: nonce, + metaHash: metaHash, + }; +}; diff --git a/test/utils/dataTypes.ts b/test/utils/dataTypes.ts new file mode 100644 index 00000000..b4ffb127 --- /dev/null +++ b/test/utils/dataTypes.ts @@ -0,0 +1,19 @@ +import { AddressLike } from "ethers"; + +export interface SafeProtocolAction { + to: AddressLike; + value: bigint; + data: string; +} + +export interface SafeTransaction { + actions: SafeProtocolAction[]; + nonce: bigint; + metaHash: Uint8Array; +} + +export interface SafeRootAccess { + action: SafeProtocolAction; + nonce: bigint; + metaHash: Uint8Array; +}