MultiSendCallOnly.spec.ts 8.16 KB
Newer Older
vicotor's avatar
vicotor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
import { expect } from "chai";
import hre, { deployments, waffle } from "hardhat";
import "@nomiclabs/hardhat-ethers";
import { deployContract, getMock, getMultiSendCallOnly, getSafeWithOwners } from "../utils/setup";
import { buildContractCall, buildSafeTransaction, executeTx, MetaTransaction, safeApproveHash } from "../../src/utils/execution";
import { buildMultiSendSafeTx } from "../../src/utils/multisend";
import { parseEther } from "@ethersproject/units";

describe("MultiSendCallOnly", async () => {

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

    const setupTests = deployments.createFixture(async ({ deployments }) => {
        await deployments.fixture();
        const setterSource = `
            contract StorageSetter {
                function setStorage(bytes3 data) public {
                    bytes32 slot = 0x4242424242424242424242424242424242424242424242424242424242424242;
                    // solhint-disable-next-line no-inline-assembly
                    assembly {
                        sstore(slot, data)
                    }
                }
            }`
        const storageSetter = await deployContract(user1, setterSource);
        return {
            safe: await getSafeWithOwners([user1.address]),
            multiSend: await getMultiSendCallOnly(),
            mock: await getMock(),
            storageSetter
        }
    })

    describe("multiSend", async () => {

        it('Should fail when using invalid operation', async () => {
            const { safe, multiSend } = await setupTests()

            const txs = [buildSafeTransaction({to: user2.address, operation: 2, nonce: 0})]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.revertedWith("GS013")
        })

        it('Should fail when using delegatecall operation', async () => {
            const { safe, multiSend } = await setupTests()

            const txs = [buildSafeTransaction({to: user2.address, operation: 1, nonce: 0})]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.revertedWith("GS013")
        })

        it('Can execute empty multisend', async () => {
            const { safe, multiSend } = await setupTests()

            const txs: MetaTransaction[] = []
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.emit(safe, "ExecutionSuccess")
        })

        it('Can execute single ether transfer', async () => {
            const { safe, multiSend } = await setupTests()
            await user1.sendTransaction({to: safe.address, value: parseEther("1")})
            const userBalance = await hre.ethers.provider.getBalance(user2.address)
            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1"))

            const txs: MetaTransaction[] = [buildSafeTransaction({to: user2.address, value: parseEther("1"), nonce: 0})]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.emit(safe, "ExecutionSuccess")

            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("0"))
            await expect(await hre.ethers.provider.getBalance(user2.address)).to.be.deep.eq(userBalance.add(parseEther("1")))
        })

        it('reverts all tx if any fails', async () => {
            const { safe, multiSend } = await setupTests()
            await user1.sendTransaction({to: safe.address, value: parseEther("1")})
            const userBalance = await hre.ethers.provider.getBalance(user2.address)
            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1"))

            const txs: MetaTransaction[] = [
                buildSafeTransaction({to: user2.address, value: parseEther("1"), nonce: 0}),
                buildSafeTransaction({to: user2.address, value: parseEther("1"), nonce: 0}),
            ]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce(), { safeTxGas: 1 })
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.emit(safe, "ExecutionFailure")

            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1"))
            await expect(await hre.ethers.provider.getBalance(user2.address)).to.be.deep.eq(userBalance)
        })

        it('can be used when ETH is sent with execution', async () => {
            const { safe, multiSend, storageSetter } = await setupTests()

            const txs: MetaTransaction[] = [
                buildContractCall(storageSetter, "setStorage", ["0xbaddad"], 0)
            ]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            
            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("0"))

            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ], { value: parseEther("1") })
            ).to.emit(safe, "ExecutionSuccess")

            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1"))
        })

        it('can execute contract calls', async () => {
            const { safe, multiSend, storageSetter } = await setupTests()

            const txs: MetaTransaction[] = [
                buildContractCall(storageSetter, "setStorage", ["0xbaddad"], 0)
            ]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.emit(safe, "ExecutionSuccess")

            await expect(
                await hre.ethers.provider.getStorageAt(safe.address, "0x4242424242424242424242424242424242424242424242424242424242424242")
            ).to.be.eq("0x" + "".padEnd(64, "0"))
            await expect(
                await hre.ethers.provider.getStorageAt(storageSetter.address, "0x4242424242424242424242424242424242424242424242424242424242424242")
            ).to.be.eq("0x" + "baddad".padEnd(64, "0"))
        })

        it('can execute combinations', async () => {
            const { safe, multiSend, storageSetter } = await setupTests()
            await user1.sendTransaction({to: safe.address, value: parseEther("1")})
            const userBalance = await hre.ethers.provider.getBalance(user2.address)
            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1"))

            const txs: MetaTransaction[] = [
                buildSafeTransaction({to: user2.address, value: parseEther("1"), nonce: 0}),
                buildContractCall(storageSetter, "setStorage", ["0xbaddad"], 0)
            ]
            const safeTx = buildMultiSendSafeTx(multiSend, txs, await safe.nonce())
            await expect(
                executeTx(safe, safeTx, [ await safeApproveHash(user1, safe, safeTx, true) ])
            ).to.emit(safe, "ExecutionSuccess")
            
            await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("0"))
            await expect(await hre.ethers.provider.getBalance(user2.address)).to.be.deep.eq(userBalance.add(parseEther("1")))
            await expect(
                await hre.ethers.provider.getStorageAt(safe.address, "0x4242424242424242424242424242424242424242424242424242424242424242")
            ).to.be.eq("0x" + "".padEnd(64, "0"))
            await expect(
                await hre.ethers.provider.getStorageAt(storageSetter.address, "0x4242424242424242424242424242424242424242424242424242424242424242")
            ).to.be.eq("0x" + "baddad".padEnd(64, "0"))
        })
    })
})