flash-sdk 1.0.29 → 1.0.31

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.
@@ -72,6 +72,31 @@ var PerpetualsClient = (function () {
72
72
  function PerpetualsClient(provider, programId, opts) {
73
73
  var _this = this;
74
74
  var _a;
75
+ this.addressLookupTables = [];
76
+ this.loadAddressLookupTable = function (poolConfig) { return __awaiter(_this, void 0, void 0, function () {
77
+ var _i, _a, address, addressLookupTable;
78
+ return __generator(this, function (_b) {
79
+ switch (_b.label) {
80
+ case 0:
81
+ _i = 0, _a = poolConfig.addressLookupTableAddresses;
82
+ _b.label = 1;
83
+ case 1:
84
+ if (!(_i < _a.length)) return [3, 4];
85
+ address = _a[_i];
86
+ return [4, this.provider.connection.getAddressLookupTable(address)];
87
+ case 2:
88
+ addressLookupTable = (_b.sent()).value;
89
+ if (addressLookupTable) {
90
+ this.addressLookupTables.push(addressLookupTable);
91
+ }
92
+ _b.label = 3;
93
+ case 3:
94
+ _i++;
95
+ return [3, 1];
96
+ case 4: return [2];
97
+ }
98
+ });
99
+ }); };
75
100
  this.findProgramAddress = function (label, extraSeeds) {
76
101
  if (extraSeeds === void 0) { extraSeeds = null; }
77
102
  var seeds = [Buffer.from(anchor_1.utils.bytes.utf8.encode(label))];
@@ -556,7 +581,7 @@ var PerpetualsClient = (function () {
556
581
  }
557
582
  });
558
583
  }); };
