@atomiqlabs/chain-solana 10.0.0-dev.3 → 11.0.0

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