@atomiqlabs/chain-solana 7.3.6 → 7.3.7

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.
@@ -284,9 +284,9 @@ class SolanaSwapProgram extends SolanaProgramBase_1.SolanaProgramBase {
284
284
  if (inContract)
285
285
  return yield this.getIntermediaryBalance(publicKey, token);
286
286
  let { balance, ataExists } = yield this.Tokens.getTokenBalance(publicKey, token);
287
- if (token.equals(this.Tokens.WSOL_ADDRESS) && !ataExists) {
288
- const feeCosts = new BN(this.Tokens.SPL_ATA_RENT_EXEMPT);
289
- balance = BN.max(balance.sub(feeCosts), new BN(0));
287
+ if (token.equals(this.Tokens.WSOL_ADDRESS)) {
288
+ const accountRentExemptCost = new BN(1000000);
289
+ balance = BN.max(balance.sub(accountRentExemptCost), new BN(0));
290
290
  }
291
291
  this.logger.debug("getBalance(): token balance, token: " + token.toBase58() + " balance: " + balance.toString(10));
292
292
  return balance;
@@ -117,13 +117,12 @@ export declare class SwapClaim extends SolanaSwapModule {
117
117
  }, feeRate?: string): Promise<SolanaTx[] | null>;
118
118
  getClaimFeeRate(signer: PublicKey, swapData: SolanaSwapData): Promise<string>;
119
119
  /**
120
- * Get the estimated solana transaction fee of the claim transaction, this fee might be negative since it
121
- * includes the rebate for closing the swap PDA
120
+ * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
121
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
122
122
  */
123
123
  getClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN>;
124
124
  /**
125
- * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
126
- * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
125
+ * Get the estimated solana transaction fee of the claim transaction, without
127
126
  */
128
127
  getRawClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN>;
129
128
  }
@@ -273,20 +273,16 @@ class SwapClaim extends SolanaSwapModule_1.SolanaSwapModule {
273
273
  return this.root.Fees.getFeeRate(accounts);
274
274
  }
275
275
  /**
276
- * Get the estimated solana transaction fee of the claim transaction, this fee might be negative since it
277
- * includes the rebate for closing the swap PDA
276
+ * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
277
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
278
278
  */
279
279
  getClaimFee(signer, swapData, feeRate) {
280
280
  return __awaiter(this, void 0, void 0, function* () {
281
- if (swapData == null)
282
- return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT + 5000);
283
- feeRate = feeRate || (yield this.getClaimFeeRate(signer, swapData));
284
- return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT + 5000).add(this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate));
281
+ return new BN(swapData == null || swapData.payOut ? this.root.Tokens.SPL_ATA_RENT_EXEMPT : 0).add(yield this.getRawClaimFee(signer, swapData, feeRate));
285
282
  });
286
283
  }
287
284
  /**
288
- * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
289
- * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
285
+ * Get the estimated solana transaction fee of the claim transaction, without
290
286
  */
291
287
  getRawClaimFee(signer, swapData, feeRate) {
292
288
  return __awaiter(this, void 0, void 0, function* () {
@@ -294,7 +290,7 @@ class SwapClaim extends SolanaSwapModule_1.SolanaSwapModule {
294
290
  return new BN(5000);
295
291
  feeRate = feeRate || (yield this.getClaimFeeRate(signer, swapData));
296
292
  //Include rent exempt in claim fee, to take into consideration worst case cost when user destroys ATA
297
- return new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT + 5000).add(this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate));
293
+ return new BN(5000).add(this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate));
298
294
  });
299
295
  }
300
296
  }