559
- this.liquidate = function (wallet, poolName, tokenMint, side, receivingAccount, rewardsReceivingAccount) { return __awaiter(_this, void 0, void 0, function () {
584
+ this.liquidate = function (wallet, poolName, tokenMint, collateralMint, side, receivingAccount, rewardsReceivingAccount) { return __awaiter(_this, void 0, void 0, function () {
560
585
  var _a, _b;
561
586
  var _c;
562
587
  return __generator(this, function (_d) {
@@ -576,8 +601,12 @@ var PerpetualsClient = (function () {
576
601
  custody: this.getCustodyKey(poolName, tokenMint)
577
602
  };
578
603
  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),
604
+ case 1:
605
+ _c.custodyOracleAccount = _d.sent(),
606
+ _c.collateralCustody = this.getCustodyKey(poolName, collateralMint);
607
+ return [4, this.getCustodyOracleAccountKey(poolName, collateralMint)];
608
+ case 2: return [4, _b.apply(_a, [(_c.collateralCustodyOracleAccount = _d.sent(),
609
+ _c.collateralCustodyTokenAccount = this.getCustodyTokenAccountKey(poolName, collateralMint),
581
610
  _c.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
582
611
  _c)])
583
612
  .rpc()
@@ -585,7 +614,7 @@ var PerpetualsClient = (function () {
585
614
  console.error(err);
586
615
  throw err;
587
616
  })];
588
- case 2: return [2, _d.sent()];
617
+ case 3: return [2, _d.sent()];
589
618
  }
590
619
  });
591
620
  }); };
@@ -751,8 +780,8 @@ var PerpetualsClient = (function () {
751
780
  case 0:
752
781
  _b = (_a = this.program.methods
753
782
  .getLiquidationPrice({
754
- addCollateral: addCollateral,
755
- removeCollateral: removeCollateral,
783
+ addCollateralDelta: addCollateral,
784
+ removeCollateralDelta: removeCollateral,
756
785
  }))
757
786
  .accounts;
758
787
  _c = {
@@ -785,8 +814,8 @@ var PerpetualsClient = (function () {
785
814
  case 0:
786
815
  _b = (_a = this.program.methods
787
816
  .getLiquidationPrice({
788
- addCollateral: addCollateral,
789
- removeCollateral: removeCollateral,
817
+ addCollateralDelta: addCollateral,
818
+ removeCollateralDelta: removeCollateral,
790
819
  }))
791
820
  .accounts;
792
821
  _c = {
@@ -992,20 +1021,21 @@ var PerpetualsClient = (function () {
992
1021
  this.getAumSdk = function (poolAccount, token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime) {
993
1022
  return poolAccount.getAssetsUnderManagementUsd(token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime);
994
1023
  };
995
- this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
1024
+ this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
996
1025
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
997
1026
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
998
1027
  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;
1028
+ var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, tokenAccountBalance, _a, positionAccount, params, instruction, error_2;
1000
1029
  return __generator(this, function (_b) {
1001
1030
  switch (_b.label) {
1002
1031
  case 0:
1003
- console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1032
+ console.log("open position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
1004
1033
  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)];
1034
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1035
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1036
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1007
1037
  case 1:
1008
- userCustodyTokenAccount = _b.sent();
1038
+ userCollateralTokenAccount = _b.sent();
1009
1039
  preInstructions = [];
1010
1040
  instructions = [];
1011
1041
  postInstructions = [];
@@ -1013,8 +1043,8 @@ var PerpetualsClient = (function () {
1013
1043
  _b.label = 2;
1014
1044
  case 2:
1015
1045
  _b.trys.push([2, 10, , 11]);
1016
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3, 4];
1017
- console.log("payTokenSymbol === sol", payTokenSymbol);
1046
+ if (!(collateralSymbol == 'SOL' && createUserWSOLATA)) return [3, 4];
1047
+ console.log("collateralSymbol === SOL", collateralSymbol);
1018
1048
  wrappedSolAccount = new web3_js_1.Keypair();
1019
1049
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1020
1050
  case 3:
@@ -1040,14 +1070,14 @@ var PerpetualsClient = (function () {
1040
1070
  if (!(createUserWSOLATA == false)) return [3, 5];
1041
1071
  console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1042
1072
  return [3, 8];
1043
- case 5: return [4, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1073
+ case 5: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
1044
1074
  case 6:
1045
1075
  if (!(_b.sent())) {
1046
1076
  throw "Insufficient Funds , token Account doesn't exist";
1047
1077
  }
1048
1078
  if (!!skipBalanceChecks) return [3, 8];
1049
1079
  _a = anchor_1.BN.bind;
1050
- return [4, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1080
+ return [4, this.provider.connection.getTokenAccountBalance(userCollateralTokenAccount)];
1051
1081
  case 7:
1052
1082
  tokenAccountBalance = new (_a.apply(anchor_1.BN, [void 0, (_b.sent()).value.amount]))();
1053
1083
  if (tokenAccountBalance.lt(collateralWithfee)) {
@@ -1059,7 +1089,7 @@ var PerpetualsClient = (function () {
1059
1089
  Buffer.from("position"),
1060
1090
  publicKey.toBuffer(),
1061
1091
  poolConfig.poolAddress.toBuffer(),
1062
- payTokenCustody.custodyAccount.toBuffer(),
1092
+ marketCustodyConfig.custodyAccount.toBuffer(),
1063
1093
  (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1064
1094
  ], this.programId)[0];
1065
1095
  params = {
@@ -1072,14 +1102,17 @@ var PerpetualsClient = (function () {
1072
1102
  .openPosition(params)
1073
1103
  .accounts({
1074
1104
  owner: publicKey,
1075
- fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCustodyTokenAccount,
1105
+ fundingAccount: marketSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCollateralTokenAccount,
1076
1106
  transferAuthority: poolConfig.transferAuthority,
1077
1107
  perpetuals: poolConfig.perpetuals,
1078
1108
  pool: poolConfig.poolAddress,
1079
1109
  position: positionAccount,
1080
- custody: payTokenCustody.custodyAccount,
1081
- custodyOracleAccount: payTokenCustody.oracleAddress,
1082
- custodyTokenAccount: payTokenCustody.tokenAccount,
1110
+ custody: marketCustodyConfig.custodyAccount,
1111
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1112
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1113
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1114
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1115
+ eventAuthority: this.eventAuthority.publicKey,
1083
1116
  systemProgram: web3_js_1.SystemProgram.programId,
1084
1117
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1085
1118
  }).instruction()];
@@ -1099,15 +1132,15 @@ var PerpetualsClient = (function () {
1099
1132
  });
1100
1133
  });
1101
1134
  };
1102
- this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA) {
1135
+ this.closePosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA) {
1103
1136
  if (createUserATA === void 0) { createUserATA = true; }
1104
1137
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1105
1138
  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;
1139
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, marketCustodyConfig, positionAccount, params, instruction, closeWsolATAIns, error_3;
1107
1140
  return __generator(this, function (_b) {
1108
1141
  switch (_b.label) {
1109
1142
  case 0:
1110
- console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1143
+ console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
1111
1144
  publicKey = this.provider.wallet.publicKey;
1112
1145
  preInstructions = [];
1113
1146
  instructions = [];
@@ -1116,7 +1149,7 @@ var PerpetualsClient = (function () {
1116
1149
  _b.label = 1;
1117
1150
  case 1:
1118
1151
  _b.trys.push([1, 9, , 10]);
1119
- if (!(receivingTokenSymbol == 'SOL')) return [3, 3];
1152
+ if (!(collateralSymbol == 'SOL')) return [3, 3];
1120
1153
  wrappedSolAccount = new web3_js_1.Keypair();
1121
1154
  userReceivingTokenAccount = wrappedSolAccount.publicKey;
1122
1155
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
@@ -1137,7 +1170,7 @@ var PerpetualsClient = (function () {
1137
1170
  ];
1138
1171
  additionalSigners.push(wrappedSolAccount);
1139
1172
  return [3, 7];
1140
- case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1173
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1141
1174
  case 4:
1142
1175
  userReceivingTokenAccount = _b.sent();
1143
1176
  _a = createUserATA;
@@ -1148,16 +1181,17 @@ var PerpetualsClient = (function () {
1148
1181
  _b.label = 6;
1149
1182
  case 6:
1150
1183
  if (_a) {
1151
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1184
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
1152
1185
  }
1153
1186
  _b.label = 7;
1154
1187
  case 7:
1155
- receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1188
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1189
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1156
1190
  positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1157
1191
  Buffer.from("position"),
1158
1192
  publicKey.toBuffer(),
1159
1193
  poolConfig.poolAddress.toBuffer(),
1160
- receivingTokenCustody.custodyAccount.toBuffer(),
1194
+ marketCustodyConfig.custodyAccount.toBuffer(),
1161
1195
  (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1162
1196
  ], this.programId)[0];
1163
1197
  params = {
@@ -1172,15 +1206,18 @@ var PerpetualsClient = (function () {
1172
1206
  perpetuals: poolConfig.perpetuals,
1173
1207
  pool: poolConfig.poolAddress,
1174
1208
  position: positionAccount,
1175
- custody: receivingTokenCustody.custodyAccount,
1176
- custodyOracleAccount: receivingTokenCustody.oracleAddress,
1177
- custodyTokenAccount: receivingTokenCustody.tokenAccount,
1209
+ custody: marketCustodyConfig.custodyAccount,
1210
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1211
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1212
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1213
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1214
+ eventAuthority: this.eventAuthority.publicKey,
1178
1215
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1179
1216
  }).instruction()];
1180
1217
  case 8:
1181
1218
  instruction = _b.sent();
1182
1219
  instructions.push(instruction);
1183
- if (receivingTokenSymbol == 'WSOL' && closeUsersWSOLATA) {
1220
+ if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
1184
1221
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1185
1222
  postInstructions.push(closeWsolATAIns);
1186
1223
  }
@@ -1197,187 +1234,6 @@ var PerpetualsClient = (function () {
1197
1234
  });
1198
1235
  });
1199
1236
  };
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
1237
  this.swap = function (poolReceivingTokenSymbol, poolDispensingTokenSymbol, amountIn, minAmountOut, poolConfig, unWrapSol) {
1382
1238
  if (unWrapSol === void 0) { unWrapSol = false; }
1383
1239
  return __awaiter(_this, void 0, void 0, function () {
@@ -1484,6 +1340,7 @@ var PerpetualsClient = (function () {
1484
1340
  dispensingCustody: poolDispensingCustodyConfig.custodyAccount,
1485
1341
  dispensingCustodyOracleAccount: poolDispensingCustodyConfig.oracleAddress,
1486
1342
  dispensingCustodyTokenAccount: poolDispensingCustodyConfig.tokenAccount,
1343
+ eventAuthority: this.eventAuthority.publicKey,
1487
1344
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1488
1345
  })
1489
1346
  .instruction()];
@@ -1504,18 +1361,19 @@ var PerpetualsClient = (function () {
1504
1361
  });
1505
1362
  });
1506
1363
  };
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;
1364
+ this.addCollateral = function (collateralWithoutFee, fee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1365
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userPayingTokenAccount, collateralWithfee, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, inx;
1509
1366
  return __generator(this, function (_d) {
1510
1367
  switch (_d.label) {
1511
1368
  case 0:
1512
1369
  publicKey = this.provider.wallet.publicKey;
1513
- custody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1514
- if (!custody) {
1370
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1371
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1372
+ if (!collateralCustodyConfig || !marketCustodyConfig) {
1515
1373
  throw "payTokenCustody not found";
1516
1374
  }
1517
1375
  instructions = [];
1518
- userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(custody.mintKey, publicKey);
1376
+ userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey);
1519
1377
  collateralWithfee = collateralWithoutFee.add(fee);
1520
1378
  if (!(collateralSymbol == 'SOL')) return [3, 7];
1521
1379
  console.log("collateralSymbol === sol", collateralSymbol);
@@ -1557,7 +1415,7 @@ var PerpetualsClient = (function () {
1557
1415
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1558
1416
  _d.label = 7;
1559
1417
  case 7: return [4, this.program.methods.addCollateral({
1560
- collateral: collateralWithoutFee
1418
+ collateralDelta: collateralWithoutFee
1561
1419
  }).accounts({
1562
1420
  owner: publicKey,
1563
1421
  position: positionPubKey,
@@ -1565,9 +1423,12 @@ var PerpetualsClient = (function () {
1565
1423
  transferAuthority: poolConfig.transferAuthority,
1566
1424
  perpetuals: poolConfig.perpetuals,
1567
1425
  pool: poolConfig.poolAddress,
1568
- custody: custody.custodyAccount,
1569
- custodyOracleAccount: custody.oracleAddress,
1570
- custodyTokenAccount: custody.tokenAccount,
1426
+ custody: marketCustodyConfig.custodyAccount,
1427
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1428
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1429
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1430
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1431
+ eventAuthority: this.eventAuthority.publicKey,
1571
1432
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1572
1433
  })
1573
1434
  .instruction()];
@@ -1578,16 +1439,19 @@ var PerpetualsClient = (function () {
1578
1439
  }
1579
1440
  });
1580
1441
  }); };
1581
- this.removeCollateral = function (collateralWithoutFee, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1582
- var publicKey, custody, instructions, userReceivingTokenAccount, ix, closeWsolATAIns;
1442
+ this.removeCollateral = function (collateralWithoutFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1443
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userReceivingTokenAccount, ix, closeWsolATAIns;
1583
1444
  return __generator(this, function (_a) {
1584
1445
  switch (_a.label) {
1585
1446
  case 0:
1586
1447
  publicKey = this.provider.wallet.publicKey;
1587
- custody = poolConfig.custodies.find(function (i) {
1448
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) {
1588
1449
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1589
1450
  });
1590
- if (!custody) {
1451
+ marketCustodyConfig = poolConfig.custodies.find(function (i) {
1452
+ return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1453
+ });
1454
+ if (!collateralCustodyConfig) {
1591
1455
  throw "payTokenCustody not found";
1592
1456
  }
1593
1457
  instructions = [];
@@ -1599,7 +1463,7 @@ var PerpetualsClient = (function () {
1599
1463
  }
1600
1464
  return [4, this.program.methods
1601
1465
  .removeCollateral({
1602
- collateralUsd: collateralWithoutFee,
1466
+ collateralDelta: collateralWithoutFee,
1603
1467
  })
1604
1468
  .accounts({
1605
1469
  owner: publicKey,
@@ -1608,9 +1472,12 @@ var PerpetualsClient = (function () {
1608
1472
  perpetuals: poolConfig.perpetuals,
1609
1473
  pool: poolConfig.poolAddress,
1610
1474
  position: positionPubKey,
1611
- custody: custody.custodyAccount,
1612
- custodyOracleAccount: custody.oracleAddress,
1613
- custodyTokenAccount: custody.tokenAccount,
1475
+ custody: marketCustodyConfig.custodyAccount,
1476
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1477
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1478
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1479
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1480
+ eventAuthority: this.eventAuthority.publicKey,
1614
1481
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1615
1482
  })
1616
1483
  .instruction()];
@@ -1734,6 +1601,7 @@ var PerpetualsClient = (function () {
1734
1601
  custodyOracleAccount: payTokenCustody.oracleAddress,
1735
1602
  custodyTokenAccount: payTokenCustody.tokenAccount,
1736
1603
  lpTokenMint: poolConfig.lpTokenMint,
1604
+ eventAuthority: this.eventAuthority.publicKey,
1737
1605
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1738
1606
  })
1739
1607
  .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
@@ -1809,6 +1677,7 @@ var PerpetualsClient = (function () {
1809
1677
  custodyOracleAccount: recieveTokenCustody.oracleAddress,
1810
1678
  custodyTokenAccount: recieveTokenCustody.tokenAccount,
1811
1679
  lpTokenMint: poolConfig.lpTokenMint,
1680
+ eventAuthority: this.eventAuthority.publicKey,
1812
1681
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1813
1682
  })
1814
1683
  .remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
@@ -1842,6 +1711,7 @@ var PerpetualsClient = (function () {
1842
1711
  this.multisig = this.findProgramAddress("multisig");
1843
1712
  this.authority = this.findProgramAddress("transfer_authority");
1844
1713
  this.perpetuals = this.findProgramAddress("perpetuals");
1714
+ this.eventAuthority = this.findProgramAddress("__event_authority");
1845
1715
  this.prioritizationFee = (opts === null || opts === void 0 ? void 0 : opts.prioritizationFee) || 0;
1846
1716
  this.postSendTxCallback = opts === null || opts === void 0 ? void 0 : opts.postSendTxCallback;
1847
1717
  this.txConfirmationCommitment = (_a = opts === null || opts === void 0 ? void 0 : opts.txConfirmationCommitment) !== null && _a !== void 0 ? _a : 'processed';
@@ -1850,13 +1720,12 @@ var PerpetualsClient = (function () {
1850
1720
  };
1851
1721
  }
1852
1722
  PerpetualsClient.prototype.sendTransaction = function (ixs, opts) {
1853
- var _a;
1854
1723
  if (opts === void 0) { opts = {}; }
1855
1724
  return __awaiter(this, void 0, void 0, function () {
1856
- return __generator(this, function (_b) {
1857
- switch (_b.label) {
1858
- case 0: return [4, (0, rpc_1.sendTransaction)(this.program.provider, ixs, (_a = opts.alts) !== null && _a !== void 0 ? _a : [], __assign({ postSendTxCallback: this.postSendTxCallback, prioritizationFee: this.prioritizationFee, txConfirmationCommitment: this.txConfirmationCommitment }, opts))];
1859
- case 1: return [2, _b.sent()];
1725
+ return __generator(this, function (_a) {
1726
+ switch (_a.label) {
1727
+ case 0: return [4, (0, rpc_1.sendTransaction)(this.program.provider, ixs, __assign({ postSendTxCallback: this.postSendTxCallback, prioritizationFee: this.prioritizationFee }, opts))];
1728
+ case 1: return [2, _a.sent()];
1860
1729
  }
1861
1730
  });
1862
1731
  });
@@ -27,9 +27,10 @@ export declare class PoolConfig {
27
27
  perpetuals: PublicKey;
28
28
  transferAuthority: PublicKey;
29
29
  multisig: PublicKey;
30
+ addressLookupTableAddresses: PublicKey[];
30
31
  tokens: Token[];
31
32
  custodies: CustodyConfig[];
32
- constructor(programId: PublicKey, cluster: Cluster, poolName: string, poolAddress: PublicKey, lpTokenMint: PublicKey, lpDecimals: number, perpetuals: PublicKey, transferAuthority: PublicKey, multisig: PublicKey, tokens: Token[], custodies: CustodyConfig[]);
33
+ constructor(programId: PublicKey, cluster: Cluster, poolName: string, poolAddress: PublicKey, lpTokenMint: PublicKey, lpDecimals: number, perpetuals: PublicKey, transferAuthority: PublicKey, multisig: PublicKey, addressLookupTableAddresses: PublicKey[], tokens: Token[], custodies: CustodyConfig[]);
33
34
  getAllTokenMints(): PublicKey[];
34
35
  getNonStableTokens(): PublicKey[];
35
36
  getAllCustodies(): PublicKey[];