@atomiqlabs/chain-solana 12.0.14 → 13.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +12 -29
  3. package/dist/index.js +32 -45
  4. package/dist/solana/SolanaChainType.d.ts +15 -11
  5. package/dist/solana/SolanaChainType.js +2 -2
  6. package/dist/solana/SolanaChains.d.ts +9 -20
  7. package/dist/solana/SolanaChains.js +25 -25
  8. package/dist/solana/SolanaInitializer.d.ts +38 -18
  9. package/dist/solana/SolanaInitializer.js +75 -63
  10. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +230 -228
  11. package/dist/solana/btcrelay/SolanaBtcRelay.js +463 -441
  12. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +33 -29
  13. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +37 -34
  14. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +50 -46
  15. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +81 -78
  16. package/dist/solana/btcrelay/program/programIdl.json +671 -671
  17. package/dist/solana/chain/SolanaAction.d.ts +26 -26
  18. package/dist/solana/chain/SolanaAction.js +86 -86
  19. package/dist/solana/chain/SolanaChainInterface.d.ts +77 -65
  20. package/dist/solana/chain/SolanaChainInterface.js +138 -125
  21. package/dist/solana/chain/SolanaModule.d.ts +14 -14
  22. package/dist/solana/chain/SolanaModule.js +13 -13
  23. package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
  24. package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
  25. package/dist/solana/chain/modules/SolanaBlocks.d.ts +32 -28
  26. package/dist/solana/chain/modules/SolanaBlocks.js +76 -72
  27. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  28. package/dist/solana/chain/modules/SolanaEvents.js +238 -238
  29. package/dist/solana/chain/modules/SolanaFees.d.ts +122 -121
  30. package/dist/solana/chain/modules/SolanaFees.js +381 -379
  31. package/dist/solana/chain/modules/SolanaSignatures.d.ts +24 -23
  32. package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
  33. package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
  34. package/dist/solana/chain/modules/SolanaSlots.js +69 -68
  35. package/dist/solana/chain/modules/SolanaTokens.d.ts +123 -136
  36. package/dist/solana/chain/modules/SolanaTokens.js +242 -248
  37. package/dist/solana/chain/modules/SolanaTransactions.d.ts +139 -124
  38. package/dist/solana/chain/modules/SolanaTransactions.js +404 -323
  39. package/dist/solana/events/SolanaChainEvents.d.ts +35 -88
  40. package/dist/solana/events/SolanaChainEvents.js +92 -256
  41. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +128 -75
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +337 -172
  43. package/dist/solana/program/SolanaProgramBase.d.ts +45 -40
  44. package/dist/solana/program/SolanaProgramBase.js +43 -43
  45. package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
  46. package/dist/solana/program/SolanaProgramModule.js +11 -11
  47. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
  48. package/dist/solana/program/modules/SolanaProgramEvents.js +117 -114
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +102 -71
  50. package/dist/solana/swaps/SolanaSwapData.js +335 -292
  51. package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
  52. package/dist/solana/swaps/SolanaSwapModule.js +12 -11
  53. package/dist/solana/swaps/SolanaSwapProgram.d.ts +202 -224
  54. package/dist/solana/swaps/SolanaSwapProgram.js +587 -570
  55. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  56. package/dist/solana/swaps/SwapTypeEnum.js +43 -42
  57. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +95 -94
  58. package/dist/solana/swaps/modules/SolanaDataAccount.js +230 -231
  59. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +69 -71
  60. package/dist/solana/swaps/modules/SolanaLpVault.js +171 -173
  61. package/dist/solana/swaps/modules/SwapClaim.d.ts +126 -129
  62. package/dist/solana/swaps/modules/SwapClaim.js +292 -291
  63. package/dist/solana/swaps/modules/SwapInit.d.ts +208 -217
  64. package/dist/solana/swaps/modules/SwapInit.js +521 -519
  65. package/dist/solana/swaps/modules/SwapRefund.d.ts +81 -82
  66. package/dist/solana/swaps/modules/SwapRefund.js +277 -262
  67. package/dist/solana/swaps/programIdl.json +945 -945
  68. package/dist/solana/swaps/programTypes.d.ts +943 -943
  69. package/dist/solana/swaps/programTypes.js +945 -945
  70. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +13 -9
  71. package/dist/solana/wallet/SolanaKeypairWallet.js +37 -33
  72. package/dist/solana/wallet/SolanaSigner.d.ts +15 -11
  73. package/dist/solana/wallet/SolanaSigner.js +21 -17
  74. package/dist/utils/Utils.d.ts +58 -53
  75. package/dist/utils/Utils.js +170 -170
  76. package/package.json +44 -41
  77. package/src/index.ts +17 -36
  78. package/src/solana/SolanaChainType.ts +32 -27
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +126 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +618 -589
  82. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +60 -57
  83. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +105 -102
  84. package/src/solana/btcrelay/program/programIdl.json +670 -670
  85. package/src/solana/chain/SolanaAction.ts +108 -108
  86. package/src/solana/chain/SolanaChainInterface.ts +219 -192
  87. package/src/solana/chain/SolanaModule.ts +20 -20
  88. package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
  89. package/src/solana/chain/modules/SolanaBlocks.ts +83 -78
  90. package/src/solana/chain/modules/SolanaEvents.ts +271 -270
  91. package/src/solana/chain/modules/SolanaFees.ts +454 -450
  92. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  93. package/src/solana/chain/modules/SolanaSlots.ts +83 -82
  94. package/src/solana/chain/modules/SolanaTokens.ts +300 -307
  95. package/src/solana/chain/modules/SolanaTransactions.ts +452 -365
  96. package/src/solana/events/SolanaChainEvents.ts +110 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +399 -209
  98. package/src/solana/program/SolanaProgramBase.ts +79 -79
  99. package/src/solana/program/SolanaProgramModule.ts +15 -15
  100. package/src/solana/program/modules/SolanaProgramEvents.ts +157 -155
  101. package/src/solana/swaps/SolanaSwapData.ts +420 -430
  102. package/src/solana/swaps/SolanaSwapModule.ts +17 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +858 -854
  104. package/src/solana/swaps/SwapTypeEnum.ts +30 -29
  105. package/src/solana/swaps/modules/SolanaDataAccount.ts +300 -307
  106. package/src/solana/swaps/modules/SolanaLpVault.ts +208 -215
  107. package/src/solana/swaps/modules/SwapClaim.ts +385 -389
  108. package/src/solana/swaps/modules/SwapInit.ts +657 -663
  109. package/src/solana/swaps/modules/SwapRefund.ts +335 -323
  110. package/src/solana/swaps/programIdl.json +944 -944
  111. package/src/solana/swaps/programTypes.ts +1885 -1885
  112. package/src/solana/wallet/SolanaKeypairWallet.ts +40 -36
  113. package/src/solana/wallet/SolanaSigner.ts +28 -24
  114. package/src/utils/Utils.ts +194 -180