@@ -207,6 +207,7 @@ export declare class SwapInit extends SolanaSwapModule {
207
207
  getInitFeeRate(offerer?: PublicKey, claimer?: PublicKey, token?: PublicKey, paymentHash?: string): Promise<string>;
208
208
  /**
209
209
  * Get the estimated solana fee of the init transaction, this includes the required deposit for creating swap PDA
210
+ * and also deposit for ATAs
210
211
  */
211
212
  getInitFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN>;
212
213
  /**
@@ -487,10 +487,29 @@ class SwapInit extends SolanaSwapModule_1.SolanaSwapModule {
487
487
  }
488
488
  /**
489
489
  * Get the estimated solana fee of the init transaction, this includes the required deposit for creating swap PDA
490
+ * and also deposit for ATAs
490
491
  */
491
492
  getInitFee(swapData, feeRate) {
492
493
  return __awaiter(this, void 0, void 0, function* () {
493
- return new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(yield this.getRawInitFee(swapData, feeRate));
494
+ if (swapData == null)
495
+ return new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(yield this.getRawInitFee(swapData, feeRate));
496
+ feeRate = feeRate ||
497
+ (swapData.payIn
498
+ ? yield this.getInitPayInFeeRate(swapData.offerer, swapData.claimer, swapData.token, swapData.paymentHash)
499
+ : yield this.getInitFeeRate(swapData.offerer, swapData.claimer, swapData.token, swapData.paymentHash));
500
+ const [rawFee, initAta] = yield Promise.all([
501
+ this.getRawInitFee(swapData, feeRate),
502
+ swapData != null && swapData.payOut ?
503
+ this.root.Tokens.getATAOrNull((0, spl_token_1.getAssociatedTokenAddressSync)(swapData.claimer, swapData.token)).then(acc => acc == null) :
504
+ Promise.resolve(null)
505
+ ]);
506
+ let resultingFee = new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(rawFee);
507
+ if (initAta)
508
+ resultingFee = resultingFee.add(new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT));
509
+ if (swapData.payIn && this.shouldWrapOnInit(swapData, feeRate) && this.extractAtaDataFromFeeRate(feeRate).initAta) {
510
+ resultingFee = resultingFee.add(new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT));
511
+ }
512
+ return resultingFee;
494
513
  });
495
514
  }
496
515
  /**
@@ -72,8 +72,8 @@ export declare class SwapRefund extends SolanaSwapModule {
72
72
  txsRefundWithAuthorization(swapData: SolanaSwapData, timeout: string, prefix: string, signature: string, check?: boolean, initAta?: boolean, feeRate?: string): Promise<SolanaTx[]>;
73
73
  getRefundFeeRate(swapData: SolanaSwapData): Promise<string>;
74
74
  /**
75
- * Get the estimated solana transaction fee of the refund transaction, this fee might be negative since it
76
- * includes the rebate for closing the swap PDA
75
+ * Get the estimated solana transaction fee of the refund transaction, in the worst case scenario in case where the
76
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
77
77
  */
78
78
  getRefundFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN>;
79
79
  /**
@@ -235,13 +235,12 @@ class SwapRefund extends SolanaSwapModule_1.SolanaSwapModule {
235
235
  return this.root.Fees.getFeeRate(accounts);
236
236
  }
237
237
  /**
238
- * Get the estimated solana transaction fee of the refund transaction, this fee might be negative since it
239
- * includes the rebate for closing the swap PDA
238
+ * Get the estimated solana transaction fee of the refund transaction, in the worst case scenario in case where the
239
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
240
240
  */
241
241
  getRefundFee(swapData, feeRate) {
242
242
  return __awaiter(this, void 0, void 0, function* () {
243
- const rawFee = yield this.getRawRefundFee(swapData, feeRate);
244
- return rawFee.sub(new BN(this.root.ESCROW_STATE_RENT_EXEMPT));
243
+ return new BN(swapData == null || swapData.payIn ? this.root.Tokens.SPL_ATA_RENT_EXEMPT : 0).add(yield this.getRawRefundFee(swapData, feeRate));
245
244
  });
246
245
  }
247
246
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomiqlabs/chain-solana",
3
- "version": "7.3.6",
3
+ "version": "7.3.7",
4
4
  "description": "Solana specific base implementation",
5
5
  "main": "./dist/index.js",
6
6
  "types:": "./dist/index.d.ts",
@@ -360,9 +360,9 @@ export class SolanaSwapProgram
360
360
  if(inContract) return await this.getIntermediaryBalance(publicKey, token);
361
361
 
362
362
  let { balance, ataExists } = await this.Tokens.getTokenBalance(publicKey, token);
363
- if(token.equals(this.Tokens.WSOL_ADDRESS) && !ataExists) {
364
- const feeCosts = new BN(this.Tokens.SPL_ATA_RENT_EXEMPT);
365
- balance = BN.max(balance.sub(feeCosts), new BN(0));
363
+ if(token.equals(this.Tokens.WSOL_ADDRESS)) {
364
+ const accountRentExemptCost = new BN(1000000);
365
+ balance = BN.max(balance.sub(accountRentExemptCost), new BN(0));
366
366
  }
367
367
  this.logger.debug("getBalance(): token balance, token: "+token.toBase58()+" balance: "+balance.toString(10));
368
368
  return balance;
@@ -366,22 +366,17 @@ export class SwapClaim extends SolanaSwapModule {
366
366
  }
367
367
 
368
368
  /**
369
- * Get the estimated solana transaction fee of the claim transaction, this fee might be negative since it
370
- * includes the rebate for closing the swap PDA
369
+ * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
370
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
371
371
  */
372
372
  public async getClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
373
- if(swapData==null) return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT+5000);
374
-
375
- feeRate = feeRate || await this.getClaimFeeRate(signer, swapData);
376
-
377
- return new BN(-this.root.ESCROW_STATE_RENT_EXEMPT+5000).add(
378
- this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate)
373
+ return new BN(swapData==null || swapData.payOut ? this.root.Tokens.SPL_ATA_RENT_EXEMPT : 0).add(
374
+ await this.getRawClaimFee(signer, swapData, feeRate)
379
375
  );
