flash-sdk 2.14.3 → 2.15.1

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.
@@ -2220,11 +2220,12 @@ var PerpetualsClient = (function () {
2220
2220
  }
2221
2221
  });
2222
2222
  }); };
2223
- this.openPosition = function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, skipBalanceChecks) {
2223
+ this.openPosition = function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, skipBalanceChecks, ephemeralSignerPubkey) {
2224
2224
  if (nftTradingAccount === void 0) { nftTradingAccount = web3_js_1.PublicKey.default; }
2225
2225
  if (nftReferralAccount === void 0) { nftReferralAccount = web3_js_1.PublicKey.default; }
2226
2226
  if (nftRebateTokenAccount === void 0) { nftRebateTokenAccount = web3_js_1.PublicKey.default; }
2227
2227
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
2228
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2228
2229
  return __awaiter(_this, void 0, void 0, function () {
2229
2230
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2230
2231
  return __generator(this, function (_c) {
@@ -2234,7 +2235,7 @@ var PerpetualsClient = (function () {
2234
2235
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2235
2236
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2236
2237
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
2237
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
2238
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true)];
2238
2239
  case 1:
2239
2240
  userCollateralTokenAccount = _c.sent();
2240
2241
  preInstructions = [];
@@ -2243,7 +2244,6 @@ var PerpetualsClient = (function () {
2243
2244
  additionalSigners = [];
2244
2245
  if (!(collateralSymbol == 'SOL')) return [3, 4];
2245
2246
  console.log("collateralSymbol === SOL", collateralSymbol);
2246
- wrappedSolAccount = new web3_js_1.Keypair();
2247
2247
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2248
2248
  case 2:
2249
2249
  accCreationLamports = (_c.sent());
@@ -2256,20 +2256,23 @@ var PerpetualsClient = (function () {
2256
2256
  if (unWrappedSolBalance.lt(lamports)) {
2257
2257
  throw "Insufficient SOL Funds";
2258
2258
  }
2259
+ if (!ephemeralSignerPubkey) {
2260
+ wrappedSolAccount = new web3_js_1.Keypair();
2261
+ additionalSigners.push(wrappedSolAccount);
2262
+ }
2259
2263
  preInstructions = [
2260
2264
  web3_js_1.SystemProgram.createAccount({
2261
2265
  fromPubkey: publicKey,
2262
- newAccountPubkey: wrappedSolAccount.publicKey,
2266
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2263
2267
  lamports: lamports.toNumber(),
2264
2268
  space: 165,
2265
2269
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2266
2270
  }),
2267
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2271
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2268
2272
  ];
2269
2273
  postInstructions = [
2270
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2274
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2271
2275
  ];
2272
- additionalSigners.push(wrappedSolAccount);
2273
2276
  return [3, 7];
2274
2277
  case 4: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
2275
2278
  case 5:
@@ -2298,7 +2301,7 @@ var PerpetualsClient = (function () {
2298
2301
  .accounts({
2299
2302
  owner: publicKey,
2300
2303
  feePayer: publicKey,
2301
- fundingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCollateralTokenAccount,
2304
+ fundingAccount: collateralSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userCollateralTokenAccount,
2302
2305
  perpetuals: poolConfig.perpetuals,
2303
2306
  pool: poolConfig.poolAddress,
2304
2307
  position: positionAccount,
@@ -2328,11 +2331,12 @@ var PerpetualsClient = (function () {
2328
2331
  });
2329
2332
  });
2330
2333
  };
2331
- this.openPositionWithSwap = function (targetSymbol, collateralSymbol, inputSymbol, amountIn, minAmountOut, priceWithSlippage, size, side, swapPoolConfig, positionPoolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, skipBalanceChecks) {
2334
+ this.openPositionWithSwap = function (targetSymbol, collateralSymbol, inputSymbol, amountIn, minAmountOut, priceWithSlippage, size, side, swapPoolConfig, positionPoolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, skipBalanceChecks, ephemeralSignerPubkey) {
2332
2335
  if (nftTradingAccount === void 0) { nftTradingAccount = web3_js_1.PublicKey.default; }
2333
2336
  if (nftReferralAccount === void 0) { nftReferralAccount = web3_js_1.PublicKey.default; }
2334
2337
  if (nftRebateTokenAccount === void 0) { nftRebateTokenAccount = web3_js_1.PublicKey.default; }
2335
2338
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
2339
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2336
2340
  return __awaiter(_this, void 0, void 0, function () {
2337
2341
  var publicKey, targetCustodyConfig, collateralCustodyConfig, swapOutCustodyConfig, swapInCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userOutputTokenAccount, marketAccount, positionAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _c, custody, instruction;
2338
2342
  return __generator(this, function (_d) {
@@ -2352,7 +2356,6 @@ var PerpetualsClient = (function () {
2352
2356
  additionalSigners = [];
2353
2357
  if (!(inputSymbol == 'SOL')) return [3, 3];
2354
2358
  console.log("inputSymbol === SOL", inputSymbol);
2355
- wrappedSolAccount = new web3_js_1.Keypair();
2356
2359
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2357
2360
  case 1:
2358
2361
  accCreationLamports = (_d.sent());
@@ -2365,20 +2368,23 @@ var PerpetualsClient = (function () {
2365
2368
  if (unWrappedSolBalance.lt(lamports)) {
2366
2369
  throw "Insufficient SOL Funds";
2367
2370
  }
2371
+ if (!ephemeralSignerPubkey) {
2372
+ wrappedSolAccount = new web3_js_1.Keypair();
2373
+ additionalSigners.push(wrappedSolAccount);
2374
+ }
2368
2375
  preInstructions = [
2369
2376
  web3_js_1.SystemProgram.createAccount({
2370
2377
  fromPubkey: publicKey,
2371
- newAccountPubkey: wrappedSolAccount.publicKey,
2378
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2372
2379
  lamports: lamports.toNumber(),
2373
2380
  space: 165,
2374
2381
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2375
2382
  }),
2376
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2383
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2377
2384
  ];
2378
2385
  postInstructions = [
2379
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2386
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2380
2387
  ];
2381
- additionalSigners.push(wrappedSolAccount);
2382
2388
  return [3, 6];
2383
2389
  case 3:
2384
2390
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(swapPoolConfig.getTokenFromSymbol(inputSymbol).mintKey, publicKey, true);
@@ -2432,7 +2438,7 @@ var PerpetualsClient = (function () {
2432
2438
  .accounts({
2433
2439
  perpProgram: this.programId,
2434
2440
  owner: publicKey,
2435
- fundingAccount: inputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
2441
+ fundingAccount: inputSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userInputTokenAccount,
2436
2442
  receivingAccount: userOutputTokenAccount,
2437
2443
  transferAuthority: swapPoolConfig.transferAuthority,
2438
2444
  perpetuals: swapPoolConfig.perpetuals,
@@ -2469,12 +2475,13 @@ var PerpetualsClient = (function () {
2469
2475
  });
2470
2476
  });
2471
2477
  };
2472
- this.closePosition = function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, createUserATA, closeUsersWSOLATA) {
2478
+ this.closePosition = function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
2473
2479
  if (nftTradingAccount === void 0) { nftTradingAccount = web3_js_1.PublicKey.default; }
2474
2480
  if (nftReferralAccount === void 0) { nftReferralAccount = web3_js_1.PublicKey.default; }
2475
2481
  if (nftRebateTokenAccount === void 0) { nftRebateTokenAccount = web3_js_1.PublicKey.default; }
2476
2482
  if (createUserATA === void 0) { createUserATA = true; }
2477
2483
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2484
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2478
2485
  return __awaiter(_this, void 0, void 0, function () {
2479
2486
  var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_2;
2480
2487
  return __generator(this, function (_b) {
@@ -2490,26 +2497,28 @@ var PerpetualsClient = (function () {
2490
2497
  case 1:
2491
2498
  _b.trys.push([1, 9, , 10]);
2492
2499
  if (!(collateralSymbol == 'SOL')) return [3, 3];
2493
- wrappedSolAccount = new web3_js_1.Keypair();
2494
2500
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2495
2501
  case 2:
2496
2502
  lamports = (_b.sent());
2503
+ if (!ephemeralSignerPubkey) {
2504
+ wrappedSolAccount = new web3_js_1.Keypair();
2505
+ additionalSigners.push(wrappedSolAccount);
2506
+ }
2497
2507
  preInstructions = [
2498
2508
  web3_js_1.SystemProgram.createAccount({
2499
2509
  fromPubkey: publicKey,
2500
- newAccountPubkey: wrappedSolAccount.publicKey,
2510
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2501
2511
  lamports: lamports,
2502
2512
  space: 165,
2503
2513
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2504
2514
  }),
2505
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2515
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2506
2516
  ];
2507
2517
  postInstructions = [
2508
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2518
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2509
2519
  ];
2510
- additionalSigners.push(wrappedSolAccount);
2511
2520
  return [3, 7];
2512
- case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
2521
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true)];
2513
2522
  case 4:
2514
2523
  userReceivingTokenAccount = _b.sent();
2515
2524
  _a = createUserATA;
@@ -2536,7 +2545,7 @@ var PerpetualsClient = (function () {
2536
2545
  .accounts({
2537
2546
  feePayer: publicKey,
2538
2547
  owner: publicKey,
2539
- receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
2548
+ receivingAccount: collateralSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
2540
2549
  transferAuthority: poolConfig.transferAuthority,
2541
2550
  perpetuals: poolConfig.perpetuals,
2542
2551
  pool: poolConfig.poolAddress,
@@ -2574,10 +2583,11 @@ var PerpetualsClient = (function () {
2574
2583
  });
2575
2584
  });
2576
2585
  };
2577
- this.closePositionWithSwap = function (targetSymbol, outputSymbol, collateralSymbol, minAmountOut, priceAfterSlippage, side, swapPoolConfig, positionPoolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount) {
2586
+ this.closePositionWithSwap = function (targetSymbol, outputSymbol, collateralSymbol, minAmountOut, priceAfterSlippage, side, swapPoolConfig, positionPoolConfig, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, ephemeralSignerPubkey) {
2578
2587
  if (nftTradingAccount === void 0) { nftTradingAccount = web3_js_1.PublicKey.default; }
2579
2588
  if (nftReferralAccount === void 0) { nftReferralAccount = web3_js_1.PublicKey.default; }
2580
2589
  if (nftRebateTokenAccount === void 0) { nftRebateTokenAccount = web3_js_1.PublicKey.default; }
2590
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2581
2591
  return __awaiter(_this, void 0, void 0, function () {
2582
2592
  var publicKey, targetCustodyConfig, collateralCustodyConfig, swapInCustodyConfig, swapOutCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody, instruction;
2583
2593
  return __generator(this, function (_b) {
@@ -2597,26 +2607,29 @@ var PerpetualsClient = (function () {
2597
2607
  additionalSigners = [];
2598
2608
  if (!(outputSymbol == 'SOL')) return [3, 2];
2599
2609
  console.log("outputSymbol === SOL", outputSymbol);
2600
- wrappedSolAccount = new web3_js_1.Keypair();
2601
2610
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2602
2611
  case 1:
2603
2612
  lamports = (_b.sent());
2613
+ if (!ephemeralSignerPubkey) {
2614
+ wrappedSolAccount = new web3_js_1.Keypair();
2615
+ additionalSigners.push(wrappedSolAccount);
2616
+ }
2604
2617
  preInstructions = [
2605
2618
  web3_js_1.SystemProgram.createAccount({
2606
2619
  fromPubkey: publicKey,
2607
- newAccountPubkey: wrappedSolAccount.publicKey,
2620
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2608
2621
  lamports: lamports,
2609
2622
  space: 165,
2610
2623
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2611
2624
  }),
2612
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2625
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2613
2626
  ];
2614
2627
  postInstructions = [
2615
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2628
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2616
2629
  ];
2617
2630
  additionalSigners.push(wrappedSolAccount);
2618
2631
  return [3, 5];
2619
- case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(swapPoolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey)];
2632
+ case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(swapPoolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true)];
2620
2633
  case 3:
2621
2634
  userReceivingTokenAccount = _b.sent();
2622
2635
  return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
@@ -2659,7 +2672,7 @@ var PerpetualsClient = (function () {
2659
2672
  perpProgram: this.programId,
2660
2673
  owner: publicKey,
2661
2674
  fundingAccount: userCollateralTokenAccount,
2662
- receivingAccount: outputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
2675
+ receivingAccount: outputSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
2663
2676
  transferAuthority: swapPoolConfig.transferAuthority,
2664
2677
  perpetuals: swapPoolConfig.perpetuals,
2665
2678
  swapPool: swapPoolConfig.poolAddress,
@@ -2694,11 +2707,12 @@ var PerpetualsClient = (function () {
2694
2707
  });
2695
2708
  });
2696
2709
  };
2697
- this.swap = function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, useFeesPool, createUserATA, unWrapSol, skipBalanceChecks) {
2710
+ this.swap = function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, useFeesPool, createUserATA, unWrapSol, skipBalanceChecks, ephemeralSignerPubkey) {
2698
2711
  if (useFeesPool === void 0) { useFeesPool = false; }
2699
2712
  if (createUserATA === void 0) { createUserATA = true; }
2700
2713
  if (unWrapSol === void 0) { unWrapSol = false; }
2701
2714
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
2715
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2702
2716
  return __awaiter(_this, void 0, void 0, function () {
2703
2717
  var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, _d, custodyAccountMetas, custodyOracleAccountMetas, _i, _e, custody, params, inx, closeWsolATAIns, err_2;
2704
2718
  return __generator(this, function (_f) {
@@ -2718,7 +2732,7 @@ var PerpetualsClient = (function () {
2718
2732
  postInstructions = [];
2719
2733
  additionalSigners = [];
2720
2734
  if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
2721
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
2735
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
2722
2736
  case 1:
2723
2737
  wsolAssociatedTokenAccount = _f.sent();
2724
2738
  return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
@@ -2746,7 +2760,7 @@ var PerpetualsClient = (function () {
2746
2760
  case 4:
2747
2761
  if (!(userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL')) return [3, 6];
2748
2762
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
2749
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
2763
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
2750
2764
  case 5:
2751
2765
  wsolAssociatedTokenAccount = _f.sent();
2752
2766
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(wsolAssociatedTokenAccount, publicKey, publicKey);
@@ -2759,7 +2773,6 @@ var PerpetualsClient = (function () {
2759
2773
  _f.trys.push([6, 21, , 22]);
2760
2774
  if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
2761
2775
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
2762
- wrappedSolAccount = new web3_js_1.Keypair();
2763
2776
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2764
2777
  case 7:
2765
2778
  accCreationLamports = (_f.sent());
@@ -2772,22 +2785,26 @@ var PerpetualsClient = (function () {
2772
2785
  if (unWrappedSolBalance.lt(amountIn)) {
2773
2786
  throw "Insufficient SOL Funds";
2774
2787
  }
2788
+ if (!ephemeralSignerPubkey) {
2789
+ wrappedSolAccount = new web3_js_1.Keypair();
2790
+ additionalSigners.push(wrappedSolAccount);
2791
+ }
2792
+ ;
2775
2793
  preInstructions = [
2776
2794
  web3_js_1.SystemProgram.createAccount({
2777
2795
  fromPubkey: publicKey,
2778
- newAccountPubkey: wrappedSolAccount.publicKey,
2796
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2779
2797
  lamports: lamports.toNumber(),
2780
2798
  space: 165,
2781
2799
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2782
2800
  }),
2783
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2801
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2784
2802
  ];
2785
2803
  postInstructions = [
2786
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2804
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2787
2805
  ];
2788
- additionalSigners.push(wrappedSolAccount);
2789
2806
  return [3, 13];
2790
- case 9: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey, publicKey)];
2807
+ case 9: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey, publicKey, true)];
2791
2808
  case 10:
2792
2809
  userInputTokenAccount = _f.sent();
2793
2810
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
@@ -2806,26 +2823,29 @@ var PerpetualsClient = (function () {
2806
2823
  _f.label = 13;
2807
2824
  case 13:
2808
2825
  if (!(userOutputTokenSymbol == 'SOL')) return [3, 15];
2809
- wrappedSolAccount = new web3_js_1.Keypair();
2810
2826
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2811
2827
  case 14:
2812
2828
  lamports = (_f.sent());
2829
+ if (!ephemeralSignerPubkey) {
2830
+ wrappedSolAccount = new web3_js_1.Keypair();
2831
+ additionalSigners.push(wrappedSolAccount);
2832
+ }
2833
+ ;
2813
2834
  preInstructions = [
2814
2835
  web3_js_1.SystemProgram.createAccount({
2815
2836
  fromPubkey: publicKey,
2816
- newAccountPubkey: wrappedSolAccount.publicKey,
2837
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2817
2838
  lamports: lamports,
2818
2839
  space: 165,
2819
2840
  programId: spl_token_1.TOKEN_PROGRAM_ID,
2820
2841
  }),
2821
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
2842
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2822
2843
  ];
2823
2844
  postInstructions = [
2824
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
2845
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2825
2846
  ];
2826
- additionalSigners.push(wrappedSolAccount);
2827
2847
  return [3, 19];
2828
- case 15: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey)];
2848
+ case 15: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey, true)];
2829
2849
  case 16:
2830
2850
  userOutputTokenAccount = _f.sent();
2831
2851
  _d = createUserATA;
@@ -2864,8 +2884,8 @@ var PerpetualsClient = (function () {
2864
2884
  .swap(params)
2865
2885
  .accounts({
2866
2886
  owner: publicKey,
2867
- fundingAccount: userInputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
2868
- receivingAccount: userOutputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userOutputTokenAccount,
2887
+ fundingAccount: userInputTokenSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userInputTokenAccount,
2888
+ receivingAccount: userOutputTokenSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userOutputTokenAccount,
2869
2889
  transferAuthority: poolConfig.transferAuthority,
2870
2890
  perpetuals: poolConfig.perpetuals,
2871
2891
  pool: poolConfig.poolAddress,
@@ -2973,8 +2993,9 @@ var PerpetualsClient = (function () {
2973
2993
  }
2974
2994
  });
2975
2995
  }); };
2976
- this.addCollateral = function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks) {
2996
+ this.addCollateral = function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
2977
2997
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
2998
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2978
2999
  return __awaiter(_this, void 0, void 0, function () {
2979
3000
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, solBal, tokenAccountBalance, _b, instruction;
2980
3001
  return __generator(this, function (_c) {
@@ -2994,7 +3015,6 @@ var PerpetualsClient = (function () {
2994
3015
  additionalSigners = [];
2995
3016
  if (!(collateralSymbol == 'SOL')) return [3, 4];
2996
3017
  console.log("collateralSymbol === SOL", collateralSymbol);
2997
- wrappedSolAccount = new web3_js_1.Keypair();
2998
3018
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
2999
3019
  case 1:
3000
3020
  accCreationLamports = (_c.sent());
@@ -3012,20 +3032,23 @@ var PerpetualsClient = (function () {
3012
3032
  case 3:
3013
3033
  solBal = _c.sent();
3014
3034
  console.log("solBal>>:", solBal);
3035
+ if (!ephemeralSignerPubkey) {
3036
+ wrappedSolAccount = new web3_js_1.Keypair();
3037
+ additionalSigners.push(wrappedSolAccount);
3038
+ }
3015
3039
  preInstructions = [
3016
3040
  web3_js_1.SystemProgram.createAccount({
3017
3041
  fromPubkey: publicKey,
3018
- newAccountPubkey: wrappedSolAccount.publicKey,
3042
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3019
3043
  lamports: lamports.toNumber(),
3020
3044
  space: 165,
3021
3045
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3022
3046
  }),
3023
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3047
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3024
3048
  ];
3025
3049
  postInstructions = [
3026
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3050
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3027
3051
  ];
3028
- additionalSigners.push(wrappedSolAccount);
3029
3052
  return [3, 7];
3030
3053
  case 4: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3031
3054
  case 5:
@@ -3047,7 +3070,7 @@ var PerpetualsClient = (function () {
3047
3070
  owner: publicKey,
3048
3071
  position: positionPubKey,
3049
3072
  market: marketAccount,
3050
- fundingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
3073
+ fundingAccount: collateralSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userPayingTokenAccount,
3051
3074
  perpetuals: poolConfig.perpetuals,
3052
3075
  pool: poolConfig.poolAddress,
3053
3076
  targetCustody: targetCustodyConfig.custodyAccount,
@@ -3072,8 +3095,9 @@ var PerpetualsClient = (function () {
3072
3095
  });
3073
3096
  });
3074
3097
  };
3075
- this.addCollateralWithSwap = function (targetSymbol, inputSymbol, collateralSymbol, amountIn, minAmountOut, side, positionPubKey, poolConfigSwap, poolConfigPosition, skipBalanceChecks) {
3098
+ this.addCollateralWithSwap = function (targetSymbol, inputSymbol, collateralSymbol, amountIn, minAmountOut, side, positionPubKey, poolConfigSwap, poolConfigPosition, skipBalanceChecks, ephemeralSignerPubkey) {
3076
3099
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3100
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3077
3101
  return __awaiter(_this, void 0, void 0, function () {
3078
3102
  var publicKey, collateralCustodyConfig, targetCustodyConfig, outputCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _c, custody, marketAccount, instruction;
3079
3103
  return __generator(this, function (_d) {
@@ -3096,7 +3120,6 @@ var PerpetualsClient = (function () {
3096
3120
  additionalSigners = [];
3097
3121
  if (!(inputSymbol == 'SOL')) return [3, 3];
3098
3122
  console.log("inputSymbol === SOL", inputSymbol);
3099
- wrappedSolAccount = new web3_js_1.Keypair();
3100
3123
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3101
3124
  case 1:
3102
3125
  accCreationLamports = (_d.sent());
@@ -3110,20 +3133,23 @@ var PerpetualsClient = (function () {
3110
3133
  if (unWrappedSolBalance.lt(lamports)) {
3111
3134
  throw "Insufficient SOL Funds";
3112
3135
  }
3136
+ if (!ephemeralSignerPubkey) {
3137
+ wrappedSolAccount = new web3_js_1.Keypair();
3138
+ additionalSigners.push(wrappedSolAccount);
3139
+ }
3113
3140
  preInstructions = [
3114
3141
  web3_js_1.SystemProgram.createAccount({
3115
3142
  fromPubkey: publicKey,
3116
- newAccountPubkey: wrappedSolAccount.publicKey,
3143
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3117
3144
  lamports: lamports.toNumber(),
3118
3145
  space: 165,
3119
3146
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3120
3147
  }),
3121
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3148
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3122
3149
  ];
3123
3150
  postInstructions = [
3124
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3151
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3125
3152
  ];
3126
- additionalSigners.push(wrappedSolAccount);
3127
3153
  return [3, 6];
3128
3154
  case 3:
3129
3155
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(inputCustodyConfig.mintKey, publicKey, true);
@@ -3170,7 +3196,7 @@ var PerpetualsClient = (function () {
3170
3196
  }).accounts({
3171
3197
  perpProgram: this.programId,
3172
3198
  owner: publicKey,
3173
- fundingAccount: inputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
3199
+ fundingAccount: inputSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userInputTokenAccount,
3174
3200
  receivingAccount: userCollateralTokenAccount,
3175
3201
  transferAuthority: poolConfigSwap.transferAuthority,
3176
3202
  perpetuals: poolConfigSwap.perpetuals,
@@ -3206,9 +3232,10 @@ var PerpetualsClient = (function () {
3206
3232
  });
3207
3233
  });
3208
3234
  };
3209
- this.removeCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA) {
3235
+ this.removeCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
3210
3236
  if (createUserATA === void 0) { createUserATA = true; }
3211
3237
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3238
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3212
3239
  return __awaiter(_this, void 0, void 0, function () {
3213
3240
  var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_3;
3214
3241
  return __generator(this, function (_b) {
@@ -3234,27 +3261,29 @@ var PerpetualsClient = (function () {
3234
3261
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3235
3262
  if (!(collateralSymbol == 'SOL')) return [3, 3];
3236
3263
  console.log("remove collateral in SOL ...create WSOL temp and close it ");
3237
- wrappedSolAccount = new web3_js_1.Keypair();
3238
3264
  userReceivingTokenAccount = wrappedSolAccount.publicKey;
3239
3265
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3240
3266
  case 2:
3241
3267
  lamports = (_b.sent());
3268
+ if (!ephemeralSignerPubkey) {
3269
+ wrappedSolAccount = new web3_js_1.Keypair();
3270
+ additionalSigners.push(wrappedSolAccount);
3271
+ }
3242
3272
  preInstructions = [
3243
3273
  web3_js_1.SystemProgram.createAccount({
3244
3274
  fromPubkey: publicKey,
3245
- newAccountPubkey: wrappedSolAccount.publicKey,
3275
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3246
3276
  lamports: lamports,
3247
3277
  space: 165,
3248
3278
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3249
3279
  }),
3250
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3280
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3251
3281
  ];
3252
3282
  postInstructions = [
3253
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3283
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3254
3284
  ];
3255
- additionalSigners.push(wrappedSolAccount);
3256
3285
  return [3, 7];
3257
- case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
3286
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true)];
3258
3287
  case 4:
3259
3288
  userReceivingTokenAccount = _b.sent();
3260
3289
  _a = createUserATA;
@@ -3276,7 +3305,7 @@ var PerpetualsClient = (function () {
3276
3305
  })
3277
3306
  .accounts({
3278
3307
  owner: publicKey,
3279
- receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
3308
+ receivingAccount: collateralSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
3280
3309
  transferAuthority: poolConfig.transferAuthority,
3281
3310
  perpetuals: poolConfig.perpetuals,
3282
3311
  pool: poolConfig.poolAddress,
@@ -3313,120 +3342,125 @@ var PerpetualsClient = (function () {
3313
3342
  });
3314
3343
  });
3315
3344
  };
3316
- this.removeCollateralWithSwap = function (targetSymbol, collateralSymbol, outputSymbol, minAmountOut, collateralDelta, side, poolConfigSwap, poolConfigPosition) { return __awaiter(_this, void 0, void 0, function () {
3317
- var publicKey, targetCustodyConfig, collateralCustodyConfig, inputCusotdyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody, instruction;
3318
- return __generator(this, function (_b) {
3319
- switch (_b.label) {
3320
- case 0:
3321
- publicKey = this.provider.wallet.publicKey;
3322
- targetCustodyConfig = poolConfigPosition.custodies.find(function (i) { return i.mintKey.equals(poolConfigPosition.getTokenFromSymbol(targetSymbol).mintKey); });
3323
- collateralCustodyConfig = poolConfigPosition.custodies.find(function (i) { return i.mintKey.equals(poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey); });
3324
- inputCusotdyConfig = poolConfigSwap.custodies.find(function (i) { return i.mintKey.equals(poolConfigSwap.getTokenFromSymbol(collateralSymbol).mintKey); });
3325
- outputCustodyConfig = poolConfigSwap.custodies.find(function (i) { return i.mintKey.equals(poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey); });
3326
- if (outputCustodyConfig.mintKey.equals(collateralCustodyConfig.mintKey)) {
3327
- throw "Dont use swap, just call remove collateral";
3328
- }
3329
- preInstructions = [];
3330
- instructions = [];
3331
- postInstructions = [];
3332
- additionalSigners = [];
3333
- if (!(outputSymbol == 'SOL')) return [3, 2];
3334
- console.log("outputSymbol === SOL", outputSymbol);
3335
- wrappedSolAccount = new web3_js_1.Keypair();
3336
- return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3337
- case 1:
3338
- lamports = (_b.sent());
3339
- preInstructions = [
3340
- web3_js_1.SystemProgram.createAccount({
3341
- fromPubkey: publicKey,
3342
- newAccountPubkey: wrappedSolAccount.publicKey,
3343
- lamports: lamports,
3344
- space: 165,
3345
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3346
- }),
3347
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3348
- ];
3349
- postInstructions = [
3350
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3351
- ];
3352
- additionalSigners.push(wrappedSolAccount);
3353
- return [3, 5];
3354
- case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey, publicKey)];
3355
- case 3:
3356
- userReceivingTokenAccount = _b.sent();
3357
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3358
- case 4:
3359
- if (!(_b.sent())) {
3360
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey));
3361
- }
3362
- _b.label = 5;
3363
- case 5:
3364
- userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true);
3365
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3366
- case 6:
3367
- if (!(_b.sent())) {
3368
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey));
3369
- }
3370
- marketAccount = poolConfigPosition.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3371
- positionAccount = poolConfigPosition.getPositionFromMarketPk(publicKey, marketAccount);
3372
- custodyAccountMetas = [];
3373
- custodyOracleAccountMetas = [];
3374
- for (_i = 0, _a = poolConfigSwap.custodies; _i < _a.length; _i++) {
3375
- custody = _a[_i];
3376
- custodyAccountMetas.push({
3377
- pubkey: custody.custodyAccount,
3378
- isSigner: false,
3379
- isWritable: false,
3380
- });
3381
- custodyOracleAccountMetas.push({
3382
- pubkey: this.useExtOracleAccount ? custody.extOracleAccount : custody.intOracleAccount,
3383
- isSigner: false,
3384
- isWritable: false,
3385
- });
3386
- }
3387
- return [4, this.programPerpComposability.methods
3388
- .removeCollateralAndSwap({
3389
- minAmountOut: minAmountOut,
3390
- collateralDelta: collateralDelta,
3391
- })
3392
- .accounts({
3393
- perpProgram: this.programId,
3394
- owner: publicKey,
3395
- fundingAccount: userCollateralTokenAccount,
3396
- receivingAccount: outputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
3397
- transferAuthority: poolConfigSwap.transferAuthority,
3398
- perpetuals: poolConfigSwap.perpetuals,
3399
- swapPool: poolConfigSwap.poolAddress,
3400
- receivingCustody: inputCusotdyConfig.custodyAccount,
3401
- receivingCustodyOracleAccount: this.useExtOracleAccount ? inputCusotdyConfig.extOracleAccount : inputCusotdyConfig.intOracleAccount,
3402
- receivingCustodyTokenAccount: inputCusotdyConfig.tokenAccount,
3403
- dispensingCustody: outputCustodyConfig.custodyAccount,
3404
- dispensingCustodyOracleAccount: this.useExtOracleAccount ? outputCustodyConfig.extOracleAccount : outputCustodyConfig.intOracleAccount,
3405
- dispensingCustodyTokenAccount: outputCustodyConfig.tokenAccount,
3406
- positionPool: poolConfigPosition.poolAddress,
3407
- position: positionAccount,
3408
- market: marketAccount,
3409
- targetCustody: targetCustodyConfig.custodyAccount,
3410
- targetOracleAccount: this.useExtOracleAccount ? targetCustodyConfig.extOracleAccount : targetCustodyConfig.intOracleAccount,
3411
- collateralCustody: collateralCustodyConfig.custodyAccount,
3412
- collateralOracleAccount: this.useExtOracleAccount ? collateralCustodyConfig.extOracleAccount : collateralCustodyConfig.intOracleAccount,
3413
- collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
3414
- eventAuthority: this.eventAuthority.publicKey,
3415
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3416
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3417
- })
3418
- .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
3419
- .instruction()];
3420
- case 7:
3421
- instruction = _b.sent();
3422
- instructions.push(instruction);
3423
- return [2, {
3424
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3425
- additionalSigners: additionalSigners
3426
- }];
3427
- }
3345
+ this.removeCollateralWithSwap = function (targetSymbol, collateralSymbol, outputSymbol, minAmountOut, collateralDelta, side, poolConfigSwap, poolConfigPosition, ephemeralSignerPubkey) {
3346
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3347
+ return __awaiter(_this, void 0, void 0, function () {
3348
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, inputCusotdyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody, instruction;
3349
+ return __generator(this, function (_b) {
3350
+ switch (_b.label) {
3351
+ case 0:
3352
+ publicKey = this.provider.wallet.publicKey;
3353
+ targetCustodyConfig = poolConfigPosition.custodies.find(function (i) { return i.mintKey.equals(poolConfigPosition.getTokenFromSymbol(targetSymbol).mintKey); });
3354
+ collateralCustodyConfig = poolConfigPosition.custodies.find(function (i) { return i.mintKey.equals(poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey); });
3355
+ inputCusotdyConfig = poolConfigSwap.custodies.find(function (i) { return i.mintKey.equals(poolConfigSwap.getTokenFromSymbol(collateralSymbol).mintKey); });
3356
+ outputCustodyConfig = poolConfigSwap.custodies.find(function (i) { return i.mintKey.equals(poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey); });
3357
+ if (outputCustodyConfig.mintKey.equals(collateralCustodyConfig.mintKey)) {
3358
+ throw "Dont use swap, just call remove collateral";
3359
+ }
3360
+ preInstructions = [];
3361
+ instructions = [];
3362
+ postInstructions = [];
3363
+ additionalSigners = [];
3364
+ if (!(outputSymbol == 'SOL')) return [3, 2];
3365
+ console.log("outputSymbol === SOL", outputSymbol);
3366
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3367
+ case 1:
3368
+ lamports = (_b.sent());
3369
+ if (!ephemeralSignerPubkey) {
3370
+ wrappedSolAccount = new web3_js_1.Keypair();
3371
+ additionalSigners.push(wrappedSolAccount);
3372
+ }
3373
+ preInstructions = [
3374
+ web3_js_1.SystemProgram.createAccount({
3375
+ fromPubkey: publicKey,
3376
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3377
+ lamports: lamports,
3378
+ space: 165,
3379
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3380
+ }),
3381
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3382
+ ];
3383
+ postInstructions = [
3384
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3385
+ ];
3386
+ return [3, 5];
3387
+ case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true)];
3388
+ case 3:
3389
+ userReceivingTokenAccount = _b.sent();
3390
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3391
+ case 4:
3392
+ if (!(_b.sent())) {
3393
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfigSwap.getTokenFromSymbol(outputSymbol).mintKey));
3394
+ }
3395
+ _b.label = 5;
3396
+ case 5:
3397
+ userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true);
3398
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3399
+ case 6:
3400
+ if (!(_b.sent())) {
3401
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfigPosition.getTokenFromSymbol(collateralSymbol).mintKey));
3402
+ }
3403
+ marketAccount = poolConfigPosition.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3404
+ positionAccount = poolConfigPosition.getPositionFromMarketPk(publicKey, marketAccount);
3405
+ custodyAccountMetas = [];
3406
+ custodyOracleAccountMetas = [];
3407
+ for (_i = 0, _a = poolConfigSwap.custodies; _i < _a.length; _i++) {
3408
+ custody = _a[_i];
3409
+ custodyAccountMetas.push({
3410
+ pubkey: custody.custodyAccount,
3411
+ isSigner: false,
3412
+ isWritable: false,
3413
+ });
3414
+ custodyOracleAccountMetas.push({
3415
+ pubkey: this.useExtOracleAccount ? custody.extOracleAccount : custody.intOracleAccount,
3416
+ isSigner: false,
3417
+ isWritable: false,
3418
+ });
3419
+ }
3420
+ return [4, this.programPerpComposability.methods
3421
+ .removeCollateralAndSwap({
3422
+ minAmountOut: minAmountOut,
3423
+ collateralDelta: collateralDelta,
3424
+ })
3425
+ .accounts({
3426
+ perpProgram: this.programId,
3427
+ owner: publicKey,
3428
+ fundingAccount: userCollateralTokenAccount,
3429
+ receivingAccount: outputSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
3430
+ transferAuthority: poolConfigSwap.transferAuthority,
3431
+ perpetuals: poolConfigSwap.perpetuals,
3432
+ swapPool: poolConfigSwap.poolAddress,
3433
+ receivingCustody: inputCusotdyConfig.custodyAccount,
3434
+ receivingCustodyOracleAccount: this.useExtOracleAccount ? inputCusotdyConfig.extOracleAccount : inputCusotdyConfig.intOracleAccount,
3435
+ receivingCustodyTokenAccount: inputCusotdyConfig.tokenAccount,
3436
+ dispensingCustody: outputCustodyConfig.custodyAccount,
3437
+ dispensingCustodyOracleAccount: this.useExtOracleAccount ? outputCustodyConfig.extOracleAccount : outputCustodyConfig.intOracleAccount,
3438
+ dispensingCustodyTokenAccount: outputCustodyConfig.tokenAccount,
3439
+ positionPool: poolConfigPosition.poolAddress,
3440
+ position: positionAccount,
3441
+ market: marketAccount,
3442
+ targetCustody: targetCustodyConfig.custodyAccount,
3443
+ targetOracleAccount: this.useExtOracleAccount ? targetCustodyConfig.extOracleAccount : targetCustodyConfig.intOracleAccount,
3444
+ collateralCustody: collateralCustodyConfig.custodyAccount,
3445
+ collateralOracleAccount: this.useExtOracleAccount ? collateralCustodyConfig.extOracleAccount : collateralCustodyConfig.intOracleAccount,
3446
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
3447
+ eventAuthority: this.eventAuthority.publicKey,
3448
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3449
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3450
+ })
3451
+ .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
3452
+ .instruction()];
3453
+ case 7:
3454
+ instruction = _b.sent();
3455
+ instructions.push(instruction);
3456
+ return [2, {
3457
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3458
+ additionalSigners: additionalSigners
3459
+ }];
3460
+ }
3461
+ });
3428
3462
  });
3429
- }); };
3463
+ };
3430
3464
  this.increaseSize = function (targetSymbol, collateralSymbol, positionPubKey, side, poolConfig, priceWithSlippage, sizeDelta, privilege, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount) {
3431
3465
  if (nftTradingAccount === void 0) { nftTradingAccount = web3_js_1.PublicKey.default; }
3432
3466
  if (nftReferralAccount === void 0) { nftReferralAccount = web3_js_1.PublicKey.default; }
@@ -3547,8 +3581,9 @@ var PerpetualsClient = (function () {
3547
3581
  });
3548
3582
  });
3549
3583
  };
3550
- this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks) {
3584
+ this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
3551
3585
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3586
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3552
3587
  return __awaiter(_this, void 0, void 0, function () {
3553
3588
  var publicKey, payTokenCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, accCreationLamports, lamports, unWrappedSolBalance, _d, tokenAccountBalance, _e, instruction, err_4;
3554
3589
  return __generator(this, function (_f) {
@@ -3566,10 +3601,10 @@ var PerpetualsClient = (function () {
3566
3601
  _f.label = 1;
3567
3602
  case 1:
3568
3603
  _f.trys.push([1, 12, , 13]);
3569
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustodyConfig.mintKey, publicKey)];
3604
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustodyConfig.mintKey, publicKey, true)];
3570
3605
  case 2:
3571
3606
  userPayingTokenAccount = _f.sent();
3572
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey)];
3607
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey, true)];
3573
3608
  case 3:
3574
3609
  lpTokenAccount = _f.sent();
3575
3610
  custodyAccountMetas = [];
@@ -3603,7 +3638,6 @@ var PerpetualsClient = (function () {
3603
3638
  }
3604
3639
  if (!(payTokenSymbol == 'SOL')) return [3, 7];
3605
3640
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3606
- wrappedSolAccount = new web3_js_1.Keypair();
3607
3641
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3608
3642
  case 5:
3609
3643
  accCreationLamports = (_f.sent());
@@ -3616,20 +3650,23 @@ var PerpetualsClient = (function () {
3616
3650
  if (unWrappedSolBalance.lt(lamports)) {
3617
3651
  throw "Insufficient SOL Funds";
3618
3652
  }
3653
+ if (!ephemeralSignerPubkey) {
3654
+ wrappedSolAccount = new web3_js_1.Keypair();
3655
+ additionalSigners.push(wrappedSolAccount);
3656
+ }
3619
3657
  preInstructions = [
3620
3658
  web3_js_1.SystemProgram.createAccount({
3621
3659
  fromPubkey: publicKey,
3622
- newAccountPubkey: wrappedSolAccount.publicKey,
3660
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3623
3661
  lamports: lamports.toNumber(),
3624
3662
  space: 165,
3625
3663
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3626
3664
  }),
3627
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3665
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3628
3666
  ];
3629
3667
  postInstructions = [
3630
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3668
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3631
3669
  ];
3632
- additionalSigners.push(wrappedSolAccount);
3633
3670
  return [3, 10];
3634
3671
  case 7: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3635
3672
  case 8:
@@ -3652,7 +3689,7 @@ var PerpetualsClient = (function () {
3652
3689
  })
3653
3690
  .accounts({
3654
3691
  owner: publicKey,
3655
- fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
3692
+ fundingAccount: payTokenSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userPayingTokenAccount,
3656
3693
  lpTokenAccount: lpTokenAccount,
3657
3694
  transferAuthority: poolConfig.transferAuthority,
3658
3695
  perpetuals: poolConfig.perpetuals,
@@ -3684,8 +3721,9 @@ var PerpetualsClient = (function () {
3684
3721
  });
3685
3722
  });
3686
3723
  };
3687
- this.addLiquidityAndStake = function (inputSymbol, amountIn, minLpAmountOut, poolConfig, skipBalanceChecks) {
3724
+ this.addLiquidityAndStake = function (inputSymbol, amountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
3688
3725
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3726
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3689
3727
  return __awaiter(_this, void 0, void 0, function () {
3690
3728
  var publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lpTokenMint, inputCustodyConfig, lpTokenAccount, flpStakeAccount, poolStakedLpVault, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _c, custody, _d, _e, market, instruction;
3691
3729
  return __generator(this, function (_f) {
@@ -3698,14 +3736,13 @@ var PerpetualsClient = (function () {
3698
3736
  additionalSigners = [];
3699
3737
  lpTokenMint = poolConfig.stakedLpTokenMint;
3700
3738
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
3701
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(lpTokenMint, publicKey)];
3739
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(lpTokenMint, publicKey, true)];
3702
3740
  case 1:
3703
3741
  lpTokenAccount = _f.sent();
3704
3742
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3705
3743
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3706
3744
  if (!(inputSymbol == 'SOL')) return [3, 5];
3707
3745
  console.log("inputSymbol === SOL", inputSymbol);
3708
- wrappedSolAccount = new web3_js_1.Keypair();
3709
3746
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3710
3747
  case 2:
3711
3748
  accCreationLamports = (_f.sent());
@@ -3724,20 +3761,23 @@ var PerpetualsClient = (function () {
3724
3761
  if (!(_f.sent())) {
3725
3762
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3726
3763
  }
3764
+ if (!ephemeralSignerPubkey) {
3765
+ wrappedSolAccount = new web3_js_1.Keypair();
3766
+ additionalSigners.push(wrappedSolAccount);
3767
+ }
3727
3768
  preInstructions = [
3728
3769
  web3_js_1.SystemProgram.createAccount({
3729
3770
  fromPubkey: publicKey,
3730
- newAccountPubkey: wrappedSolAccount.publicKey,
3771
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3731
3772
  lamports: lamports.toNumber(),
3732
3773
  space: 165,
3733
3774
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3734
3775
  }),
3735
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3776
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3736
3777
  ];
3737
3778
  postInstructions = [
3738
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3779
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3739
3780
  ];
3740
- additionalSigners.push(wrappedSolAccount);
3741
3781
  return [3, 8];
3742
3782
  case 5:
3743
3783
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(inputCustodyConfig.mintKey, publicKey, true);
@@ -3786,7 +3826,7 @@ var PerpetualsClient = (function () {
3786
3826
  }).accounts({
3787
3827
  owner: publicKey,
3788
3828
  feePayer: publicKey,
3789
- fundingAccount: inputSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
3829
+ fundingAccount: inputSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userInputTokenAccount,
3790
3830
  transferAuthority: poolConfig.transferAuthority,
3791
3831
  perpetuals: poolConfig.perpetuals,
3792
3832
  pool: poolConfig.poolAddress,
@@ -3815,10 +3855,11 @@ var PerpetualsClient = (function () {
3815
3855
  });
3816
3856
  });
3817
3857
  };
3818
- this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA, createUserATA, closeUsersWSOLATA) {
3858
+ this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
3819
3859
  if (closeLpATA === void 0) { closeLpATA = false; }
3820
3860
  if (createUserATA === void 0) { createUserATA = true; }
3821
3861
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3862
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3822
3863
  return __awaiter(_this, void 0, void 0, function () {
3823
3864
  var recieveTokenCustodyConfig, publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, stakedLpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, lamports, _d, removeLiquidityTx, closeInx, closeWsolATAIns, err_5;
3824
3865
  return __generator(this, function (_e) {
@@ -3836,7 +3877,7 @@ var PerpetualsClient = (function () {
3836
3877
  _e.label = 1;
3837
3878
  case 1:
3838
3879
  _e.trys.push([1, 10, , 11]);
3839
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey)];
3880
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey, true)];
3840
3881
  case 2:
3841
3882
  stakedLpTokenAccount = _e.sent();
3842
3883
  custodyAccountMetas = [];
@@ -3864,27 +3905,28 @@ var PerpetualsClient = (function () {
3864
3905
  });
3865
3906
  }
3866
3907
  if (!(recieveTokenSymbol == 'SOL')) return [3, 4];
3867
- wrappedSolAccount = new web3_js_1.Keypair();
3868
- userReceivingTokenAccount = wrappedSolAccount.publicKey;
3869
3908
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3870
3909
  case 3:
3871
3910
  lamports = (_e.sent());
3911
+ if (!ephemeralSignerPubkey) {
3912
+ wrappedSolAccount = new web3_js_1.Keypair();
3913
+ additionalSigners.push(wrappedSolAccount);
3914
+ }
3872
3915
  preInstructions = [
3873
3916
  web3_js_1.SystemProgram.createAccount({
3874
3917
  fromPubkey: publicKey,
3875
- newAccountPubkey: wrappedSolAccount.publicKey,
3918
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3876
3919
  lamports: lamports,
3877
3920
  space: 165,
3878
3921
  programId: spl_token_1.TOKEN_PROGRAM_ID,
3879
3922
  }),
3880
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3923
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3881
3924
  ];
3882
3925
  postInstructions = [
3883
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3926
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3884
3927
  ];
3885
- additionalSigners.push(wrappedSolAccount);
3886
3928
  return [3, 8];
3887
- case 4: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey, publicKey)];
3929
+ case 4: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey, publicKey, true)];
3888
3930
  case 5:
3889
3931
  userReceivingTokenAccount = _e.sent();
3890
3932
  _d = createUserATA;
@@ -3907,7 +3949,7 @@ var PerpetualsClient = (function () {
3907
3949
  })
3908
3950
  .accounts({
3909
3951
  owner: publicKey,
3910
- receivingAccount: recieveTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
3952
+ receivingAccount: recieveTokenSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
3911
3953
  lpTokenAccount: stakedLpTokenAccount,
3912
3954
  transferAuthority: poolConfig.transferAuthority,
3913
3955
  perpetuals: poolConfig.perpetuals,
@@ -4003,7 +4045,7 @@ var PerpetualsClient = (function () {
4003
4045
  nftMint.toBuffer(),
4004
4046
  ], this.programId)[0];
4005
4047
  metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
4006
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, owner)];
4048
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, owner, true)];
4007
4049
  case 2:
4008
4050
  nftTokenAccount = _a.sent();
4009
4051
  return [4, this.program.methods
@@ -4057,7 +4099,7 @@ var PerpetualsClient = (function () {
4057
4099
  Buffer.from("referral"),
4058
4100
  publicKey.toBuffer(),
4059
4101
  ], this.programId)[0];
4060
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, publicKey)];
4102
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, publicKey, true)];
4061
4103
  case 2:
4062
4104
  nftTokenAccount = _a.sent();
4063
4105
  flpStakeAccountMetas = [];
@@ -4267,7 +4309,7 @@ var PerpetualsClient = (function () {
4267
4309
  lpTokenMint = poolConfig.stakedLpTokenMint;
4268
4310
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
4269
4311
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), owner.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4270
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, owner)];
4312
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, owner, true)];
4271
4313
  case 2:
4272
4314
  userLpTokenAccount = _a.sent();
4273
4315
  return [4, this.program.methods
@@ -4558,7 +4600,7 @@ var PerpetualsClient = (function () {
4558
4600
  pool = poolConfig.poolAddress;
4559
4601
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), pool.toBuffer(), lpTokenMint.toBuffer()], this.program.programId)[0];
4560
4602
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4561
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey)];
4603
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey, true)];
4562
4604
  case 2:
4563
4605
  userLpTokenAccount = _b.sent();
4564
4606
  _a = createUserLPTA;
@@ -4747,7 +4789,7 @@ var PerpetualsClient = (function () {
4747
4789
  _a.label = 1;
4748
4790
  case 1:
4749
4791
  _a.trys.push([1, 4, , 5]);
4750
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey)];
4792
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey, true)];
4751
4793
  case 2:
4752
4794
  fundingAccount = _a.sent();
4753
4795
  rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programFbnftReward.programId)[0];
@@ -4793,11 +4835,11 @@ var PerpetualsClient = (function () {
4793
4835
  _b.label = 1;
4794
4836
  case 1:
4795
4837
  _b.trys.push([1, 7, , 8]);
4796
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, publicKey)];
4838
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, publicKey, true)];
4797
4839
  case 2:
4798
4840
  nftTokenAccount = _b.sent();
4799
4841
  metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
4800
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey)];
4842
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey, true)];
4801
4843
  case 3:
4802
4844
  receivingTokenAccount = _b.sent();
4803
4845
  _a = createUserATA;
@@ -4974,9 +5016,10 @@ var PerpetualsClient = (function () {
4974
5016
  }
4975
5017
  });
4976
5018
  }); };
4977
- this.forceClosePosition = function (positionAccount, targetSymbol, collateralSymbol, side, isStopLoss, poolConfig, createUserATA, closeUsersWSOLATA) {
5019
+ this.forceClosePosition = function (positionAccount, targetSymbol, collateralSymbol, side, isStopLoss, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
4978
5020
  if (createUserATA === void 0) { createUserATA = true; }
4979
5021
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
5022
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4980
5023
  return __awaiter(_this, void 0, void 0, function () {
4981
5024
  var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, forceClosePosition, closeWsolATAIns, err_24;
4982
5025
  return __generator(this, function (_b) {
@@ -4995,26 +5038,28 @@ var PerpetualsClient = (function () {
4995
5038
  _b.trys.push([1, 9, , 10]);
4996
5039
  if (!(collateralSymbol == 'SOL')) return [3, 3];
4997
5040
  console.log("collateralSymbol === SOL", collateralSymbol);
4998
- wrappedSolAccount = new web3_js_1.Keypair();
4999
5041
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
5000
5042
  case 2:
5001
5043
  lamports = (_b.sent());
5044
+ if (!ephemeralSignerPubkey) {
5045
+ wrappedSolAccount = new web3_js_1.Keypair();
5046
+ additionalSigners.push(wrappedSolAccount);
5047
+ }
5002
5048
  preInstructions = [
5003
5049
  web3_js_1.SystemProgram.createAccount({
5004
5050
  fromPubkey: payerPubkey,
5005
- newAccountPubkey: wrappedSolAccount.publicKey,
5051
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5006
5052
  lamports: lamports,
5007
5053
  space: 165,
5008
5054
  programId: spl_token_1.TOKEN_PROGRAM_ID,
5009
5055
  }),
5010
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, positionAccount.owner),
5056
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, positionAccount.owner),
5011
5057
  ];
5012
5058
  postInstructions = [
5013
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, positionAccount.owner, positionAccount.owner),
5059
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), positionAccount.owner, positionAccount.owner),
5014
5060
  ];
5015
- additionalSigners.push(wrappedSolAccount);
5016
5061
  return [3, 7];
5017
- case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, positionAccount.owner)];
5062
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, positionAccount.owner, true)];
5018
5063
  case 4:
5019
5064
  userReceivingTokenAccount = _b.sent();
5020
5065
  _a = createUserATA;
@@ -5035,7 +5080,7 @@ var PerpetualsClient = (function () {
5035
5080
  })
5036
5081
  .accounts({
5037
5082
  owner: positionAccount.owner,
5038
- receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
5083
+ receivingAccount: collateralSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
5039
5084
  transferAuthority: poolConfig.transferAuthority,
5040
5085
  perpetuals: this.perpetuals.publicKey,
5041
5086
  pool: poolConfig.poolAddress,
@@ -5116,7 +5161,7 @@ var PerpetualsClient = (function () {
5116
5161
  case 0:
5117
5162
  publicKey = this.provider.wallet.publicKey;
5118
5163
  custodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(rewardSymbol).mintKey); });
5119
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(rewardSymbol).mintKey, publicKey)];
5164
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(rewardSymbol).mintKey, publicKey, true)];
5120
5165
  case 1:
5121
5166
  receivingTokenAccount = _a.sent();
5122
5167
  instructions = [];
@@ -5235,7 +5280,7 @@ var PerpetualsClient = (function () {
5235
5280
  admin: this.provider.wallet.publicKey,
5236
5281
  multisig: poolConfig.multisig
5237
5282
  };
5238
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(tokenMint, this.provider.wallet.publicKey)];
5283
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(tokenMint, this.provider.wallet.publicKey, true)];
5239
5284
  case 2: return [4, _e.apply(_d, [(_f.fundingAccount = _g.sent(),
5240
5285
  _f.perpetuals = poolConfig.perpetuals,
5241
5286
  _f.pool = poolConfig.poolAddress,
@@ -5307,145 +5352,151 @@ var PerpetualsClient = (function () {
5307
5352
  }
5308
5353
  });
5309
5354
  }); };
5310
- this.addCompoundingLiquidity = function (amountIn, minCompoundingAmountOut, inTokenSymbol, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5311
- var publicKey, preInstructions, instructions, additionalSigners, postInstructions, rewardCustody, inCustodyConfig, lpTokenMint, compoundingTokenMint, wrappedSolAccount, lpTokenAccount, compoundingTokenAccount, fundingAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, accCreationLamports, lamports, unWrappedSolBalance, _d, addCompoundingLiquidity, err_30;
5312
- return __generator(this, function (_e) {
5313
- switch (_e.label) {
5314
- case 0:
5315
- publicKey = this.provider.wallet.publicKey;
5316
- preInstructions = [];
5317
- instructions = [];
5318
- additionalSigners = [];
5319
- postInstructions = [];
5320
- rewardCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(rewardTokenMint); });
5321
- inCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inTokenSymbol).mintKey); });
5322
- lpTokenMint = poolConfig.stakedLpTokenMint;
5323
- compoundingTokenMint = poolConfig.compoundingTokenMint;
5324
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey)];
5325
- case 1:
5326
- lpTokenAccount = _e.sent();
5327
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey)];
5328
- case 2:
5329
- compoundingTokenAccount = _e.sent();
5330
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(inCustodyConfig.mintKey, publicKey)];
5331
- case 3:
5332
- fundingAccount = _e.sent();
5333
- custodyAccountMetas = [];
5334
- custodyOracleAccountMetas = [];
5335
- markets = [];
5336
- for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
5337
- custody = _a[_i];
5338
- custodyAccountMetas.push({
5339
- pubkey: custody.custodyAccount,
5340
- isSigner: false,
5341
- isWritable: false,
5342
- });
5343
- custodyOracleAccountMetas.push({
5344
- pubkey: this.useExtOracleAccount ? custody.extOracleAccount : custody.intOracleAccount,
5345
- isSigner: false,
5346
- isWritable: false,
5347
- });
5348
- }
5349
- for (_b = 0, _c = poolConfig.markets; _b < _c.length; _b++) {
5350
- market = _c[_b];
5351
- markets.push({
5352
- pubkey: market.marketAccount,
5353
- isSigner: false,
5354
- isWritable: false,
5355
- });
5356
- }
5357
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
5358
- case 4:
5359
- if (!(_e.sent())) {
5360
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
5361
- }
5362
- return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
5363
- case 5:
5364
- if (!(_e.sent())) {
5365
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
5366
- }
5367
- if (!(inTokenSymbol == 'SOL')) return [3, 8];
5368
- console.log("inTokenSymbol === SOL", inTokenSymbol);
5369
- wrappedSolAccount = new web3_js_1.Keypair();
5370
- return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
5371
- case 6:
5372
- accCreationLamports = (_e.sent());
5373
- console.log("accCreationLamports:", accCreationLamports);
5374
- lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
5375
- _d = anchor_1.BN.bind;
5376
- return [4, this.provider.connection.getBalance(publicKey)];
5377
- case 7:
5378
- unWrappedSolBalance = new (_d.apply(anchor_1.BN, [void 0, _e.sent()]))();
5379
- if (unWrappedSolBalance.lt(lamports)) {
5380
- throw "Insufficient SOL Funds";
5381
- }
5382
- preInstructions = [
5383
- web3_js_1.SystemProgram.createAccount({
5384
- fromPubkey: publicKey,
5385
- newAccountPubkey: wrappedSolAccount.publicKey,
5386
- lamports: lamports.toNumber(),
5387
- space: 165,
5388
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5389
- }),
5390
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
5391
- ];
5392
- postInstructions = [
5393
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
5394
- ];
5395
- additionalSigners.push(wrappedSolAccount);
5396
- return [3, 10];
5397
- case 8: return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
5398
- case 9:
5399
- if (!(_e.sent())) {
5400
- throw "Insufficient Funds , token Account doesn't exist";
5401
- }
5402
- _e.label = 10;
5403
- case 10:
5404
- _e.trys.push([10, 12, , 13]);
5405
- return [4, this.program.methods
5406
- .addCompoundingLiquidity({
5407
- amountIn: amountIn,
5408
- minCompoundingAmountOut: minCompoundingAmountOut
5409
- })
5410
- .accounts({
5411
- owner: publicKey,
5412
- fundingAccount: inTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : fundingAccount,
5413
- compoundingTokenAccount: compoundingTokenAccount,
5414
- poolCompoundingLpVault: poolConfig.compoundingLpVault,
5415
- transferAuthority: poolConfig.transferAuthority,
5416
- perpetuals: poolConfig.perpetuals,
5417
- pool: poolConfig.poolAddress,
5418
- inCustody: inCustodyConfig.custodyAccount,
5419
- inCustodyOracleAccount: this.useExtOracleAccount ? inCustodyConfig.extOracleAccount : inCustodyConfig.intOracleAccount,
5420
- inCustodyTokenAccount: inCustodyConfig.tokenAccount,
5421
- rewardCustody: rewardCustody.custodyAccount,
5422
- rewardCustodyOracleAccount: this.useExtOracleAccount ? rewardCustody.extOracleAccount : rewardCustody.intOracleAccount,
5423
- lpTokenMint: lpTokenMint,
5424
- compoundingTokenMint: compoundingTokenMint,
5425
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5426
- eventAuthority: this.eventAuthority.publicKey,
5427
- program: this.program.programId,
5428
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
5429
- })
5430
- .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true))
5431
- .instruction()];
5432
- case 11:
5433
- addCompoundingLiquidity = _e.sent();
5434
- instructions.push(addCompoundingLiquidity);
5435
- return [3, 13];
5436
- case 12:
5437
- err_30 = _e.sent();
5438
- console.log("perpClient addCompoundingLiquidity error:: ", err_30);
5439
- return [3, 13];
5440
- case 13: return [2, {
5441
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5442
- additionalSigners: additionalSigners
5443
- }];
5444
- }
5355
+ this.addCompoundingLiquidity = function (amountIn, minCompoundingAmountOut, inTokenSymbol, rewardTokenMint, poolConfig, ephemeralSignerPubkey) {
5356
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5357
+ return __awaiter(_this, void 0, void 0, function () {
5358
+ var publicKey, preInstructions, instructions, additionalSigners, postInstructions, rewardCustody, inCustodyConfig, lpTokenMint, compoundingTokenMint, wrappedSolAccount, lpTokenAccount, compoundingTokenAccount, fundingAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, accCreationLamports, lamports, unWrappedSolBalance, _d, addCompoundingLiquidity, err_30;
5359
+ return __generator(this, function (_e) {
5360
+ switch (_e.label) {
5361
+ case 0:
5362
+ publicKey = this.provider.wallet.publicKey;
5363
+ preInstructions = [];
5364
+ instructions = [];
5365
+ additionalSigners = [];
5366
+ postInstructions = [];
5367
+ rewardCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(rewardTokenMint); });
5368
+ inCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inTokenSymbol).mintKey); });
5369
+ lpTokenMint = poolConfig.stakedLpTokenMint;
5370
+ compoundingTokenMint = poolConfig.compoundingTokenMint;
5371
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.stakedLpTokenMint, publicKey, true)];
5372
+ case 1:
5373
+ lpTokenAccount = _e.sent();
5374
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey, true)];
5375
+ case 2:
5376
+ compoundingTokenAccount = _e.sent();
5377
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(inCustodyConfig.mintKey, publicKey, true)];
5378
+ case 3:
5379
+ fundingAccount = _e.sent();
5380
+ custodyAccountMetas = [];
5381
+ custodyOracleAccountMetas = [];
5382
+ markets = [];
5383
+ for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
5384
+ custody = _a[_i];
5385
+ custodyAccountMetas.push({
5386
+ pubkey: custody.custodyAccount,
5387
+ isSigner: false,
5388
+ isWritable: false,
5389
+ });
5390
+ custodyOracleAccountMetas.push({
5391
+ pubkey: this.useExtOracleAccount ? custody.extOracleAccount : custody.intOracleAccount,
5392
+ isSigner: false,
5393
+ isWritable: false,
5394
+ });
5395
+ }
5396
+ for (_b = 0, _c = poolConfig.markets; _b < _c.length; _b++) {
5397
+ market = _c[_b];
5398
+ markets.push({
5399
+ pubkey: market.marketAccount,
5400
+ isSigner: false,
5401
+ isWritable: false,
5402
+ });
5403
+ }
5404
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
5405
+ case 4:
5406
+ if (!(_e.sent())) {
5407
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
5408
+ }
5409
+ return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
5410
+ case 5:
5411
+ if (!(_e.sent())) {
5412
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
5413
+ }
5414
+ if (!(inTokenSymbol == 'SOL')) return [3, 8];
5415
+ console.log("inTokenSymbol === SOL", inTokenSymbol);
5416
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
5417
+ case 6:
5418
+ accCreationLamports = (_e.sent());
5419
+ console.log("accCreationLamports:", accCreationLamports);
5420
+ lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
5421
+ _d = anchor_1.BN.bind;
5422
+ return [4, this.provider.connection.getBalance(publicKey)];
5423
+ case 7:
5424
+ unWrappedSolBalance = new (_d.apply(anchor_1.BN, [void 0, _e.sent()]))();
5425
+ if (unWrappedSolBalance.lt(lamports)) {
5426
+ throw "Insufficient SOL Funds";
5427
+ }
5428
+ if (!ephemeralSignerPubkey) {
5429
+ wrappedSolAccount = new web3_js_1.Keypair();
5430
+ additionalSigners.push(wrappedSolAccount);
5431
+ }
5432
+ preInstructions = [
5433
+ web3_js_1.SystemProgram.createAccount({
5434
+ fromPubkey: publicKey,
5435
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5436
+ lamports: lamports.toNumber(),
5437
+ space: 165,
5438
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5439
+ }),
5440
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5441
+ ];
5442
+ postInstructions = [
5443
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5444
+ ];
5445
+ return [3, 10];
5446
+ case 8: return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
5447
+ case 9:
5448
+ if (!(_e.sent())) {
5449
+ throw "Insufficient Funds , token Account doesn't exist";
5450
+ }
5451
+ _e.label = 10;
5452
+ case 10:
5453
+ _e.trys.push([10, 12, , 13]);
5454
+ return [4, this.program.methods
5455
+ .addCompoundingLiquidity({
5456
+ amountIn: amountIn,
5457
+ minCompoundingAmountOut: minCompoundingAmountOut
5458
+ })
5459
+ .accounts({
5460
+ owner: publicKey,
5461
+ fundingAccount: inTokenSymbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : fundingAccount,
5462
+ compoundingTokenAccount: compoundingTokenAccount,
5463
+ poolCompoundingLpVault: poolConfig.compoundingLpVault,
5464
+ transferAuthority: poolConfig.transferAuthority,
5465
+ perpetuals: poolConfig.perpetuals,
5466
+ pool: poolConfig.poolAddress,
5467
+ inCustody: inCustodyConfig.custodyAccount,
5468
+ inCustodyOracleAccount: this.useExtOracleAccount ? inCustodyConfig.extOracleAccount : inCustodyConfig.intOracleAccount,
5469
+ inCustodyTokenAccount: inCustodyConfig.tokenAccount,
5470
+ rewardCustody: rewardCustody.custodyAccount,
5471
+ rewardCustodyOracleAccount: this.useExtOracleAccount ? rewardCustody.extOracleAccount : rewardCustody.intOracleAccount,
5472
+ lpTokenMint: lpTokenMint,
5473
+ compoundingTokenMint: compoundingTokenMint,
5474
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5475
+ eventAuthority: this.eventAuthority.publicKey,
5476
+ program: this.program.programId,
5477
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
5478
+ })
5479
+ .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true))
5480
+ .instruction()];
5481
+ case 11:
5482
+ addCompoundingLiquidity = _e.sent();
5483
+ instructions.push(addCompoundingLiquidity);
5484
+ return [3, 13];
5485
+ case 12:
5486
+ err_30 = _e.sent();
5487
+ console.log("perpClient addCompoundingLiquidity error:: ", err_30);
5488
+ return [3, 13];
5489
+ case 13: return [2, {
5490
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5491
+ additionalSigners: additionalSigners
5492
+ }];
5493
+ }
5494
+ });
5445
5495
  });
