Skip to content

Commit

Permalink
[#4] Create builder.ts, dataTypes.ts, and use builder functions to cr…
Browse files Browse the repository at this point in the history
…eate tx variables
  • Loading branch information
akshay-ap committed Jul 3, 2023
1 parent 0b78794 commit d49a836
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
112 changes: 33 additions & 79 deletions test/SafeProtocolMediator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/moduleTx";

describe("SafeProtocolMediator", async () => {
let deployer: SignerWithAddress, user1: SignerWithAddress;
Expand Down Expand Up @@ -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",
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions test/utils/builder.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
19 changes: 19 additions & 0 deletions test/utils/dataTypes.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit d49a836

Please sign in to comment.