Skip to content

Commit eff756d

Browse files
authored
Add setup event (#279)
1 parent 4831c5f commit eff756d

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import "@nomiclabs/hardhat-ethers";
2+
import { setupBenchmarkContracts } from "./utils/setup"
3+
4+
const contractSetup = setupBenchmarkContracts(undefined, true)
5+
describe("GnosisSafe", async () => {
6+
it("creation", async () => {
7+
await contractSetup()
8+
})
9+
})

benchmark/utils/setup.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export interface Contracts {
1313
additions: any | undefined
1414
}
1515

16-
const generateTarget = async (owners: Wallet[], threshold: number, guardAddress: string) => {
16+
const generateTarget = async (owners: Wallet[], threshold: number, guardAddress: string, logGasUsage?: boolean) => {
1717
const fallbackHandler = await getDefaultCallbackHandler()
18-
const safe = await getSafeWithOwners(owners.map((owner) => owner.address), threshold, fallbackHandler.address)
18+
const safe = await getSafeWithOwners(owners.map((owner) => owner.address), threshold, fallbackHandler.address, logGasUsage)
1919
await executeContractCallWithSigners(safe, safe, "setGuard", [guardAddress], owners)
2020
return safe
2121
}
@@ -28,14 +28,14 @@ export const configs = [
2828
{ name: "3 out of 5", signers: [user1, user2, user3, user4, user5], threshold: 3 },
2929
]
3030

31-
const setupBenchmarkContracts = async (benchmarkFixture?: () => Promise<any>) => {
32-
return await deployments.createFixture(async ({ deployments }) => {
31+
export const setupBenchmarkContracts = (benchmarkFixture?: () => Promise<any>, logGasUsage?: boolean) => {
32+
return deployments.createFixture(async ({ deployments }) => {
3333
await deployments.fixture();
3434
const guardFactory = await hre.ethers.getContractFactory("DelegateCallTransactionGuard");
3535
const guard = await guardFactory.deploy(AddressZero)
3636
const targets = []
3737
for (const config of configs) {
38-
targets.push(await generateTarget(config.signers, config.threshold, config.useGuard ? guard.address : AddressZero))
38+
targets.push(await generateTarget(config.signers, config.threshold, config.useGuard ? guard.address : AddressZero, logGasUsage))
3939
}
4040
return {
4141
targets,
@@ -54,7 +54,7 @@ export interface Benchmark {
5454
export const benchmark = async (topic: string, benchmarks: Benchmark[]) => {
5555
for (const benchmark of benchmarks) {
5656
const { name, prepare, after, fixture } = benchmark
57-
const contractSetup = await setupBenchmarkContracts(fixture)
57+
const contractSetup = setupBenchmarkContracts(fixture)
5858
describe(`${topic} - ${name}`, async () => {
5959
it("with an EOA", async () => {
6060
const contracts = await contractSetup()

contracts/GnosisSafe.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ contract GnosisSafe
3838
//);
3939
bytes32 private constant SAFE_MSG_TYPEHASH = 0x60b3cbf8b4a223d68d641b3b6ddf9a298e7f33710cf3d3a9d1146b5a6150fbca;
4040

41+
event SafeSetup(
42+
address indexed initiator,
43+
address[] owners,
44+
uint256 threshold,
45+
address initializer,
46+
address fallbackHandler
47+
);
4148
event ApproveHash(
4249
bytes32 indexed approvedHash,
4350
address indexed owner
@@ -99,6 +106,7 @@ contract GnosisSafe
99106
// baseGas = 0, gasPrice = 1 and gas = payment => amount = (payment + 0) * 1 = payment
100107
handlePayment(payment, 0, 1, paymentToken, paymentReceiver);
101108
}
109+
emit SafeSetup(msg.sender, _owners, _threshold, to, fallbackHandler);
102110
}
103111

104112
/// @dev Allows to execute a Safe transaction confirmed by required number of owners and then pays the account that submitted the transaction.

test/core/GnosisSafe.Execution.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ describe("GnosisSafe", async () => {
120120
executeTx(safe, tx, [await safeApproveHash(user1, safe, tx, true)]).then((tx) => { executedTx = tx; return tx })
121121
).to.emit(safe, "ExecutionSuccess")
122122
const receipt = await hre.ethers.provider.getTransactionReceipt(executedTx!!.hash)
123-
console.log(receipt.logs)
124123
const successEvent = safe.interface.decodeEventLog("ExecutionSuccess", receipt.logs[0].data, receipt.logs[0].topics)
125124
expect(successEvent.txHash).to.be.eq(calculateSafeTransactionHash(safe, tx, await chainId()))
126125
// Gas costs are around 3000, so even if we specified a safeTxGas from 100000 we should not use more

test/core/GnosisSafe.Setup.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("GnosisSafe", async () => {
2222
}
2323
})
2424

25-
describe("Setup", async () => {
25+
describe("setup", async () => {
2626
it('should not allow to call setup on singleton', async () => {
2727
await deployments.fixture();
2828
const singleton = await getSafeSingleton()
@@ -45,7 +45,9 @@ describe("GnosisSafe", async () => {
4545

4646
it('should set domain hash', async () => {
4747
const { template } = await setupTests()
48-
await template.setup([user1.address, user2.address, user3.address], 2, AddressZero, "0x", AddressZero, AddressZero, 0, AddressZero)
48+
await expect(
49+
template.setup([user1.address, user2.address, user3.address], 2, AddressZero, "0x", AddressZero, AddressZero, 0, AddressZero)
50+
).to.emit(template, "SafeSetup").withArgs(user1.address, [user1.address, user2.address, user3.address], 2, AddressZero, AddressZero)
4951
await expect(await template.domainSeparator()).to.be.eq(calculateSafeDomainSeparator(template, await chainId()))
5052
await expect(await template.getOwners()).to.be.deep.eq([user1.address, user2.address, user3.address])
5153
await expect(await template.getThreshold()).to.be.deep.eq(BigNumber.from(2))
@@ -129,7 +131,9 @@ describe("GnosisSafe", async () => {
129131
}`
130132
const testIntializer = await deployContract(user1, source);
131133
const initData = testIntializer.interface.encodeFunctionData("init", ["0x42baddad"])
132-
await template.setup([user1.address, user2.address, user3.address], 2, testIntializer.address, initData, AddressOne, AddressZero, 0, AddressZero)
134+
await expect(
135+
template.setup([user1.address, user2.address, user3.address], 2, testIntializer.address, initData, AddressOne, AddressZero, 0, AddressZero)
136+
).to.emit(template, "SafeSetup").withArgs(user1.address, [user1.address, user2.address, user3.address], 2, testIntializer.address, AddressOne)
133137
await expect(await template.domainSeparator()).to.be.eq(calculateSafeDomainSeparator(template, await chainId()))
134138
await expect(await template.getOwners()).to.be.deep.eq([user1.address, user2.address, user3.address])
135139
await expect(await template.getThreshold()).to.be.deep.eq(BigNumber.from(2))

test/utils/execution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ export const buildSignatureBytes = (signatures: SafeSignature[]): string => {
111111
return signatureBytes
112112
}
113113

114-
export const logGas = async (message: string, tx: Promise<any>): Promise<any> => {
114+
export const logGas = async (message: string, tx: Promise<any>, skip?: boolean): Promise<any> => {
115115
return tx.then(async (result) => {
116116
const receipt = await result.wait()
117-
console.log(" Used", receipt.gasUsed.toNumber(), `gas for >${message}<`)
117+
if (!skip) console.log(" Used", receipt.gasUsed.toNumber(), `gas for >${message}<`)
118118
return result
119119
})
120120
}

test/utils/setup.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import hre, { deployments } from "hardhat"
22
import { Wallet, Contract } from "ethers"
33
import { AddressZero } from "@ethersproject/constants";
44
import solc from "solc"
5+
import { logGas } from "./execution";
56

67
export const defaultCallbackHandlerDeployment = async () => {
78
return await deployments.get("DefaultCallbackHandler");
@@ -68,9 +69,13 @@ export const getSafeTemplate = async () => {
6869
return Safe.attach(template);
6970
}
7071

71-
export const getSafeWithOwners = async (owners: string[], threshold?: number, fallbackHandler?: string) => {
72+
export const getSafeWithOwners = async (owners: string[], threshold?: number, fallbackHandler?: string, logGasUsage?: boolean) => {
7273
const template = await getSafeTemplate()
73-
await template.setup(owners, threshold || owners.length, AddressZero, "0x", fallbackHandler || AddressZero, AddressZero, 0, AddressZero)
74+
await logGas(
75+
`Setup Safe with ${owners.length} owner(s)${fallbackHandler && fallbackHandler !== AddressZero ? " and fallback handler" : ""}`,
76+
template.setup(owners, threshold || owners.length, AddressZero, "0x", fallbackHandler || AddressZero, AddressZero, 0, AddressZero),
77+
!logGasUsage
78+
)
7479
return template
7580
}
7681

0 commit comments

Comments
 (0)