events.spec.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. eventParser.parseLogs(logs, () => {
  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. eventParser.parseLogs(logs, () => {
  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. eventParser.parseLogs(logs, (event) => {
  116. expect(event.name).toEqual("NftSold");
  117. done();
  118. });
  119. });
  120. it("Find event from logs", (done) => {
  121. const logs = [
  122. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 invoke [1]",
  123. "Program log: Instruction: CancelListing",
  124. "Program log: TRANSFERED SOME TOKENS",
  125. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  126. "Program log: Instruction: Transfer",
  127. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
  128. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  129. "Program log: TRANSFERED SOME TOKENS",
  130. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  131. "Program log: Instruction: CloseAccount",
  132. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
  133. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  134. "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
  135. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 consumed 31435 of 200000 compute units",
  136. "Program J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54 success",
  137. ];
  138. const idl = {
  139. version: "0.0.0",
  140. name: "basic_2",
  141. instructions: [
  142. {
  143. name: "cancelListing",
  144. accounts: [
  145. {
  146. name: "globalState",
  147. isMut: true,
  148. isSigner: false,
  149. },
  150. {
  151. name: "nftHolderAccount",
  152. isMut: true,
  153. isSigner: false,
  154. },
  155. {
  156. name: "listingAccount",
  157. isMut: true,
  158. isSigner: false,
  159. },
  160. {
  161. name: "nftAssociatedAccount",
  162. isMut: true,
  163. isSigner: false,
  164. },
  165. {
  166. name: "signer",
  167. isMut: true,
  168. isSigner: true,
  169. },
  170. {
  171. name: "tokenProgram",
  172. isMut: false,
  173. isSigner: false,
  174. },
  175. ],
  176. args: [],
  177. },
  178. ],
  179. events: [
  180. {
  181. name: "ListingClosed",
  182. fields: [
  183. {
  184. name: "initializer",
  185. type: "publicKey" as "publicKey",
  186. index: false,
  187. },
  188. {
  189. name: "nftMintAddress",
  190. type: "publicKey" as "publicKey",
  191. index: false,
  192. },
  193. {
  194. name: "accountAddress",
  195. type: "publicKey" as "publicKey",
  196. index: false,
  197. },
  198. ],
  199. },
  200. ],
  201. };
  202. const coder = new BorshCoder(idl);
  203. const programId = new PublicKey(
  204. "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
  205. );
  206. const eventParser = new EventParser(programId, coder);
  207. eventParser.parseLogs(logs, (event) => {
  208. expect(event.name).toEqual("ListingClosed");
  209. done();
  210. });
  211. });
  212. it("Listen to different program and send other program logs with same name", () => {
  213. const logs = [
  214. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP invoke [1]",
  215. "Program log: Instruction: CancelListing",
  216. "Program log: TRANSFERED SOME TOKENS",
  217. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  218. "Program log: Instruction: Transfer",
  219. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2549 of 182795 compute units",
  220. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  221. "Program log: TRANSFERED SOME TOKENS",
  222. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
  223. "Program log: Instruction: CloseAccount",
  224. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 1745 of 176782 compute units",
  225. "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
  226. "Program log: Vtv9xLjCsE60Ati9kl3VVU/5y8DMMeC4LaGdMLkX8WU+G59Wsi3wfky8rnO9otGb56CTRerWx3hB5M/SlRYBdht0fi+crAgFYsJcx2CHszpSWRkXNxYQ6DxQ/JqIvKnLC/8Mln7310A=",
  227. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP consumed 31435 of 200000 compute units",
  228. "Program 5VcVB7jEjdWJBkriXxayCrUUkwfhrPK3rXtnkxxUvMFP success",
  229. ];
  230. const idl = {
  231. version: "0.0.0",
  232. name: "basic_2",
  233. instructions: [],
  234. events: [
  235. {
  236. name: "ListingClosed",
  237. fields: [
  238. {
  239. name: "initializer",
  240. type: "publicKey" as "publicKey",
  241. index: false,
  242. },
  243. {
  244. name: "nftMintAddress",
  245. type: "publicKey" as "publicKey",
  246. index: false,
  247. },
  248. {
  249. name: "accountAddress",
  250. type: "publicKey" as "publicKey",
  251. index: false,
  252. },
  253. ],
  254. },
  255. ],
  256. };
  257. const coder = new BorshCoder(idl);
  258. const programId = new PublicKey(
  259. "J2XMGdW2qQLx7rAdwWtSZpTXDgAQ988BLP9QTgUZvm54"
  260. );
  261. const eventParser = new EventParser(programId, coder);
  262. eventParser.parseLogs(logs, () => {
  263. throw new Error("Should never find logs");
  264. });
  265. });
  266. });