flash-sdk 1.0.12 → 1.0.14

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.
@@ -46,7 +46,7 @@ var CustodyAccount = /** @class */ (function () {
46
46
  return constants_1.BN_ZERO;
47
47
  }
48
48
  var cumulative_interest = this.getCumulativeInterest(curtime);
49
- console.log("cumulative_interest:", cumulative_interest.toString());
49
+ // console.log("cumulative_interest:",cumulative_interest.toString())
50
50
  var position_interest;
51
51
  if (cumulative_interest.gt(position.cumulativeInterestSnapshot)) {
52
52
  position_interest = cumulative_interest.sub(position.cumulativeInterestSnapshot);
@@ -1935,13 +1935,13 @@ export declare class PerpetualsClient {
1935
1935
  getRemoveLiquidityAmountAndFee: (poolName: string, tokenMint: PublicKey, lpAmount: BN) => Promise<any>;
1936
1936
  getEntryPriceAndFee: (poolName: string, tokenMint: PublicKey, collateral: BN, size: BN, side: PositionSide) => Promise<any>;
1937
1937
  getExitPriceAndFee: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
1938
- getLiquidationPrice: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral: BN, removeCollateral: BN) => Promise<any>;
1939
- getLiquidationPriceTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral: BN, removeCollateral: BN) => Promise<string>;
1940
- getLiquidationPrice2: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral: BN, removeCollateral: BN) => Promise<any>;
1938
+ getLiquidationPriceView: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral?: BN, removeCollateral?: BN) => Promise<any>;
1939
+ getLiquidationPriceTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral?: BN, removeCollateral?: BN) => Promise<string>;
1940
+ getLiquidationPrice: (postionKey: PublicKey, postionData: Position, tokenPrice: OraclePrice, tokenEmaPrice: OraclePrice, custodyAccount: CustodyAccount, poolAccount: PoolAccount, currentTime: BN, addCollateral?: BN, removeCollateral?: BN) => OraclePrice;
1941
1941
  getLiquidationState: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
1942
- getPnl: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
1942
+ getPnlView: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
1943
1943
  getPnlTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<string>;
