forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBridgeERC1155.behavior.js
More file actions
333 lines (298 loc) · 12.4 KB
/
BridgeERC1155.behavior.js
File metadata and controls
333 lines (298 loc) · 12.4 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
const ids = [17n, 42n];
const values = [100n, 320n];
function shouldBehaveLikeBridgeERC1155({ chainAIsCustodial = false, chainBIsCustodial = false } = {}) {
describe('bridge ERC1155 like', function () {
beforeEach(function () {
// helper
this.encodePayload = (from, to, ids, values) =>
ethers.AbiCoder.defaultAbiCoder().encode(
['bytes', 'bytes', 'uint256[]', 'uint256[]'],
[this.chain.toErc7930(from), to.target ?? to.address ?? to, ids, values],
);
});
it('bridge setup', async function () {
await expect(this.bridgeA.getLink(this.chain.erc7930)).to.eventually.deep.equal([
this.gateway.target,
this.chain.toErc7930(this.bridgeB),
]);
await expect(this.bridgeB.getLink(this.chain.erc7930)).to.eventually.deep.equal([
this.gateway.target,
this.chain.toErc7930(this.bridgeA),
]);
});
describe('crosschain send (both direction)', async function () {
it('single', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// Alice sends tokens from chain A to Bruce on chain B.
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256,uint256)')(
alice,
this.chain.toErc7930(bruce),
ids[0],
values[0],
),
)
// bridge on chain A takes custody of the token
.to.emit(this.tokenA, 'TransferSingle')
.withArgs(
chainAIsCustodial ? this.bridgeA : alice,
alice,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
ids[0],
values[0],
)
// crosschain transfer sent
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.chain.toErc7930(bruce), ids.slice(0, 1), values.slice(0, 1))
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.chain.toErc7930(alice), bruce, ids.slice(0, 1), values.slice(0, 1))
// tokens are minted on chain B
.to.emit(this.tokenB, 'TransferSingle')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
bruce,
ids[0],
values[0],
);
// Bruce sends tokens from chain B to Chris on chain A.
await expect(
this.bridgeB.connect(bruce).getFunction('crosschainTransferFrom(address,bytes,uint256,uint256)')(
bruce,
this.chain.toErc7930(chris),
ids[0],
values[0],
),
)
// tokens are burned on chain B
.to.emit(this.tokenB, 'TransferSingle')
.withArgs(
chainBIsCustodial ? this.bridgeB : bruce,
bruce,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids[0],
values[0],
)
// crosschain transfer sent
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, bruce, this.chain.toErc7930(chris), ids.slice(0, 1), values.slice(0, 1))
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.chain.toErc7930(bruce), chris, ids.slice(0, 1), values.slice(0, 1))
// bridge on chain A releases custody of the token
.to.emit(this.tokenA, 'TransferSingle')
.withArgs(
chainAIsCustodial ? this.bridgeA : this.gateway,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
chris,
ids[0],
values[0],
);
});
it('batch', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// Alice sends tokens from chain A to Bruce on chain B.
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.chain.toErc7930(bruce),
ids,
values,
),
)
// bridge on chain A takes custody of the token
.to.emit(this.tokenA, 'TransferBatch')
.withArgs(
chainAIsCustodial ? this.bridgeA : alice,
alice,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
ids,
values,
)
// crosschain transfer sent
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.chain.toErc7930(bruce), ids, values)
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.chain.toErc7930(alice), bruce, ids, values)
// tokens are minted on chain B
.to.emit(this.tokenB, 'TransferBatch')
.withArgs(
chainBIsCustodial ? this.bridgeB : this.gateway,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
bruce,
ids,
values,
);
// Bruce sends tokens from chain B to Chris on chain A.
await expect(
this.bridgeB.connect(bruce).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
bruce,
this.chain.toErc7930(chris),
ids,
values,
),
)
// tokens are burned on chain B
.to.emit(this.tokenB, 'TransferBatch')
.withArgs(
chainBIsCustodial ? this.bridgeB : bruce,
bruce,
chainBIsCustodial ? this.bridgeB : ethers.ZeroAddress,
ids,
values,
)
// crosschain transfer sent
.to.emit(this.bridgeB, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, bruce, this.chain.toErc7930(chris), ids, values)
// ERC-7786 event
.to.emit(this.gateway, 'MessageSent')
// crosschain transfer received
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferReceived')
.withArgs(anyValue, this.chain.toErc7930(bruce), chris, ids, values)
// bridge on chain A releases custody of the token
.to.emit(this.tokenA, 'TransferBatch')
.withArgs(
chainAIsCustodial ? this.bridgeA : this.gateway,
chainAIsCustodial ? this.bridgeA : ethers.ZeroAddress,
chris,
ids,
values,
);
});
});
describe('transfer with allowance', function () {
it('spender is owner', async function () {
const [alice, bruce] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.chain.toErc7930(bruce),
ids,
values,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.chain.toErc7930(bruce), ids, values);
});
it('spender is allowed for all', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await this.tokenA.connect(alice).setApprovalForAll(chris, true);
await expect(
this.bridgeA.connect(chris).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.chain.toErc7930(bruce),
ids,
values,
),
)
.to.emit(this.bridgeA, 'CrosschainMultiTokenTransferSent')
.withArgs(anyValue, alice, this.chain.toErc7930(bruce), ids, values);
});
});
describe('invalid transfer', function () {
it('missing allowance', async function () {
const [alice, bruce, chris] = this.accounts;
await this.tokenA.$_mintBatch(alice, ids, values, '0x');
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
// chris is not allowed
await expect(
this.bridgeA.connect(chris).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.chain.toErc7930(bruce),
ids,
values,
),
)
.to.be.revertedWithCustomError(this.tokenA, 'ERC1155MissingApprovalForAll')
.withArgs(chris, alice);
});
it('insufficient balance', async function () {
const [alice, bruce] = this.accounts;
await this.tokenA.connect(alice).setApprovalForAll(this.bridgeA, true);
await expect(
this.bridgeA.connect(alice).getFunction('crosschainTransferFrom(address,bytes,uint256[],uint256[])')(
alice,
this.chain.toErc7930(bruce),
ids,
values,
),
)
.to.be.revertedWithCustomError(this.tokenA, 'ERC1155InsufficientBalance')
.withArgs(alice, 0n, values[0], ids[0]);
});
});
describe('restrictions', function () {
it('only gateway can relay messages', async function () {
const [notGateway] = this.accounts;
await expect(
this.bridgeA
.connect(notGateway)
.receiveMessage(
ethers.ZeroHash,
this.chain.toErc7930(this.tokenB),
this.encodePayload(notGateway, notGateway, ids, values),
),
)
.to.be.revertedWithCustomError(this.bridgeA, 'ERC7786RecipientUnauthorizedGateway')
.withArgs(notGateway, this.chain.toErc7930(this.tokenB));
});
it('only counterpart can send a crosschain message', async function () {
const [invalid] = this.accounts;
await expect(
this.gateway
.connect(invalid)
.sendMessage(this.chain.toErc7930(this.bridgeA), this.encodePayload(invalid, invalid, ids, values), []),
)
.to.be.revertedWithCustomError(this.bridgeA, 'ERC7786RecipientUnauthorizedGateway')
.withArgs(this.gateway, this.chain.toErc7930(invalid));
});
});
describe('reconfiguration', function () {
it('updating a link emits an event', async function () {
const newGateway = await ethers.deployContract('$ERC7786GatewayMock');
const newCounterpart = this.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(newGateway, newCounterpart, true))
.to.emit(this.bridgeA, 'LinkRegistered')
.withArgs(newGateway, newCounterpart);
await expect(this.bridgeA.getLink(this.chain.erc7930)).to.eventually.deep.equal([
newGateway.target,
newCounterpart,
]);
});
it('cannot override configuration if "allowOverride" is false', async function () {
const newGateway = await ethers.deployContract('$ERC7786GatewayMock');
const newCounterpart = this.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(newGateway, newCounterpart, false))
.to.be.revertedWithCustomError(this.bridgeA, 'LinkAlreadyRegistered')
.withArgs(this.chain.erc7930);
});
it('reject invalid gateway', async function () {
const notAGateway = this.accounts[0];
const newCounterpart = this.chain.toErc7930(this.accounts[0]);
await expect(this.bridgeA.$_setLink(notAGateway, newCounterpart, false)).to.be.revertedWithoutReason();
});
});
});
}
module.exports = {
shouldBehaveLikeBridgeERC1155,
};