|
1 |
| -import { ChainName } from "@certusone/wormhole-sdk"; |
2 |
| -import { PACKET_DATA_SIZE, PublicKey, SystemProgram } from "@solana/web3.js"; |
3 |
| -import { ActionName, decodeHeader, encodeHeader, ExecutePostedVaa } from ".."; |
| 1 | +import { PublicKey, SystemProgram } from "@solana/web3.js"; |
| 2 | +import { PythGovernanceHeader, ExecutePostedVaa } from ".."; |
4 | 3 |
|
5 | 4 | test("GovernancePayload ser/de", (done) => {
|
6 | 5 | jest.setTimeout(60000);
|
7 | 6 |
|
8 | 7 | // Valid header 1
|
9 |
| - let expectedGovernanceHeader = { |
10 |
| - targetChainId: "pythnet" as ChainName, |
11 |
| - action: "ExecutePostedVaa" as ActionName, |
12 |
| - }; |
13 |
| - let buffer = Buffer.alloc(PACKET_DATA_SIZE); |
14 |
| - let span = encodeHeader(expectedGovernanceHeader, buffer); |
| 8 | + let expectedGovernanceHeader = new PythGovernanceHeader( |
| 9 | + "pythnet", |
| 10 | + "ExecutePostedVaa" |
| 11 | + ); |
| 12 | + let buffer = expectedGovernanceHeader.encode(); |
15 | 13 | expect(
|
16 |
| - buffer.subarray(0, span).equals(Buffer.from([80, 84, 71, 77, 0, 0, 0, 26])) |
| 14 | + buffer.equals(Buffer.from([80, 84, 71, 77, 0, 0, 0, 26])) |
17 | 15 | ).toBeTruthy();
|
18 |
| - |
19 |
| - let governanceHeader = decodeHeader(buffer.subarray(0, span)); |
20 |
| - expect(governanceHeader?.targetChainId).toBe("pythnet"); |
21 |
| - expect(governanceHeader?.action).toBe("ExecutePostedVaa"); |
| 16 | + let governanceHeader = PythGovernanceHeader.decode(buffer); |
| 17 | + expect(governanceHeader.targetChainId).toBe("pythnet"); |
| 18 | + expect(governanceHeader.action).toBe("ExecutePostedVaa"); |
22 | 19 |
|
23 | 20 | // Valid header 2
|
24 |
| - expectedGovernanceHeader = { |
25 |
| - targetChainId: "unset" as ChainName, |
26 |
| - action: "ExecutePostedVaa" as ActionName, |
27 |
| - }; |
28 |
| - buffer = Buffer.alloc(PACKET_DATA_SIZE); |
29 |
| - span = encodeHeader(expectedGovernanceHeader, buffer); |
30 |
| - expect( |
31 |
| - buffer.subarray(0, span).equals(Buffer.from([80, 84, 71, 77, 0, 0, 0, 0])) |
32 |
| - ).toBeTruthy(); |
33 |
| - governanceHeader = decodeHeader(buffer.subarray(0, span)); |
| 21 | + expectedGovernanceHeader = new PythGovernanceHeader( |
| 22 | + "unset", |
| 23 | + "ExecutePostedVaa" |
| 24 | + ); |
| 25 | + buffer = expectedGovernanceHeader.encode(); |
| 26 | + expect(buffer.equals(Buffer.from([80, 84, 71, 77, 0, 0, 0, 0]))).toBeTruthy(); |
| 27 | + governanceHeader = PythGovernanceHeader.decode(buffer); |
34 | 28 | expect(governanceHeader?.targetChainId).toBe("unset");
|
35 | 29 | expect(governanceHeader?.action).toBe("ExecutePostedVaa");
|
36 | 30 |
|
37 | 31 | // Valid header 3
|
38 |
| - expectedGovernanceHeader = { |
39 |
| - targetChainId: "solana" as ChainName, |
40 |
| - action: "SetFee" as ActionName, |
41 |
| - }; |
42 |
| - buffer = Buffer.alloc(PACKET_DATA_SIZE); |
43 |
| - span = encodeHeader(expectedGovernanceHeader, buffer); |
44 |
| - expect( |
45 |
| - buffer.subarray(0, span).equals(Buffer.from([80, 84, 71, 77, 1, 3, 0, 1])) |
46 |
| - ).toBeTruthy(); |
47 |
| - governanceHeader = decodeHeader(buffer.subarray(0, span)); |
| 32 | + expectedGovernanceHeader = new PythGovernanceHeader("solana", "SetFee"); |
| 33 | + buffer = expectedGovernanceHeader.encode(); |
| 34 | + expect(buffer.equals(Buffer.from([80, 84, 71, 77, 1, 3, 0, 1]))).toBeTruthy(); |
| 35 | + governanceHeader = PythGovernanceHeader.decode(buffer); |
48 | 36 | expect(governanceHeader?.targetChainId).toBe("solana");
|
49 | 37 | expect(governanceHeader?.action).toBe("SetFee");
|
50 | 38 |
|
51 | 39 | // Wrong magic number
|
52 | 40 | expect(() =>
|
53 |
| - decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0])) |
| 41 | + PythGovernanceHeader.decode( |
| 42 | + Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]) |
| 43 | + ) |
54 | 44 | ).toThrow("Wrong magic number");
|
55 | 45 |
|
56 | 46 | // Wrong chain
|
57 | 47 | expect(() =>
|
58 |
| - decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0])) |
| 48 | + PythGovernanceHeader.decode( |
| 49 | + Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]) |
| 50 | + ) |
59 | 51 | ).toThrow("Chain Id not found");
|
60 | 52 |
|
61 | 53 | // Wrong module/action combination
|
62 | 54 | expect(() =>
|
63 |
| - decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0])) |
| 55 | + PythGovernanceHeader.decode( |
| 56 | + Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]) |
| 57 | + ) |
64 | 58 | ).toThrow("Invalid header, action doesn't match module");
|
65 | 59 |
|
66 | 60 | // Decode executePostVaa with empty instructions
|
|
0 commit comments