flash-sdk 2.28.12 → 2.29.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.
@@ -3,7 +3,7 @@ import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
3
3
  import { PublicKey, TransactionInstruction, Commitment, Signer, AddressLookupTableAccount, VersionedTransaction } from "@solana/web3.js";
4
4
  import { PoolAccount } from "./PoolAccount";
5
5
  import { PositionAccount } from "./PositionAccount";
6
- import { AddLiquidityAmountAndFee, BorrowRateParams, EntryPriceAndFee, ExitPriceAndFee, Fees, OracleParams, Permissions, PricingParams, RemoveCollateralData, RemoveLiquidityAmountAndFee, Side, TokenRatios, MinAndMaxPrice, FeesAction, ContractOraclePrice, Privilege, PerpetualsAccount, Trading } from "./types";
6
+ import { AddLiquidityAmountAndFee, BorrowRateParams, ExitPriceAndFee, Fees, OracleParams, Permissions, PricingParams, RemoveCollateralData, RemoveLiquidityAmountAndFee, Side, TokenRatios, MinAndMaxPrice, FeesAction, ContractOraclePrice, Privilege, PerpetualsAccount, Trading, EntryPriceAndFeeV2, EntryPriceAndFee } from "./types";
7
7
  import { OraclePrice } from "./OraclePrice";
8
8
  import { CustodyAccount } from "./CustodyAccount";
9
9
  import { Perpetuals } from "./idl/perpetuals";
@@ -165,7 +165,7 @@ export declare class PerpetualsClient {
165
165
  };
166
166
  openPosition: BN;
167
167
  closePosition: BN;
168
- removeCollateral: BN;
168
+ volatility: BN;
169
169
  };
170
170
  borrowRate: {
171
171
  baseRate: BN;
@@ -454,7 +454,7 @@ export declare class PerpetualsClient {
454
454
  };
455
455
  openPosition: BN;
456
456
  closePosition: BN;
457
- removeCollateral: BN;
457
+ volatility: BN;
458
458
  };
459
459
  borrowRate: {
460
460
  baseRate: BN;
@@ -742,7 +742,7 @@ export declare class PerpetualsClient {
742
742
  };
743
743
  openPosition: BN;
744
744
  closePosition: BN;
745
- removeCollateral: BN;
745
+ volatility: BN;
746
746
  };
747
747
  borrowRate: {
748
748
  baseRate: BN;
@@ -1034,7 +1034,7 @@ export declare class PerpetualsClient {
1034
1034
  };
1035
1035
  openPosition: BN;
1036
1036
  closePosition: BN;
1037
- removeCollateral: BN;
1037
+ volatility: BN;
1038
1038
  };
1039
1039
  borrowRate: {
1040
1040
  baseRate: BN;
@@ -1325,7 +1325,7 @@ export declare class PerpetualsClient {
1325
1325
  };
1326
1326
  openPosition: BN;
1327
1327
  closePosition: BN;
1328
- removeCollateral: BN;
1328
+ volatility: BN;
1329
1329
  };
1330
1330
  borrowRate: {
1331
1331
  baseRate: BN;
@@ -1613,7 +1613,7 @@ export declare class PerpetualsClient {
1613
1613
  };
1614
1614
  openPosition: BN;
1615
1615
  closePosition: BN;
1616
- removeCollateral: BN;
1616
+ volatility: BN;
1617
1617
  };
1618
1618
  borrowRate: {
1619
1619
  baseRate: BN;
@@ -1901,7 +1901,7 @@ export declare class PerpetualsClient {
1901
1901
  };
1902
1902
  openPosition: BN;
1903
1903
  closePosition: BN;
1904
- removeCollateral: BN;
1904
+ volatility: BN;
1905
1905
  };