@@ -1,80 +1,80 @@
1
- import {AnchorProvider, Idl, Program} from "@coral-xyz/anchor";
2
- import {SolanaFees} from "../chain/modules/SolanaFees";
3
- import {SolanaChainInterface, SolanaRetryPolicy} from "../chain/SolanaChainInterface";
4
- import {SolanaProgramEvents} from "./modules/SolanaProgramEvents";
5
- import {Connection, Keypair, PublicKey} from "@solana/web3.js";
6
- import {sha256} from "@noble/hashes/sha2";
7
- import {Buffer} from "buffer";
8
- import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
9
- import {getLogger} from "../../utils/Utils";
10
-
11
- /**
12
- * Base class providing program specific utilities
13
- */
14
- export class SolanaProgramBase<T extends Idl> {
15
-
16
- protected readonly logger = getLogger(this.constructor.name+": ");
17
-
18
- program: Program<T>;
19
-
20
- public readonly Events: SolanaProgramEvents<T>;
21
- public readonly Chain: SolanaChainInterface;
22
-
23
- constructor(
24
- chainInterface: SolanaChainInterface,
25
- programIdl: any,
26
- programAddress?: string
27
- ) {
28
- this.Chain = chainInterface;
29
-
30
- this.program = new Program<T>(
31
- programIdl as any,
32
- programAddress || programIdl.metadata.address,
33
- new AnchorProvider(chainInterface.connection, new SolanaKeypairWallet(Keypair.generate()), {})
34
- );
35
-
36
- this.Events = new SolanaProgramEvents(chainInterface, this);
37
- }
38
-
39
- /**
40
- * Derives static PDA address from the seed
41
- *
42
- * @param seed
43
- */
44
- public pda(seed: string): PublicKey;
45
- /**
46
- * Returns a function for deriving a dynamic PDA address from seed + dynamic arguments
47
- *
48
- * @param seed
49
- * @param func function translating the function argument to Buffer[]
50
- */
51
- public pda<T extends Array<any>>(seed: string, func: (...args: T) => Buffer[]): (...args: T) => PublicKey;
52
- public pda<T extends Array<any>>(seed: string, func?: (...args: T) => Buffer[]): PublicKey | ((...args: T) => PublicKey) {
53
- if(func==null) {
54
- return PublicKey.findProgramAddressSync(
55
- [Buffer.from(seed)],
56
- this.program.programId
57
- )[0];
58
- }
59
- return (...args: T) => {
60
- const res = func(...args);
61
- return PublicKey.findProgramAddressSync(
62
- [Buffer.from(seed)].concat(res),
63
- this.program.programId
64
- )[0]
65
- }
66
- }
67
- /**
68
- * Returns a function for deriving a dynamic deterministic keypair from dynamic arguments
69
- *
70
- * @param func function translating the function argument to Buffer[] to be used for deriving the keypair
71
- */
72
- public keypair<T extends Array<any>>(func: (...args: T) => Buffer[]): (...args: T) => Keypair {
73
- return (...args: T) => {
74
- const res = func(...args);
75
- const buff = sha256(Buffer.concat(res));
76
- return Keypair.fromSeed(buff);
77
- }
78
- }
79
-
1
+ import {AnchorProvider, Idl, Program} from "@coral-xyz/anchor";
2
+ import {SolanaFees} from "../chain/modules/SolanaFees";
3
+ import {SolanaChainInterface, SolanaRetryPolicy} from "../chain/SolanaChainInterface";
4
+ import {SolanaProgramEvents} from "./modules/SolanaProgramEvents";
5
+ import {Connection, Keypair, PublicKey} from "@solana/web3.js";
6
+ import {sha256} from "@noble/hashes/sha2";
7
+ import {Buffer} from "buffer";
8
+ import {SolanaKeypairWallet} from "../wallet/SolanaKeypairWallet";
9
+ import {getLogger} from "../../utils/Utils";
10
+
11
+ /**
12
+ * Base class providing program specific utilities
13
+ */
14
+ export class SolanaProgramBase<T extends Idl> {
15
+
16
+ protected readonly logger = getLogger(this.constructor.name+": ");
17
+
18
+ program: Program<T>;
19
+
20
+ public readonly Events: SolanaProgramEvents<T>;
21
+ public readonly Chain: SolanaChainInterface;
22
+
23
+ constructor(
24
+ chainInterface: SolanaChainInterface,
25
+ programIdl: any,
26
+ programAddress?: string
27
+ ) {
28
+ this.Chain = chainInterface;
29
+
30
+ this.program = new Program<T>(
31
+ programIdl as any,
32
+ programAddress || programIdl.metadata.address,
33
+ new AnchorProvider(chainInterface.connection, new SolanaKeypairWallet(Keypair.generate()), {})
34
+ );
35
+
36
+ this.Events = new SolanaProgramEvents(chainInterface, this);
37
+ }
38
+
39
+ /**
40
+ * Derives static PDA address from the seed
41
+ *
42
+ * @param seed
43
+ */
44
+ public pda(seed: string): PublicKey;
45
+ /**
46
+ * Returns a function for deriving a dynamic PDA address from seed + dynamic arguments
47
+ *
48
+ * @param seed
49
+ * @param func function translating the function argument to Buffer[]
50
+ */
51
+ public pda<T extends Array<any>>(seed: string, func: (...args: T) => Buffer[]): (...args: T) => PublicKey;
52
+ public pda<T extends Array<any>>(seed: string, func?: (...args: T) => Buffer[]): PublicKey | ((...args: T) => PublicKey) {
53
+ if(func==null) {
54
+ return PublicKey.findProgramAddressSync(
55
+ [Buffer.from(seed)],
56
+ this.program.programId
57
+ )[0];
58
+ }
59
+ return (...args: T) => {
60
+ const res = func(...args);
61
+ return PublicKey.findProgramAddressSync(
62
+ [Buffer.from(seed)].concat(res as Buffer<ArrayBuffer>[]),
63
+ this.program.programId
64
+ )[0]
65
+ }
66
+ }
67
+ /**
68
+ * Returns a function for deriving a dynamic deterministic keypair from dynamic arguments
69
+ *
70
+ * @param func function translating the function argument to Buffer[] to be used for deriving the keypair
71
+ */
72
+ public keypair<T extends Array<any>>(func: (...args: T) => Buffer[]): (...args: T) => Keypair {
73
+ return (...args: T) => {
74
+ const res = func(...args);
75
+ const buff = sha256(Buffer.concat(res));
76
+ return Keypair.fromSeed(buff);
77
+ }
78
+ }
79
+
80
80
  }
@@ -1,16 +1,16 @@
1
- import {SolanaModule} from "../chain/SolanaModule";
2
- import {Idl} from "@coral-xyz/anchor";
3
- import {SolanaProgramBase} from "./SolanaProgramBase";
4
- import {SolanaChainInterface} from "../chain/SolanaChainInterface";
5
-
6
-
7
- export class SolanaProgramModule<IDL extends Idl> extends SolanaModule {
8
-
9
- protected readonly program: SolanaProgramBase<IDL>;
10
-
11
- constructor(chainInterface: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
12
- super(chainInterface);
13
- this.program = program;
14
- }
15
-
1
+ import {SolanaModule} from "../chain/SolanaModule";
2
+ import {Idl} from "@coral-xyz/anchor";
3
+ import {SolanaProgramBase} from "./SolanaProgramBase";
4
+ import {SolanaChainInterface} from "../chain/SolanaChainInterface";
5
+
6
+
7
+ export class SolanaProgramModule<IDL extends Idl> extends SolanaModule {
8
+
9
+ protected readonly program: SolanaProgramBase<IDL>;
10
+
11
+ constructor(chainInterface: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
12
+ super(chainInterface);
13
+ this.program = program;
14
+ }
15
+
16
16
  }
@@ -1,156 +1,158 @@
1
- import {SolanaEvents} from "../../chain/modules/SolanaEvents";
2
- import {BorshCoder, DecodeType, Event, EventParser, Idl, IdlTypes, Instruction} from "@coral-xyz/anchor";
3
- import {IdlField, IdlInstruction} from "@coral-xyz/anchor/dist/cjs/idl";
4
- import {
5
- ConfirmedSignatureInfo,
6
- ParsedMessage, ParsedTransactionWithMeta,
7
- PartiallyDecodedInstruction,
8
- PublicKey,
9
- VersionedTransaction, VersionedTransactionResponse
10
- } from "@solana/web3.js";
11
- import {SolanaProgramBase} from "../SolanaProgramBase";
12
- import {SolanaChainInterface} from "../../chain/SolanaChainInterface";
13
-
14
- type DecodedFieldOrNull<D, Defined> = D extends IdlField ? DecodeType<D["type"], Defined> : unknown;
15
- type ArgsTuple<A extends IdlField[], Defined> = {
16
- [K in A[number]["name"]]: DecodedFieldOrNull<Extract<A[number], { name: K }>, Defined>
17
- };
18
-
19
- export type InstructionWithAccounts<IDL extends Idl> = SingleInstructionWithAccounts<IDL["instructions"][number], IDL>;
20
-
21
- export type SingleInstructionWithAccounts<I extends IdlInstruction, IDL extends Idl> = {
22
- name: I["name"],
23
- accounts: {
24
- [key in I["accounts"][number]["name"]]: PublicKey
25
- },
26
- data: ArgsTuple<I["args"], IdlTypes<IDL>>
27
- };
28
-
29
- export type ProgramEvent<IDL extends Idl> = Event<IDL["events"][number], Record<string, any>>;
30
-
31
- export class SolanaProgramEvents<IDL extends Idl> extends SolanaEvents {
32
-
33
- private readonly programCoder: BorshCoder;
34
- private readonly eventParser: EventParser;
35
- private readonly program: SolanaProgramBase<IDL>;
36
- private readonly nameMappedInstructions: {[name: string]: IdlInstruction};
37
-
38
- constructor(chain: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
39
- super(chain);
40
- this.program = program;
41
- this.programCoder = new BorshCoder(program.program.idl);
42
- this.eventParser = new EventParser(program.program.programId, this.programCoder);
43
- this.nameMappedInstructions = {};
44
- for(let ix of program.program.idl.instructions) {
45
- this.nameMappedInstructions[ix.name] = ix;
46
- }
47
- }
48
-
49
- /**
50
- * Runs a search backwards in time, processing the events for a specific topic public key
51
- *
52
- * @param topicKey
53
- * @param processor called for every event, should return a value if the correct event was found, or null
54
- * if the search should continue
55
- * @param abortSignal
56
- * @param logBatchSize how many signatures should be fetched in one getSignaturesForAddress call
57
- * @param startBlockheight
58
- */
59
- public findInEvents<T>(
60
- topicKey: PublicKey,
61
- processor: (event: ProgramEvent<IDL>, tx: ParsedTransactionWithMeta) => Promise<T>,
62
- abortSignal?: AbortSignal,
63
- logBatchSize?: number,
64
- startBlockheight?: number
65
- ): Promise<T> {
66
- return this.findInSignatures<T>(topicKey, async (data: {signatures?: ConfirmedSignatureInfo[], txs?: ParsedTransactionWithMeta[]}) => {
67
- if(data.signatures) {
68
- for(let info of data.signatures) {
69
- if(info.err) continue;
70
-
71
- const tx = await this.connection.getParsedTransaction(info.signature, {
72
- commitment: "confirmed",
73
- maxSupportedTransactionVersion: 0
74
- });
75
- if(tx.meta.err) continue;
76
-
77
- const events = this.parseLogs(tx.meta.logMessages);
78
- events.reverse();
79
-
80
- for(let event of events) {
81
- if(abortSignal!=null) abortSignal.throwIfAborted();
82
- const result: T = await processor(event, tx);
83
- if(result!=null) return result;
84
- }
85
- }
86
- } else {
87
- for(let tx of data.txs) {
88
- if(tx.meta.err) continue;
89
-
90
- const events = this.parseLogs(tx.meta.logMessages);
91
- events.reverse();
92
-
93
- for(let event of events) {
94
- if(abortSignal!=null) abortSignal.throwIfAborted();
95
- const result: T = await processor(event, tx);
96
- if(result!=null) return result;
97
- }
98
- }
99
- }
100
- }, abortSignal, logBatchSize, startBlockheight);
101
- }
102
-
103
- /**
104
- * Decodes the instructions for this program from the transaction, leaves null in the returned instructions array
105
- * for every instruction that doesn't correspond to this program (as those are impossible to parse)
106
- *
107
- * @param transactionMessage
108
- */
109
- public decodeInstructions(transactionMessage: ParsedMessage): InstructionWithAccounts<IDL>[] {
110
- const instructions: InstructionWithAccounts<IDL>[] = [];
111
-
112
- for(let _ix of transactionMessage.instructions) {
113
- if(!_ix.programId.equals(this.program.program.programId)) {
114
- instructions.push(null);
115
- continue;
116
- }
117
-
118
- const ix: PartiallyDecodedInstruction = _ix as PartiallyDecodedInstruction;
119
- if(ix.data==null) continue;
120
-
121
- const parsedIx: Instruction = this.programCoder.instruction.decode(ix.data, 'base58');
122
- const accountsData = this.nameMappedInstructions[parsedIx.name];
123
- let accounts: {[name: string]: PublicKey};
124
- if(accountsData!=null && accountsData.accounts!=null) {
125
- accounts = {};
126
- for(let i=0;i<accountsData.accounts.length;i++) {
127
- accounts[accountsData.accounts[i].name] = ix.accounts[i];
128
- }
129
- }
130
- instructions.push({
131
- name: parsedIx.name,
132
- data: parsedIx.data as any,
133
- accounts: accounts as any
134
- });
135
- }
136
-
137
- return instructions;
138
- }
139
-
140
- /**
141
- * Parses program event related to this program from transaction logs
142
- *
143
- * @param logs
144
- */
145
- public parseLogs(logs: string[]): ProgramEvent<IDL>[] {
146
- const eventsGenerator = this.eventParser.parseLogs(logs);
147
-
148
- const events: ProgramEvent<IDL>[] = [];
149
- for(let log of eventsGenerator) {
150
- events.push(log as ProgramEvent<IDL>);
151
- }
152
-
153
- return events;
154
- }
155
-
1
+ import {SolanaEvents} from "../../chain/modules/SolanaEvents";
2
+ import {BorshCoder, DecodeType, Event, EventParser, Idl, IdlTypes, Instruction} from "@coral-xyz/anchor";
3
+ import {IdlEvent, IdlField, IdlInstruction} from "@coral-xyz/anchor/dist/cjs/idl";
4
+ import {
5
+ ConfirmedSignatureInfo,
6
+ ParsedMessage, ParsedTransactionWithMeta,
7
+ PartiallyDecodedInstruction,
8
+ PublicKey,
9
+ VersionedTransaction, VersionedTransactionResponse
10
+ } from "@solana/web3.js";
11
+ import {SolanaProgramBase} from "../SolanaProgramBase";
12
+ import {SolanaChainInterface} from "../../chain/SolanaChainInterface";
13
+
14
+ type DecodedFieldOrNull<D, Defined> = D extends IdlField ? DecodeType<D["type"], Defined> : unknown;
15
+ type ArgsTuple<A extends IdlField[], Defined> = {
16
+ [K in A[number]["name"]]: DecodedFieldOrNull<Extract<A[number], { name: K }>, Defined>
17
+ };
18
+
19
+ export type InstructionWithAccounts<IDL extends Idl> = SingleInstructionWithAccounts<IDL["instructions"][number], IDL>;
20
+
21
+ export type SingleInstructionWithAccounts<I extends IdlInstruction, IDL extends Idl> = {
22
+ name: I["name"],
23
+ accounts: {
24
+ [key in I["accounts"][number]["name"]]: PublicKey
25
+ },
26
+ data: ArgsTuple<I["args"], IdlTypes<IDL>>
27
+ };
28
+
29
+ export type ProgramEvent<IDL extends Idl> = Event<NonNullable<IDL["events"]>[number], Record<string, any>>;
30
+
31
+ export class SolanaProgramEvents<IDL extends Idl> extends SolanaEvents {
32
+
33
+ private readonly programCoder: BorshCoder;
34
+ private readonly eventParser: EventParser;
35
+ private readonly program: SolanaProgramBase<IDL>;
36
+ private readonly nameMappedInstructions: {[name: string]: IdlInstruction};
37
+
38
+ constructor(chain: SolanaChainInterface, program: SolanaProgramBase<IDL>) {
39
+ super(chain);
40
+ this.program = program;
41
+ this.programCoder = new BorshCoder(program.program.idl);
42
+ this.eventParser = new EventParser(program.program.programId, this.programCoder);
43
+ this.nameMappedInstructions = {};
44
+ for(let ix of program.program.idl.instructions) {
45
+ this.nameMappedInstructions[ix.name] = ix;
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Runs a search backwards in time, processing the events for a specific topic public key
51
+ *
52
+ * @param topicKey
53
+ * @param processor called for every event, should return a value if the correct event was found, or null
54
+ * if the search should continue
55
+ * @param abortSignal
56
+ * @param logBatchSize how many signatures should be fetched in one getSignaturesForAddress call
57
+ * @param startBlockheight
58
+ */
59
+ public findInEvents<T>(
60
+ topicKey: PublicKey,
61
+ processor: (event: ProgramEvent<IDL>, tx: ParsedTransactionWithMeta) => Promise<T | null | undefined>,
62
+ abortSignal?: AbortSignal,
63
+ logBatchSize?: number,
64
+ startBlockheight?: number
65
+ ): Promise<T | null> {
66
+ return this.findInSignatures<T>(topicKey, async (data: {signatures?: ConfirmedSignatureInfo[], txs?: ParsedTransactionWithMeta[]}) => {
67
+ if(data.signatures) {
68
+ for(let info of data.signatures) {
69
+ if(info.err) continue;
70
+
71
+ const tx = await this.connection.getParsedTransaction(info.signature, {
72
+ commitment: "confirmed",
73
+ maxSupportedTransactionVersion: 0
74
+ });
75
+ if(tx==null || tx.meta==null || tx.meta.err || tx.meta.logMessages==null) continue;
76
+
77
+ const events = this.parseLogs(tx.meta.logMessages);
78
+ events.reverse();
79
+
80
+ for(let event of events) {
81
+ if(abortSignal!=null) abortSignal.throwIfAborted();
82
+ const result: T | undefined | null = await processor(event, tx);
83
+ if(result!=null) return result;
84
+ }
85
+ }
86
+ } else if(data.txs) {
87
+ for(let tx of data.txs) {
88
+ if(tx.meta==null || tx.meta.err || tx.meta.logMessages==null) continue;
89
+
90
+ const events = this.parseLogs(tx.meta.logMessages);
91
+ events.reverse();
92
+
93
+ for(let event of events) {
94
+ if(abortSignal!=null) abortSignal.throwIfAborted();
95
+ const result: T | undefined | null = await processor(event, tx);
96
+ if(result!=null) return result;
97
+ }
98
+ }
99
+ }
100
+ return null;
101
+ }, abortSignal, logBatchSize, startBlockheight);
102
+ }
103
+
104
+ /**
105
+ * Decodes the instructions for this program from the transaction, leaves null in the returned instructions array
106
+ * for every instruction that doesn't correspond to this program (as those are impossible to parse)
107
+ *
108
+ * @param transactionMessage
109
+ */
110
+ public decodeInstructions(transactionMessage: ParsedMessage): (InstructionWithAccounts<IDL> | null)[] {
111
+ const instructions: (InstructionWithAccounts<IDL> | null)[] = [];
112
+
113
+ for(let _ix of transactionMessage.instructions) {
114
+ if(!_ix.programId.equals(this.program.program.programId)) {
115
+ instructions.push(null);
116
+ continue;
117
+ }
118
+
119
+ const ix: PartiallyDecodedInstruction = _ix as PartiallyDecodedInstruction;
120
+ if(ix.data==null) continue;
121
+
122
+ const parsedIx: Instruction | null = this.programCoder.instruction.decode(ix.data, 'base58');
123
+ if(parsedIx==null) throw new Error(`Failed to decode transaction instruction: ${ix.data}!`);
124
+ const accountsData = this.nameMappedInstructions[parsedIx.name];
125
+ let accounts: {[name: string]: PublicKey} | null = null;
126
+ if(accountsData!=null && accountsData.accounts!=null) {
127
+ accounts = {};
128
+ for(let i=0;i<accountsData.accounts.length;i++) {
129
+ accounts[accountsData.accounts[i].name] = ix.accounts[i];
130
+ }
131
+ }
132
+ instructions.push({
133
+ name: parsedIx.name,
134
+ data: parsedIx.data as any,
135
+ accounts: accounts as any
136
+ });
137
+ }
138
+
139
+ return instructions;
140
+ }
141
+
142
+ /**
143
+ * Parses program event related to this program from transaction logs
144
+ *
145
+ * @param logs
146
+ */
147
+ public parseLogs(logs: string[]): ProgramEvent<IDL>[] {
148
+ const eventsGenerator = this.eventParser.parseLogs(logs);
149
+
150
+ const events: ProgramEvent<IDL>[] = [];
151
+ for(let log of eventsGenerator) {
152
+ events.push(log as ProgramEvent<IDL>);
153
+ }
154
+
155
+ return events;
156
+ }
157
+
156
158
  }