1944
- getPnl2: (postionKey: PublicKey, postionData: Position, tokenPrice: OraclePrice, tokenEmaPrice: OraclePrice, custodyAccount: CustodyAccount, poolAccount: PoolAccount, currentTime: BN) => {
1944
+ getPnl: (postionKey: PublicKey, postionData: Position, tokenPrice: OraclePrice, tokenEmaPrice: OraclePrice, custodyAccount: CustodyAccount, poolAccount: PoolAccount, currentTime: BN) => {
1945
1945
  profit: BN;
1946
1946
  loss: BN;
1947
1947
  };
@@ -1949,7 +1949,7 @@ export declare class PerpetualsClient {
1949
1949
  getAumView: (poolName: string) => Promise<any>;
1950
1950
  getAumTrx: (poolName: string) => Promise<string>;
1951
1951
  getAumSdk: (poolAccount: PoolAccount, token_prices: OraclePrice[], token_ema_prices: OraclePrice[], custodies: CustodyAccount[], aum_calc_mode: AumCalcMode, currentTime: BN) => BN;
1952
- openPosition: (payTokenSymbol: string, priceAfterSlippage: BN, collateral: BN, size: BN, side: Side, poolConfig: PoolConfig, createUserWSOLATA?: boolean) => Promise<TransactionInstruction[]>;
1952
+ openPosition: (payTokenSymbol: string, priceAfterSlippage: BN, collateral: BN, fee: BN, size: BN, side: Side, poolConfig: PoolConfig, createUserWSOLATA?: boolean) => Promise<TransactionInstruction[]>;
1953
1953
  closePosition: (receivingTokenSymbol: string, priceAfterSlippage: BN, side: Side, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
1954
1954
  swap: (poolReceivingTokenSymbol: string, poolDispensingTokenSymbol: string, amountIn: BN, minAmountOut: BN, poolConfig: PoolConfig, unWrapSol?: boolean) => Promise<TransactionInstruction[]>;
1955
1955
  addLiquidity: (payTokenSymbol: string, tokenAmountIn: BN, minLpAmountOut: BN, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
@@ -64,6 +64,7 @@ var js_sha256_1 = require("js-sha256");
64
64
  var bs58_1 = require("bs58");
65
65
  var PositionAccount_1 = require("./PositionAccount");
66
66
  var types_1 = require("./types");
67
+ var OraclePrice_1 = require("./OraclePrice");
67
68
  var perpetuals_1 = require("./idl/perpetuals");
68
69
  var rpc_1 = require("./utils/rpc");
69
70
  var utils_1 = require("./utils");
@@ -220,7 +221,7 @@ var PerpetualsClient = /** @class */ (function () {
220
221
  wallet,
221
222
  pool,
222
223
  custody,
223
- side === "long" ? [1] : [0],
224
+ side === "long" ? [1] : [2],
224
225
  ]).publicKey;
225
226
  };
226
227
  this.getPosition = function (postionKey) { return __awaiter(_this, void 0, void 0, function () {
@@ -773,96 +774,88 @@ var PerpetualsClient = /** @class */ (function () {
773
774
  }
774
775
  });
775
776
  }); };
776
- this.getLiquidationPrice = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
777
- var _a, _b;
778
- var _c;
779
- return __generator(this, function (_d) {
780
- switch (_d.label) {
781
- case 0:
782
- _b = (_a = this.program.methods
783
- .getLiquidationPrice({
784
- addCollateral: addCollateral,
785
- removeCollateral: removeCollateral,
786
- }))
787
- .accounts;
788
- _c = {
789
- perpetuals: this.perpetuals.publicKey,
790
- pool: this.getPoolKey(poolName),
791
- position: this.getPositionKey(wallet, poolName, tokenMint, side),
792
- custody: this.getCustodyKey(poolName, tokenMint)
793
- };
794
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
795
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
796
- _c)])
797
- .view()
798
- .catch(function (err) {
799
- console.error(err);
800
- throw err;
801
- })];
802
- case 2: return [2 /*return*/, _d.sent()];
803
- }
804
- });
805
- }); };
806
- this.getLiquidationPriceTrx = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
807
- var _a, _b;
808
- var _c;
809
- return __generator(this, function (_d) {
810
- switch (_d.label) {
811
- case 0:
812
- _b = (_a = this.program.methods
813
- .getLiquidationPrice({
814
- addCollateral: addCollateral,
815
- removeCollateral: removeCollateral,
816
- }))
817
- .accounts;
818
- _c = {
819
- perpetuals: this.perpetuals.publicKey,
820
- pool: this.getPoolKey(poolName),
821
- position: this.getPositionKey(wallet, poolName, tokenMint, side),
822
- custody: this.getCustodyKey(poolName, tokenMint)
823
- };
824
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
825
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
826
- _c)])
827
- .rpc()
828
- .catch(function (err) {
829
- console.error(err);
830
- throw err;
831
- })];
832
- case 2: return [2 /*return*/, _d.sent()];
833
- }
777
+ this.getLiquidationPriceView = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) {
778
+ if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
779
+ if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
780
+ return __awaiter(_this, void 0, void 0, function () {
781
+ var _a, _b;
782
+ var _c;
783
+ return __generator(this, function (_d) {
784
+ switch (_d.label) {
785
+ case 0:
786
+ _b = (_a = this.program.methods
787
+ .getLiquidationPrice({
788
+ addCollateral: addCollateral,
789
+ removeCollateral: removeCollateral,
790
+ }))
791
+ .accounts;
792
+ _c = {
793
+ perpetuals: this.perpetuals.publicKey,
794
+ pool: this.getPoolKey(poolName),
795
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
796
+ custody: this.getCustodyKey(poolName, tokenMint)
797
+ };
798
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
799
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
800
+ _c)])
801
+ .view()
802
+ .catch(function (err) {
803
+ console.error(err);
804
+ throw err;
805
+ })];
806
+ case 2: return [2 /*return*/, _d.sent()];
807
+ }
808
+ });
834
809
  });
