Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Oct 20, 2023
1 parent e466e6c commit a2c87cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
8 changes: 3 additions & 5 deletions contracts/libraries/SafeToL2Migration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {SafeStorage} from "../libraries/SafeStorage.sol";
import {Enum} from "../common/Enum.sol";

interface ISafe {
// solhint-disable-next-line
function VERSION() external view returns (string memory);
}

Expand Down Expand Up @@ -72,11 +73,8 @@ contract SafeToL2Migration is SafeStorage {
// Simulate a L2 transaction so indexer picks up the Safe
// 0xef2624ae - keccack("migrateToL2(address)")
bytes memory data = abi.encodeWithSelector(0xef2624ae, l2Singleton);
bytes memory additionalInfo;
{
// nonce, sender, threshold
additionalInfo = abi.encode(nonce, msg.sender, threshold);
}
// nonce, sender, threshold
bytes memory additionalInfo = abi.encode(nonce - 1, msg.sender, threshold);
emit SafeMultiSigTransaction(
MIGRATION_SINGLETON,
0,
Expand Down
31 changes: 28 additions & 3 deletions test/libraries/SafeToL2Migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ describe("SafeToL2Migration library", () => {
singleton130L2Address,
} = await setupTests();
const safeAddress = await safe130.getAddress();
expect(await safe130.nonce()).to.be.eq(0);

// Increase nonce by sending eth
await user1.sendTransaction({ to: safeAddress, value: ethers.parseEther("1") });
const nonce = 0;
const safeTx = buildSafeTransaction({ to: user1.address, value: ethers.parseEther("1"), nonce });
await executeTxWithSigners(safe130, safeTx, [user1]);

expect(await safe130.nonce()).to.be.eq(1);
await expect(
executeContractCallWithSigners(safe130, migration, "migrateToL2", [singleton130L2Address], [user1], true),
).to.be.revertedWith("GS013");
Expand All @@ -155,13 +157,36 @@ describe("SafeToL2Migration library", () => {
const safeAddress = await safe130.getAddress();
// The emit matcher checks the address, which is the Safe as delegatecall is used
const migrationSafe = migration.attach(safeAddress);

await expect(executeContractCallWithSigners(safe130, migration, "migrateToL2", [singleton130L2Address], [user1], true))
const migrationAddress = await migration.getAddress();

const functionName = "migrateToL2";
const expectedData = migration.interface.encodeFunctionData(functionName, [singleton130L2Address]);
const safeThreshold = await safe130.getThreshold();
const additionalInfo = hre.ethers.AbiCoder.defaultAbiCoder().encode(
["uint256", "address", "uint256"],
[0, user1.address, safeThreshold],
);
await expect(executeContractCallWithSigners(safe130, migration, functionName, [singleton130L2Address], [user1], true))
.to.emit(migrationSafe, "ChangedMasterCopy")
.withArgs(singleton130L2Address);
.withArgs(singleton130L2Address)
.to.emit(migrationSafe, "SafeMultiSigTransaction")
.withArgs(
migrationAddress,
0,
expectedData,
1,
0,
0,
0,
AddressZero,
AddressZero,
"0x", // We cannot detect signatures
additionalInfo,
);

const singletonResp = await user1.call({ to: safeAddress, data: migratedInterface.encodeFunctionData("masterCopy") });
expect(migratedInterface.decodeFunctionResult("masterCopy", singletonResp)[0]).to.eq(singleton130L2Address);
expect(await safe130.nonce()).to.be.eq(1);
});

it("doesn't touch important storage slots", async () => {
Expand Down

0 comments on commit a2c87cc

Please sign in to comment.