@atomiqlabs/chain-solana 12.0.12 → 12.0.13

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 +29 -29
  3. package/dist/index.js +45 -45
  4. package/dist/solana/SolanaChainType.d.ts +11 -11
  5. package/dist/solana/SolanaChainType.js +2 -2
  6. package/dist/solana/SolanaChains.d.ts +20 -20
  7. package/dist/solana/SolanaChains.js +25 -25
  8. package/dist/solana/SolanaInitializer.d.ts +18 -18
  9. package/dist/solana/SolanaInitializer.js +63 -63
  10. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
  11. package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
  12. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
  13. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
  14. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
  15. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -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 +65 -65
  20. package/dist/solana/chain/SolanaChainInterface.js +125 -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 +28 -28
  26. package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
  27. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  28. package/dist/solana/chain/modules/SolanaEvents.js +225 -225
  29. package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
  30. package/dist/solana/chain/modules/SolanaFees.js +379 -379
  31. package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -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 +68 -68
  35. package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
  36. package/dist/solana/chain/modules/SolanaTokens.js +248 -248
  37. package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
  38. package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
  39. package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
  40. package/dist/solana/events/SolanaChainEvents.js +256 -256
  41. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
  43. package/dist/solana/program/SolanaProgramBase.d.ts +40 -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 +114 -114
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
  50. package/dist/solana/swaps/SolanaSwapData.js +292 -292
  51. package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
  52. package/dist/solana/swaps/SolanaSwapModule.js +11 -11
  53. package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
  54. package/dist/solana/swaps/SolanaSwapProgram.js +570 -570
  55. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  56. package/dist/solana/swaps/SwapTypeEnum.js +42 -42
  57. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
  58. package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
  59. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
  60. package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
  61. package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
  62. package/dist/solana/swaps/modules/SwapClaim.js +291 -291
  63. package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
  64. package/dist/solana/swaps/modules/SwapInit.js +519 -519
  65. package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
  66. package/dist/solana/swaps/modules/SwapRefund.js +262 -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 +9 -9
  71. package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
  72. package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
  73. package/dist/solana/wallet/SolanaSigner.js +17 -17
  74. package/dist/utils/Utils.d.ts +53 -53
  75. package/dist/utils/Utils.js +170 -170
  76. package/package.json +41 -41
  77. package/src/index.ts +36 -36
  78. package/src/solana/SolanaChainType.ts +27 -27
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +102 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
  82. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
  83. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -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 +192 -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 +78 -78
  90. package/src/solana/chain/modules/SolanaEvents.ts +256 -256
  91. package/src/solana/chain/modules/SolanaFees.ts +450 -450
  92. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  93. package/src/solana/chain/modules/SolanaSlots.ts +82 -82
  94. package/src/solana/chain/modules/SolanaTokens.ts +307 -307
  95. package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
  96. package/src/solana/events/SolanaChainEvents.ts +299 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +209 -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 +155 -155
  101. package/src/solana/swaps/SolanaSwapData.ts +430 -430
  102. package/src/solana/swaps/SolanaSwapModule.ts +16 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +854 -854
  104. package/src/solana/swaps/SwapTypeEnum.ts +29 -29
  105. package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
  106. package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
  107. package/src/solana/swaps/modules/SwapClaim.ts +389 -389
  108. package/src/solana/swaps/modules/SwapInit.ts +663 -663
  109. package/src/solana/swaps/modules/SwapRefund.ts +323 -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 +36 -36
  113. package/src/solana/wallet/SolanaSigner.ts +24 -24
  114. package/src/utils/Utils.ts +180 -180