835
- }); };
836
- this.getLiquidationPrice2 = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) { return __awaiter(_this, void 0, void 0, function () {
837
- var _a, _b;
838
- var _c;
839
- return __generator(this, function (_d) {
840
- switch (_d.label) {
841
- case 0:
842
- _b = (_a = this.program.methods
843
- .getLiquidationPrice({
844
- addCollateral: addCollateral,
845
- removeCollateral: removeCollateral,
846
- }))
847
- .accounts;
848
- _c = {
849
- perpetuals: this.perpetuals.publicKey,
850
- pool: this.getPoolKey(poolName),
851
- position: this.getPositionKey(wallet, poolName, tokenMint, side),
852
- custody: this.getCustodyKey(poolName, tokenMint)
853
- };
854
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
855
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
856
- _c)])
857
- .view()
858
- .catch(function (err) {
859
- console.error(err);
860
- throw err;
861
- })];
862
- case 2: return [2 /*return*/, _d.sent()];
863
- }
810
+ };
811
+ this.getLiquidationPriceTrx = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) {
812
+ if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
813
+ if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
814
+ return __awaiter(_this, void 0, void 0, function () {
815
+ var _a, _b;
816
+ var _c;
817
+ return __generator(this, function (_d) {
818
+ switch (_d.label) {
819
+ case 0:
820
+ _b = (_a = this.program.methods
821
+ .getLiquidationPrice({
822
+ addCollateral: addCollateral,
823
+ removeCollateral: removeCollateral,
824
+ }))
825
+ .accounts;
826
+ _c = {
827
+ perpetuals: this.perpetuals.publicKey,
828
+ pool: this.getPoolKey(poolName),
829
+ position: this.getPositionKey(wallet, poolName, tokenMint, side),
830
+ custody: this.getCustodyKey(poolName, tokenMint)
831
+ };
832
+ return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
833
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
834
+ _c)])
835
+ .rpc()
836
+ .catch(function (err) {
837
+ console.error(err);
838
+ throw err;
839
+ })];
840
+ case 2: return [2 /*return*/, _d.sent()];
841
+ }
842
+ });
864
843
  });
865
- }); };
844
+ };
845
+ // TODO: currently just static, need to write
846
+ this.getLiquidationPrice = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime, addCollateral, removeCollateral) {
847
+ if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
848
+ if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
849
+ // const positionAccount = PositionAccount.from(postionKey, postionData);
850
+ var x;
851
+ if ((0, types_1.isVariant)(postionData.side, 'long')) {
852
+ x = tokenPrice.price.mul(new anchor_1.BN(90)).div(new anchor_1.BN(90));
853
+ }
854
+ else {
855
+ x = tokenPrice.price.mul(new anchor_1.BN(100)).div(new anchor_1.BN(90));
856
+ }
857
+ return new OraclePrice_1.OraclePrice({ price: x, exponent: tokenPrice.exponent });
858
+ };
866
859
  this.getLiquidationState = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
867
860
  var _a, _b;
868
861
  var _c;
@@ -890,7 +883,7 @@ var PerpetualsClient = /** @class */ (function () {
890
883
  }
891
884
  });
892
885
  }); };
893
- this.getPnl = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
886
+ this.getPnlView = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
894
887
  var pos, _a, _b;
895
888
  var _c;
896
889
  return __generator(this, function (_d) {
@@ -948,7 +941,7 @@ var PerpetualsClient = /** @class */ (function () {
948
941
  }
949
942
  });
950
943
  }); };
