Skip to content

Commit

Permalink
[#4] Add test delegate contract, add test case for root access action
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-ap committed Jun 26, 2023
1 parent 7c5a350 commit 97fea95
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion contracts/SafeProtocolMediator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract SafeProtocolMediator is ISafeProtocolMediator, Ownable2Step {

// Events
event ActionsExecuted(address safe, bytes32 metaHash);
event RootAccessActionsExecuted(address safe, bytes32 metaHash);
event RootAccessActionExecuted(address safe, bytes32 metaHash);
event ModuleEnabled(address safe, address module, bool allowRootAccess);
event ModuleDisabled(address safe, address module);
event ActionExecutionFailed(address safe, bytes32 metaHash, uint256 index);
Expand Down Expand Up @@ -119,6 +119,9 @@ contract SafeProtocolMediator is ISafeProtocolMediator, Ownable2Step {

data = "";
success = safe.execTransactionFromModule(safeProtocolAction.to, safeProtocolAction.value, safeProtocolAction.data, 1);
if (success) {
emit RootAccessActionExecuted(address(safe), rootAccess.metaHash);
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions contracts/test/TestDelegateCallReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.18;

contract TestDelegateCallReceiver {
address immutable ethReceiver;

Check warning on line 5 in contracts/test/TestDelegateCallReceiver.sol

View workflow job for this annotation

GitHub Actions / lint

Explicitly mark visibility of state

Check warning on line 5 in contracts/test/TestDelegateCallReceiver.sol

View workflow job for this annotation

GitHub Actions / lint

Explicitly mark visibility of state

constructor(address _ethReceiver) {
ethReceiver = _ethReceiver;
}

receive() external payable {
(bool success, bytes memory data) = ethReceiver.call{value: address(this).balance}("");

Check warning on line 12 in contracts/test/TestDelegateCallReceiver.sol

View workflow job for this annotation

GitHub Actions / lint

Variable "data" is unused

Check warning on line 12 in contracts/test/TestDelegateCallReceiver.sol

View workflow job for this annotation

GitHub Actions / lint

Variable "data" is unused
if (!success) {
revert("Failed to send eth");
}
}
}
41 changes: 41 additions & 0 deletions test/SafeProtocolMediator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,46 @@ describe("SafeProtocolMediator", async () => {
"ModuleRequiresRootAccess",
);
});

it("Should run a transaction from root access enabled module", async () => {
const { safeProtocolMediator, safe } = await loadFixture(deployContractsFixture);

const testDelegateCallReceiver = await (await hre.ethers.getContractFactory("TestDelegateCallReceiver")).deploy(user2.address);

// Enable module
const module = await (await hre.ethers.getContractFactory("TestModuleWithRootAccess")).deploy();
const data = safeProtocolMediator.interface.encodeFunctionData("enableModule", [await module.getAddress(), true]);
await safe.exec(await safeProtocolMediator.getAddress(), 0, data);

const amount = hre.ethers.parseEther("1");
await (
await deployer.sendTransaction({
to: await safe.getAddress(),
value: amount,
})
).wait();
// TODO: Replace with builder function
const safeTx = {
action: {
to: await testDelegateCallReceiver.getAddress(),
value: hre.ethers.parseEther("1"),
data: "0x",
},
nonce: 1,
metaHash: hre.ethers.randomBytes(32),
};

const balanceBefore = (await hre.ethers.provider.getBalance(user2.address)).toString();
const tx = await module.executeFromModule(safeProtocolMediator, safe, safeTx);
await tx.wait();
const balanceAfter = (await hre.ethers.provider.getBalance(user2.address)).toString();

expect(BigNumber(balanceAfter)).to.eql(BigNumber(balanceBefore).plus(amount.toString()));
expect((await hre.ethers.provider.getBalance(await safe.getAddress())).toString()).to.eql("0");

await expect(tx)
.to.emit(safeProtocolMediator, "RootAccessActionExecuted")
.withArgs(await safe.getAddress(), safeTx.metaHash);
});
});
});

0 comments on commit 97fea95

Please sign in to comment.