forked from BitGo/eth-multisig-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforwarderFactory.js
271 lines (238 loc) · 6.85 KB
/
forwarderFactory.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require('should');
const truffleAssert = require('truffle-assertions');
const helpers = require('./helpers');
const util = require('ethereumjs-util');
const abi = require('ethereumjs-abi');
const BigNumber = require('bignumber.js');
const Forwarder = artifacts.require('./Forwarder.sol');
const ForwarderFactory = artifacts.require('./ForwarderFactory.sol');
const hre = require('hardhat');
const createForwarderFactory = async () => {
const forwarderContract = await Forwarder.new([], {});
const forwarderFactory = await ForwarderFactory.new(
forwarderContract.address
);
return {
implementationAddress: forwarderContract.address,
factory: forwarderFactory
};
};
const getBalanceInWei = async (address) => {
return new BigNumber(await web3.eth.getBalance(address));
};
const createForwarder = async (
factory,
implementationAddress,
parent,
salt,
shouldAutoFlushERC721 = true,
shouldAutoFlushERC1155 = true,
sender
) => {
const inputSalt = util.setLengthLeft(
Buffer.from(util.stripHexPrefix(salt), 'hex'),
32
);
const calculationSalt = abi.soliditySHA3(
['address', 'bytes32'],
[parent, inputSalt]
);
const initCode = helpers.getInitCode(
util.stripHexPrefix(implementationAddress)
);
const forwarderAddress = helpers.getNextContractAddressCreate2(
factory.address,
calculationSalt,
initCode
);
await factory.createForwarder(
parent,
inputSalt,
shouldAutoFlushERC721,
shouldAutoFlushERC1155,
{
from: sender
}
);
return forwarderAddress;
};
describe('ForwarderFactory', function () {
let accounts;
before(async () => {
await hre.network.provider.send('hardhat_reset');
accounts = await web3.eth.getAccounts();
});
it('Should create a functional forwarder using the factory', async function () {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
);
const startBalance = await getBalanceInWei(parent);
const startForwarderBalance = await getBalanceInWei(forwarderAddress);
const amount = web3.utils.toWei('2', 'ether');
await web3.eth.sendTransaction({
from: accounts[1],
to: forwarderAddress,
value: amount
});
const endBalance = await getBalanceInWei(parent);
startBalance.plus(amount).eq(endBalance).should.be.true();
const endForwarderBalance = await getBalanceInWei(forwarderAddress);
endForwarderBalance.eq(startForwarderBalance).should.be.true();
});
it('Different salt should create at different addresses', async function () {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
);
const salt2 = '0x12345678';
const forwarderAddress2 = await createForwarder(
factory,
implementationAddress,
parent,
salt2,
undefined,
undefined,
accounts[1]
);
forwarderAddress.should.not.equal(forwarderAddress2);
});
it('Different creators should create at different addresses', async function () {
const { factory, implementationAddress } = await createForwarderFactory();
const { factory: factory2, implementationAddress: implementationAddress2 } =
await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
);
const forwarderAddress2 = await createForwarder(
factory2,
implementationAddress2,
parent,
salt,
undefined,
undefined,
accounts[1]
);
forwarderAddress.should.not.equal(forwarderAddress2);
});
it('Different parents should create at different addresses', async function () {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
);
const parent2 = accounts[1];
const forwarderAddress2 = await createForwarder(
factory,
implementationAddress,
parent2,
salt,
undefined,
undefined,
accounts[1]
);
forwarderAddress.should.not.equal(forwarderAddress2);
});
[
[true, 'true'],
[false, 'false']
].map(([shouldAutoFlush, label]) => {
it(`should assign the create a forwarder with ${label} autoflush721 params`, async () => {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
shouldAutoFlush,
undefined,
accounts[1]
);
const forwarderContract = await hre.ethers.getContractAt(
'Forwarder',
forwarderAddress
);
const autoFlush721 = await forwarderContract.autoFlush721();
autoFlush721.should.equal(shouldAutoFlush);
});
it(`should assign the create a forwarder with ${label} autoflush1155 params`, async () => {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
shouldAutoFlush,
accounts[1]
);
const forwarderContract = await hre.ethers.getContractAt(
'Forwarder',
forwarderAddress
);
const autoFlush1155 = await forwarderContract.autoFlush1155();
autoFlush1155.should.equal(shouldAutoFlush);
});
});
it('Should fail to create two contracts with the same inputs', async function () {
const { factory, implementationAddress } = await createForwarderFactory();
const parent = accounts[0];
const salt = '0x1234';
const forwarderAddress = await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
);
await helpers.assertVMException(
async () =>
await createForwarder(
factory,
implementationAddress,
parent,
salt,
undefined,
undefined,
accounts[1]
)
);
});
});