Skip to content

Commit

Permalink
Closes #265: Added test for migration from 1.2.0 to 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeissner committed Mar 24, 2021
1 parent c7ff4ec commit 5f2b454
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/core/GnosisSafe.Execution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("GnosisSafe", async () => {
}
})

describe("execTransactions", async () => {
describe("execTransaction", async () => {

it('should revert if too little gas is provided', async () => {
const { safe } = await setupTests()
Expand Down
44 changes: 44 additions & 0 deletions test/migration/UpgradeFromSafe120.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect } from "chai";
import hre, { ethers, deployments, waffle } from "hardhat";
import "@nomiclabs/hardhat-ethers";
import { AddressZero } from "@ethersproject/constants";
import { getSafeWithOwners, getSafeSingleton, migrationContract, getFactory, getMock } from "../utils/setup";
import { buildSafeTransaction, executeTx, executeTxWithSigners, safeApproveHash } from "../utils/execution";
import { verificationTests } from "./subTests.spec";
import deploymentData from "./safeDeployment.json";
import { calculateProxyAddress } from "../utils/proxies";

describe("Upgrade from Safe 1.2.0", () => {

const [user1] = waffle.provider.getWallets();

const ChangeMasterCopyInterface = new ethers.utils.Interface(["function changeMasterCopy(address target)"])

// We migrate the Safe and run the verification tests
const setupTests = deployments.createFixture(async ({ deployments }) => {
await deployments.fixture();
const singleton120 = (await (await user1.sendTransaction({ data: deploymentData.safe120 })).wait()).contractAddress
const singleton130 = (await getSafeSingleton()).address
const factory = await getFactory()
const saltNonce = 42
const proxyAddress = await calculateProxyAddress(factory, singleton120, "0x", saltNonce)
await factory.createProxyWithNonce(singleton120, "0x", saltNonce).then((tx: any) => tx.wait())

const Safe = await hre.ethers.getContractFactory("GnosisSafe");
const safe = Safe.attach(proxyAddress)
await safe.setup([user1.address], 1, AddressZero, "0x", AddressZero, AddressZero, 0, AddressZero)

expect(await safe.VERSION()).to.be.eq("1.2.0")
const nonce = await safe.callStatic.nonce()
const data = ChangeMasterCopyInterface.encodeFunctionData("changeMasterCopy", [singleton130])
const tx = buildSafeTransaction({ to: safe.address, data, nonce })
await executeTx(safe, tx, [await safeApproveHash(user1, safe, tx, true)])
expect(await safe.VERSION()).to.be.eq("1.3.0")

return {
migratedSafe: safe,
mock: await getMock()
}
})
verificationTests(setupTests)
})
3 changes: 3 additions & 0 deletions test/migration/safeDeployment.json

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/migration/subTests.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Contract } from "@ethersproject/contracts"
import { parseEther } from "@ethersproject/units"
import { expect } from "chai";
import hre, { ethers, waffle } from "hardhat";
import { buildSafeTransaction, executeTx, executeTxWithSigners } from "../utils/execution"

interface TestSetup {
migratedSafe: Contract,
mock: Contract
}

export const verificationTests = (setupTests: () => Promise<TestSetup>) => {

const [user1, user2] = waffle.provider.getWallets();

describe("execTransaction", async () => {
it('should be able to transfer ETH', async () => {
const { migratedSafe } = await setupTests()
await user1.sendTransaction({ to: migratedSafe.address, value: parseEther("1") })
const nonce = await migratedSafe.nonce()
const tx = buildSafeTransaction({ to: user2.address, value: parseEther("1"), nonce })

const userBalance = await ethers.provider.getBalance(user2.address)
await expect(await ethers.provider.getBalance(migratedSafe.address)).to.be.deep.eq(parseEther("1"))

await executeTxWithSigners(migratedSafe, tx, [user1])

await expect(await ethers.provider.getBalance(user2.address)).to.be.deep.eq(userBalance.add(parseEther("1")))
await expect(await ethers.provider.getBalance(migratedSafe.address)).to.be.deep.eq(parseEther("0"))
})
})
}
8 changes: 6 additions & 2 deletions test/utils/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ export const buildContractCall = (contract: Contract, method: string, params: an
}, overrides))
}

export const executeTxWithSigners = async (safe: Contract, tx: SafeTransaction, signers: Wallet[], overrides?: any) => {
const sigs = await Promise.all(signers.map((signer) => safeSignTypedData(signer, safe, tx)))
return executeTx(safe, tx, sigs, overrides)
}

export const executeContractCallWithSigners = async (safe: Contract, contract: Contract, method: string, params: any[], signers: Wallet[], delegateCall?: boolean, overrides?: Partial<SafeTransaction>) => {
const tx = buildContractCall(contract, method, params, await safe.nonce(), delegateCall, overrides)
const sigs = await Promise.all(signers.map((signer) => safeSignTypedData(signer, safe, tx)))
return executeTx(safe, tx, sigs)
return executeTxWithSigners(safe, tx, signers)
}

export const buildSafeTransaction = (template: {
Expand Down

0 comments on commit 5f2b454

Please sign in to comment.