@@ -1,430 +1,430 @@
1
- import {PublicKey} from "@solana/web3.js";
2
- import * as BN from "bn.js";
3
- import {ChainSwapType, SwapData} from "@atomiqlabs/base";
4
- import {SwapProgram} from "./programTypes";
5
- import {IdlAccounts, IdlTypes} from "@coral-xyz/anchor";
6
- import {SwapTypeEnum} from "./SwapTypeEnum";
7
- import {Buffer} from "buffer";
8
- import {getAssociatedTokenAddressSync} from "@solana/spl-token";
9
- import {toBigInt, toClaimHash, toEscrowHash} from "../../utils/Utils";
10
- import {SolanaTokens} from "../chain/modules/SolanaTokens";
11
- import {SingleInstructionWithAccounts} from "../program/modules/SolanaProgramEvents";
12
-
13
- export type InitInstruction = SingleInstructionWithAccounts<SwapProgram["instructions"][2 | 3], SwapProgram>;
14
-
15
- const EXPIRY_BLOCKHEIGHT_THRESHOLD = new BN("1000000000");
16
-
17
- export class SolanaSwapData extends SwapData {
18
-
19
- offerer: PublicKey;
20
- claimer: PublicKey;
21
- token: PublicKey;
22
- amount: BN;
23
- paymentHash: string;
24
- sequence: BN;
25
- expiry: BN;
26
- nonce: BN;
27
- confirmations: number;
28
- payOut: boolean;
29
- kind: number;
30
- payIn: boolean;
31
- claimerAta?: PublicKey;
32
- offererAta?: PublicKey;
33
-
34
- securityDeposit: BN;
35
- claimerBounty: BN;
36
-
37
- txoHash: string;
38
-
39
- constructor(
40
- offerer: PublicKey,
41
- claimer: PublicKey,
42
- token: PublicKey,
43
- amount: BN,
44
- paymentHash: string,
45
- sequence: BN,
46
- expiry: BN,
47
-
48
- nonce: BN,
49
- confirmations: number,
50
- payOut: boolean,
51
- kind: number,
52
- payIn: boolean,
53
- offererAta: PublicKey,
54
- claimerAta: PublicKey,
55
-
56
- securityDeposit: BN,
57
- claimerBounty: BN,
58
-
59
- txoHash: string
60
- );
61
-
62
- constructor(data: any);
63
-
64
- constructor(
65
- offererOrData: PublicKey | any,
66
- claimer?: PublicKey,
67
- token?: PublicKey,
68
- amount?: BN,
69
- paymentHash?: string,
70
- sequence?: BN,
71
- expiry?: BN,
72
- nonce?: BN,
73
- confirmations?: number,
74
- payOut?: boolean,
75
- kind?: number,
76
- payIn?: boolean,
77
- offererAta?: PublicKey,
78
- claimerAta?: PublicKey,
79
- securityDeposit?: BN,
80
- claimerBounty?: BN,
81
- txoHash?: string,
82
- ) {
83
- super();
84
- if(claimer!=null || token!=null || amount!=null || paymentHash!=null || expiry!=null ||
85
- nonce!=null || confirmations!=null || payOut!=null || kind!=null || payIn!=null || claimerAta!=null) {
86
-
87
- this.offerer = offererOrData;
88
- this.claimer = claimer;
89
- this.token = token;
90
- this.amount = amount;
91
- this.paymentHash = paymentHash;
92
- this.sequence = sequence;
93
- this.expiry = expiry;
94
- this.nonce = nonce;
95
- this.confirmations = confirmations;
96
- this.payOut = payOut;
97
- this.kind = kind;
98
- this.payIn = payIn;
99
- this.claimerAta = claimerAta;
100
- this.offererAta = offererAta;
101
- this.securityDeposit = securityDeposit;
102
- this.claimerBounty = claimerBounty;
103
- this.txoHash = txoHash;
104
- } else {
105
- this.offerer = offererOrData.offerer==null ? null : new PublicKey(offererOrData.offerer);
106
- this.claimer = offererOrData.claimer==null ? null : new PublicKey(offererOrData.claimer);
107
- this.token = offererOrData.token==null ? null : new PublicKey(offererOrData.token);
108
- this.amount = offererOrData.amount==null ? null : new BN(offererOrData.amount);
109
- this.paymentHash = offererOrData.paymentHash;
110
- this.sequence = offererOrData.sequence==null ? null : new BN(offererOrData.sequence);
111
- this.expiry = offererOrData.expiry==null ? null : new BN(offererOrData.expiry);
112
- this.nonce = offererOrData.nonce==null ? null : new BN(offererOrData.nonce);
113
- this.confirmations = offererOrData.confirmations;
114
- this.payOut = offererOrData.payOut;
115
- this.kind = offererOrData.kind;
116
- this.payIn = offererOrData.payIn;
117
- this.claimerAta = offererOrData.claimerAta==null ? null : new PublicKey(offererOrData.claimerAta);
118
- this.offererAta = offererOrData.offererAta==null ? null : new PublicKey(offererOrData.offererAta);
119
- this.securityDeposit = offererOrData.securityDeposit==null ? null : new BN(offererOrData.securityDeposit);
120
- this.claimerBounty = offererOrData.claimerBounty==null ? null : new BN(offererOrData.claimerBounty);
121
- this.txoHash = offererOrData.txoHash;
122
- }
123
- }
124
-
125
- getOfferer(): string {
126
- return this.offerer.toBase58();
127
- }
128
-
129
- setOfferer(newOfferer: string) {
130
- this.offerer = new PublicKey(newOfferer);
131
- this.offererAta = getAssociatedTokenAddressSync(this.token, this.offerer);
132
- this.payIn = true;
133
- }
134
-
135
- getClaimer(): string {
136
- return this.claimer.toBase58();
137
- }
138
-
139
- setClaimer(newClaimer: string) {
140
- this.claimer = new PublicKey(newClaimer);
141
- this.payIn = false;
142
- this.payOut = true;
143
- this.claimerAta = getAssociatedTokenAddressSync(this.token, this.claimer);
144
- }
145
-
146
- serialize(): any {
147
- return {
148
- type: "sol",
149
- offerer: this.offerer==null ? null : this.offerer.toBase58(),
150
- claimer: this.claimer==null ? null : this.claimer.toBase58(),
151
- token: this.token==null ? null : this.token.toBase58(),
152
- amount: this.amount==null ? null : this.amount.toString(10),
153
- paymentHash: this.paymentHash,
154
- sequence: this.sequence==null ? null : this.sequence.toString(10),
155
- expiry: this.expiry==null ? null : this.expiry.toString(10),
156
- nonce: this.nonce==null ? null : this.nonce.toString(10),
157
- confirmations: this.confirmations,
158
- payOut: this.payOut,
159
- kind: this.kind,
160
- payIn: this.payIn,
161
- offererAta: this.offererAta==null ? null : this.offererAta.toBase58(),
162
- claimerAta: this.claimerAta==null ? null : this.claimerAta.toBase58(),
163
- securityDeposit: this.securityDeposit==null ? null : this.securityDeposit.toString(10),
164
- claimerBounty: this.claimerBounty==null ? null : this.claimerBounty.toString(10),
165
- txoHash: this.txoHash
166
- }
167
- }
168
-
169
- getAmount(): bigint {
170
- return toBigInt(this.amount);
171
- }
172
-
173
- getToken(): string {
174
- return this.token.toString();
175
- }
176
-
177
- isToken(token: string): boolean {
178
- return this.token.equals(new PublicKey(token));
179
- }
180
-
181
- getType(): ChainSwapType {
182
- return SolanaSwapData.kindToType(this.kind);
183
- }
184
-
185
- getExpiry(): bigint {
186
- if(this.expiry.lt(EXPIRY_BLOCKHEIGHT_THRESHOLD)) return null;
187
- return toBigInt(this.expiry);
188
- }
189
-
190
- getConfirmationsHint(): number {
191
- return this.confirmations;
192
- }
193
-
194
- getNonceHint(): bigint {
195
- return toBigInt(this.nonce);
196
- }
197
-
198
- isPayIn(): boolean {
199
- return this.payIn;
200
- }
201
-
202
- isPayOut(): boolean {
203
- return this.payOut;
204
- }
205
-
206
- getClaimHash(): string {
207
- return toClaimHash(this.paymentHash, toBigInt(this.nonce), this.confirmations);
208
- }
209
-
210
- getEscrowHash(): string {
211
- return toEscrowHash(this.paymentHash, this.sequence);
212
- }
213
-
214
- getSequence(): bigint {
215
- return toBigInt(this.sequence);
216
- }
217
-
218
- getTxoHashHint(): string {
219
- if(this.txoHash==="0000000000000000000000000000000000000000000000000000000000000000") return null; //Txo hash opt-out flag
220
- return this.txoHash;
221
- }
222
-
223
- getHTLCHashHint(): string {
224
- if(this.getType()===ChainSwapType.HTLC) return this.paymentHash;
225
- return null;
226
- }
227
-
228
- getExtraData(): string {
229
- return this.txoHash;
230
- }
231
-
232
- setExtraData(txoHash: string): void {
233
- this.txoHash = txoHash;
234
- }
235
-
236
- getSecurityDeposit(): bigint {
237
- return toBigInt(this.securityDeposit);
238
- }
239
-
240
- getClaimerBounty(): bigint {
241
- return toBigInt(this.claimerBounty);
242
- }
243
-
244
- getTotalDeposit(): bigint {
245
- return toBigInt(this.claimerBounty.lt(this.securityDeposit) ? this.securityDeposit : this.claimerBounty);
246
- }
247
-
248
- toSwapDataStruct(): IdlTypes<SwapProgram>["SwapData"] {
249
- return {
250
- kind: SwapTypeEnum.fromNumber(this.kind as 0 | 1 | 2 | 3),
251
- confirmations: this.confirmations,
252
- nonce: this.nonce,
253
- hash: [...Buffer.from(this.paymentHash, "hex")],
254
- payIn: this.payIn,
255
- payOut: this.payOut,
256
- amount: this.amount,
257
- expiry: this.expiry,
258
- sequence: this.sequence
259
- }
260
- }
261
-
262
- correctPDA(account: IdlAccounts<SwapProgram>["escrowState"]): boolean {
263
- return SwapTypeEnum.toNumber(account.data.kind)===this.kind &&
264
- account.data.confirmations===this.confirmations &&
265
- this.nonce.eq(account.data.nonce) &&
266
- Buffer.from(account.data.hash).toString("hex")===this.paymentHash &&
267
- account.data.payIn===this.payIn &&
268
- account.data.payOut===this.payOut &&
269
- this.amount.eq(account.data.amount) &&
270
- this.expiry.eq(account.data.expiry) &&
271
- this.sequence.eq(account.data.sequence) &&
272
-
273
- account.offerer.equals(this.offerer) &&
274
- (this.offererAta==null || account.offererAta.equals(this.offererAta)) &&
275
- account.claimer.equals(this.claimer) &&
276
- (this.claimerAta==null || account.claimerAta.equals(this.claimerAta)) &&
277
- account.mint.equals(this.token) &&
278
- this.claimerBounty.eq(account.claimerBounty) &&
279
- this.securityDeposit.eq(account.securityDeposit);
280
- }
281
-
282
- equals(other: SolanaSwapData): boolean {
283
- if(this.claimerAta==null && other.claimerAta!=null) return false;
284
- if(this.claimerAta!=null && other.claimerAta==null) return false;
285
- if(this.claimerAta!=null && other.claimerAta!=null) {
286
- if(!this.claimerAta.equals(other.claimerAta)) return false;
287
- }
288
-
289
- if(this.offererAta==null && other.offererAta!=null) return false;
290
- if(this.offererAta!=null && other.offererAta==null) return false;
291
- if(this.offererAta!=null && other.offererAta!=null) {
292
- if(!this.offererAta.equals(other.offererAta)) return false;
293
- }
294
-
295
- return other.kind===this.kind &&
296
- other.confirmations===this.confirmations &&
297
- this.nonce.eq(other.nonce) &&
298
- other.paymentHash===this.paymentHash &&
299
- this.sequence.eq(other.sequence) &&
300
- other.payIn===this.payIn &&
301
- other.payOut===this.payOut &&
302
- other.offerer.equals(this.offerer) &&
303
- other.claimer.equals(this.claimer) &&
304
- other.expiry.eq(this.expiry) &&
305
- other.amount.eq(this.amount) &&
306
- other.securityDeposit.eq(this.securityDeposit) &&
307
- other.claimerBounty.eq(this.claimerBounty) &&
308
- other.token.equals(this.token)
309
- }
310
-
311
- /**
312
- * Converts initialize instruction data into {SolanaSwapData}
313
- *
314
- * @param initIx
315
- * @param txoHash
316
- * @private
317
- * @returns {SolanaSwapData} converted and parsed swap data
318
- */
319
- static fromInstruction(
320
- initIx: InitInstruction,
321
- txoHash: string
322
- ): SolanaSwapData {
323
- const paymentHash: Buffer = Buffer.from(initIx.data.swapData.hash);
324
- let securityDeposit: BN = new BN(0);
325
- let claimerBounty: BN = new BN(0);
326
- let payIn: boolean = true;
327
- if(initIx.name === "offererInitialize") {
328
- payIn = false;
329
- securityDeposit = initIx.data.securityDeposit;
330
- claimerBounty = initIx.data.claimerBounty;
331
- }
332
-
333
- return new SolanaSwapData(
334
- initIx.accounts.offerer,
335
- initIx.accounts.claimer,
336
- initIx.accounts.mint,
337
- initIx.data.swapData.amount,
338
- paymentHash.toString("hex"),
339
- initIx.data.swapData.sequence,
340
- initIx.data.swapData.expiry,
341
- initIx.data.swapData.nonce,
342
- initIx.data.swapData.confirmations,
343
- initIx.data.swapData.payOut,
344
- SwapTypeEnum.toNumber(initIx.data.swapData.kind),
345
- payIn,
346
- initIx.name === "offererInitializePayIn" ? initIx.accounts.offererAta : PublicKey.default,
347
- initIx.data.swapData.payOut ? initIx.accounts.claimerAta : PublicKey.default,
348
- securityDeposit,
349
- claimerBounty,
350
- txoHash
351
- );
352
- }
353
-
354
- static fromEscrowState(account: IdlAccounts<SwapProgram>["escrowState"]) {
355
- const data: IdlTypes<SwapProgram>["SwapData"] = account.data;
356
-
357
- return new SolanaSwapData(
358
- account.offerer,
359
- account.claimer,
360
- account.mint,
361
- data.amount,
362
- Buffer.from(data.hash).toString("hex"),
363
- data.sequence,
364
- data.expiry,
365
- data.nonce,
366
- data.confirmations,
367
- data.payOut,
368
- SwapTypeEnum.toNumber(data.kind),
369
- data.payIn,
370
- account.offererAta,
371
- account.claimerAta,
372
- account.securityDeposit,
373
- account.claimerBounty,
374
- null
375
- );
376
- }
377
-
378
- static typeToKind(type: ChainSwapType): number {
379
- switch (type) {
380
- case ChainSwapType.HTLC:
381
- return 0;
382
- case ChainSwapType.CHAIN:
383
- return 1;
384
- case ChainSwapType.CHAIN_NONCED:
385
- return 2;
386
- case ChainSwapType.CHAIN_TXID:
387
- return 3;
388
- }
389
- return null;
390
- }
391
-
392
- static kindToType(value: number): ChainSwapType {
393
- switch(value) {
394
- case 0:
395
- return ChainSwapType.HTLC;
396
- case 1:
397
- return ChainSwapType.CHAIN;
398
- case 2:
399
- return ChainSwapType.CHAIN_NONCED;
400
- case 3:
401
- return ChainSwapType.CHAIN_TXID;
402
- }
403
- return null;
404
- }
405
-
406
- isClaimer(address: string) {
407
- const _address = new PublicKey(address);
408
- if(this.isPayOut()) {
409
- //Also check that swapData's ATA is correct
410
- const ourAta = getAssociatedTokenAddressSync(this.token, _address);
411
- if(!this.claimerAta.equals(ourAta)) return false;
412
- }
413
- return this.claimer.equals(new PublicKey(address));
414
- }
415
-
416
- isOfferer(address: string) {
417
- return this.offerer.equals(new PublicKey(address));
418
- }
419
-
420
- getDepositToken(): string {
421
- return SolanaTokens.WSOL_ADDRESS.toString();
422
- }
423
-
424
- isDepositToken(token: string): boolean {
425
- return SolanaTokens.WSOL_ADDRESS.equals(new PublicKey(token));
426
- }
427
-
428
- }
429
-
430
- SwapData.deserializers["sol"] = SolanaSwapData;
1
+ import {PublicKey} from "@solana/web3.js";
2
+ import * as BN from "bn.js";
3
+ import {ChainSwapType, SwapData} from "@atomiqlabs/base";
4
+ import {SwapProgram} from "./programTypes";
5
+ import {IdlAccounts, IdlTypes} from "@coral-xyz/anchor";
6
+ import {SwapTypeEnum} from "./SwapTypeEnum";
7
+ import {Buffer} from "buffer";
8
+ import {getAssociatedTokenAddressSync} from "@solana/spl-token";
9
+ import {toBigInt, toClaimHash, toEscrowHash} from "../../utils/Utils";
10
+ import {SolanaTokens} from "../chain/modules/SolanaTokens";
11
+ import {SingleInstructionWithAccounts} from "../program/modules/SolanaProgramEvents";
12
+
13
+ export type InitInstruction = SingleInstructionWithAccounts<SwapProgram["instructions"][2 | 3], SwapProgram>;
14
+
15
+ const EXPIRY_BLOCKHEIGHT_THRESHOLD = new BN("1000000000");
16
+
17
+ export class SolanaSwapData extends SwapData {
18
+
19
+ offerer: PublicKey;
20
+ claimer: PublicKey;
21
+ token: PublicKey;
22
+ amount: BN;
23
+ paymentHash: string;
24
+ sequence: BN;
25
+ expiry: BN;
26
+ nonce: BN;
27
+ confirmations: number;
28
+ payOut: boolean;
29
+ kind: number;
30
+ payIn: boolean;
31
+ claimerAta?: PublicKey;
32
+ offererAta?: PublicKey;
33
+
34
+ securityDeposit: BN;
35
+ claimerBounty: BN;
36
+
37
+ txoHash: string;
38
+
39
+ constructor(
40
+ offerer: PublicKey,
41
+ claimer: PublicKey,
42
+ token: PublicKey,
43
+ amount: BN,
44
+ paymentHash: string,
45
+ sequence: BN,
46
+ expiry: BN,
47
+
48
+ nonce: BN,
49
+ confirmations: number,
50
+ payOut: boolean,
51
+ kind: number,
52
+ payIn: boolean,
53
+ offererAta: PublicKey,
54
+ claimerAta: PublicKey,
55
+
56
+ securityDeposit: BN,
57
+ claimerBounty: BN,
58
+
59
+ txoHash: string
60
+ );
61
+
62
+ constructor(data: any);
63
+
64
+ constructor(
65
+ offererOrData: PublicKey | any,
66
+ claimer?: PublicKey,
67
+ token?: PublicKey,
68
+ amount?: BN,
69
+ paymentHash?: string,
70
+ sequence?: BN,
71
+ expiry?: BN,
72
+ nonce?: BN,
73
+ confirmations?: number,
74
+ payOut?: boolean,
75
+ kind?: number,
76
+ payIn?: boolean,
77
+ offererAta?: PublicKey,
78
+ claimerAta?: PublicKey,
79
+ securityDeposit?: BN,
80
+ claimerBounty?: BN,
81
+ txoHash?: string,
82
+ ) {
83
+ super();
84
+ if(claimer!=null || token!=null || amount!=null || paymentHash!=null || expiry!=null ||
85
+ nonce!=null || confirmations!=null || payOut!=null || kind!=null || payIn!=null || claimerAta!=null) {
86
+
87
+ this.offerer = offererOrData;
88
+ this.claimer = claimer;
89
+ this.token = token;
90
+ this.amount = amount;
91
+ this.paymentHash = paymentHash;
92
+ this.sequence = sequence;
93
+ this.expiry = expiry;
94
+ this.nonce = nonce;
95
+ this.confirmations = confirmations;
96
+ this.payOut = payOut;
97
+ this.kind = kind;
98
+ this.payIn = payIn;
99
+ this.claimerAta = claimerAta;
100
+ this.offererAta = offererAta;
101
+ this.securityDeposit = securityDeposit;
102
+ this.claimerBounty = claimerBounty;
103
+ this.txoHash = txoHash;
104
+ } else {
105
+ this.offerer = offererOrData.offerer==null ? null : new PublicKey(offererOrData.offerer);
106
+ this.claimer = offererOrData.claimer==null ? null : new PublicKey(offererOrData.claimer);
107
+ this.token = offererOrData.token==null ? null : new PublicKey(offererOrData.token);
108
+ this.amount = offererOrData.amount==null ? null : new BN(offererOrData.amount);
109
+ this.paymentHash = offererOrData.paymentHash;
110
+ this.sequence = offererOrData.sequence==null ? null : new BN(offererOrData.sequence);
111
+ this.expiry = offererOrData.expiry==null ? null : new BN(offererOrData.expiry);
112
+ this.nonce = offererOrData.nonce==null ? null : new BN(offererOrData.nonce);
113
+ this.confirmations = offererOrData.confirmations;
114
+ this.payOut = offererOrData.payOut;
115
+ this.kind = offererOrData.kind;
116
+ this.payIn = offererOrData.payIn;
117
+ this.claimerAta = offererOrData.claimerAta==null ? null : new PublicKey(offererOrData.claimerAta);
118
+ this.offererAta = offererOrData.offererAta==null ? null : new PublicKey(offererOrData.offererAta);
119
+ this.securityDeposit = offererOrData.securityDeposit==null ? null : new BN(offererOrData.securityDeposit);
120
+ this.claimerBounty = offererOrData.claimerBounty==null ? null : new BN(offererOrData.claimerBounty);
121
+ this.txoHash = offererOrData.txoHash;
122
+ }
123
+ }
124
+
125
+ getOfferer(): string {
126
+ return this.offerer.toBase58();
127
+ }
128
+
129
+ setOfferer(newOfferer: string) {
130
+ this.offerer = new PublicKey(newOfferer);
131
+ this.offererAta = getAssociatedTokenAddressSync(this.token, this.offerer);
132
+ this.payIn = true;
133
+ }
134
+
135
+ getClaimer(): string {
136
+ return this.claimer.toBase58();
137
+ }
138
+
139
+ setClaimer(newClaimer: string) {
140
+ this.claimer = new PublicKey(newClaimer);
141
+ this.payIn = false;
142
+ this.payOut = true;
143
+ this.claimerAta = getAssociatedTokenAddressSync(this.token, this.claimer);
144
+ }
145
+
146
+ serialize(): any {
147
+ return {
148
+ type: "sol",
149
+ offerer: this.offerer==null ? null : this.offerer.toBase58(),
150
+ claimer: this.claimer==null ? null : this.claimer.toBase58(),
151
+ token: this.token==null ? null : this.token.toBase58(),
152
+ amount: this.amount==null ? null : this.amount.toString(10),
153
+ paymentHash: this.paymentHash,
154
+ sequence: this.sequence==null ? null : this.sequence.toString(10),
155
+ expiry: this.expiry==null ? null : this.expiry.toString(10),
156
+ nonce: this.nonce==null ? null : this.nonce.toString(10),
157
+ confirmations: this.confirmations,
158
+ payOut: this.payOut,
159
+ kind: this.kind,
160
+ payIn: this.payIn,
161
+ offererAta: this.offererAta==null ? null : this.offererAta.toBase58(),
162
+ claimerAta: this.claimerAta==null ? null : this.claimerAta.toBase58(),
163
+ securityDeposit: this.securityDeposit==null ? null : this.securityDeposit.toString(10),
164
+ claimerBounty: this.claimerBounty==null ? null : this.claimerBounty.toString(10),
165
+ txoHash: this.txoHash
166
+ }
167
+ }
168
+
169
+ getAmount(): bigint {
170
+ return toBigInt(this.amount);
171
+ }
172
+
173
+ getToken(): string {
174
+ return this.token.toString();
175
+ }
176
+
177
+ isToken(token: string): boolean {
178
+ return this.token.equals(new PublicKey(token));
179
+ }
180
+
181
+ getType(): ChainSwapType {
182
+ return SolanaSwapData.kindToType(this.kind);
183
+ }
184
+
185
+ getExpiry(): bigint {
186
+ if(this.expiry.lt(EXPIRY_BLOCKHEIGHT_THRESHOLD)) return null;
187
+ return toBigInt(this.expiry);
188
+ }
189
+
190
+ getConfirmationsHint(): number {
191
+ return this.confirmations;
192
+ }
193
+
194
+ getNonceHint(): bigint {
195
+ return toBigInt(this.nonce);
196
+ }
197
+
198
+ isPayIn(): boolean {
199
+ return this.payIn;
200
+ }
201
+
202
+ isPayOut(): boolean {
203
+ return this.payOut;
204
+ }
205
+
206
+ getClaimHash(): string {
207
+ return toClaimHash(this.paymentHash, toBigInt(this.nonce), this.confirmations);
208
+ }
209
+
210
+ getEscrowHash(): string {
211
+ return toEscrowHash(this.paymentHash, this.sequence);
212
+ }
213
+
214
+ getSequence(): bigint {
215
+ return toBigInt(this.sequence);
216
+ }
217
+
218
+ getTxoHashHint(): string {
219
+ if(this.txoHash==="0000000000000000000000000000000000000000000000000000000000000000") return null; //Txo hash opt-out flag
220
+ return this.txoHash;
221
+ }
222
+
223
+ getHTLCHashHint(): string {
224
+ if(this.getType()===ChainSwapType.HTLC) return this.paymentHash;
225
+ return null;
226
+ }
227
+
228
+ getExtraData(): string {
229
+ return this.txoHash;
230
+ }
231
+
232
+ setExtraData(txoHash: string): void {
233
+ this.txoHash = txoHash;
234
+ }
235
+
236
+ getSecurityDeposit(): bigint {
237
+ return toBigInt(this.securityDeposit);
238
+ }
239
+
240
+ getClaimerBounty(): bigint {
241
+ return toBigInt(this.claimerBounty);
242
+ }
243
+
244
+ getTotalDeposit(): bigint {
245
+ return toBigInt(this.claimerBounty.lt(this.securityDeposit) ? this.securityDeposit : this.claimerBounty);
246
+ }
247
+
248
+ toSwapDataStruct(): IdlTypes<SwapProgram>["SwapData"] {
249
+ return {
250
+ kind: SwapTypeEnum.fromNumber(this.kind as 0 | 1 | 2 | 3),
251
+ confirmations: this.confirmations,
252
+ nonce: this.nonce,
253
+ hash: [...Buffer.from(this.paymentHash, "hex")],
254
+ payIn: this.payIn,
255
+ payOut: this.payOut,
256
+ amount: this.amount,
257
+ expiry: this.expiry,
258
+ sequence: this.sequence
259
+ }
260
+ }
261
+
262
+ correctPDA(account: IdlAccounts<SwapProgram>["escrowState"]): boolean {
263
+ return SwapTypeEnum.toNumber(account.data.kind)===this.kind &&
264
+ account.data.confirmations===this.confirmations &&
265
+ this.nonce.eq(account.data.nonce) &&
266
+ Buffer.from(account.data.hash).toString("hex")===this.paymentHash &&
267
+ account.data.payIn===this.payIn &&
268
+ account.data.payOut===this.payOut &&
269
+ this.amount.eq(account.data.amount) &&
270
+ this.expiry.eq(account.data.expiry) &&
271
+ this.sequence.eq(account.data.sequence) &&
272
+
273
+ account.offerer.equals(this.offerer) &&
274
+ (this.offererAta==null || account.offererAta.equals(this.offererAta)) &&
275
+ account.claimer.equals(this.claimer) &&
276
+ (this.claimerAta==null || account.claimerAta.equals(this.claimerAta)) &&
277
+ account.mint.equals(this.token) &&
278
+ this.claimerBounty.eq(account.claimerBounty) &&
279
+ this.securityDeposit.eq(account.securityDeposit);
280
+ }
281
+
282
+ equals(other: SolanaSwapData): boolean {
283
+ if(this.claimerAta==null && other.claimerAta!=null) return false;
284
+ if(this.claimerAta!=null && other.claimerAta==null) return false;
285
+ if(this.claimerAta!=null && other.claimerAta!=null) {
286
+ if(!this.claimerAta.equals(other.claimerAta)) return false;
287
+ }
288
+
289
+ if(this.offererAta==null && other.offererAta!=null) return false;
290
+ if(this.offererAta!=null && other.offererAta==null) return false;
291
+ if(this.offererAta!=null && other.offererAta!=null) {
292
+ if(!this.offererAta.equals(other.offererAta)) return false;
293
+ }
294
+
295
+ return other.kind===this.kind &&
296
+ other.confirmations===this.confirmations &&
297
+ this.nonce.eq(other.nonce) &&
298
+ other.paymentHash===this.paymentHash &&
299
+ this.sequence.eq(other.sequence) &&
300
+ other.payIn===this.payIn &&
301
+ other.payOut===this.payOut &&
302
+ other.offerer.equals(this.offerer) &&
303
+ other.claimer.equals(this.claimer) &&
304
+ other.expiry.eq(this.expiry) &&
305
+ other.amount.eq(this.amount) &&
306
+ other.securityDeposit.eq(this.securityDeposit) &&
307
+ other.claimerBounty.eq(this.claimerBounty) &&
308
+ other.token.equals(this.token)
309
+ }
310
+
311
+ /**
312
+ * Converts initialize instruction data into {SolanaSwapData}
313
+ *
314
+ * @param initIx
315
+ * @param txoHash
316
+ * @private
317
+ * @returns {SolanaSwapData} converted and parsed swap data
318
+ */
319
+ static fromInstruction(
320
+ initIx: InitInstruction,
321
+ txoHash: string
322
+ ): SolanaSwapData {
323
+ const paymentHash: Buffer = Buffer.from(initIx.data.swapData.hash);
324
+ let securityDeposit: BN = new BN(0);
325
+ let claimerBounty: BN = new BN(0);
326
+ let payIn: boolean = true;
327
+ if(initIx.name === "offererInitialize") {
328
+ payIn = false;
329
+ securityDeposit = initIx.data.securityDeposit;
330
+ claimerBounty = initIx.data.claimerBounty;
331
+ }
332
+
333
+ return new SolanaSwapData(
334
+ initIx.accounts.offerer,
335
+ initIx.accounts.claimer,
336
+ initIx.accounts.mint,
337
+ initIx.data.swapData.amount,
338
+ paymentHash.toString("hex"),
339
+ initIx.data.swapData.sequence,
340
+ initIx.data.swapData.expiry,
341
+ initIx.data.swapData.nonce,
342
+ initIx.data.swapData.confirmations,
343
+ initIx.data.swapData.payOut,
344
+ SwapTypeEnum.toNumber(initIx.data.swapData.kind),
345
+ payIn,
346
+ initIx.name === "offererInitializePayIn" ? initIx.accounts.offererAta : PublicKey.default,
347
+ initIx.data.swapData.payOut ? initIx.accounts.claimerAta : PublicKey.default,
348
+ securityDeposit,
349
+ claimerBounty,
350
+ txoHash
351
+ );
352
+ }
353
+
354
+ static fromEscrowState(account: IdlAccounts<SwapProgram>["escrowState"]) {
355
+ const data: IdlTypes<SwapProgram>["SwapData"] = account.data;
356
+
357
+ return new SolanaSwapData(
358
+ account.offerer,
359
+ account.claimer,
360
+ account.mint,
361
+ data.amount,
362
+ Buffer.from(data.hash).toString("hex"),
363
+ data.sequence,
364
+ data.expiry,
365
+ data.nonce,
366
+ data.confirmations,
367
+ data.payOut,
368
+ SwapTypeEnum.toNumber(data.kind),
369
+ data.payIn,
370
+ account.offererAta,
371
+ account.claimerAta,
372
+ account.securityDeposit,
373
+ account.claimerBounty,
374
+ null
375
+ );
376
+ }
377
+
378
+ static typeToKind(type: ChainSwapType): number {
379
+ switch (type) {
380
+ case ChainSwapType.HTLC:
381
+ return 0;
382
+ case ChainSwapType.CHAIN:
383
+ return 1;
384
+ case ChainSwapType.CHAIN_NONCED:
385
+ return 2;
386
+ case ChainSwapType.CHAIN_TXID:
387
+ return 3;
388
+ }
389
+ return null;
390
+ }
391
+
392
+ static kindToType(value: number): ChainSwapType {
393
+ switch(value) {
394
+ case 0:
395
+ return ChainSwapType.HTLC;
396
+ case 1:
397
+ return ChainSwapType.CHAIN;
398
+ case 2:
399
+ return ChainSwapType.CHAIN_NONCED;
400
+ case 3:
401
+ return ChainSwapType.CHAIN_TXID;
402
+ }
403
+ return null;
404
+ }
405
+
406
+ isClaimer(address: string) {
407
+ const _address = new PublicKey(address);
408
+ if(this.isPayOut()) {
409
+ //Also check that swapData's ATA is correct
410
+ const ourAta = getAssociatedTokenAddressSync(this.token, _address);
411
+ if(!this.claimerAta.equals(ourAta)) return false;
412
+ }
413
+ return this.claimer.equals(new PublicKey(address));
414
+ }
415
+
416
+ isOfferer(address: string) {
417
+ return this.offerer.equals(new PublicKey(address));
418
+ }
419
+
420
+ getDepositToken(): string {
421
+ return SolanaTokens.WSOL_ADDRESS.toString();
422
+ }
423
+
424
+ isDepositToken(token: string): boolean {
425
+ return SolanaTokens.WSOL_ADDRESS.equals(new PublicKey(token));
426
+ }
427
+
428
+ }
429
+
430
+ SwapData.deserializers["sol"] = SolanaSwapData;