@atomiqlabs/chain-solana 13.3.0 → 13.5.6

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 (54) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.js +2 -2
  3. package/dist/solana/SolanaChainType.d.ts +1 -1
  4. package/dist/solana/SolanaChains.d.ts +5 -2
  5. package/dist/solana/SolanaChains.js +25 -13
  6. package/dist/solana/SolanaInitializer.d.ts +30 -4
  7. package/dist/solana/SolanaInitializer.js +105 -15
  8. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +4 -2
  9. package/dist/solana/btcrelay/SolanaBtcRelay.js +7 -1
  10. package/dist/solana/chain/SolanaAction.d.ts +1 -1
  11. package/dist/solana/chain/SolanaAction.js +2 -2
  12. package/dist/solana/chain/SolanaChainInterface.d.ts +6 -1
  13. package/dist/solana/chain/SolanaChainInterface.js +30 -0
  14. package/dist/solana/events/SolanaChainEvents.d.ts +5 -1
  15. package/dist/solana/events/SolanaChainEvents.js +16 -6
  16. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +39 -16
  17. package/dist/solana/events/SolanaChainEventsBrowser.js +96 -61
  18. package/dist/solana/swaps/SolanaSwapData.d.ts +50 -5
  19. package/dist/solana/swaps/SolanaSwapData.js +52 -8
  20. package/dist/solana/swaps/SolanaSwapModule.d.ts +4 -3
  21. package/dist/solana/swaps/SolanaSwapProgram.d.ts +12 -6
  22. package/dist/solana/swaps/SolanaSwapProgram.js +73 -47
  23. package/dist/solana/swaps/modules/SwapClaim.js +2 -0
  24. package/dist/solana/swaps/modules/SwapInit.d.ts +10 -5
  25. package/dist/solana/swaps/modules/SwapInit.js +222 -85
  26. package/dist/solana/swaps/modules/SwapRefund.d.ts +8 -2
  27. package/dist/solana/swaps/modules/SwapRefund.js +38 -22
  28. package/dist/solana/swaps/v1/programIdl.json +945 -0
  29. package/dist/solana/swaps/v1/programTypes.d.ts +943 -0
  30. package/dist/solana/swaps/v1/programTypes.js +945 -0
  31. package/dist/solana/swaps/v2/programIdl.json +952 -0
  32. package/dist/solana/swaps/v2/programTypes.d.ts +950 -0
  33. package/dist/solana/swaps/v2/programTypes.js +952 -0
  34. package/package.json +2 -2
  35. package/src/index.ts +2 -2
  36. package/src/solana/SolanaChainType.ts +2 -2
  37. package/src/solana/SolanaChains.ts +29 -14
  38. package/src/solana/SolanaInitializer.ts +147 -25
  39. package/src/solana/btcrelay/SolanaBtcRelay.ts +10 -2
  40. package/src/solana/chain/SolanaAction.ts +2 -2
  41. package/src/solana/chain/SolanaChainInterface.ts +35 -1
  42. package/src/solana/events/SolanaChainEvents.ts +22 -11
  43. package/src/solana/events/SolanaChainEventsBrowser.ts +110 -67
  44. package/src/solana/swaps/SolanaSwapData.ts +95 -11
  45. package/src/solana/swaps/SolanaSwapModule.ts +5 -3
  46. package/src/solana/swaps/SolanaSwapProgram.ts +87 -43
  47. package/src/solana/swaps/modules/SolanaLpVault.ts +2 -2
  48. package/src/solana/swaps/modules/SwapClaim.ts +3 -1
  49. package/src/solana/swaps/modules/SwapInit.ts +227 -99
  50. package/src/solana/swaps/modules/SwapRefund.ts +38 -20
  51. package/src/solana/swaps/v2/programIdl.json +952 -0
  52. package/src/solana/swaps/v2/programTypes.ts +1899 -0
  53. /package/src/solana/swaps/{programIdl.json → v1/programIdl.json} +0 -0
  54. /package/src/solana/swaps/{programTypes.ts → v1/programTypes.ts} +0 -0
@@ -12,15 +12,17 @@ const Utils_1 = require("../../../utils/Utils");
12
12
  const buffer_1 = require("buffer");
13
13
  const SolanaTokens_1 = require("../../chain/modules/SolanaTokens");
14
14
  const BN = require("bn.js");
15
+ const SolanaSwapProgram_1 = require("../SolanaSwapProgram");
15
16
  class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
16
17
  /**
17
18
  * Action for generic Refund instruction
18
19
  *
20
+ * @param signer
19
21
  * @param swapData
20
22
  * @param refundAuthTimeout optional refund authorization timeout (should be 0 for refunding expired swaps)
21
23
  * @private
22
24
  */
