-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathevent.test.ts
More file actions
167 lines (138 loc) · 7.11 KB
/
event.test.ts
File metadata and controls
167 lines (138 loc) · 7.11 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
import { getProgramIdl } from "@solanafm/explorer-kit-idls";
import { describe, expect, it } from "vitest";
import { checkIfEventParser, checkIfInstructionParser, ParserType, SolanaFMParser } from "../../src";
describe("createAnchorEventParser", () => {
it("should construct an anchor event parser for a given valid IDL", async () => {
const programId = "JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
expect(eventParser).not.toBeNull();
expect(checkIfInstructionParser(eventParser)).toBe(false);
expect(checkIfEventParser(eventParser)).toBe(true);
}
});
it("should construct an anchor event parser for a given valid IDL and parses the event data", async () => {
const programId = "JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB";
const instructionData =
"UWzjvs3QCsTK4JG4rMDoQTFs3+aKZ2iVocDNV3WrI/6ICjX+qUE0Hcb6evO+2606PWXzaqvJdDGxu+TC0vbg5HymAgNFL11hZygFAAAAAABm5RiKEwih25C20x8/vcqMPfJnjIES3909GSxaPMRXqLFJAAAAAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(instructionData);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("Swap");
}
}
});
it("should construct an anchor event parser for a given valid IDL and parses the event data and properly map the data type with the given IDL", async () => {
const programId = "JUP4Fb2cqiRUcaTHdrPC8h2gNsA2ETXiPDD33WcGuJB";
const instructionData =
"UWzjvs3QCsTK4JG4rMDoQTFs3+aKZ2iVocDNV3WrI/6ICjX+qUE0Hcb6evO+2606PWXzaqvJdDGxu+TC0vbg5HymAgNFL11hZygFAAAAAABm5RiKEwih25C20x8/vcqMPfJnjIES3909GSxaPMRXqLFJAAAAAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(instructionData, true);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("Swap");
expect(decodedData?.data["inputMint"].type).toBe("publicKey");
}
}
});
});
describe("createShankEventParser", () => {
it("should construct an shank event parser for a given valid IDL", async () => {
const programId = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
expect(eventParser).not.toBeNull();
expect(checkIfInstructionParser(eventParser)).toBe(false);
expect(checkIfEventParser(eventParser)).toBe(true);
}
});
it("should construct an anchor event parser for a given valid IDL and parses the event data", async () => {
const programId = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
const eventData = "A8Ce5gUAAAAAnhJhfQsAAAACAAAAAAAAAMCe5gUAAAAAeU9gpSYAAAD7ESaLIGIAAJn+yu8OAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(eventData);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("swapBaseIn");
}
}
});
it("should construct an anchor event parser for a given valid IDL and parses the event data and properly map the data type with the given IDL", async () => {
const programId = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
const eventData = "A8Ce5gUAAAAAnhJhfQsAAAACAAAAAAAAAMCe5gUAAAAAeU9gpSYAAAD7ESaLIGIAAJn+yu8OAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(eventData, true);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("swapBaseIn");
expect(decodedData?.data["minimumAmountOut"].type).toBe("u64");
}
}
});
});
describe("createNewAnchorParser", () => {
it("should construct an anchor 0.3.0 event parser for a given valid IDL", async () => {
const programId = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
expect(eventParser).not.toBeNull();
expect(checkIfEventParser(eventParser)).toBe(true);
}
});
it("should construct an anchor 0.3.0 event parser for a given valid IDL and parses the event data", async () => {
const programId = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";
const eventData =
"QMbN6CYIceKpKlqLTylZUoQlUKqT/VuVtazmqOuSDJOULkNpDCDscwabiFf+q4GE+2h/Y0YYwDXaxDncGus7VZig8AAAAAABG+xuAQAAAABuE+O/mSsnCUc4smt9N6Da8XiGrtyfTQYxWwx20dhGYdoM2iAAAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(eventData);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("SwapEvent");
}
}
});
it("should construct an anchor 0.3.0 event parser for a given valid IDL, parses the event data and properly map the data type with the given IDL", async () => {
const programId = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";
const eventData =
"QMbN6CYIceKpKlqLTylZUoQlUKqT/VuVtazmqOuSDJOULkNpDCDscwabiFf+q4GE+2h/Y0YYwDXaxDncGus7VZig8AAAAAABG+xuAQAAAABuE+O/mSsnCUc4smt9N6Da8XiGrtyfTQYxWwx20dhGYdoM2iAAAAAA";
const idlItem = await getProgramIdl(programId);
if (idlItem) {
const parser = new SolanaFMParser(idlItem, programId);
const eventParser = parser.createParser(ParserType.EVENT);
if (eventParser && checkIfEventParser(eventParser)) {
const decodedData = eventParser.parseEvents(eventData, true);
expect(decodedData).not.toBeNull();
expect(decodedData?.type).toBe("event");
expect(decodedData?.name).toBe("SwapEvent");
expect(decodedData?.data["input_amount"].type).toBe("u64");
}
}
});
});