1906
1906
  borrowRate: {
1907
1907
  baseRate: BN;
@@ -2189,7 +2189,7 @@ export declare class PerpetualsClient {
2189
2189
  };
2190
2190
  openPosition: BN;
2191
2191
  closePosition: BN;
2192
- removeCollateral: BN;
2192
+ volatility: BN;
2193
2193
  };
2194
2194
  borrowRate: {
2195
2195
  baseRate: BN;
@@ -2527,7 +2527,7 @@ export declare class PerpetualsClient {
2527
2527
  };
2528
2528
  openPosition: BN;
2529
2529
  closePosition: BN;
2530
- removeCollateral: BN;
2530
+ volatility: BN;
2531
2531
  };
2532
2532
  borrowRate: {
2533
2533
  baseRate: BN;
@@ -2815,7 +2815,7 @@ export declare class PerpetualsClient {
2815
2815
  };
2816
2816
  openPosition: BN;
2817
2817
  closePosition: BN;
2818
- removeCollateral: BN;
2818
+ volatility: BN;
2819
2819
  };
2820
2820
  borrowRate: {
2821
2821
  baseRate: BN;
@@ -3103,7 +3103,7 @@ export declare class PerpetualsClient {
3103
3103
  };
3104
3104
  openPosition: BN;
3105
3105
  closePosition: BN;
3106
- removeCollateral: BN;
3106
+ volatility: BN;
3107
3107
  };
3108
3108
  borrowRate: {
3109
3109
  baseRate: BN;
@@ -3391,7 +3391,7 @@ export declare class PerpetualsClient {
3391
3391
  };
3392
3392
  openPosition: BN;
3393
3393
  closePosition: BN;
3394
- removeCollateral: BN;
3394
+ volatility: BN;
3395
3395
  };
3396
3396
  borrowRate: {
3397
3397
  baseRate: BN;
@@ -3679,7 +3679,7 @@ export declare class PerpetualsClient {
3679
3679
  };
3680
3680
  openPosition: BN;
3681
3681
  closePosition: BN;
3682
- removeCollateral: BN;
3682
+ volatility: BN;
3683
3683
  };