951
- this.getPnl2 = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime) {
944
+ this.getPnl = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime) {
952
945
  var positionAccount = PositionAccount_1.PositionAccount.from(postionKey, postionData);
953
946
  // console.log("positionAccount:",positionAccount);
954
947
  // console.log("side :", postionData.side, (isVariant(postionData.side, 'long')))
@@ -1044,11 +1037,11 @@ var PerpetualsClient = /** @class */ (function () {
1044
1037
  // TODO: Balance checks - DONE
1045
1038
  // TODO: ATA check - else create - DONE
1046
1039
  // TODO: for close Accounts - NOT NEEDED
1047
- this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateral, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
1040
+ this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
1048
1041
  ) {
1049
1042
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1050
1043
  return __awaiter(_this, void 0, void 0, function () {
1051
- var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_2;
1044
+ var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_2;
1052
1045
  return __generator(this, function (_e) {
1053
1046
  switch (_e.label) {
1054
1047
  case 0:
@@ -1061,65 +1054,73 @@ var PerpetualsClient = /** @class */ (function () {
1061
1054
  instructions = [];
1062
1055
  _e.label = 2;
1063
1056
  case 2:
1064
- _e.trys.push([2, 16, , 17]);
1065
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 10];
1057
+ _e.trys.push([2, 15, , 16]);
1058
+ if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 9];
1066
1059
  console.log("payTokenSymbol === sol", payTokenSymbol);
1067
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1068
- case 3:
1069
- wsolAssociatedTokenAccount = _e.sent();
1060
+ wsolAssociatedTokenAccount = userCustodyTokenAccount;
1070
1061
  return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1071
- case 4:
1062
+ case 3:
1072
1063
  wsolATAExist = _e.sent();
1073
1064
  if (!wsolATAExist) {
1074
1065
  console.log("wsol ata does not exist");
1075
1066
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1076
1067
  }
1068
+ collateralWithfee = collateral.add(fee);
1077
1069
  _a = anchor_1.BN.bind;
1078
- if (!wsolATAExist) return [3 /*break*/, 6];
1070
+ if (!wsolATAExist) return [3 /*break*/, 5];
1079
1071
  return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1080
- case 5:
1072
+ case 4:
1081
1073
  _b = (_e.sent()).value.amount;
1082
- return [3 /*break*/, 7];
1083
- case 6:
1074
+ return [3 /*break*/, 6];
1075
+ case 5:
1084
1076
  _b = 0;
1085
- _e.label = 7;
1086
- case 7:
1077
+ _e.label = 6;
1078
+ case 6:
1087
1079
  wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1088
- if (!wsolBalance.lt(collateral)) return [3 /*break*/, 9];
1080
+ if (!wsolBalance.lt(collateralWithfee)) return [3 /*break*/, 8];
1081
+ console.log("WSOL balance insufficient \n so Convert SOL to WSOL");
1089
1082
  _c = anchor_1.BN.bind;
1090
1083
  return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
1091
- case 8:
1084
+ case 7:
1092
1085
  unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
1093
1086
  totalSolBal = unWrappedSolBalance.add(wsolBalance);
1094
- if (totalSolBal.lt(collateral)) {
1087
+ // const rentExemptSolNeedforATA = new BN(0.02 * LAMPORTS_PER_SOL);
1088
+ // console.log("rentExemptSolNeedforATA:",rentExemptSolNeedforATA.toString())
1089
+ //TODOD :: add(rentExemptSolNeedforATA)
1090
+ if (totalSolBal.lt(collateralWithfee)) {
1095
1091
  throw "Insufficient SOL Funds";
1096
1092
  }
1097
- conversionAmt = collateral.sub(wsolBalance);
1093
+ else {
1094
+ console.log("SOL balance sufficient so transfer from SOL to WSOL ATA");
1095
+ }
1096
+ conversionAmt = collateralWithfee.sub(wsolBalance);
1097
+ console.log("conversionAmt:", conversionAmt.toString());
1098
1098
  instructions.push(web3_js_1.SystemProgram.transfer({
1099
1099
  fromPubkey: publicKey,
1100
1100
  toPubkey: wsolAssociatedTokenAccount,
1101
1101
  lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
1102
1102
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1103
- _e.label = 9;
1104
- case 9: return [3 /*break*/, 14];
1105
- case 10:
1106
- if (!(createUserWSOLATA == false)) return [3 /*break*/, 11];
1107
- console.log("skip WSOL checks and creation");
1108
- return [3 /*break*/, 14];
1109
- case 11: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1110
- case 12:
1103
+ _e.label = 8;
1104
+ case 8: return [3 /*break*/, 13];
1105
+ case 9:
1106
+ if (!(createUserWSOLATA == false)) return [3 /*break*/, 10];
1107
+ console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1108
+ return [3 /*break*/, 13];
1109
+ case 10: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1110
+ case 11:
1111
+ // for other tokens check if ATA and balance
1111
1112
  if (!(_e.sent())) {
1112
1113
  throw "Insufficient Funds , token Account doesn't exist";
1113
1114
  }
1114
1115
  _d = anchor_1.BN.bind;
1115
1116
  return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1116
- case 13:
1117
+ case 12:
1117
1118
  tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
1118
- if (tokenAccountBalance.lt(collateral)) {
1119
+ if (tokenAccountBalance.lt(collateral.add(fee))) {
1119
1120
  throw "Insufficient Funds";
1120
1121
  }
1121
- _e.label = 14;
1122
- case 14:
1122
+ _e.label = 13;
1123
+ case 13:
1123
1124
  positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1124
1125
  Buffer.from("position"),
1125
1126
  publicKey.toBuffer(),
@@ -1148,15 +1149,15 @@ var PerpetualsClient = /** @class */ (function () {
1148
1149
  systemProgram: web3_js_1.SystemProgram.programId,
1149
1150
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1150
1151
  }).instruction()];
1151
- case 15:
1152
+ case 14:
1152
1153
  instruction = _e.sent();
1153
1154
  instructions.push(instruction);
1154
- return [3 /*break*/, 17];
1155
- case 16:
1155
+ return [3 /*break*/, 16];
1156
+ case 15:
1156
1157
  error_2 = _e.sent();
1157
1158
  console.log("perpClient openPosition error:", error_2);
1158
- return [3 /*break*/, 17];
1159
- case 17: return [2 /*return*/, instructions];
1159
+ return [3 /*break*/, 16];
1160
+ case 16: return [2 /*return*/, instructions];
1160
1161
  }
1161
1162
  });
1162
1163
  });
@@ -1458,35 +1459,33 @@ var PerpetualsClient = /** @class */ (function () {
1458
1459
  throw "Insufficient Funds";
1459
1460
  }
1460
1461
  _f.label = 15;
1461
- case 15:
1462
- console.log("in add liq", tokenAmountIn);
1463
- return [4 /*yield*/, this.program.methods
1464
- .addLiquidity({
1465
- amountIn: tokenAmountIn,
1466
- minLpAmountOut: minLpAmountOut
1467
- })
1468
- .accounts({
1469
- owner: publicKey,
1470
- fundingAccount: userPayingTokenAccount,
1471
- lpTokenAccount: lpTokenAccount,
1472
- transferAuthority: poolConfig.transferAuthority,
1473
- perpetuals: poolConfig.perpetuals,
1474
- pool: poolConfig.poolAddress,
1475
- custody: payTokenCustody.custodyAccount,
1476
- custodyOracleAccount: payTokenCustody.oracleAddress,
1477
- custodyTokenAccount: payTokenCustody.tokenAccount,
1478
- lpTokenMint: poolConfig.lpTokenMint,
1479
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1480
- })
1481
- .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
1482
- .instruction()];
1462
+ case 15: return [4 /*yield*/, this.program.methods
1463
+ .addLiquidity({
1464
+ amountIn: tokenAmountIn,
1465
+ minLpAmountOut: minLpAmountOut
1466
+ })
1467
+ .accounts({
1468
+ owner: publicKey,
1469
+ fundingAccount: userPayingTokenAccount,
1470
+ lpTokenAccount: lpTokenAccount,
1471
+ transferAuthority: poolConfig.transferAuthority,
1472
+ perpetuals: poolConfig.perpetuals,
1473
+ pool: poolConfig.poolAddress,
1474
+ custody: payTokenCustody.custodyAccount,
1475
+ custodyOracleAccount: payTokenCustody.oracleAddress,
1476
+ custodyTokenAccount: payTokenCustody.tokenAccount,
1477
+ lpTokenMint: poolConfig.lpTokenMint,
1478
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1479
+ })
1480
+ .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
1481
+ .instruction()];
1483
1482
  case 16:
1484
1483
  inx = _f.sent();
1485
1484
  instructions.push(inx);
1486
1485
  return [3 /*break*/, 18];
1487
1486
  case 17:
1488
1487
  err_3 = _f.sent();
1489
- console.log("perpClient addLiquidity error:: ", err_3);
1488
+ console.error("perpClient addLiquidity error:: ", err_3);
1490
1489
  throw err_3;
1491
1490
  case 18: return [2 /*return*/, instructions];
1492
1491
  }
@@ -80,10 +80,10 @@ var PoolAccount = /** @class */ (function () {
80
80
  PoolAccount.getPriceAfterSlippage = function (isEntry, slippageBps, token_price, token_ema_price, side, custody) {
81
81
  if (isEntry) {
82
82
  var current_price = this.getEntryPrice(token_price, token_ema_price, side, custody);
83
- console.log("getEntryPrice current_price:", current_price.toString());
83
+ // console.log("getEntryPrice current_price:",current_price.toString())
84
84
  // TODO:: checked USE
85
85
  var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS));
86
- console.log("getPriceAfterSlippage spread_i:", spread_i.toString());
86
+ // console.log("getPriceAfterSlippage spread_i:",spread_i.toString());
87
87
  if ((0, types_1.isVariant)(side, 'long')) {
88
88
  return current_price.add(spread_i);
89
89
  }
@@ -100,10 +100,10 @@ var PoolAccount = /** @class */ (function () {
100
100
  else {
101
101
  // Opp in during close
102
102
  var current_price = this.getExitPrice(token_price, token_ema_price, side, custody);
103
- console.log("getExitPrice current_price:", current_price.toString());
103
+ // console.log("getExitPrice current_price:",current_price.toString())
104
104
  // TODO:: checked USE
105
105
  var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS));
106
- console.log("spread_i:", spread_i);
106
+ // console.log("spread_i:",spread_i);
107
107
  if ((0, types_1.isVariant)(side, 'long')) {
108
108
  if (spread_i.lt(current_price)) {
109
109
  return current_price.sub(spread_i);
@@ -120,7 +120,7 @@ var PoolAccount = /** @class */ (function () {
120
120
  };
121
121
  PoolAccount.getPrice = function (token_price, token_ema_price, side, spread) {
122
122
  if ((0, types_1.isVariant)(side, 'long')) {
123
- console.log("inside long");
123
+ // console.log("inside long")
124
124
  var max_price = void 0;
125
125
  if (token_price.cmp(token_ema_price)) {
126
126
  max_price = token_price;
@@ -130,14 +130,14 @@ var PoolAccount = /** @class */ (function () {
130
130
  }
131
131
  ;
132
132
  var spread_i = (0, utils_1.checkedDecimalCeilMul)(max_price.price, max_price.exponent, spread, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), max_price.exponent);
133
- console.log("getPrice spread_i:", spread_i.toString());
133
+ // console.log("getPrice spread_i:",spread_i.toString());
134
134
  return new OraclePrice_1.OraclePrice({
135
135
  price: max_price.price.add(spread_i),
136
136
  exponent: max_price.exponent,
137
137
  });
138
138
  }
139
139
  else {
140
- console.log("inside short ");
140
+ // console.log("inside short ")
141
141
  var min_price = void 0;
142
142
  if (token_price.cmp(token_ema_price)) {
143
143
  min_price = token_ema_price;
@@ -147,7 +147,7 @@ var PoolAccount = /** @class */ (function () {
147
147
  }
148
148
  ;
149
149
  var spread_i = (0, utils_1.checkedDecimalMul)(min_price.price, min_price.exponent, spread, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), min_price.exponent);
150
- console.log("spread_i:", spread_i.toString());
150
+ // console.log("spread_i:",spread_i.toString())
151
151
  var price = void 0;
152
152
  if (spread_i.lt(min_price.price)) {
153
153
  price = min_price.price.sub(spread_i);
@@ -179,9 +179,9 @@ var PoolAccount = /** @class */ (function () {
179
179
  }
180
180
  ;
181
181
  var exit_price = PoolAccount.getExitPrice(token_price, token_ema_price, position.side, custody);
182
- console.log("exit_price:", exit_price, exit_price === null || exit_price === void 0 ? void 0 : exit_price.toString());
182
+ // console.log("exit_price:",exit_price, exit_price?.toString())
183
183
  var size = token_ema_price.getTokenAmount(position.sizeUsd, custody.decimals);
184
- console.log("size:", size, size === null || size === void 0 ? void 0 : size.toString());
184
+ // console.log("size:",size, size?.toString())
185
185
  var exit_fee;
186
186
  if (liquidation) {
187
187
  exit_fee = this.getLiquidationFee(size, custody);
@@ -190,13 +190,13 @@ var PoolAccount = /** @class */ (function () {
190
190
  exit_fee = this.getExitFee(size, custody);
191
191
  }
192
192
  ;
193
- console.log("exit_fee:", exit_fee.toString());
193
+ // console.log("exit_fee:",exit_fee.toString())
194
194
  var exit_fee_usd = token_ema_price.getAssetAmountUsd(exit_fee, custody.decimals);
195
- console.log("exit_fee_usd:", exit_fee_usd.toString());
195
+ // console.log("exit_fee_usd:",exit_fee_usd.toString())
196
196
  var interest_usd = custody.getInterestAmountUsd(position, curtime);
197
- console.log("interest_usd:", interest_usd.toString());
197
+ // console.log("interest_usd:",interest_usd.toString())
198
198
  var unrealized_loss_usd = (exit_fee_usd.add(interest_usd)).add(position.unrealizedLossUsd);
199
- console.log("unrealized_loss_usd:", unrealized_loss_usd.toString());
199
+ // console.log("unrealized_loss_usd:",unrealized_loss_usd.toString())
200
200
  var price_diff_profit, price_diff_loss;
201
201
  if ((0, types_1.isVariant)(position.side, 'long')) {
202
202
  if (exit_price.gt(position.price)) {
@@ -217,19 +217,19 @@ var PoolAccount = /** @class */ (function () {
217
217
  price_diff_loss = exit_price.sub(position.price);
218
218
  }
219
219
  ;
220
- console.log("1 price_diff_profit:", price_diff_profit.toString());
221
- console.log("2 price_diff_loss:", price_diff_loss.toString());
220
+ // console.log("1 price_diff_profit:",price_diff_profit.toString())
221
+ // console.log("2 price_diff_loss:",price_diff_loss.toString())
222
222
  var position_price = (0, utils_1.scaleToExponent)(position.price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), new anchor_1.BN(-1 * constants_1.USD_DECIMALS));
223
- console.log("position_price:", position_price.toString());
223
+ // console.log("position_price:",position_price.toString())
224
224
  if (price_diff_profit.gt(constants_1.BN_ZERO)) {
225
225
  var potential_profit_usd = (position.sizeUsd.mul(price_diff_profit)).div(position_price);
226
226
  potential_profit_usd = potential_profit_usd.add(position.unrealizedProfitUsd);
227
- console.log("potential_profit_usd:", potential_profit_usd.toString());
227
+ // console.log("potential_profit_usd:",potential_profit_usd.toString())
228
228
  if (potential_profit_usd.gte(unrealized_loss_usd)) {
229
229
  var cur_profit_usd = potential_profit_usd.sub(unrealized_loss_usd);
230
230
  var max_profit_usd = min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
231
- console.log("cur_profit_usd:", cur_profit_usd.toString());
232
- console.log("max_profit_usd:", max_profit_usd.toString());
231
+ // console.log("cur_profit_usd:",cur_profit_usd.toString())
232
+ // console.log("max_profit_usd:",max_profit_usd.toString())
233
233
  return {
234
234
  profit: anchor_1.BN.min(max_profit_usd, cur_profit_usd),
235
235
  loss: constants_1.BN_ZERO,
@@ -237,8 +237,8 @@ var PoolAccount = /** @class */ (function () {
237
237
  };
238
238
  }
239
239
  else {
240
- console.log(" -- unrealized_loss_usd:", unrealized_loss_usd.toString());
241
- console.log(" -- potential_profit_usd:", potential_profit_usd.toString());
240
+ // console.log(" -- unrealized_loss_usd:",unrealized_loss_usd.toString())
241
+ // console.log(" -- potential_profit_usd:",potential_profit_usd.toString())
242
242
  return {
243
243
  profit: constants_1.BN_ZERO,
244
244
  loss: unrealized_loss_usd.sub(potential_profit_usd),
@@ -249,9 +249,9 @@ var PoolAccount = /** @class */ (function () {
249
249
  else {
250
250
  var potential_loss_usd = (0, utils_1.checkedCeilDiv)(position.sizeUsd.mul(price_diff_loss), position_price);
251
251
  potential_loss_usd = potential_loss_usd.add(unrealized_loss_usd);
252
- console.log("potential_loss_usd:", potential_loss_usd.toString());
252
+ // console.log("potential_loss_usd:",potential_loss_usd.toString())
253
253
  if (potential_loss_usd.gte(position.unrealizedProfitUsd)) {
254
- console.log("position.unrealizedProfitUsd:", position.unrealizedProfitUsd.toString());
254
+ // console.log("position.unrealizedProfitUsd:",position.unrealizedProfitUsd.toString())
255
255
  return {
256
256
  profit: constants_1.BN_ZERO,
257
257
  loss: potential_loss_usd.sub(position.unrealizedProfitUsd),
@@ -261,8 +261,8 @@ var PoolAccount = /** @class */ (function () {
261
261
  else {
262
262
  var cur_profit_usd = position.unrealizedProfitUsd.sub(potential_loss_usd);
263
263
  var max_profit_usd = min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
264
- console.log("cur_profit_usd:", cur_profit_usd.toString());
265
- console.log("max_profit_usd:", max_profit_usd.toString());
264
+ // console.log("cur_profit_usd:",cur_profit_usd.toString())
265
+ // console.log("max_profit_usd:",max_profit_usd.toString())
266
266
  return {
267
267
  profit: anchor_1.BN.min(max_profit_usd, cur_profit_usd),
268
268
  loss: constants_1.BN_ZERO,