flash-sdk 2.0.51 → 2.0.53

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.
@@ -2184,6 +2184,7 @@ export declare class PerpetualsClient {
2184
2184
  getCumulativeLockFeeSync: (custodyAccount: CustodyAccount, currentTimestamp: BN) => BN;
2185
2185
  getLockFeeAndUnsettledUsdForPosition: (position: PositionAccount, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN) => BN;
2186
2186
  getLiquidationPriceSync: (collateralAmount: BN, sizeAmount: BN, entryPriceUsd: BN, lockAndUnsettledFeeUsd: BN, marketCorrelation: boolean, side: Side, custodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, positionAccount: PositionAccount) => BN;
2187
+ getMaxProfitPriceSync: (entryPriceUsd: BN, marketCorrelation: boolean, side: Side, positionAccount: PositionAccount) => BN;
2187
2188
  getPnlSync: (positionAccount: PositionAccount, targetTokenPrice: OraclePrice, targetTokenEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN, delay: BN, poolConfig: PoolConfig) => {
2188
2189
  profitUsd: BN;
2189
2190
  lossUsd: BN;
@@ -2304,6 +2305,14 @@ export declare class PerpetualsClient {
2304
2305
  instructions: TransactionInstruction[];
2305
2306
  additionalSigners: Signer[];
2306
2307
  }>;
2308
+ setTriggerPrice: (targetSymbol: string, collateralSymbol: string, side: Side, triggerPrice: ContractOraclePrice, isStopLoss: boolean, poolConfig: PoolConfig) => Promise<{
2309
+ instructions: TransactionInstruction[];
2310
+ additionalSigners: Signer[];
2311
+ }>;
2312
+ forceClosePosition: (targetSymbol: string, collateralSymbol: string, side: Side, isStopLoss: boolean, poolConfig: PoolConfig, nftTradingAccount: PublicKey, nftReferralAccount: PublicKey, nftRebateTokenAccount: PublicKey, privilege: Privilege, createUserATA?: boolean, closeUsersWSOLATA?: boolean) => Promise<{
2313
+ instructions: TransactionInstruction[];
2314
+ additionalSigners: Signer[];
2315
+ }>;
2307
2316
  setPoolConfig: (permissions: Permissions, oracleAuthority: PublicKey, maxAumUsd: BN, stakingFeeShareBps: BN, poolConfig: PoolConfig) => Promise<{
2308
2317
  instructions: TransactionInstruction[];
2309
2318
  additionalSigners: Signer[];
@@ -1150,9 +1150,8 @@ var PerpetualsClient = (function () {
1150
1150
  }
1151
1151
  newPosition.collateralAmount = newPosition.collateralAmount.sub(collateralAmountReceived);
1152
1152
  newPosition.collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(newPosition.collateralAmount, collateralCustodyAccount.decimals);
1153
- var entryPrice = OraclePrice_1.OraclePrice.from({ price: newPosition.entryPrice.price, exponent: new anchor_1.BN(newPosition.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1154
1153
  var finalInterestUsd = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
1155
- var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice.scale_to_exponent(new anchor_1.BN(constants_1.USD_DECIMALS).neg()).price, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
1154
+ var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, newPosition.entryPrice.price, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
1156
1155
  var finalPnl = _this.getPnlSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1157
1156
  var finalPnlUsd = finalPnl.profitUsd.sub(finalPnl.lossUsd);
1158
1157
  var newExitFee = _this.getExitFeeSync(newPosition, targetCustodyAccount, collateralCustodyAccount, collateralPrice, collateralEmaPrice);
@@ -1170,9 +1169,8 @@ var PerpetualsClient = (function () {
1170
1169
  };
1171
1170
  }
1172
1171
  else {
1173
- var entryPrice = OraclePrice_1.OraclePrice.from({ price: newPosition.entryPrice.price, exponent: new anchor_1.BN(newPosition.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1174
1172
  var finalInterestUsd = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
1175
- var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice.scale_to_exponent(new anchor_1.BN(constants_1.USD_DECIMALS).neg()).price, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
1173
+ var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, newPosition.entryPrice.price, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
1176
1174
  var finalPnl = _this.getPnlSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1177
1175
  var finalPnlUsd = finalPnl.profitUsd.sub(finalPnl.lossUsd);
1178
1176
  var exitFee = _this.getExitFeeSync(newPosition, targetCustodyAccount, collateralCustodyAccount, collateralPrice, collateralEmaPrice);
@@ -1324,6 +1322,23 @@ var PerpetualsClient = (function () {
1324
1322
  }
1325
1323
  return liquidationPrice.isNeg() ? constants_1.BN_ZERO : liquidationPrice;
1326
1324
  };
1325
+ this.getMaxProfitPriceSync = function (entryPriceUsd, marketCorrelation, side, positionAccount) {
1326
+ if (positionAccount.sizeAmount.isZero()) {
1327
+ return constants_1.BN_ZERO;
1328
+ }
1329
+ var priceDiffProfit = positionAccount.lockedUsd.mul(new anchor_1.BN(positionAccount.sizeDecimals)).div(positionAccount.sizeAmount);
1330
+ var maxProfitPrice = constants_1.BN_ZERO;
1331
+ if ((0, types_1.isVariant)(side, 'long')) {
1332
+ if (marketCorrelation) {
1333
+ return constants_1.BN_ZERO;
1334
+ }
1335
+ maxProfitPrice = entryPriceUsd.add(priceDiffProfit);
1336
+ }
1337
+ else {
1338
+ maxProfitPrice = entryPriceUsd.sub(priceDiffProfit);
1339
+ }
1340
+ return maxProfitPrice.isNeg() ? constants_1.BN_ZERO : maxProfitPrice;
1341
+ };
1327
1342
  this.getPnlSync = function (positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, delay, poolConfig) {
1328
1343
  if (positionAccount.sizeUsd.isZero() || positionAccount.entryPrice.price.isZero()) {
1329
1344
  return {
@@ -3752,8 +3767,157 @@ var PerpetualsClient = (function () {
3752
3767
  });
3753
3768
  });
3754
3769
  };
3770
+ this.setTriggerPrice = function (targetSymbol, collateralSymbol, side, triggerPrice, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
3771
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, positionAccount, instructions, additionalSigners, setTriggerPrice, err_19;
3772
+ return __generator(this, function (_a) {
3773
+ switch (_a.label) {
3774
+ case 0:
3775
+ publicKey = this.provider.wallet.publicKey;
3776
+ targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3777
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3778
+ marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3779
+ positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3780
+ instructions = [];
3781
+ additionalSigners = [];
3782
+ _a.label = 1;
3783
+ case 1:
3784
+ _a.trys.push([1, 3, , 4]);
3785
+ return [4, this.program.methods
3786
+ .setTriggerPrice({
3787
+ triggerPrice: triggerPrice,
3788
+ isStopLoss: isStopLoss
3789
+ })
3790
+ .accounts({
3791
+ owner: publicKey,
3792
+ perpetuals: this.perpetuals.publicKey,
3793
+ pool: poolConfig.poolAddress,
3794
+ position: positionAccount,
3795
+ market: marketAccount,
3796
+ targetCustody: targetCustodyConfig.custodyAccount,
3797
+ targetOracleAccount: targetCustodyConfig.oracleAddress,
3798
+ collateralCustody: collateralCustodyConfig.custodyAccount,
3799
+ collateralOracleAccount: collateralCustodyConfig.oracleAddress,
3800
+ eventAuthority: this.eventAuthority.publicKey,
3801
+ program: this.programId,
3802
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3803
+ })
3804
+ .instruction()];
3805
+ case 2:
3806
+ setTriggerPrice = _a.sent();
3807
+ instructions.push(setTriggerPrice);
3808
+ return [3, 4];
3809
+ case 3:
3810
+ err_19 = _a.sent();
3811
+ console.log("perpClient setTriggerPrice error:: ", err_19);
3812
+ throw err_19;
3813
+ case 4: return [2, {
3814
+ instructions: __spreadArray([], instructions, true),
3815
+ additionalSigners: additionalSigners
3816
+ }];
3817
+ }
3818
+ });
3819
+ }); };
3820
+ this.forceClosePosition = function (targetSymbol, collateralSymbol, side, isStopLoss, poolConfig, nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, privilege, createUserATA, closeUsersWSOLATA) {
3821
+ if (createUserATA === void 0) { createUserATA = true; }
3822
+ if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3823
+ return __awaiter(_this, void 0, void 0, function () {
3824
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, positionAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, forceClosePosition, closeWsolATAIns, err_20;
3825
+ return __generator(this, function (_b) {
3826
+ switch (_b.label) {
3827
+ case 0:
3828
+ publicKey = this.provider.wallet.publicKey;
3829
+ targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3830
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3831
+ marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3832
+ positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3833
+ preInstructions = [];
3834
+ instructions = [];
3835
+ postInstructions = [];
3836
+ additionalSigners = [];
3837
+ _b.label = 1;
3838
+ case 1:
3839
+ _b.trys.push([1, 9, , 10]);
3840
+ if (!(collateralSymbol == 'SOL')) return [3, 3];
3841
+ wrappedSolAccount = new web3_js_1.Keypair();
3842
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
3843
+ case 2:
3844
+ lamports = (_b.sent());
3845
+ preInstructions = [
3846
+ web3_js_1.SystemProgram.createAccount({
3847
+ fromPubkey: publicKey,
3848
+ newAccountPubkey: wrappedSolAccount.publicKey,
3849
+ lamports: lamports,
3850
+ space: 165,
3851
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3852
+ }),
3853
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
3854
+ ];
3855
+ postInstructions = [
3856
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
3857
+ ];
3858
+ additionalSigners.push(wrappedSolAccount);
3859
+ return [3, 7];
3860
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
3861
+ case 4:
3862
+ userReceivingTokenAccount = _b.sent();
3863
+ _a = createUserATA;
3864
+ if (!_a) return [3, 6];
3865
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3866
+ case 5:
3867
+ _a = !(_b.sent());
3868
+ _b.label = 6;
3869
+ case 6:
3870
+ if (_a) {
3871
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
3872
+ }
3873
+ _b.label = 7;
3874
+ case 7: return [4, this.program.methods
3875
+ .forceClosePosition({
3876
+ privilege: privilege,
3877
+ isStopLoss: isStopLoss
3878
+ })
3879
+ .accounts({
3880
+ owner: publicKey,
3881
+ receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
3882
+ transferAuthority: poolConfig.transferAuthority,
3883
+ perpetuals: this.perpetuals.publicKey,
3884
+ pool: poolConfig.poolAddress,
3885
+ position: positionAccount,
3886
+ market: marketAccount,
3887
+ targetCustody: targetCustodyConfig.custodyAccount,
3888
+ targetOracleAccount: targetCustodyConfig.oracleAddress,
3889
+ collateralCustody: collateralCustodyConfig.custodyAccount,
3890
+ collateralOracleAccount: collateralCustodyConfig.oracleAddress,
3891
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
3892
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
3893
+ eventAuthority: this.eventAuthority.publicKey,
3894
+ program: this.programId,
3895
+ ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
3896
+ })
3897
+ .remainingAccounts(__spreadArray([], (0, getNftAccounts_1.getNftAccounts)(nftTradingAccount, nftReferralAccount, nftRebateTokenAccount, privilege), true))
3898
+ .instruction()];
3899
+ case 8:
3900
+ forceClosePosition = _b.sent();
3901
+ instructions.push(forceClosePosition);
3902
+ if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3903
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3904
+ postInstructions.push(closeWsolATAIns);
3905
+ }
3906
+ return [3, 10];
3907
+ case 9:
3908
+ err_20 = _b.sent();
3909
+ console.log("perpClient forceClosePosition error:: ", err_20);
3910
+ throw err_20;
3911
+ case 10: return [2, {
3912
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3913
+ additionalSigners: additionalSigners
3914
+ }];
3915
+ }
3916
+ });
3917
+ });
3918
+ };
3755
3919
  this.setPoolConfig = function (permissions, oracleAuthority, maxAumUsd, stakingFeeShareBps, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
3756
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPoolConfigInstruction, err_19;
3920
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPoolConfigInstruction, err_21;
3757
3921
  return __generator(this, function (_a) {
3758
3922
  switch (_a.label) {
3759
3923
  case 0:
@@ -3783,9 +3947,9 @@ var PerpetualsClient = (function () {
3783
3947
  instructions.push(setPoolConfigInstruction);
3784
3948
  return [3, 4];
3785
3949
  case 3:
3786
- err_19 = _a.sent();
3787
- console.log("perpClient setPool error:: ", err_19);
3788
- throw err_19;
3950
+ err_21 = _a.sent();
3951
+ console.log("perpClient setPool error:: ", err_21);
3952
+ throw err_21;
3789
3953
  case 4: return [2, {
3790
3954
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3791
3955
  additionalSigners: additionalSigners
@@ -3794,7 +3958,7 @@ var PerpetualsClient = (function () {
3794
3958
  });
3795
3959
  }); };
3796
3960
  this.setPermissions = function (permissions) { return __awaiter(_this, void 0, void 0, function () {
3797
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPermissionsInstruction, err_20;
3961
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPermissionsInstruction, err_22;
3798
3962
  return __generator(this, function (_a) {
3799
3963
  switch (_a.label) {
3800
3964
  case 0:
@@ -3821,9 +3985,9 @@ var PerpetualsClient = (function () {
3821
3985
  instructions.push(setPermissionsInstruction);
3822
3986
  return [3, 4];
3823
3987
  case 3:
3824
- err_20 = _a.sent();
3825
- console.log("perpClient setPool error:: ", err_20);
3826
- throw err_20;
3988
+ err_22 = _a.sent();
3989
+ console.log("perpClient setPool error:: ", err_22);
3990
+ throw err_22;
3827
3991
  case 4: return [2, {
3828
3992
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3829
3993
  additionalSigners: additionalSigners