3684
3684
  borrowRate: {
3685
3685
  baseRate: BN;
@@ -3904,6 +3904,7 @@ export declare class PerpetualsClient {
3904
3904
  getLeverageSync: (sizeUsd: BN, collateralAmount: BN, collateralMinOraclePrice: OraclePrice, collateralTokenDecimals: number, pnlUsd: BN) => BN;
3905
3905
  getLeverageAtAmountEntryWithSwapSync: (positionAccount: PositionAccount | null, inputDeltaAmount: BN, sizeDeltaAmount: BN, side: Side, poolAccount: PoolAccount, inputTokenPrice: OraclePrice, inputTokenEmaPrice: OraclePrice, inputTokenCustodyAccount: CustodyAccount, swapOutTokenPrice: OraclePrice, swapOutTokenEmaPrice: OraclePrice, swapOutTokenCustodyAccount: CustodyAccount, collateralTokenPrice: OraclePrice, collateralTokenEmaPrice: OraclePrice, collateralTokenCustodyAccount: CustodyAccount, targetTokenPrice: OraclePrice, targetTokenEmaPrice: OraclePrice, targetTokenCustodyAccount: CustodyAccount, swapPoolAumUsdMax: BN, poolConfigPosition: PoolConfig, poolConfigSwap: PoolConfig, pnlUsd: BN) => BN;
3906
3906
  getEntryPriceAndFeeSync: (positionAccount: PositionAccount | null, marketCorrelation: boolean, collateralDeltaAmount: BN, sizeDeltaAmount: BN, side: Side, targetPrice: OraclePrice, targetEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN, discountBps?: BN) => EntryPriceAndFee;
3907
+ getEntryPriceAndFeeSyncV2: (positionAccount: PositionAccount | null, marketCorrelation: boolean, collateralDeltaAmount: BN, sizeDeltaAmount: BN, side: Side, targetPrice: OraclePrice, targetEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, collateralCustodyAccount: CustodyAccount, currentTimestamp: BN, discountBps?: BN) => EntryPriceAndFeeV2;
3907
3908
  getEntryPriceUsdSync: (side: Side, targetPrice: OraclePrice, targetEmaPrice: OraclePrice, targetCustodyAccount: CustodyAccount, lockedUsd: BN) => OraclePrice;
3908
3909
  getPriceAfterSlippage(isEntry: boolean, slippageBps: BN, targetPrice: OraclePrice, side: Side): ContractOraclePrice;
3909
3910
  getExitFeeSync: (positionAccount: PositionAccount, targetCustody: CustodyAccount, collateralCustodyAccount: CustodyAccount, collateralPrice: OraclePrice, collateralEmaPrice: OraclePrice, discountBps?: BN) => {
@@ -970,6 +970,75 @@ var PerpetualsClient = (function () {
970
970
  liquidationPrice: liquidationPrice
971
971
  };
972
972
  };
973
+ this.getEntryPriceAndFeeSyncV2 = function (positionAccount, marketCorrelation, collateralDeltaAmount, sizeDeltaAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, discountBps) {
974
+ if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
975
+ if (collateralDeltaAmount.isNeg() || sizeDeltaAmount.isNeg()) {
976
+ throw new Error("Delta Amounts cannot be negative.");
977
+ }
978
+ var lockedUsd = targetPrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
979
+ var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount, lockedUsd);
980
+ if (positionAccount === null) {
981
+ var data = __assign({}, types_1.DEFAULT_POSITION);
982
+ positionAccount = PositionAccount_1.PositionAccount.from(web3_js_1.PublicKey.default, data);
983
+ var sizeUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
984
+ positionAccount.sizeUsd = sizeUsd;
985
+ positionAccount.sizeDecimals = targetCustodyAccount.decimals;
986
+ positionAccount.collateralDecimals = collateralCustodyAccount.decimals;
987
+ positionAccount.lockedDecimals = collateralCustodyAccount.decimals;
988
+ }
989
+ else {
990
+ positionAccount = positionAccount.clone();
991
+ var positionEntryPrice = OraclePrice_1.OraclePrice.from({
992
+ price: positionAccount.entryPrice.price,
993
+ exponent: new anchor_1.BN(positionAccount.entryPrice.exponent),
994
+ confidence: constants_1.BN_ZERO,
995
+ timestamp: constants_1.BN_ZERO
996
+ });
997
+ entryOraclePrice.price = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryOraclePrice.price, sizeDeltaAmount);
998
+ var sizeDeltaUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
999
+ positionAccount.sizeUsd = positionAccount.sizeUsd.add(sizeDeltaUsd);
1000
+ }
1001
+ positionAccount.collateralAmount = positionAccount.collateralAmount.add(collateralDeltaAmount);
1002
+ positionAccount.sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
1003
+ var lockFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
1004
+ var liquidationPrice = _this.getLiquidationPriceSync(positionAccount.collateralAmount, positionAccount.sizeAmount, entryOraclePrice, lockFeeUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
1005
+ var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
1006
+ var collateralTokenMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
1007
+ var feeUsd = constants_1.BN_ZERO;
1008
+ var feeAmount = constants_1.BN_ZERO;
1009
+ var feeUsdAfterDiscount = constants_1.BN_ZERO;
1010
+ var feeAmountAfterDiscount = constants_1.BN_ZERO;
1011
+ if (positionAccount !== null && sizeDeltaAmount.isZero()) {
1012
+ }
1013
+ else {
1014
+ feeUsd = sizeAmountUsd.mul(targetCustodyAccount.fees.openPosition).div(new anchor_1.BN(constants_1.RATE_POWER));
1015
+ feeAmount = feeUsd.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
1016
+ if (discountBps.gt(constants_1.BN_ZERO)) {
1017
+ feeUsdAfterDiscount = feeUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
1018
+ feeUsdAfterDiscount = feeUsd.sub(feeUsdAfterDiscount);
1019
+ feeAmountAfterDiscount = feeUsdAfterDiscount.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
1020
+ }
1021
+ else {
1022
+ feeUsdAfterDiscount = feeUsd;
1023
+ feeAmountAfterDiscount = feeAmount;
1024
+ }
1025
+ }
1026
+ var divergenceBps;
1027
+ divergenceBps = targetPrice.getDivergenceFactor(targetEmaPrice);
1028
+ var vbFeeUsd = constants_1.BN_ZERO;
1029
+ if (divergenceBps.gte(targetCustodyAccount.oracle.maxDivergenceBps)) {
1030
+ vbFeeUsd = targetCustodyAccount.fees.volatility.mul(sizeAmountUsd);
1031
+ }
1032
+ return {
1033
+ entryOraclePrice: entryOraclePrice,
1034
+ feeUsd: feeUsd,
1035
+ feeAmount: feeAmount,
1036
+ vbFeeUsd: vbFeeUsd,
1037
+ feeUsdAfterDiscount: feeUsdAfterDiscount,
1038
+ feeAmountAfterDiscount: feeAmountAfterDiscount,
1039
+ liquidationPrice: liquidationPrice
1040
+ };
1041
+ };
973
1042
  this.getEntryPriceUsdSync = function (side, targetPrice, targetEmaPrice, targetCustodyAccount, lockedUsd) {
974
1043
  var _a = _this.getMinAndMaxOraclePriceSync(targetPrice, targetEmaPrice, targetCustodyAccount), minPrice = _a.min, maxPrice = _a.max;
975
1044
  var spread = _this.getTradeSpread(targetCustodyAccount, lockedUsd);
@@ -1239,10 +1308,9 @@ var PerpetualsClient = (function () {
1239
1308
  collateralAmountReceived = maxWithdrawableAmount;
1240
1309
  collateralAmountRecievedUsd = collateralMinMaxPrice.min.getAssetAmountUsd(maxWithdrawableAmount, collateralCustodyAccount.decimals);
1241
1310
  }
1242
- var collateralFeeUsd = collateralAmountRecievedUsd.mul(targetCustodyAccount.fees.removeCollateral).div(new anchor_1.BN(constants_1.RATE_POWER));
1243
1311
  if (discountBps.gt(constants_1.BN_ZERO)) {
1244
- var discount = collateralFeeUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
1245
- collateralFeeUsd = exitFeeUsd.sub(discount);
1312
+ var discount = collateralAmountRecievedUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
1313
+ collateralAmountRecievedUsd = exitFeeUsd.sub(discount);
1246
1314
  }
1247
1315
  newPosition.collateralAmount = newPosition.collateralAmount.sub(collateralAmountReceived);
1248
1316
  newPosition.collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(newPosition.collateralAmount, collateralCustodyAccount.decimals);
@@ -1256,8 +1324,8 @@ var PerpetualsClient = (function () {
1256
1324
  var newLev = _this.getLeverageSync(newPosition.sizeUsd, newPosition.collateralAmount, collateralMinMaxPrice.min, collateralCustodyAccount.decimals, finalPnlUsdWithFee);
1257
1325
  return {
1258
1326
  newSizeUsd: newPosition.sizeUsd,
1259
- feeUsd: feeUsd.add(collateralFeeUsd),
1260
- feeUsdWithDiscount: feeUsdWithDiscount.add(collateralFeeUsd),
1327
+ feeUsd: feeUsd.add(collateralAmountRecievedUsd),
1328
+ feeUsdWithDiscount: feeUsdWithDiscount.add(collateralAmountRecievedUsd),
1261
1329
  newLev: newLev,
1262
1330
  liquidationPrice: finalLiquidationPrice,
1263
1331
  collateralAmountRecieved: collateralAmountReceived,