flash-sdk 1.0.29 → 1.0.30

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.
@@ -556,7 +556,7 @@ var PerpetualsClient = (function () {
556
556
  }
557
557
  });
558
558
  }); };
559
- this.liquidate = function (wallet, poolName, tokenMint, side, receivingAccount, rewardsReceivingAccount) { return __awaiter(_this, void 0, void 0, function () {
559
+ this.liquidate = function (wallet, poolName, tokenMint, collateralMint, side, receivingAccount, rewardsReceivingAccount) { return __awaiter(_this, void 0, void 0, function () {
560
560
  var _a, _b;
561
561
  var _c;
562
562
  return __generator(this, function (_d) {
@@ -576,8 +576,12 @@ var PerpetualsClient = (function () {
576
576
  custody: this.getCustodyKey(poolName, tokenMint)
577
577
  };
578
578
  return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
579
- case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
580
- _c.custodyTokenAccount = this.getCustodyTokenAccountKey(poolName, tokenMint),
579
+ case 1:
580
+ _c.custodyOracleAccount = _d.sent(),
581
+ _c.collateralCustody = this.getCustodyKey(poolName, collateralMint);
582
+ return [4, this.getCustodyOracleAccountKey(poolName, collateralMint)];
583
+ case 2: return [4, _b.apply(_a, [(_c.collateralCustodyOracleAccount = _d.sent(),
584
+ _c.collateralCustodyTokenAccount = this.getCustodyTokenAccountKey(poolName, collateralMint),
581
585
  _c.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
582
586
  _c)])
583
587
  .rpc()
@@ -585,7 +589,7 @@ var PerpetualsClient = (function () {
585
589
  console.error(err);
586
590
  throw err;
587
591
  })];
588
- case 2: return [2, _d.sent()];
592
+ case 3: return [2, _d.sent()];
589
593
  }
590
594
  });
591
595
  }); };
@@ -751,8 +755,8 @@ var PerpetualsClient = (function () {
751
755
  case 0:
752
756
  _b = (_a = this.program.methods
753
757
  .getLiquidationPrice({
754
- addCollateral: addCollateral,
755
- removeCollateral: removeCollateral,
758
+ addCollateralDelta: addCollateral,
759
+ removeCollateralDelta: removeCollateral,
756
760
  }))
757
761
  .accounts;
758
762
  _c = {
@@ -785,8 +789,8 @@ var PerpetualsClient = (function () {
785
789
  case 0:
786
790
  _b = (_a = this.program.methods
787
791
  .getLiquidationPrice({
788
- addCollateral: addCollateral,
789
- removeCollateral: removeCollateral,
792
+ addCollateralDelta: addCollateral,
793
+ removeCollateralDelta: removeCollateral,
790
794
  }))
791
795
  .accounts;
792
796
  _c = {
@@ -992,20 +996,21 @@ var PerpetualsClient = (function () {
992
996
  this.getAumSdk = function (poolAccount, token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime) {
993
997
  return poolAccount.getAssetsUnderManagementUsd(token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime);
994
998
  };
995
- this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
999
+ this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
996
1000
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
997
1001
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
998
1002
  return __awaiter(_this, void 0, void 0, function () {
999
- var publicKey, payTokenCustody, userCustodyTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, tokenAccountBalance, _a, positionAccount, params, instruction, error_2;
1003
+ var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, tokenAccountBalance, _a, positionAccount, params, instruction, error_2;
1000
1004
  return __generator(this, function (_b) {
1001
1005
  switch (_b.label) {
1002
1006
  case 0:
1003
- console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1007
+ console.log("open position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
1004
1008
  publicKey = this.provider.wallet.publicKey;
1005
- payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1006
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1009
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1010
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1011
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1007
1012
  case 1:
1008
- userCustodyTokenAccount = _b.sent();
1013
+ userCollateralTokenAccount = _b.sent();
1009
1014
  preInstructions = [];
1010
1015
  instructions = [];
1011
1016
  postInstructions = [];
@@ -1013,8 +1018,8 @@ var PerpetualsClient = (function () {
1013
1018
  _b.label = 2;
1014
1019
  case 2:
1015
1020
  _b.trys.push([2, 10, , 11]);
1016
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3, 4];
1017
- console.log("payTokenSymbol === sol", payTokenSymbol);
1021
+ if (!(collateralSymbol == 'SOL' && createUserWSOLATA)) return [3, 4];
1022
+ console.log("collateralSymbol === SOL", collateralSymbol);
1018
1023
  wrappedSolAccount = new web3_js_1.Keypair();
1019
1024
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1020
1025
  case 3:
@@ -1040,14 +1045,14 @@ var PerpetualsClient = (function () {
1040
1045
  if (!(createUserWSOLATA == false)) return [3, 5];
1041
1046
  console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1042
1047
  return [3, 8];
1043
- case 5: return [4, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1048
+ case 5: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
1044
1049
  case 6:
1045
1050
  if (!(_b.sent())) {
1046
1051
  throw "Insufficient Funds , token Account doesn't exist";
1047
1052
  }
1048
1053
  if (!!skipBalanceChecks) return [3, 8];
1049
1054
  _a = anchor_1.BN.bind;
1050
- return [4, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1055
+ return [4, this.provider.connection.getTokenAccountBalance(userCollateralTokenAccount)];
1051
1056
  case 7:
1052
1057
  tokenAccountBalance = new (_a.apply(anchor_1.BN, [void 0, (_b.sent()).value.amount]))();
1053
1058
  if (tokenAccountBalance.lt(collateralWithfee)) {
@@ -1059,7 +1064,7 @@ var PerpetualsClient = (function () {
1059
1064
  Buffer.from("position"),
1060
1065
  publicKey.toBuffer(),
1061
1066
  poolConfig.poolAddress.toBuffer(),
1062
- payTokenCustody.custodyAccount.toBuffer(),
1067
+ marketCustodyConfig.custodyAccount.toBuffer(),
1063
1068
  (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1064
1069
  ], this.programId)[0];
1065
1070
  params = {
@@ -1072,14 +1077,16 @@ var PerpetualsClient = (function () {
1072
1077
  .openPosition(params)
1073
1078
  .accounts({
1074
1079
  owner: publicKey,
1075
- fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCustodyTokenAccount,
1080
+ fundingAccount: marketSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCollateralTokenAccount,
1076
1081
  transferAuthority: poolConfig.transferAuthority,
1077
1082
  perpetuals: poolConfig.perpetuals,
1078
1083
  pool: poolConfig.poolAddress,
1079
1084
  position: positionAccount,
1080
- custody: payTokenCustody.custodyAccount,
1081
- custodyOracleAccount: payTokenCustody.oracleAddress,
1082
- custodyTokenAccount: payTokenCustody.tokenAccount,
1085
+ custody: marketCustodyConfig.custodyAccount,
1086
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1087
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1088
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1089
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1083
1090
  systemProgram: web3_js_1.SystemProgram.programId,
1084
1091
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1085
1092
  }).instruction()];
@@ -1099,15 +1106,15 @@ var PerpetualsClient = (function () {
1099
1106
  });
1100
1107
  });
1101
1108
  };
1102
- this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA) {
1109
+ this.closePosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA) {
1103
1110
  if (createUserATA === void 0) { createUserATA = true; }
1104
1111
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1105
1112
  return __awaiter(_this, void 0, void 0, function () {
1106
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_3;
1113
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, marketCustodyConfig, positionAccount, params, instruction, closeWsolATAIns, error_3;
1107
1114
  return __generator(this, function (_b) {
1108
1115
  switch (_b.label) {
1109
1116
  case 0:
1110
- console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1117
+ console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
1111
1118
  publicKey = this.provider.wallet.publicKey;
1112
1119
  preInstructions = [];
1113
1120
  instructions = [];
@@ -1116,7 +1123,7 @@ var PerpetualsClient = (function () {
1116
1123
  _b.label = 1;
1117
1124
  case 1:
1118
1125
  _b.trys.push([1, 9, , 10]);
1119
- if (!(receivingTokenSymbol == 'SOL')) return [3, 3];
1126
+ if (!(collateralSymbol == 'SOL')) return [3, 3];
1120
1127
  wrappedSolAccount = new web3_js_1.Keypair();
1121
1128
  userReceivingTokenAccount = wrappedSolAccount.publicKey;
1122
1129
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
@@ -1137,7 +1144,7 @@ var PerpetualsClient = (function () {
1137
1144
  ];
1138
1145
  additionalSigners.push(wrappedSolAccount);
1139
1146
  return [3, 7];
1140
- case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1147
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1141
1148
  case 4:
1142
1149
  userReceivingTokenAccount = _b.sent();
1143
1150
  _a = createUserATA;
@@ -1148,16 +1155,17 @@ var PerpetualsClient = (function () {
1148
1155
  _b.label = 6;
1149
1156
  case 6:
1150
1157
  if (_a) {
1151
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1158
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
1152
1159
  }
1153
1160
  _b.label = 7;
1154
1161
  case 7:
1155
- receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1162
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1163
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1156
1164
  positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1157
1165
  Buffer.from("position"),
1158
1166
  publicKey.toBuffer(),
1159
1167
  poolConfig.poolAddress.toBuffer(),
1160
- receivingTokenCustody.custodyAccount.toBuffer(),
1168
+ marketCustodyConfig.custodyAccount.toBuffer(),
1161
1169
  (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1162
1170
  ], this.programId)[0];
1163
1171
  params = {
@@ -1172,15 +1180,17 @@ var PerpetualsClient = (function () {
1172
1180
  perpetuals: poolConfig.perpetuals,
1173
1181
  pool: poolConfig.poolAddress,
1174
1182
  position: positionAccount,
1175
- custody: receivingTokenCustody.custodyAccount,
1176
- custodyOracleAccount: receivingTokenCustody.oracleAddress,
1177
- custodyTokenAccount: receivingTokenCustody.tokenAccount,
1183
+ custody: marketCustodyConfig.custodyAccount,
1184
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1185
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1186
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1187
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1178
1188
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1179
1189
  }).instruction()];
1180
1190
  case 8:
1181
1191
  instruction = _b.sent();
1182
1192
  instructions.push(instruction);
1183
- if (receivingTokenSymbol == 'WSOL' && closeUsersWSOLATA) {
1193
+ if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
1184
1194
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1185
1195
  postInstructions.push(closeWsolATAIns);
1186
1196
  }
@@ -1197,187 +1207,6 @@ var PerpetualsClient = (function () {
1197
1207
  });
1198
1208
  });
1199
1209
  };
1200
- this.openPositionOld = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA) {
1201
- if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1202
- return __awaiter(_this, void 0, void 0, function () {
1203
- var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_4;
1204
- return __generator(this, function (_e) {
1205
- switch (_e.label) {
1206
- case 0:
1207
- console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1208
- publicKey = this.provider.wallet.publicKey;
1209
- payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1210
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1211
- case 1:
1212
- userCustodyTokenAccount = _e.sent();
1213
- instructions = [];
1214
- _e.label = 2;
1215
- case 2:
1216
- _e.trys.push([2, 15, , 16]);
1217
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3, 9];
1218
- console.log("payTokenSymbol === sol", payTokenSymbol);
1219
- wsolAssociatedTokenAccount = userCustodyTokenAccount;
1220
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1221
- case 3:
1222
- wsolATAExist = _e.sent();
1223
- if (!wsolATAExist) {
1224
- console.log("wsol ata does not exist");
1225
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1226
- }
1227
- collateralWithfee = collateral.add(fee);
1228
- _a = anchor_1.BN.bind;
1229
- if (!wsolATAExist) return [3, 5];
1230
- return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1231
- case 4:
1232
- _b = (_e.sent()).value.amount;
1233
- return [3, 6];
1234
- case 5:
1235
- _b = 0;
1236
- _e.label = 6;
1237
- case 6:
1238
- wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1239
- if (!wsolBalance.lt(collateralWithfee)) return [3, 8];
1240
- console.log("WSOL balance insufficient \n so Convert SOL to WSOL");
1241
- _c = anchor_1.BN.bind;
1242
- return [4, this.provider.connection.getBalance(publicKey)];
1243
- case 7:
1244
- unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
1245
- totalSolBal = unWrappedSolBalance.add(wsolBalance);
1246
- if (totalSolBal.lt(collateralWithfee)) {
1247
- throw "Insufficient SOL Funds";
1248
- }
1249
- else {
1250
- console.log("SOL balance sufficient so transfer from SOL to WSOL ATA");
1251
- }
1252
- conversionAmt = collateralWithfee.sub(wsolBalance);
1253
- console.log("conversionAmt:", conversionAmt.toString());
1254
- instructions.push(web3_js_1.SystemProgram.transfer({
1255
- fromPubkey: publicKey,
1256
- toPubkey: wsolAssociatedTokenAccount,
1257
- lamports: conversionAmt.toNumber(),
1258
- }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1259
- _e.label = 8;
1260
- case 8: return [3, 13];
1261
- case 9:
1262
- if (!(createUserWSOLATA == false)) return [3, 10];
1263
- console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1264
- return [3, 13];
1265
- case 10: return [4, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1266
- case 11:
1267
- if (!(_e.sent())) {
1268
- throw "Insufficient Funds , token Account doesn't exist";
1269
- }
1270
- _d = anchor_1.BN.bind;
1271
- return [4, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1272
- case 12:
1273
- tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
1274
- if (tokenAccountBalance.lt(collateral.add(fee))) {
1275
- throw "Insufficient Funds";
1276
- }
1277
- _e.label = 13;
1278
- case 13:
1279
- positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1280
- Buffer.from("position"),
1281
- publicKey.toBuffer(),
1282
- poolConfig.poolAddress.toBuffer(),
1283
- payTokenCustody.custodyAccount.toBuffer(),
1284
- (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1285
- ], this.programId)[0];
1286
- params = {
1287
- price: priceAfterSlippage,
1288
- collateral: collateral,
1289
- size: size,
1290
- side: side,
1291
- };
1292
- return [4, this.program.methods
1293
- .openPosition(params)
1294
- .accounts({
1295
- owner: publicKey,
1296
- fundingAccount: userCustodyTokenAccount,
1297
- transferAuthority: poolConfig.transferAuthority,
1298
- perpetuals: poolConfig.perpetuals,
1299
- pool: poolConfig.poolAddress,
1300
- position: positionAccount,
1301
- custody: payTokenCustody.custodyAccount,
1302
- custodyOracleAccount: payTokenCustody.oracleAddress,
1303
- custodyTokenAccount: payTokenCustody.tokenAccount,
1304
- systemProgram: web3_js_1.SystemProgram.programId,
1305
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1306
- }).instruction()];
1307
- case 14:
1308
- instruction = _e.sent();
1309
- instructions.push(instruction);
1310
- return [3, 16];
1311
- case 15:
1312
- error_4 = _e.sent();
1313
- console.log("perpClient openPosition error:", error_4);
1314
- return [3, 16];
1315
- case 16: return [2, instructions];
1316
- }
1317
- });
1318
- });
1319
- };
1320
- this.closePositionOld = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1321
- var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_5;
1322
- return __generator(this, function (_a) {
1323
- switch (_a.label) {
1324
- case 0:
1325
- console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1326
- publicKey = this.provider.wallet.publicKey;
1327
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1328
- case 1:
1329
- userReceivingTokenAccount = _a.sent();
1330
- instructions = [];
1331
- _a.label = 2;
1332
- case 2:
1333
- _a.trys.push([2, 5, , 6]);
1334
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1335
- case 3:
1336
- if (!(_a.sent())) {
1337
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1338
- }
1339
- receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1340
- positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1341
- Buffer.from("position"),
1342
- publicKey.toBuffer(),
1343
- poolConfig.poolAddress.toBuffer(),
1344
- receivingTokenCustody.custodyAccount.toBuffer(),
1345
- (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1346
- ], this.programId)[0];
1347
- console.log("positionAccount:", positionAccount.toBase58());
1348
- params = {
1349
- price: priceAfterSlippage,
1350
- };
1351
- return [4, this.program.methods
1352
- .closePosition(params)
1353
- .accounts({
1354
- owner: publicKey,
1355
- receivingAccount: userReceivingTokenAccount,
1356
- transferAuthority: poolConfig.transferAuthority,
1357
- perpetuals: poolConfig.perpetuals,
1358
- pool: poolConfig.poolAddress,
1359
- position: positionAccount,
1360
- custody: receivingTokenCustody.custodyAccount,
1361
- custodyOracleAccount: receivingTokenCustody.oracleAddress,
1362
- custodyTokenAccount: receivingTokenCustody.tokenAccount,
1363
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1364
- }).instruction()];
1365
- case 4:
1366
- instruction = _a.sent();
1367
- instructions.push(instruction);
1368
- if (receivingTokenSymbol == 'SOL') {
1369
- closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1370
- instructions.push(closeWsolATAIns);
1371
- }
1372
- return [3, 6];
1373
- case 5:
1374
- error_5 = _a.sent();
1375
- console.error("perpclient closePosition error:", error_5);
1376
- return [3, 6];
1377
- case 6: return [2, instructions];
1378
- }
1379
- });
1380
- }); };
1381
1210
  this.swap = function (poolReceivingTokenSymbol, poolDispensingTokenSymbol, amountIn, minAmountOut, poolConfig, unWrapSol) {
1382
1211
  if (unWrapSol === void 0) { unWrapSol = false; }
1383
1212
  return __awaiter(_this, void 0, void 0, function () {
@@ -1504,18 +1333,19 @@ var PerpetualsClient = (function () {
1504
1333
  });
1505
1334
  });
1506
1335
  };
1507
- this.addCollateral = function (collateralWithoutFee, fee, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1508
- var publicKey, custody, instructions, userPayingTokenAccount, collateralWithfee, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, inx;
1336
+ this.addCollateral = function (collateralWithoutFee, fee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1337
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userPayingTokenAccount, collateralWithfee, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, inx;
1509
1338
  return __generator(this, function (_d) {
1510
1339
  switch (_d.label) {
1511
1340
  case 0:
1512
1341
  publicKey = this.provider.wallet.publicKey;
1513
- custody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1514
- if (!custody) {
1342
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1343
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1344
+ if (!collateralCustodyConfig || !marketCustodyConfig) {
1515
1345
  throw "payTokenCustody not found";
1516
1346
  }
1517
1347
  instructions = [];
1518
- userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(custody.mintKey, publicKey);
1348
+ userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey);
1519
1349
  collateralWithfee = collateralWithoutFee.add(fee);
1520
1350
  if (!(collateralSymbol == 'SOL')) return [3, 7];
1521
1351
  console.log("collateralSymbol === sol", collateralSymbol);
@@ -1557,7 +1387,7 @@ var PerpetualsClient = (function () {
1557
1387
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1558
1388
  _d.label = 7;
1559
1389
  case 7: return [4, this.program.methods.addCollateral({
1560
- collateral: collateralWithoutFee
1390
+ collateralDelta: collateralWithoutFee
1561
1391
  }).accounts({
1562
1392
  owner: publicKey,
1563
1393
  position: positionPubKey,
@@ -1565,9 +1395,11 @@ var PerpetualsClient = (function () {
1565
1395
  transferAuthority: poolConfig.transferAuthority,
1566
1396
  perpetuals: poolConfig.perpetuals,
1567
1397
  pool: poolConfig.poolAddress,
1568
- custody: custody.custodyAccount,
1569
- custodyOracleAccount: custody.oracleAddress,
1570
- custodyTokenAccount: custody.tokenAccount,
1398
+ custody: marketCustodyConfig.custodyAccount,
1399
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1400
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1401
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1402
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1571
1403
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1572
1404
  })
1573
1405
  .instruction()];
@@ -1578,16 +1410,19 @@ var PerpetualsClient = (function () {
1578
1410
  }
1579
1411
  });
1580
1412
  }); };
1581
- this.removeCollateral = function (collateralWithoutFee, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1582
- var publicKey, custody, instructions, userReceivingTokenAccount, ix, closeWsolATAIns;
1413
+ this.removeCollateral = function (collateralWithoutFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1414
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userReceivingTokenAccount, ix, closeWsolATAIns;
1583
1415
  return __generator(this, function (_a) {
1584
1416
  switch (_a.label) {
1585
1417
  case 0:
1586
1418
  publicKey = this.provider.wallet.publicKey;
1587
- custody = poolConfig.custodies.find(function (i) {
1419
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) {
1420
+ return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1421
+ });
1422
+ marketCustodyConfig = poolConfig.custodies.find(function (i) {
1588
1423
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1589
1424
  });
1590
- if (!custody) {
1425
+ if (!collateralCustodyConfig) {
1591
1426
  throw "payTokenCustody not found";
1592
1427
  }
1593
1428
  instructions = [];
@@ -1599,7 +1434,7 @@ var PerpetualsClient = (function () {
1599
1434
  }
1600
1435
  return [4, this.program.methods
1601
1436
  .removeCollateral({
1602
- collateralUsd: collateralWithoutFee,
1437
+ collateralDelta: collateralWithoutFee,
1603
1438
  })
1604
1439
  .accounts({
1605
1440
  owner: publicKey,
@@ -1608,9 +1443,11 @@ var PerpetualsClient = (function () {
1608
1443
  perpetuals: poolConfig.perpetuals,
1609
1444
  pool: poolConfig.poolAddress,
1610
1445
  position: positionPubKey,
1611
- custody: custody.custodyAccount,
1612
- custodyOracleAccount: custody.oracleAddress,
1613
- custodyTokenAccount: custody.tokenAccount,
1446
+ custody: marketCustodyConfig.custodyAccount,
1447
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1448
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1449
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1450
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1614
1451
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1615
1452
  })
1616
1453
  .instruction()];