events.spec.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { PublicKey } from "@solana/web3.js";
  2. import { EventParser } from "../src/program/event";
  3. import { BorshCoder } from "../src";
  4. describe("Events", () => {
  5. it("Parses multiple instructions", () => {
  6. const logs = [
  7. "Program 11111111111111111111111111111111 invoke [1]",
  8. "Program 11111111111111111111111111111111 success",
  9. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
  10. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 17867 of 200000 compute units",
  11. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
  12. ];
  13. const idl = {
  14. version: "0.0.0",
  15. name: "basic_0",
  16. instructions: [
  17. {
  18. name: "initialize",
  19. accounts: [],
  20. args: [],
  21. },
  22. ],
  23. };
  24. const coder = new BorshCoder(idl);
  25. const programId = PublicKey.default;
  26. const eventParser = new EventParser(programId, coder);
  27. if (Array.from(eventParser.parseLogs(logs)).length > 0) {
  28. throw new Error("Should never find logs");
  29. }
  30. });
  31. it("Upgrade event check", () => {
  32. const logs = [
  33. "Upgraded program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54",
  34. "Program 11111111111111111111111111111111 invoke [1]",
  35. "Program 11111111111111111111111111111111 success",
  36. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
  37. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 17867 of 200000 compute units",
  38. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
  39. ];
  40. const idl = {
  41. version: "0.0.0",
  42. name: "basic_0",
  43. instructions: [
  44. {
  45. name: "initialize",
  46. accounts: [],
  47. args: [],
  48. },
  49. ],
  50. };
  51. const coder = new BorshCoder(idl);
  52. const programId = PublicKey.default;
  53. const eventParser = new EventParser(programId, coder);
  54. if (Array.from(eventParser.parseLogs(logs)).length > 0) {
  55. throw new Error("Should never find logs");
  56. }
  57. });
  58. it("Find event with different start log.", (done) => {
  59. const logs = [
  60. "Upgraded program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54",
  61. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
  62. "Program log: Instruction: BuyNft",
  63. "Program 11111111111111111111111111111111 invoke [2]",
  64. "Program log: UhUxVlc2hGeTBjNPCGmmZjvNSuBOYpfpRPJLfJmTLZueJAmbgEtIMGl9lLKKH6YKy1AQd8lrsdJPPc7joZ6kCkEKlNLKhbUv",
  65. "Program 11111111111111111111111111111111 success",
  66. "Program 11111111111111111111111111111111 invoke [2]",
  67. "Program 11111111111111111111111111111111 success",
  68. "Program 11111111111111111111111111111111 invoke [2]",
  69. "Program 11111111111111111111111111111111 success",
  70. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  71. "Program log: Instruction: Transfer",
  72. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 141128 compute units",
  73. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  74. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  75. "Program log: Instruction: CloseAccount",
  76. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 135127 compute units",
  77. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  78. "Program log: UhUxVlc2hGeTBjNPCGmmZjvNSuBOYpfpRPJLfJmTLZueJAmbgEtIMGl9lLKKH6YKy1AQd8lrsdJPPc7joZ6kCkEKlNLKhbUv",
  79. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 73106 of 200000 compute units",
  80. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
  81. ];
  82. const idl = {
  83. version: "0.0.0",
  84. name: "basic_1",
  85. instructions: [
  86. {
  87. name: "initialize",
  88. accounts: [],
  89. args: [],
  90. },
  91. ],
  92. events: [
  93. {
  94. name: "NftSold",
  95. fields: [
  96. {
  97. name: "nftMintAddress",
  98. type: "publicKey" as "publicKey",
  99. index: false,
  100. },
  101. {
  102. name: "accountAddress",
  103. type: "publicKey" as "publicKey",
  104. index: false,
  105. },
  106. ],
  107. },
  108. ],
  109. };
  110. const coder = new BorshCoder(idl);
  111. const programId = new PublicKey(
  112. "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
  113. );
  114. const eventParser = new EventParser(programId, coder);
  115. const gen = eventParser.parseLogs(logs);
  116. for (const event of gen) {
  117. expect(event.name).toEqual("NftSold");
  118. done();
  119. }
  120. });
  121. it("Find event from logs", (done) => {
  122. const logs = [
  123. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
  124. "Program log: Instruction: CancelListing",
  125. "Program log: TRANSFERED SOME TOKENS",
  126. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  127. "Program log: Instruction: Transfer",
  128. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
  129. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  130. "Program log: TRANSFERED SOME TOKENS",
  131. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  132. "Program log: Instruction: CloseAccount",
  133. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
  134. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  135. "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
  136. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 31435 of 200000 compute units",
  137. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
  138. ];
  139. const idl = {
  140. version: "0.0.0",
  141. name: "basic_2",
  142. instructions: [
  143. {
  144. name: "cancelListing",
  145. accounts: [
  146. {
  147. name: "globalState",
  148. isMut: true,
  149. isSigner: false,
  150. },
  151. {
  152. name: "nftHolderAccount",
  153. isMut: true,
  154. isSigner: false,
  155. },
  156. {
  157. name: "listingAccount",
  158. isMut: true,
  159. isSigner: false,
  160. },
  161. {
  162. name: "nftAssociatedAccount",
  163. isMut: true,
  164. isSigner: false,
  165. },
  166. {
  167. name: "signer",
  168. isMut: true,
  169. isSigner: true,
  170. },
  171. {
  172. name: "tokenProgram",
  173. isMut: false,
  174. isSigner: false,
  175. },
  176. ],
  177. args: [],
  178. },
  179. ],
  180. events: [
  181. {
  182. name: "ListingClosed",
  183. fields: [
  184. {
  185. name: "initializer",
  186. type: "publicKey" as "publicKey",
  187. index: false,
  188. },
  189. {
  190. name: "nftMintAddress",
  191. type: "publicKey" as "publicKey",
  192. index: false,
  193. },
  194. {
  195. name: "accountAddress",
  196. type: "publicKey" as "publicKey",
  197. index: false,
  198. },
  199. ],
  200. },
  201. ],
  202. };
  203. const coder = new BorshCoder(idl);
  204. const programId = new PublicKey(
  205. "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
  206. );
  207. const eventParser = new EventParser(programId, coder);
  208. const gen = eventParser.parseLogs(logs);
  209. for (const event of gen) {
  210. expect(event.name).toEqual("ListingClosed");
  211. done();
  212. }
  213. });
  214. it("Listen to different program and send other program logs with same name", () => {
  215. const logs = [
  216. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP invoke [1]",
  217. "Program log: Instruction: CancelListing",
  218. "Program log: TRANSFERED SOME TOKENS",
  219. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  220. "Program log: Instruction: Transfer",
  221. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
  222. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  223. "Program log: TRANSFERED SOME TOKENS",
  224. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  225. "Program log: Instruction: CloseAccount",
  226. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
  227. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  228. "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
  229. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP consumed 31435 of 200000 compute units",
  230. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP success",
  231. ];
  232. const idl = {
  233. version: "0.0.0",
  234. name: "basic_2",
  235. instructions: [],
  236. events: [
  237. {
  238. name: "ListingClosed",
  239. fields: [
  240. {
  241. name: "initializer",
  242. type: "publicKey" as "publicKey",
  243. index: false,
  244. },
  245. {
  246. name: "nftMintAddress",
  247. type: "publicKey" as "publicKey",
  248. index: false,
  249. },
  250. {
  251. name: "accountAddress",
  252. type: "publicKey" as "publicKey",
  253. index: false,
  254. },
  255. ],
  256. },
  257. ],
  258. };
  259. const coder = new BorshCoder(idl);
  260. const programId = new PublicKey(
  261. "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
  262. );
  263. const eventParser = new EventParser(programId, coder);
  264. if (Array.from(eventParser.parseLogs(logs)).length > 0) {
  265. throw new Error("Should never find logs");
  266. }
  267. });
  268. });