380
376
  }
381
377
 
382
378
  /**
383
- * Get the estimated solana transaction fee of the claim transaction in the worst case scenario in case where the
384
- * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
379
+ * Get the estimated solana transaction fee of the claim transaction, without
385
380
  */
386
381
  public async getRawClaimFee(signer: PublicKey, swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
387
382
  if(swapData==null) return new BN(5000);
@@ -389,7 +384,7 @@ export class SwapClaim extends SolanaSwapModule {
389
384
  feeRate = feeRate || await this.getClaimFeeRate(signer, swapData);
390
385
 
391
386
  //Include rent exempt in claim fee, to take into consideration worst case cost when user destroys ATA
392
- return new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT+5000).add(
387
+ return new BN(5000).add(
393
388
  this.root.Fees.getPriorityFee(this.getComputeBudget(swapData), feeRate)
394
389
  );
395
390
  }
@@ -602,11 +602,31 @@ export class SwapInit extends SolanaSwapModule {
602
602
 
603
603
  /**
604
604
  * Get the estimated solana fee of the init transaction, this includes the required deposit for creating swap PDA
605
+ * and also deposit for ATAs
605
606
  */
606
607
  async getInitFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
607
- return new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(
608
- await this.getRawInitFee(swapData, feeRate)
609
- );
608
+ if(swapData==null) return new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(await this.getRawInitFee(swapData, feeRate));
609
+
610
+ feeRate = feeRate ||
611
+ (swapData.payIn
612
+ ? await this.getInitPayInFeeRate(swapData.offerer, swapData.claimer, swapData.token, swapData.paymentHash)
613
+ : await this.getInitFeeRate(swapData.offerer, swapData.claimer, swapData.token, swapData.paymentHash));
614
+
615
+ const [rawFee, initAta] = await Promise.all([
616
+ this.getRawInitFee(swapData, feeRate),
617
+ swapData!=null && swapData.payOut ?
618
+ this.root.Tokens.getATAOrNull(getAssociatedTokenAddressSync(swapData.claimer, swapData.token)).then(acc => acc==null) :
619
+ Promise.resolve<null>(null)
620
+ ]);
621
+
622
+ let resultingFee = new BN(this.root.ESCROW_STATE_RENT_EXEMPT).add(rawFee);
623
+ if(initAta) resultingFee = resultingFee.add(new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT));
624
+
625
+ if(swapData.payIn && this.shouldWrapOnInit(swapData, feeRate) && this.extractAtaDataFromFeeRate(feeRate).initAta) {
626
+ resultingFee = resultingFee.add(new BN(this.root.Tokens.SPL_ATA_RENT_EXEMPT));
627
+ }
628
+
629
+ return resultingFee;
610
630
  }
611
631
 
612
632
  /**
@@ -290,12 +290,13 @@ export class SwapRefund extends SolanaSwapModule {
290
290
  }
291
291
 
292
292
  /**
293
- * Get the estimated solana transaction fee of the refund transaction, this fee might be negative since it
294
- * includes the rebate for closing the swap PDA
293
+ * Get the estimated solana transaction fee of the refund transaction, in the worst case scenario in case where the
294
+ * ATA needs to be initialized again (i.e. adding the ATA rent exempt lamports to the fee)
295
295
  */
296
296
  async getRefundFee(swapData: SolanaSwapData, feeRate?: string): Promise<BN> {
297
- const rawFee = await this.getRawRefundFee(swapData, feeRate);
298
- return rawFee.sub(new BN(this.root.ESCROW_STATE_RENT_EXEMPT));
297
+ return new BN(swapData==null || swapData.payIn ? this.root.Tokens.SPL_ATA_RENT_EXEMPT : 0).add(
298
+ await this.getRawRefundFee(swapData, feeRate)
299
+ );
299
300
  }
300
301
 
301
302
  /**