23
- async Refund(swapData, refundAuthTimeout) {
25
+ async Refund(signer, swapData, refundAuthTimeout) {
24
26
  const accounts = {
25
27
  offerer: swapData.offerer,
26
28
  claimer: swapData.claimer,
@@ -31,7 +33,7 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
31
33
  const useTimeout = refundAuthTimeout != null ? refundAuthTimeout : 0n;
32
34
  if (swapData.isPayIn()) {
33
35
  const ata = (0, spl_token_1.getAssociatedTokenAddressSync)(swapData.token, swapData.offerer);
34
- return new SolanaAction_1.SolanaAction(swapData.offerer, this.root, await this.swapProgram.methods
36
+ return new SolanaAction_1.SolanaAction(signer, this.root, await this.swapProgram.methods
35
37
  .offererRefundPayIn((0, Utils_1.toBN)(useTimeout))
36
38
  .accounts({
37
39
  ...accounts,
@@ -43,7 +45,7 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
43
45
  .instruction(), SwapRefund.CUCosts.REFUND_PAY_OUT);
44
46
  }
45
47
  else {
46
- return new SolanaAction_1.SolanaAction(swapData.offerer, this.root, await this.swapProgram.methods
48
+ return new SolanaAction_1.SolanaAction(signer, this.root, await this.swapProgram.methods
47
49
  .offererRefund((0, Utils_1.toBN)(useTimeout))
48
50
  .accounts({
49
51
  ...accounts,
@@ -55,19 +57,20 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
55
57
  /**
56
58
  * Action for refunding with signature, adds the Ed25519 verify instruction
57
59
  *
60
+ * @param signer
58
61
  * @param swapData
59
62
  * @param timeout
60
63
  * @param prefix
61
64
  * @param signature
62
65
  * @private
63
66
  */
64
- async RefundWithSignature(swapData, timeout, prefix, signature) {
65
- const action = new SolanaAction_1.SolanaAction(swapData.offerer, this.root, web3_js_1.Ed25519Program.createInstructionWithPublicKey({
67
+ async RefundWithSignature(signer, swapData, timeout, prefix, signature) {
68
+ const action = new SolanaAction_1.SolanaAction(signer, this.root, web3_js_1.Ed25519Program.createInstructionWithPublicKey({
66
69
  message: this.getRefundMessage(swapData, prefix, timeout),
67
70
  publicKey: swapData.claimer.toBuffer(),
68
71
  signature: signature
69
72
  }), 0, undefined, undefined, true);
70
- action.addAction(await this.Refund(swapData, BigInt(timeout)));
73
+ action.addAction(await this.Refund(signer, swapData, BigInt(timeout)));
71
74
  return action;
72
75
  }
73
76
  /**
@@ -92,13 +95,14 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
92
95
  /**
93
96
  * Checks whether we should unwrap the WSOL to SOL when refunding the swap
94
97
  *
98
+ * @param signer
95
99
  * @param swapData
96
100
  * @private
97
101
  */
98
- shouldUnwrap(swapData) {
102
+ shouldUnwrap(signer, swapData) {
99
103
  return swapData.isPayIn() &&
100
104
  swapData.token.equals(SolanaTokens_1.SolanaTokens.WSOL_ADDRESS) &&
101
- swapData.offerer.equals(swapData.offerer);
105
+ signer.equals(swapData.offerer);
102
106
  }
103
107
  signSwapRefund(signer, swapData, authorizationTimeout) {
104
108
  if (signer.keypair == null)
@@ -133,12 +137,18 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
133
137
  /**
134
138
  * Creates transactions required for refunding timed out swap, also unwraps WSOL to SOL
135
139
  *
140
+ * @param signer
136
141
  * @param swapData swap data to refund
137
142
  * @param check whether to check if swap is already expired and refundable
138
143
  * @param initAta should initialize ATA if it doesn't exist
139
144
  * @param feeRate fee rate to be used for the transactions
140
145
  */
141
- async txsRefund(swapData, check, initAta, feeRate) {
146
+ async txsRefund(signer, swapData, check, initAta, feeRate) {
147
+ if ((0, SolanaSwapProgram_1.isSwapProgramV1)(this.program.program)) {
148
+ //V1 only allows the offerer to refund
149
+ if (!swapData.isOfferer(signer.toString()))
150
+ throw new Error("Only offerer can refund in V1 on Solana");
151
+ }
142
152
  if (check && !await this.program.isRequestRefundable(swapData.offerer.toString(), swapData)) {
143
153
  throw new base_1.SwapDataVerificationError("Not refundable yet!");
144
154
  }
@@ -154,17 +164,17 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
154
164
  }
155
165
  if (feeRate == null)
156
166
  feeRate = await this.program.getRefundFeeRate(swapData);
157
- const shouldUnwrap = this.shouldUnwrap(swapData);
158
- const action = new SolanaAction_1.SolanaAction(swapData.offerer, this.root);
167
+ const shouldUnwrap = this.shouldUnwrap(signer, swapData);
168
+ const action = new SolanaAction_1.SolanaAction(signer, this.root);
159
169
  if (shouldInitAta) {
160
- const initAction = this.root.Tokens.InitAta(swapData.offerer, swapData.offerer, swapData.token, swapData.offererAta);
170
+ const initAction = this.root.Tokens.InitAta(signer, swapData.offerer, swapData.token, swapData.offererAta);
161
171
  if (initAction == null)
162
172
  throw new base_1.SwapDataVerificationError("Invalid claimer token account address");
163
173
  action.addAction(initAction);
164
174
  }
165
- action.add(await this.Refund(swapData));
175
+ action.add(await this.Refund(signer, swapData));
166
176
  if (shouldUnwrap)
167
- action.add(this.root.Tokens.Unwrap(swapData.offerer));
177
+ action.add(this.root.Tokens.Unwrap(signer));
168
178
  this.logger.debug("txsRefund(): creating claim transaction, swap: " + swapData.getClaimHash() +
169
179
  " initializingAta: " + shouldInitAta + " unwrapping: " + shouldUnwrap);
170
180
  return [await action.tx(feeRate)];
@@ -172,6 +182,7 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
172
182
  /**
173
183
  * Creates transactions required for refunding the swap with authorization signature, also unwraps WSOL to SOL
174
184
  *
185
+ * @param signer
175
186
  * @param swapData swap data to refund
176
187
  * @param timeout signature timeout
177
188
  * @param prefix signature prefix of the counterparty
@@ -180,7 +191,12 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
180
191
  * @param initAta should initialize ATA if it doesn't exist
181
192
  * @param feeRate fee rate to be used for the transactions
182
193
  */
183
- async txsRefundWithAuthorization(swapData, timeout, prefix, signature, check, initAta, feeRate) {
194
+ async txsRefundWithAuthorization(signer, swapData, timeout, prefix, signature, check, initAta, feeRate) {
195
+ if ((0, SolanaSwapProgram_1.isSwapProgramV1)(this.program.program)) {
196
+ //V1 only allows the offerer to refund
197
+ if (!swapData.isOfferer(signer.toString()))
198
+ throw new Error("Only offerer can refund in V1 on Solana");
199
+ }
184
200
  if (check && !await this.program.isCommited(swapData)) {
185
201
  throw new base_1.SwapDataVerificationError("Not correctly committed");
186
202
  }
@@ -198,31 +214,31 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
198
214
  if (feeRate == null)
199
215
  feeRate = await this.program.getRefundFeeRate(swapData);
200
216
  const signatureBuffer = buffer_1.Buffer.from(signature, "hex");
201
- const shouldUnwrap = this.shouldUnwrap(swapData);
202
- const action = await this.RefundWithSignature(swapData, timeout, prefix, signatureBuffer);
217
+ const shouldUnwrap = this.shouldUnwrap(signer, swapData);
218
+ const action = await this.RefundWithSignature(signer, swapData, timeout, prefix, signatureBuffer);
203
219
  if (shouldInitAta) {
204
- const initAction = this.root.Tokens.InitAta(swapData.offerer, swapData.offerer, swapData.token, swapData.offererAta);
220
+ const initAction = this.root.Tokens.InitAta(signer, swapData.offerer, swapData.token, swapData.offererAta);
205
221
  if (initAction == null)
206
222
  throw new base_1.SwapDataVerificationError("Invalid claimer token account address");
207
223
  action.addAction(initAction, 1); //Need to add it after the Ed25519 verify IX, but before the actual refund IX
208
224
  }
209
225
  if (shouldUnwrap)
210
- action.add(this.root.Tokens.Unwrap(swapData.offerer));
226
+ action.add(this.root.Tokens.Unwrap(signer));
211
227
  this.logger.debug("txsRefundWithAuthorization(): creating claim transaction, swap: " + swapData.getClaimHash() +
212
228
  " initializingAta: " + shouldInitAta + " unwrapping: " + shouldUnwrap +
213
229
  " auth expiry: " + timeout + " signature: " + signature);
214
230
  //Push a random keypair to the TX signer such that pesky Phantom
215
231
  // doesn't fuck up the instructions order!
216
232
  const tx = await action.tx(feeRate);
217
- const signer = web3_js_1.Keypair.generate();
233
+ const _signer = web3_js_1.Keypair.generate();
218
234
  const ix = tx.tx.instructions.find(val => val.programId.equals(this.program.program.programId));
219
235
  if (ix != null) {
220
236
  ix.keys.push({
221
- pubkey: signer.publicKey,
237
+ pubkey: _signer.publicKey,
222
238
  isSigner: true,
223
239
  isWritable: false
224
240
  });
225
- (tx.signers ?? (tx.signers = [])).push(signer);
241
+ (tx.signers ?? (tx.signers = [])).push(_signer);
226
242
  }
227
243
  return [tx];
228
244
  }