5446
- }); };
5447
- this.removeCompoundingLiquidity = function (compoundingAmountIn, minAmountOut, outTokenSymbol, rewardTokenMint, poolConfig, createUserATA) {
5496
+ };
5497
+ this.removeCompoundingLiquidity = function (compoundingAmountIn, minAmountOut, outTokenSymbol, rewardTokenMint, poolConfig, createUserATA, ephemeralSignerPubkey) {
5448
5498
  if (createUserATA === void 0) { createUserATA = true; }
5499
+ if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5449
5500
  return __awaiter(_this, void 0, void 0, function () {
5450
5501
  var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, _a, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _b, custody, _c, _d, market, compoundingTokenAccount, receivingAccount, removeCompoundingLiquidity, err_31;
5451
5502
  return __generator(this, function (_e) {
@@ -5461,27 +5512,28 @@ var PerpetualsClient = (function () {
5461
5512
  lpTokenMint = poolConfig.stakedLpTokenMint;
5462
5513
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5463
5514
  if (!(outCustodyConfig.symbol == 'SOL')) return [3, 2];
5464
- wrappedSolAccount = new web3_js_1.Keypair();
5465
- userReceivingTokenAccount = wrappedSolAccount.publicKey;
5466
5515
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
5467
5516
  case 1:
5468
5517
  lamports = (_e.sent());
5518
+ if (!ephemeralSignerPubkey) {
5519
+ wrappedSolAccount = new web3_js_1.Keypair();
5520
+ additionalSigners.push(wrappedSolAccount);
5521
+ }
5469
5522
  preInstructions = [
5470
5523
  web3_js_1.SystemProgram.createAccount({
5471
5524
  fromPubkey: publicKey,
5472
- newAccountPubkey: wrappedSolAccount.publicKey,
5525
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5473
5526
  lamports: lamports,
5474
5527
  space: 165,
5475
5528
  programId: spl_token_1.TOKEN_PROGRAM_ID,
5476
5529
  }),
5477
- (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
5530
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5478
5531
  ];
5479
5532
  postInstructions = [
5480
- (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
5533
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5481
5534
  ];
5482
- additionalSigners.push(wrappedSolAccount);
5483
5535
  return [3, 6];
5484
- case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(outCustodyConfig.mintKey, publicKey)];
5536
+ case 2: return [4, (0, spl_token_1.getAssociatedTokenAddress)(outCustodyConfig.mintKey, publicKey, true)];
5485
5537
  case 3:
5486
5538
  userReceivingTokenAccount = _e.sent();
5487
5539
  _a = createUserATA;
@@ -5520,10 +5572,10 @@ var PerpetualsClient = (function () {
5520
5572
  isWritable: false,
5521
5573
  });
5522
5574
  }
5523
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey)];
5575
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey, true)];
5524
5576
  case 7:
5525
5577
  compoundingTokenAccount = _e.sent();
5526
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(outCustodyConfig.mintKey, publicKey)];
5578
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(outCustodyConfig.mintKey, publicKey, true)];
5527
5579
  case 8:
5528
5580
  receivingAccount = _e.sent();
5529
5581
  _e.label = 9;
@@ -5536,7 +5588,7 @@ var PerpetualsClient = (function () {
5536
5588
  })
5537
5589
  .accounts({
5538
5590
  owner: publicKey,
5539
- receivingAccount: outCustodyConfig.symbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
5591
+ receivingAccount: outCustodyConfig.symbol == 'SOL' ? (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey) : userReceivingTokenAccount,
5540
5592
  compoundingTokenAccount: compoundingTokenAccount,
5541
5593
  poolCompoundingLpVault: poolConfig.compoundingLpVault,
5542
5594
  transferAuthority: poolConfig.transferAuthority,
@@ -5587,7 +5639,7 @@ var PerpetualsClient = (function () {
5587
5639
  rewardCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(rewardTokenMint); });
5588
5640
  lpTokenMint = poolConfig.stakedLpTokenMint;
5589
5641
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5590
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey)];
5642
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey, true)];
5591
5643
  case 1:
5592
5644
  compoudingTokenAccount = _e.sent();
5593
5645
  _a = createUserATA;
@@ -5682,7 +5734,7 @@ var PerpetualsClient = (function () {
5682
5734
  rewardCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(rewardTokenMint); });
5683
5735
  lpTokenMint = poolConfig.stakedLpTokenMint;
5684
5736
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5685
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey)];
5737
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(compoundingTokenMint, publicKey, true)];
5686
5738
  case 1:
5687
5739
  compoudingTokenAccount = _d.sent();
5688
5740
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
@@ -5965,7 +6017,7 @@ var PerpetualsClient = (function () {
5965
6017
  _a.label = 1;
5966
6018
  case 1:
5967
6019
  _a.trys.push([1, 4, , 5]);
5968
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey)];
6020
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey, true)];
5969
6021
  case 2:
5970
6022
  fundingAccount = _a.sent();
5971
6023
  rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programRewardDistribution.programId)[0];
@@ -6019,7 +6071,7 @@ var PerpetualsClient = (function () {
6019
6071
  _b.label = 1;
6020
6072
  case 1:
6021
6073
  _b.trys.push([1, 6, , 7]);
6022
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey)];
6074
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(rewardCustodyMint, publicKey, true)];
6023
6075
  case 2:
6024
6076
  receivingTokenAccount = _b.sent();
6025
6077
  _a = createUserATA;