-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path01__registration.ts
122 lines (110 loc) · 4.74 KB
/
01__registration.ts
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
import "@wormhole-foundation/sdk-evm/address";
import { ethers } from "ethers-v5";
import { ITokenRouter__factory, IMatchingEngine__factory } from "../src/types";
import {
LOCALHOSTS,
OWNER_ASSISTANT_PRIVATE_KEY,
mineWait,
ValidNetwork,
MATCHING_ENGINE_NAME,
parseLiquidityLayerEnvFile,
ChainType,
LiquidityLayerEnv,
} from "../src/testing";
import { expect } from "chai";
import { toChainId } from "@wormhole-foundation/sdk-base";
import { toUniversal } from "@wormhole-foundation/sdk-definitions";
const CHAIN_PATHWAYS: ValidNetwork[] = ["Ethereum", "Avalanche"];
describe("Registration", () => {
const envPath = `${__dirname}/../../env/localnet`;
describe(`Register Token Routers on ${MATCHING_ENGINE_NAME} Matching Engine`, () => {
const env = parseLiquidityLayerEnvFile(`${envPath}/${MATCHING_ENGINE_NAME}.env`);
const provider = new ethers.providers.StaticJsonRpcProvider(
LOCALHOSTS[MATCHING_ENGINE_NAME],
);
const assistant = new ethers.Wallet(OWNER_ASSISTANT_PRIVATE_KEY, provider);
const matchingEngineAddress = toUniversal("Avalanche", env.matchingEngineAddress).toNative(
"Avalanche",
);
const engine = IMatchingEngine__factory.connect(
matchingEngineAddress.toString(),
assistant,
);
for (const chainName of CHAIN_PATHWAYS) {
it(`Register ${chainName}`, async () => {
const targetEnv = parseLiquidityLayerEnvFile(`${envPath}/${chainName}.env`);
const [formattedAddress, mintRecipient] = fetchTokenRouterEndpoint(
targetEnv,
chainName,
);
const targetChainId = toChainId(chainName);
await engine
.addRouterEndpoint(
targetChainId,
{
router: formattedAddress,
mintRecipient,
},
targetEnv.domain,
)
.then((tx) => mineWait(provider, tx));
const registeredAddress = await engine.getRouter(targetChainId);
expect(registeredAddress.substring(2)).to.equal(
Buffer.from(formattedAddress).toString("hex"),
);
});
}
});
for (const chainName of CHAIN_PATHWAYS) {
describe(`Register Token Routers on ${chainName}`, () => {
const env = parseLiquidityLayerEnvFile(`${envPath}/${chainName}.env`);
const provider = new ethers.providers.StaticJsonRpcProvider(LOCALHOSTS[chainName]);
const assistant = new ethers.Wallet(OWNER_ASSISTANT_PRIVATE_KEY, provider);
const router = ITokenRouter__factory.connect(env.tokenRouterAddress, assistant);
for (const targetChain of CHAIN_PATHWAYS) {
if (targetChain === chainName) {
continue;
}
it(`Register ${targetChain}`, async () => {
const targetEnv = parseLiquidityLayerEnvFile(`${envPath}/${targetChain}.env`);
const [formattedAddress, mintRecipient] = fetchTokenRouterEndpoint(
targetEnv,
chainName,
);
const targetChainId = toChainId(targetChain);
await router
.addRouterEndpoint(
targetChainId,
{ router: formattedAddress, mintRecipient },
targetEnv.domain,
)
.then((tx) => mineWait(provider, tx));
const registeredAddress = await router.getRouter(targetChainId);
expect(registeredAddress.substring(2)).to.equal(
Buffer.from(formattedAddress).toString("hex"),
);
});
}
});
}
});
function fetchTokenRouterEndpoint(
targetEnv: LiquidityLayerEnv,
chainName: ValidNetwork,
): [Uint8Array, Uint8Array] {
const formattedAddress = toUniversal(chainName, targetEnv.tokenRouterAddress).toUint8Array();
let formattedMintRecipient;
if (targetEnv.chainType === ChainType.Evm) {
formattedMintRecipient = formattedAddress;
} else {
if (targetEnv.tokenRouterMintRecipient === undefined) {
throw new Error("no token router mint recipient specified");
} else {
formattedMintRecipient = toUniversal(
chainName,
targetEnv.tokenRouterMintRecipient,
).toUint8Array();
}
}
return [formattedAddress, formattedMintRecipient];
}