Skip to content

Commit 3c1cf8c

Browse files
committed
up
1 parent fca7c03 commit 3c1cf8c

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

test/crosschain/ERC7802Bridge.test.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,29 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
44

55
const AxelarHelper = require('./axelar/AxelarHelper');
66

7-
const keccak256AbiEncode = (types, values) => ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(types, values));
8-
const sortBytes32 = values => values.sort((a, b) => (BigInt(a) < BigInt(b) ? -1 : 1));
7+
const buildBridgeHash = (...chains) => {
8+
const cmp = (a, b) => (BigInt(a) < BigInt(b) ? -1 : 1);
9+
const chainIds = chains.map(({ token, flags = ethers.ZeroHash, links }) =>
10+
ethers.keccak256(
11+
ethers.AbiCoder.defaultAbiCoder().encode(
12+
['bytes', 'bytes32', 'bytes32[]'],
13+
[
14+
token,
15+
flags,
16+
links
17+
.map(({ gateway, remote }) =>
18+
ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['bytes', 'bytes'], [gateway, remote])),
19+
)
20+
.sort(cmp),
21+
],
22+
),
23+
),
24+
);
25+
return {
26+
id: ethers.solidityPackedKeccak256(['bytes32[]'], [chainIds.toSorted(cmp)]),
27+
chainIds,
28+
};
29+
};
930

1031
async function fixture() {
1132
const [admin, ...accounts] = await ethers.getSigners();
@@ -20,34 +41,26 @@ async function fixture() {
2041
const bridgeB = await ethers.deployContract('$ERC7802Bridge');
2142
const tokenB = await ethers.deployContract('$ERC20BridgeableMock', ['Token B', 'TB', admin]);
2243

23-
// Bridge side identifiers
24-
const chainAId = keccak256AbiEncode(
25-
['bytes', 'bytes32', 'bytes32[]'],
26-
[
27-
chain.toErc7930(tokenA),
28-
'0x0000000000000000000000000000000000000000000000000000000000000001', // custodial
29-
sortBytes32([keccak256AbiEncode(['bytes', 'bytes'], [chain.toErc7930(gatewayA), chain.toErc7930(bridgeB)])]),
30-
],
44+
// Compute bridge identifier and local hashes
45+
const { id, chainIds } = buildBridgeHash(
46+
{
47+
token: chain.toErc7930(tokenA),
48+
flags: '0x0000000000000000000000000000000000000000000000000000000000000001',
49+
links: [{ gateway: chain.toErc7930(gatewayA), remote: chain.toErc7930(bridgeB) }],
50+
},
51+
{
52+
token: chain.toErc7930(tokenB),
53+
flags: '0x0000000000000000000000000000000000000000000000000000000000000000',
54+
links: [{ gateway: chain.toErc7930(gatewayB), remote: chain.toErc7930(bridgeA) }],
55+
},
3156
);
3257

33-
const chainBId = keccak256AbiEncode(
34-
['bytes', 'bytes32', 'bytes32[]'],
35-
[
36-
chain.toErc7930(tokenB),
37-
'0x0000000000000000000000000000000000000000000000000000000000000000', // crosschain
38-
sortBytes32([keccak256AbiEncode(['bytes', 'bytes'], [chain.toErc7930(gatewayB), chain.toErc7930(bridgeA)])]),
39-
],
40-
);
41-
42-
// Bridge global identifier
43-
const id = ethers.solidityPackedKeccak256(['bytes32[]'], [sortBytes32([chainAId, chainBId])]);
44-
4558
// Register bridge
4659
await expect(
4760
bridgeA.createBridge(
4861
tokenA,
4962
true, // is custodial
50-
[{ id: chainBId, gateway: gatewayA, remote: chain.toErc7930(bridgeB) }], // link to B + id of B
63+
[{ id: chainIds[1], gateway: gatewayA, remote: chain.toErc7930(bridgeB) }], // link to B + id of B
5164
),
5265
)
5366
.to.emit(bridgeA, 'NewBridge')
@@ -57,7 +70,7 @@ async function fixture() {
5770
bridgeB.createBridge(
5871
tokenB,
5972
false, // is crosschain
60-
[{ id: chainAId, gateway: gatewayB, remote: chain.toErc7930(bridgeA) }], // link to B + id of B
73+
[{ id: chainIds[0], gateway: gatewayB, remote: chain.toErc7930(bridgeA) }], // link to B + id of B
6174
),
6275
)
6376
.to.emit(bridgeB, 'NewBridge')

0 commit comments

Comments
 (0)