-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtokenBridge.test.ts
330 lines (284 loc) · 9.45 KB
/
tokenBridge.test.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
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
import {
CONFIG,
ChainAddress,
Network,
Signature,
TokenBridge,
UniversalAddress,
createVAA,
encoding,
nativeChainIds,
toNative,
} from '@wormhole-foundation/sdk-connect';
import { utils } from '@wormhole-foundation/sdk-definitions/testing';
import '@wormhole-foundation/sdk-evm-core';
import '@wormhole-foundation/sdk-evm-tokenbridge';
import { EvmChains, EvmPlatform } from './../../src/index.js';
import { describe, expect, test } from '@jest/globals';
import path from 'path';
import nock from 'nock';
// Setup nock to record fixtures
const nockBack = nock.back;
nockBack.fixtures =
(__dirname ?? path.resolve() + '/__tests__/integration') + '/fixtures';
console.log(nockBack.fixtures);
let nockDone: () => void;
beforeEach(async () => {
nockBack.setMode('lockdown');
const fullTestName = expect.getState().currentTestName?.replace(/\s/g, '_');
const { nockDone: nd } = await nockBack(`${fullTestName}.json`, {
// Remove the `id` from the request body after preparing it but before
// trying to match a fixture.
after: (scope) => {
scope.filteringRequestBody((body: string) => {
const b = JSON.parse(body);
let formattedBody = b;
if (Array.isArray(b)) {
formattedBody = b.map((o) => {
delete o.id;
return o;
});
} else {
delete formattedBody.id;
}
return JSON.stringify(formattedBody);
});
},
// Remove the `id` from the request body before saving it as a fixture.
afterRecord: (defs) => {
return defs.map((d: nock.Definition) => {
if (typeof d.body !== 'object') return d;
if (Array.isArray(d.body)) {
const body = d.body as { id?: string }[];
d.body = body.map((o) => {
delete o.id;
return o;
});
} else {
const body = d.body as { id?: string };
delete body.id;
d.body = body;
}
return d;
});
},
});
// update global var
nockDone = nd;
});
afterEach(async () => {
nockDone();
nockBack.setMode('wild');
});
const network = 'Mainnet';
const configs = CONFIG[network].chains;
const TOKEN_ADDRESSES = {
Mainnet: {
Ethereum: {
wsteth: '0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
wavax: '0x85f138bfEE4ef8e540890CFb48F620571d67Eda3',
},
},
};
const bogusAddress = toNative(
'Ethereum',
'0x0000c581f595b53c5cb19bd0b3f8da6c935e2ca0',
);
const realNativeAddress = toNative(
'Ethereum',
TOKEN_ADDRESSES['Mainnet']['Ethereum']['wsteth'],
);
const realWrappedAddress = toNative(
'Ethereum',
TOKEN_ADDRESSES['Mainnet']['Ethereum']['wavax'],
);
const chain = 'Ethereum';
const destChain = 'Avalanche';
const sender = toNative('Ethereum', new Uint8Array(20));
const recipient: ChainAddress = {
chain: destChain,
address: new UniversalAddress(new Uint8Array(32)),
};
describe('TokenBridge Tests', () => {
const p = new EvmPlatform(network, configs);
let tb: TokenBridge<Network, EvmChains>;
test('Create TokenBridge', async () => {
const rpc = p.getRpc('Ethereum');
tb = await p.getProtocol('TokenBridge', rpc);
expect(tb).toBeTruthy();
});
describe('Get Wrapped Asset Details', () => {
describe('isWrappedAsset', () => {
test('Bogus', async () => {
const isWrapped = await tb.isWrappedAsset(bogusAddress);
expect(isWrapped).toBe(false);
});
test('Real Not Wrapped', async () => {
const isWrapped = await tb.isWrappedAsset(realNativeAddress);
expect(isWrapped).toBe(false);
});
test('Real Wrapped', async () => {
const isWrapped = await tb.isWrappedAsset(realWrappedAddress);
expect(isWrapped).toBe(true);
});
});
describe('getOriginalAsset', () => {
test('Bogus', async () => {
expect(() => tb.getOriginalAsset(bogusAddress)).rejects.toThrow();
});
test('Real Not Wrapped', async () => {
expect(() => tb.getOriginalAsset(realNativeAddress)).rejects.toThrow();
});
test('Real Wrapped', async () => {
const orig = await tb.getOriginalAsset(realWrappedAddress);
expect(orig.chain).toEqual('Avalanche');
expect(orig).toBeTruthy();
});
});
describe('hasWrappedAsset', () => {
test('Bogus', async () => {
const hasWrapped = await tb.hasWrappedAsset({
chain: 'Avalanche',
address: bogusAddress,
});
expect(hasWrapped).toBe(false);
});
test('Real Not Wrapped', async () => {
const hasWrapped = await tb.hasWrappedAsset({
chain: 'Avalanche',
address: realNativeAddress,
});
expect(hasWrapped).toBe(false);
});
test('Real Wrapped', async () => {
const orig = await tb.getOriginalAsset(realWrappedAddress);
const hasWrapped = await tb.hasWrappedAsset(orig);
expect(hasWrapped).toBe(true);
});
});
describe('getWrappedAsset', () => {
test('Bogus', async () => {
const hasWrapped = tb.getWrappedAsset({
chain: 'Avalanche',
address: bogusAddress,
});
expect(hasWrapped).rejects.toThrow();
});
test('Real Not Wrapped', async () => {
const hasWrapped = tb.getWrappedAsset({
chain: 'Avalanche',
address: realNativeAddress,
});
expect(hasWrapped).rejects.toThrow();
});
test('Real Wrapped', async () => {
const orig = await tb.getOriginalAsset(realWrappedAddress);
const wrappedAsset = await tb.getWrappedAsset(orig);
expect(wrappedAsset.toString()).toBe(realWrappedAddress.toString());
});
});
});
describe('Create Token Attestation Transactions', () => {
const chain = 'Ethereum';
const nativeAddress = utils.makeNativeAddress(chain);
const tbAddress = p.config[chain]!.contracts.tokenBridge!;
test('Create Attestation', async () => {
const attestation = tb.createAttestation(nativeAddress);
const allTxns = [];
for await (const atx of attestation) {
allTxns.push(atx);
}
expect(allTxns).toHaveLength(1);
const [attestTx] = allTxns;
expect(attestTx).toBeTruthy();
expect(attestTx!.chain).toEqual(chain);
const { transaction } = attestTx!;
expect(transaction.chainId).toEqual(
nativeChainIds.networkChainToNativeChainId.get(network, chain),
);
});
test('Submit Attestation', async () => {
const vaa = createVAA('TokenBridge:AttestMeta', {
payload: {
token: { address: nativeAddress.toUniversalAddress(), chain: chain },
decimals: 8,
symbol: encoding.hex.encode(new Uint8Array(16)),
name: encoding.hex.encode(new Uint8Array(16)),
},
guardianSet: 0,
signatures: [{ guardianIndex: 0, signature: new Signature(1n, 2n, 1) }],
emitterChain: chain,
emitterAddress: toNative(chain, tbAddress).toUniversalAddress(),
sequence: 0n,
consistencyLevel: 0,
timestamp: 0,
nonce: 0,
});
const submitAttestation = tb.submitAttestation(vaa);
const allTxns = [];
for await (const atx of submitAttestation) {
allTxns.push(atx);
}
expect(allTxns).toHaveLength(1);
const [attestTx] = allTxns;
expect(attestTx).toBeTruthy();
expect(attestTx!.chain).toEqual(chain);
const { transaction } = attestTx!;
expect(transaction.chainId).toEqual(
nativeChainIds.networkChainToNativeChainId.get(network, chain),
);
});
});
describe('Create TokenBridge Transactions', () => {
const tbAddress = p.config[chain]!.contracts.tokenBridge!;
describe('Token Transfer Transactions', () => {
describe('Transfer', () => {
const amount = 1000n;
const payload: Uint8Array | undefined = undefined;
test('Native', async () => {
const token = 'native';
const xfer = tb.transfer(sender, recipient, token, amount, payload);
expect(xfer).toBeTruthy();
const allTxns = [];
for await (const tx of xfer) {
allTxns.push(tx);
}
expect(allTxns).toHaveLength(1);
const [xferTx] = allTxns;
expect(xferTx).toBeTruthy();
expect(xferTx!.chain).toEqual(chain);
const { transaction } = xferTx!;
expect(transaction.chainId).toEqual(
nativeChainIds.networkChainToNativeChainId.get(network, chain),
);
});
test('Token', async () => {
const xfer = tb.transfer(
sender,
recipient,
realWrappedAddress,
amount,
payload,
);
expect(xfer).toBeTruthy();
const allTxns = [];
for await (const tx of xfer) {
allTxns.push(tx);
}
expect(allTxns).toHaveLength(2);
const [approveTx, xferTx] = allTxns;
expect(approveTx).toBeTruthy();
const { transaction: approveTransaction } = approveTx!;
expect(approveTransaction.to).toEqual(realWrappedAddress.toString());
expect(xferTx).toBeTruthy();
expect(xferTx!.chain).toEqual(chain);
const { transaction: xferTransaction } = xferTx!;
expect(xferTransaction.to).toEqual(tbAddress.toString());
expect(xferTransaction.chainId).toEqual(
nativeChainIds.networkChainToNativeChainId.get(network, chain),
);
});
});
});
});
});