flash-sdk 1.0.15 → 1.0.17

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.
@@ -116,7 +116,7 @@ var PerpetualsClient = /** @class */ (function () {
116
116
  };
117
117
  this.getPool = function (name) { return __awaiter(_this, void 0, void 0, function () {
118
118
  return __generator(this, function (_a) {
119
- console.log("pool:", this.getPoolKey(name).toBase58());
119
+ // console.log("pool:", this.getPoolKey(name).toBase58())
120
120
  return [2 /*return*/, this.program.account.pool.fetch(this.getPoolKey(name))];
121
121
  });
122
122
  }); };
@@ -163,7 +163,7 @@ var PerpetualsClient = /** @class */ (function () {
163
163
  };
164
164
  this.getCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
165
165
  return __generator(this, function (_a) {
166
- console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
166
+ // console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
167
167
  return [2 /*return*/, this.program.account.custody.fetch(this.getCustodyKey(poolName, tokenMint))];
168
168
  });
169
169
  }); };
@@ -369,7 +369,7 @@ var PerpetualsClient = /** @class */ (function () {
369
369
  err_1 = _a.sent();
370
370
  // @ts-ignore
371
371
  if (this.printErrors) {
372
- console.log(err_1);
372
+ console.error("setAdminSigners err:", err_1);
373
373
  }
374
374
  throw err_1;
375
375
  case 4: return [2 /*return*/];
@@ -434,11 +434,7 @@ var PerpetualsClient = /** @class */ (function () {
434
434
  return __generator(this, function (_a) {
435
435
  switch (_a.label) {
436
436
  case 0:
437
- console.log("CustodyKey", this.getCustodyKey(poolName, tokenMint).toBase58());
438
- console.log("getCustodyTokenAccountKey", this.getCustodyTokenAccountKey(poolName, tokenMint).toBase58());
439
- _a.label = 1;
440
- case 1:
441
- _a.trys.push([1, 3, , 4]);
437
+ _a.trys.push([0, 2, , 3]);
442
438
  return [4 /*yield*/, this.program.methods
443
439
  // @ts-ignore
444
440
  .addCustody({
@@ -469,15 +465,15 @@ var PerpetualsClient = /** @class */ (function () {
469
465
  console.error(err);
470
466
  throw err;
471
467
  })];
472
- case 2:
468
+ case 1:
473
469
  trx_id = _a.sent();
474
470
  console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
475
- return [3 /*break*/, 4];
476
- case 3:
471
+ return [3 /*break*/, 3];
472
+ case 2:
477
473
  error_1 = _a.sent();
478
- console.log("cli error :", error_1);
479
- return [3 /*break*/, 4];
480
- case 4: return [2 /*return*/];
474
+ console.error("cli error :", error_1);
475
+ return [3 /*break*/, 3];
476
+ case 3: return [2 /*return*/];
481
477
  }
482
478
  });
483
479
  }); };
@@ -485,39 +481,36 @@ var PerpetualsClient = /** @class */ (function () {
485
481
  var trx_id;
486
482
  return __generator(this, function (_a) {
487
483
  switch (_a.label) {
488
- case 0:
489
- console.log("CustodyKey", this.getCustodyKey(poolName, tokenMint).toBase58());
490
- console.log("getCustodyTokenAccountKey", this.getCustodyTokenAccountKey(poolName, tokenMint).toBase58());
491
- return [4 /*yield*/, this.program.methods
492
- //@ts-ignore
493
- .testingEditCustody({
494
- isStable: isStable,
495
- oracle: oracle,
496
- pricing: pricing,
497
- permissions: permissions,
498
- fees: fees,
499
- borrowRate: borrowRate,
500
- ratios: ratios,
501
- })
502
- .accounts({
503
- admin: this.admin,
504
- multisig: this.multisig.publicKey,
505
- transferAuthority: this.authority.publicKey,
506
- perpetuals: this.perpetuals.publicKey,
507
- pool: this.getPoolKey(poolName),
508
- custody: this.getCustodyKey(poolName, tokenMint),
509
- custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
510
- custodyTokenMint: tokenMint,
511
- systemProgram: web3_js_1.SystemProgram.programId,
512
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
513
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
514
- })
515
- // .signers([this.admin])
516
- .rpc()
517
- .catch(function (err) {
518
- console.error(err);
519
- throw err;
520
- })];
484
+ case 0: return [4 /*yield*/, this.program.methods
485
+ //@ts-ignore
486
+ .testingEditCustody({
487
+ isStable: isStable,
488
+ oracle: oracle,
489
+ pricing: pricing,
490
+ permissions: permissions,
491
+ fees: fees,
492
+ borrowRate: borrowRate,
493
+ ratios: ratios,
494
+ })
495
+ .accounts({
496
+ admin: this.admin,
497
+ multisig: this.multisig.publicKey,
498
+ transferAuthority: this.authority.publicKey,
499
+ perpetuals: this.perpetuals.publicKey,
500
+ pool: this.getPoolKey(poolName),
501
+ custody: this.getCustodyKey(poolName, tokenMint),
502
+ custodyTokenAccount: this.getCustodyTokenAccountKey(poolName, tokenMint),
503
+ custodyTokenMint: tokenMint,
504
+ systemProgram: web3_js_1.SystemProgram.programId,
505
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
506
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
507
+ })
508
+ // .signers([this.admin])
509
+ .rpc()
510
+ .catch(function (err) {
511
+ console.error(err);
512
+ throw err;
513
+ })];
521
514
  case 1:
522
515
  trx_id = _a.sent();
523
516
  console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
@@ -706,20 +699,12 @@ var PerpetualsClient = /** @class */ (function () {
706
699
  });
707
700
  }); };
708
701
  this.getEntryPriceAndFee = function (poolName, tokenMint, collateral, size, side) { return __awaiter(_this, void 0, void 0, function () {
709
- var _a, _b, _c, _d, _e;
710
- var _f;
711
- return __generator(this, function (_g) {
712
- switch (_g.label) {
702
+ var _a, _b;
703
+ var _c;
704
+ return __generator(this, function (_d) {
705
+ switch (_d.label) {
713
706
  case 0:
714
- console.log("perps: ", this.perpetuals.publicKey.toBase58());
715
- console.log("poolKey: ", this.getPoolKey(poolName).toBase58());
716
- console.log("custody key : ", this.getCustodyKey(poolName, tokenMint).toBase58());
717
- _b = (_a = console).log;
718
- _c = ["orcalve: "];
719
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
720
- case 1:
721
- _b.apply(_a, _c.concat([(_g.sent()).toBase58()]));
722
- _e = (_d = this.program.methods
707
+ _b = (_a = this.program.methods
723
708
  //@ts-ignore
724
709
  .getEntryPriceAndFee({
725
710
  collateral: collateral,
@@ -727,23 +712,27 @@ var PerpetualsClient = /** @class */ (function () {
727
712
  side: side == "long" ? { long: {} } : { short: {} },
728
713
  }))
729
714
  .accounts;
730
- _f = {
715
+ _c = {
731
716
  // signer: this.provider.wallet.publicKey,
732
717
  perpetuals: this.perpetuals.publicKey,
733
718
  pool: this.getPoolKey(poolName),
734
719
  custody: this.getCustodyKey(poolName, tokenMint)
735
720
  };
736
721
  return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
737
- case 2: return [4 /*yield*/, _e.apply(_d, [(_f.custodyOracleAccount = _g.sent(),
738
- _f)])
722
+ case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
723
+ _c)])
739
724
  .view()
740
725
  .catch(function (err) {
741
726
  console.error(err);
742
727
  throw err;
743
728
  })];
744
- case 3:
729
+ case 2:
730
+ // console.log("perps: ", this.perpetuals.publicKey.toBase58())
731
+ // console.log("poolKey: ", this.getPoolKey(poolName).toBase58())
732
+ // console.log("custody key : ", this.getCustodyKey(poolName, tokenMint).toBase58());
733
+ // console.log("oracle: ", (await this.getCustodyOracleAccountKey(poolName, tokenMint)).toBase58())
745
734
  //@ts-ignore
746
- return [2 /*return*/, _g.sent()];
735
+ return [2 /*return*/, _d.sent()];
747
736
  }
748
737
  });
749
738
  }); };
@@ -890,7 +879,6 @@ var PerpetualsClient = /** @class */ (function () {
890
879
  switch (_d.label) {
891
880
  case 0:
892
881
  pos = this.getPositionKey(wallet, poolName, tokenMint, side);
893
- console.log("pos:", pos.toBase58());
894
882
  _b = (_a = this.program.methods
895
883
  .getPnl({}))
896
884
  .accounts;
@@ -908,7 +896,9 @@ var PerpetualsClient = /** @class */ (function () {
908
896
  console.error(err);
909
897
  throw err;
910
898
  })];
911
- case 2: return [2 /*return*/, _d.sent()];
899
+ case 2:
900
+ // console.log("pos:", pos.toBase58())
901
+ return [2 /*return*/, _d.sent()];
912
902
  }
913
903
  });
914
904
  }); };
@@ -919,7 +909,6 @@ var PerpetualsClient = /** @class */ (function () {
919
909
  switch (_d.label) {
920
910
  case 0:
921
911
  pos = this.getPositionKey(wallet, poolName, tokenMint, side);
922
- console.log("pos:", pos.toBase58());
923
912
  _b = (_a = this.program.methods
924
913
  .getPnl({}))
925
914
  .accounts;
@@ -937,7 +926,9 @@ var PerpetualsClient = /** @class */ (function () {
937
926
  console.error(err);
938
927
  throw err;
939
928
  })];
940
- case 2: return [2 /*return*/, _d.sent()];
929
+ case 2:
930
+ // console.log("pos:", pos.toBase58())
931
+ return [2 /*return*/, _d.sent()];
941
932
  }
942
933
  });
943
934
  }); };
@@ -1038,11 +1029,228 @@ var PerpetualsClient = /** @class */ (function () {
1038
1029
  // TODO: ATA check - else create - DONE
1039
1030
  // Create WSOL Token account and not ATA and close it in end
1040
1031
  // TODO: close other Accounts - NOT NEEDED
1041
- this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
1032
+ this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
1033
+ if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1034
+ if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
1035
+ return __awaiter(_this, void 0, void 0, function () {
1036
+ var publicKey, payTokenCustody, userCustodyTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, tokenAccountBalance, _a, positionAccount, params, instruction, error_2;
1037
+ return __generator(this, function (_b) {
1038
+ switch (_b.label) {
1039
+ case 0:
1040
+ console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1041
+ publicKey = this.provider.wallet.publicKey;
1042
+ payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1043
+ return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1044
+ case 1:
1045
+ userCustodyTokenAccount = _b.sent();
1046
+ preInstructions = [];
1047
+ instructions = [];
1048
+ postInstructions = [];
1049
+ additionalSigners = [];
1050
+ _b.label = 2;
1051
+ case 2:
1052
+ _b.trys.push([2, 10, , 11]);
1053
+ if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 4];
1054
+ console.log("payTokenSymbol === sol", payTokenSymbol);
1055
+ wrappedSolAccount = new web3_js_1.Keypair();
1056
+ return [4 /*yield*/, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1057
+ case 3:
1058
+ accCreationLamports = (_b.sent());
1059
+ console.log("accCreationLamports:", accCreationLamports);
1060
+ lamports = collateralWithfee.add(new anchor_1.BN(accCreationLamports));
1061
+ preInstructions = [
1062
+ web3_js_1.SystemProgram.createAccount({
1063
+ fromPubkey: publicKey,
1064
+ newAccountPubkey: wrappedSolAccount.publicKey,
1065
+ lamports: lamports.toNumber(),
1066
+ space: 165,
1067
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1068
+ }),
1069
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1070
+ ];
1071
+ postInstructions = [
1072
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1073
+ ];
1074
+ additionalSigners.push(wrappedSolAccount);
1075
+ return [3 /*break*/, 8];
1076
+ case 4:
1077
+ if (!(createUserWSOLATA == false)) return [3 /*break*/, 5];
1078
+ console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1079
+ return [3 /*break*/, 8];
1080
+ case 5: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1081
+ case 6:
1082
+ // for other tokens check if ATA and balance
1083
+ if (!(_b.sent())) {
1084
+ throw "Insufficient Funds , token Account doesn't exist";
1085
+ }
1086
+ if (!!skipBalanceChecks) return [3 /*break*/, 8];
1087
+ _a = anchor_1.BN.bind;
1088
+ return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1089
+ case 7:
1090
+ tokenAccountBalance = new (_a.apply(anchor_1.BN, [void 0, (_b.sent()).value.amount]))();
1091
+ if (tokenAccountBalance.lt(collateralWithfee)) {
1092
+ throw "Insufficient Funds need more ".concat(collateralWithfee.sub(tokenAccountBalance), " tokens");
1093
+ }
1094
+ _b.label = 8;
1095
+ case 8:
1096
+ positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1097
+ Buffer.from("position"),
1098
+ publicKey.toBuffer(),
1099
+ poolConfig.poolAddress.toBuffer(),
1100
+ payTokenCustody.custodyAccount.toBuffer(),
1101
+ (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1102
+ ], this.programId)[0];
1103
+ params = {
1104
+ price: priceAfterSlippage,
1105
+ collateral: collateralWithfee,
1106
+ size: size,
1107
+ side: side,
1108
+ };
1109
+ return [4 /*yield*/, this.program.methods
1110
+ .openPosition(params)
1111
+ .accounts({
1112
+ owner: publicKey,
1113
+ fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userCustodyTokenAccount,
1114
+ transferAuthority: poolConfig.transferAuthority,
1115
+ perpetuals: poolConfig.perpetuals,
1116
+ pool: poolConfig.poolAddress,
1117
+ position: positionAccount,
1118
+ custody: payTokenCustody.custodyAccount,
1119
+ custodyOracleAccount: payTokenCustody.oracleAddress,
1120
+ custodyTokenAccount: payTokenCustody.tokenAccount,
1121
+ systemProgram: web3_js_1.SystemProgram.programId,
1122
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1123
+ }).instruction()];
1124
+ case 9:
1125
+ instruction = _b.sent();
1126
+ instructions.push(instruction);
1127
+ return [3 /*break*/, 11];
1128
+ case 10:
1129
+ error_2 = _b.sent();
1130
+ console.log("perpClient openPosition error:", error_2);
1131
+ return [3 /*break*/, 11];
1132
+ case 11: return [2 /*return*/, {
1133
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1134
+ additionalSigners: additionalSigners
1135
+ }];
1136
+ }
1137
+ });
1138
+ });
1139
+ };
1140
+ // TODO: handle SOL wrapping to WSOL and create a ATA - NOT NEEDED
1141
+ // TODO : Balance checks - NOT NEEDED
1142
+ // TODO: ATA check - else create - DONE
1143
+ // TODO: for close Accounts - DONE BY ANCHOR
1144
+ // TODO : if out token WSOL -> unwrap to SOL - DONE
1145
+ this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA // to get back WSOL=>SOL
1146
+ ) {
1147
+ if (createUserATA === void 0) { createUserATA = true; }
1148
+ if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1149
+ return __awaiter(_this, void 0, void 0, function () {
1150
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_3;
1151
+ return __generator(this, function (_b) {
1152
+ switch (_b.label) {
1153
+ case 0:
1154
+ console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1155
+ publicKey = this.provider.wallet.publicKey;
1156
+ preInstructions = [];
1157
+ instructions = [];
1158
+ postInstructions = [];
1159
+ additionalSigners = [];
1160
+ _b.label = 1;
1161
+ case 1:
1162
+ _b.trys.push([1, 9, , 10]);
1163
+ if (!(receivingTokenSymbol == 'SOL')) return [3 /*break*/, 3];
1164
+ wrappedSolAccount = new web3_js_1.Keypair();
1165
+ userReceivingTokenAccount = wrappedSolAccount.publicKey;
1166
+ return [4 /*yield*/, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1167
+ case 2:
1168
+ lamports = (_b.sent());
1169
+ preInstructions = [
1170
+ web3_js_1.SystemProgram.createAccount({
1171
+ fromPubkey: publicKey,
1172
+ newAccountPubkey: wrappedSolAccount.publicKey,
1173
+ lamports: lamports,
1174
+ space: 165,
1175
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1176
+ }),
1177
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1178
+ ];
1179
+ postInstructions = [
1180
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1181
+ ];
1182
+ additionalSigners.push(wrappedSolAccount);
1183
+ return [3 /*break*/, 7];
1184
+ case 3: return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1185
+ case 4:
1186
+ userReceivingTokenAccount = _b.sent();
1187
+ _a = createUserATA;
1188
+ if (!_a) return [3 /*break*/, 6];
1189
+ return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1190
+ case 5:
1191
+ _a = !(_b.sent());
1192
+ _b.label = 6;
1193
+ case 6:
1194
+ if (_a) {
1195
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1196
+ }
1197
+ _b.label = 7;
1198
+ case 7:
1199
+ receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1200
+ positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1201
+ Buffer.from("position"),
1202
+ publicKey.toBuffer(),
1203
+ poolConfig.poolAddress.toBuffer(),
1204
+ receivingTokenCustody.custodyAccount.toBuffer(),
1205
+ (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1206
+ ], this.programId)[0];
1207
+ params = {
1208
+ price: priceAfterSlippage,
1209
+ };
1210
+ return [4 /*yield*/, this.program.methods
1211
+ .closePosition(params)
1212
+ .accounts({
1213
+ owner: publicKey,
1214
+ receivingAccount: userReceivingTokenAccount,
1215
+ transferAuthority: poolConfig.transferAuthority,
1216
+ perpetuals: poolConfig.perpetuals,
1217
+ pool: poolConfig.poolAddress,
1218
+ position: positionAccount,
1219
+ custody: receivingTokenCustody.custodyAccount,
1220
+ custodyOracleAccount: receivingTokenCustody.oracleAddress,
1221
+ custodyTokenAccount: receivingTokenCustody.tokenAccount,
1222
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1223
+ }).instruction()];
1224
+ case 8:
1225
+ instruction = _b.sent();
1226
+ instructions.push(instruction);
1227
+ if (receivingTokenSymbol == 'WSOL' && closeUsersWSOLATA) {
1228
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1229
+ postInstructions.push(closeWsolATAIns);
1230
+ }
1231
+ return [3 /*break*/, 10];
1232
+ case 9:
1233
+ error_3 = _b.sent();
1234
+ console.error("perpclient closePosition error:", error_3);
1235
+ return [3 /*break*/, 10];
1236
+ case 10: return [2 /*return*/, {
1237
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1238
+ additionalSigners: additionalSigners
1239
+ }];
1240
+ }
1241
+ });
1242
+ });
1243
+ };
1244
+ // ==== OLD
1245
+ // TODO: handle SOL wrapping to WSOL and create a ATA - DONE
1246
+ // TODO: Balance checks - DONE
1247
+ // TODO: ATA check - else create - DONE
1248
+ // TODO: for close Accounts - NOT NEEDED
1249
+ this.openPositionOld = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
1042
1250
  ) {
1043
1251
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1044
1252
  return __awaiter(_this, void 0, void 0, function () {
1045
- var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_2;
1253
+ var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_4;
1046
1254
  return __generator(this, function (_e) {
1047
1255
  switch (_e.label) {
1048
1256
  case 0:
@@ -1155,85 +1363,82 @@ var PerpetualsClient = /** @class */ (function () {
1155
1363
  instructions.push(instruction);
1156
1364
  return [3 /*break*/, 16];
1157
1365
  case 15:
1158
- error_2 = _e.sent();
1159
- console.log("perpClient openPosition error:", error_2);
1366
+ error_4 = _e.sent();
1367
+ console.log("perpClient openPosition error:", error_4);
1160
1368
  return [3 /*break*/, 16];
1161
1369
  case 16: return [2 /*return*/, instructions];
1162
1370
  }
1163
1371
  });
1164
1372
  });
1165
1373
  };
1374
+ // ==== OLD
1166
1375
  // TODO: handle SOL wrapping to WSOL and create a ATA - NOT NEEDED
1167
1376
  // TODO : Balance checks - NOT NEEDED
1168
1377
  // TODO: ATA check - else create - DONE
1169
1378
  // TODO: for close Accounts - DONE BY ANCHOR
1170
1379
  // TODO : if out token WSOL -> unwrap to SOL - DONE
1171
- this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, closeUsersWSOLATA // to get back WSOL=>SOL
1172
- ) {
1173
- if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1174
- return __awaiter(_this, void 0, void 0, function () {
1175
- var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_3;
1176
- return __generator(this, function (_a) {
1177
- switch (_a.label) {
1178
- case 0:
1179
- console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1180
- publicKey = this.provider.wallet.publicKey;
1181
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1182
- case 1:
1183
- userReceivingTokenAccount = _a.sent();
1184
- instructions = [];
1185
- _a.label = 2;
1186
- case 2:
1187
- _a.trys.push([2, 5, , 6]);
1188
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1189
- case 3:
1190
- if (!(_a.sent())) {
1191
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1192
- }
1193
- receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1194
- positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1195
- Buffer.from("position"),
1196
- publicKey.toBuffer(),
1197
- poolConfig.poolAddress.toBuffer(),
1198
- receivingTokenCustody.custodyAccount.toBuffer(),
1199
- (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1200
- ], this.programId)[0];
1201
- console.log("positionAccount:", positionAccount.toBase58());
1202
- params = {
1203
- price: priceAfterSlippage,
1204
- };
1205
- return [4 /*yield*/, this.program.methods
1206
- .closePosition(params)
1207
- .accounts({
1208
- owner: publicKey,
1209
- receivingAccount: userReceivingTokenAccount,
1210
- transferAuthority: poolConfig.transferAuthority,
1211
- perpetuals: poolConfig.perpetuals,
1212
- pool: poolConfig.poolAddress,
1213
- position: positionAccount,
1214
- custody: receivingTokenCustody.custodyAccount,
1215
- custodyOracleAccount: receivingTokenCustody.oracleAddress,
1216
- custodyTokenAccount: receivingTokenCustody.tokenAccount,
1217
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1218
- }).instruction()];
1219
- case 4:
1220
- instruction = _a.sent();
1221
- instructions.push(instruction);
1222
- // SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
1223
- if (receivingTokenSymbol == 'SOL' && closeUsersWSOLATA) {
1224
- closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1225
- instructions.push(closeWsolATAIns);
1226
- }
1227
- return [3 /*break*/, 6];
1228
- case 5:
1229
- error_3 = _a.sent();
1230
- console.error("perpclient closePosition error:", error_3);
1231
- return [3 /*break*/, 6];
1232
- case 6: return [2 /*return*/, instructions];
1233
- }
1234
- });
1380
+ this.closePositionOld = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1381
+ var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_5;
1382
+ return __generator(this, function (_a) {
1383
+ switch (_a.label) {
1384
+ case 0:
1385
+ console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1386
+ publicKey = this.provider.wallet.publicKey;
1387
+ return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1388
+ case 1:
1389
+ userReceivingTokenAccount = _a.sent();
1390
+ instructions = [];
1391
+ _a.label = 2;
1392
+ case 2:
1393
+ _a.trys.push([2, 5, , 6]);
1394
+ return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1395
+ case 3:
1396
+ if (!(_a.sent())) {
1397
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
1398
+ }
1399
+ receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
1400
+ positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1401
+ Buffer.from("position"),
1402
+ publicKey.toBuffer(),
1403
+ poolConfig.poolAddress.toBuffer(),
1404
+ receivingTokenCustody.custodyAccount.toBuffer(),
1405
+ (0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
1406
+ ], this.programId)[0];
1407
+ console.log("positionAccount:", positionAccount.toBase58());
1408
+ params = {
1409
+ price: priceAfterSlippage,
1410
+ };
1411
+ return [4 /*yield*/, this.program.methods
1412
+ .closePosition(params)
1413
+ .accounts({
1414
+ owner: publicKey,
1415
+ receivingAccount: userReceivingTokenAccount,
1416
+ transferAuthority: poolConfig.transferAuthority,
1417
+ perpetuals: poolConfig.perpetuals,
1418
+ pool: poolConfig.poolAddress,
1419
+ position: positionAccount,
1420
+ custody: receivingTokenCustody.custodyAccount,
1421
+ custodyOracleAccount: receivingTokenCustody.oracleAddress,
1422
+ custodyTokenAccount: receivingTokenCustody.tokenAccount,
1423
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1424
+ }).instruction()];
1425
+ case 4:
1426
+ instruction = _a.sent();
1427
+ instructions.push(instruction);
1428
+ // SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
1429
+ if (receivingTokenSymbol == 'SOL') {
1430
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1431
+ instructions.push(closeWsolATAIns);
1432
+ }
1433
+ return [3 /*break*/, 6];
1434
+ case 5:
1435
+ error_5 = _a.sent();
1436
+ console.error("perpclient closePosition error:", error_5);
1437
+ return [3 /*break*/, 6];
1438
+ case 6: return [2 /*return*/, instructions];
1439
+ }
1235
1440
  });
1236
- };
1441
+ }); };
1237
1442
  // TODO: ATA check - else create - DONE
1238
1443
  // TODO: handle SOL wrapping to WSOL and create a ATA - DONE
1239
1444
  // TODO : Balance checks - NOT NEEDED
@@ -1359,7 +1564,7 @@ var PerpetualsClient = /** @class */ (function () {
1359
1564
  return [3 /*break*/, 18];
1360
1565
  case 17:
1361
1566
  err_2 = _e.sent();
1362
- console.log("perpClient Swap error:: ", err_2);
1567
+ console.error("perpClient Swap error:: ", err_2);
1363
1568
  throw err_2;
1364
1569
  case 18: return [2 /*return*/, instructions];
1365
1570
  }