flash-sdk 1.0.44 → 1.0.46

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.
@@ -1024,31 +1024,38 @@ var PerpetualsClient = (function () {
1024
1024
  this.getAumSdk = function (poolAccount, token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime) {
1025
1025
  return poolAccount.getAssetsUnderManagementUsd(token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime);
1026
1026
  };
1027
- this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, skipBalanceChecks) {
1027
+ this.openPosition = function (marketSymbol, collateralSymbol, priceAfterSlippage, collateralWithfee, size, side, poolConfig, skipBalanceChecks) {
1028
1028
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
1029
1029
  return __awaiter(_this, void 0, void 0, function () {
1030
- var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, tokenAccountBalance, _a, positionAccount, params, instruction;
1031
- return __generator(this, function (_b) {
1032
- switch (_b.label) {
1030
+ var publicKey, marketCustodyConfig, collateralCustodyConfig, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
1031
+ return __generator(this, function (_c) {
1032
+ switch (_c.label) {
1033
1033
  case 0:
1034
1034
  publicKey = this.provider.wallet.publicKey;
1035
1035
  marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1036
1036
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1037
1037
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1038
1038
  case 1:
1039
- userCollateralTokenAccount = _b.sent();
1039
+ userCollateralTokenAccount = _c.sent();
1040
1040
  preInstructions = [];
1041
1041
  instructions = [];
1042
1042
  postInstructions = [];
1043
1043
  additionalSigners = [];
1044
- if (!(collateralSymbol == 'SOL')) return [3, 3];
1044
+ if (!(collateralSymbol == 'SOL')) return [3, 4];
1045
1045
  console.log("collateralSymbol === SOL", collateralSymbol);
1046
1046
  wrappedSolAccount = new web3_js_1.Keypair();
1047
1047
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1048
1048
  case 2:
1049
- accCreationLamports = (_b.sent());
1049
+ accCreationLamports = (_c.sent());
1050
1050
  console.log("accCreationLamports:", accCreationLamports);
1051
1051
  lamports = collateralWithfee.add(new anchor_1.BN(accCreationLamports));
1052
+ _a = anchor_1.BN.bind;
1053
+ return [4, this.provider.connection.getBalance(publicKey)];
1054
+ case 3:
1055
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _c.sent()]))();
1056
+ if (unWrappedSolBalance.lt(lamports)) {
1057
+ throw "Insufficient SOL Funds";
1058
+ }
1052
1059
  preInstructions = [
1053
1060
  web3_js_1.SystemProgram.createAccount({
1054
1061
  fromPubkey: publicKey,
@@ -1063,22 +1070,22 @@ var PerpetualsClient = (function () {
1063
1070
  (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1064
1071
  ];
1065
1072
  additionalSigners.push(wrappedSolAccount);
1066
- return [3, 6];
1067
- case 3: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
1068
- case 4:
1069
- if (!(_b.sent())) {
1073
+ return [3, 7];
1074
+ case 4: return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
1075
+ case 5:
1076
+ if (!(_c.sent())) {
1070
1077
  throw "Insufficient Funds , token Account doesn't exist";
1071
1078
  }
1072
- if (!!skipBalanceChecks) return [3, 6];
1073
- _a = anchor_1.BN.bind;
1079
+ if (!!skipBalanceChecks) return [3, 7];
1080
+ _b = anchor_1.BN.bind;
1074
1081
  return [4, this.provider.connection.getTokenAccountBalance(userCollateralTokenAccount)];
1075
- case 5:
1076
- tokenAccountBalance = new (_a.apply(anchor_1.BN, [void 0, (_b.sent()).value.amount]))();
1082
+ case 6:
1083
+ tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
1077
1084
  if (tokenAccountBalance.lt(collateralWithfee)) {
1078
1085
  throw "Insufficient Funds need more ".concat(collateralWithfee.sub(tokenAccountBalance), " tokens");
1079
1086
  }
1080
- _b.label = 6;
1081
- case 6:
1087
+ _c.label = 7;
1088
+ case 7:
1082
1089
  positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
1083
1090
  Buffer.from("position"),
1084
1091
  publicKey.toBuffer(),
@@ -1103,19 +1110,19 @@ var PerpetualsClient = (function () {
1103
1110
  position: positionAccount,
1104
1111
  custody: marketCustodyConfig.custodyAccount,
1105
1112
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
1113
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1106
1114
  collateralCustody: collateralCustodyConfig.custodyAccount,
1107
1115
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1116
+ collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1108
1117
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1109
1118
  eventAuthority: this.eventAuthority.publicKey,
1110
1119
  systemProgram: web3_js_1.SystemProgram.programId,
1111
1120
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1112
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1113
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1114
1121
  program: this.programId,
1115
1122
  payer: publicKey,
1116
1123
  }).instruction()];
1117
- case 7:
1118
- instruction = _b.sent();
1124
+ case 8:
1125
+ instruction = _c.sent();
1119
1126
  instructions.push(instruction);
1120
1127
  return [2, {
1121
1128
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
@@ -1144,7 +1151,6 @@ var PerpetualsClient = (function () {
1144
1151
  _b.trys.push([1, 9, , 10]);
1145
1152
  if (!(collateralSymbol == 'SOL')) return [3, 3];
1146
1153
  wrappedSolAccount = new web3_js_1.Keypair();
1147
- userReceivingTokenAccount = wrappedSolAccount.publicKey;
1148
1154
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1149
1155
  case 2:
1150
1156
  lamports = (_b.sent());
@@ -1194,20 +1200,20 @@ var PerpetualsClient = (function () {
1194
1200
  .closePosition(params)
1195
1201
  .accounts({
1196
1202
  owner: publicKey,
1197
- receivingAccount: userReceivingTokenAccount,
1203
+ receivingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
1198
1204
  transferAuthority: poolConfig.transferAuthority,
1199
1205
  perpetuals: poolConfig.perpetuals,
1200
1206
  pool: poolConfig.poolAddress,
1201
1207
  position: positionAccount,
1202
1208
  custody: marketCustodyConfig.custodyAccount,
1203
1209
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
1210
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1204
1211
  collateralCustody: collateralCustodyConfig.custodyAccount,
1205
1212
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1213
+ collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1206
1214
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1207
1215
  eventAuthority: this.eventAuthority.publicKey,
1208
1216
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1209
- collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1210
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1211
1217
  program: this.programId,
1212
1218
  payer: publicKey,
1213
1219
  }).instruction()];
@@ -1231,93 +1237,169 @@ var PerpetualsClient = (function () {
1231
1237
  });
1232
1238
  });
1233
1239
  };
1234
- this.swap = function (poolReceivingTokenSymbol, poolDispensingTokenSymbol, amountIn, minAmountOut, poolConfig, unWrapSol) {
1240
+ this.swap = function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, createUserATA, unWrapSol, skipBalanceChecks) {
1241
+ if (createUserATA === void 0) { createUserATA = true; }
1235
1242
  if (unWrapSol === void 0) { unWrapSol = false; }
1243
+ if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
1236
1244
  return __awaiter(_this, void 0, void 0, function () {
1237
- var userReceivingTokenSymbol, userDispensingTokenSymbol, poolReceivingCustodyConfig, poolDispensingCustodyConfig, publicKey, instructions, userReceivingTokenAccount, userDispensingCustodyTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, params, inx, closeWsolATAIns, err_2;
1238
- return __generator(this, function (_e) {
1239
- switch (_e.label) {
1245
+ var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, _d, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _e, custody, params, inx, closeWsolATAIns, err_2;
1246
+ return __generator(this, function (_f) {
1247
+ switch (_f.label) {
1240
1248
  case 0:
1241
- userReceivingTokenSymbol = poolDispensingTokenSymbol;
1242
- userDispensingTokenSymbol = poolReceivingTokenSymbol;
1243
- poolReceivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(poolReceivingTokenSymbol).mintKey.toBase58(); });
1244
- if (!poolReceivingCustodyConfig) {
1245
- throw "receivingTokenCustody not found";
1249
+ userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey.toBase58(); });
1250
+ if (!userInputCustodyConfig) {
1251
+ throw "userInputCustodyConfig not found";
1246
1252
  }
1247
- poolDispensingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(poolDispensingTokenSymbol).mintKey.toBase58(); });
1248
- if (!poolDispensingCustodyConfig) {
1249
- throw "dispensingTokenCustody not found";
1253
+ userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.toBase58() === poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey.toBase58(); });
1254
+ if (!userOutputCustodyConfig) {
1255
+ throw "userOutputCustodyConfig not found";
1250
1256
  }
1251
1257
  publicKey = this.provider.wallet.publicKey;
1258
+ preInstructions = [];
1252
1259
  instructions = [];
1253
- _e.label = 1;
1254
- case 1:
1255
- _e.trys.push([1, 17, , 18]);
1256
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolDispensingCustodyConfig.mintKey, publicKey)];
1257
- case 2:
1258
- userReceivingTokenAccount = _e.sent();
1259
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1260
- case 3:
1261
- if (!(_e.sent())) {
1262
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolDispensingCustodyConfig.mintKey));
1263
- }
1264
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolReceivingCustodyConfig.mintKey, publicKey)];
1265
- case 4:
1266
- userDispensingCustodyTokenAccount = _e.sent();
1267
- if (!(userDispensingTokenSymbol == 'SOL')) return [3, 12];
1268
- console.log("userDispensingTokenSymbol === sol", userDispensingTokenSymbol);
1260
+ postInstructions = [];
1261
+ additionalSigners = [];
1262
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
1269
1263
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1270
- case 5:
1271
- wsolAssociatedTokenAccount = _e.sent();
1264
+ case 1:
1265
+ wsolAssociatedTokenAccount = _f.sent();
1272
1266
  return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1273
- case 6:
1274
- wsolATAExist = _e.sent();
1267
+ case 2:
1268
+ wsolATAExist = _f.sent();
1275
1269
  if (!wsolATAExist) {
1276
- console.log("wsol ata does not exist");
1277
1270
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1278
1271
  }
1279
1272
  _a = anchor_1.BN.bind;
1280
- if (!wsolATAExist) return [3, 8];
1281
- return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1282
- case 7:
1283
- _b = (_e.sent()).value.amount;
1284
- return [3, 9];
1285
- case 8:
1286
- _b = 0;
1287
- _e.label = 9;
1288
- case 9:
1289
- wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1290
- if (!wsolBalance.lt(amountIn)) return [3, 11];
1291
- _c = anchor_1.BN.bind;
1292
1273
  return [4, this.provider.connection.getBalance(publicKey)];
1293
- case 10:
1294
- unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
1295
- totalSolBal = unWrappedSolBalance.add(wsolBalance);
1296
- if (totalSolBal.lt(amountIn)) {
1274
+ case 3:
1275
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
1276
+ if (unWrappedSolBalance.lt(amountIn)) {
1297
1277
  throw "Insufficient SOL Funds";
1298
1278
  }
1299
- conversionAmt = amountIn.sub(wsolBalance);
1300
1279
  instructions.push(web3_js_1.SystemProgram.transfer({
1301
1280
  fromPubkey: publicKey,
1302
1281
  toPubkey: wsolAssociatedTokenAccount,
1303
- lamports: conversionAmt.toNumber(),
1282
+ lamports: amountIn.toNumber(),
1304
1283
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1305
- _e.label = 11;
1306
- case 11: return [3, 15];
1307
- case 12: return [4, (0, utils_1.checkIfAccountExists)(userDispensingCustodyTokenAccount, this.provider.connection)];
1308
- case 13:
1309
- if (!(_e.sent())) {
1284
+ return [2, {
1285
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1286
+ additionalSigners: additionalSigners
1287
+ }];
1288
+ case 4:
1289
+ if (!(userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL')) return [3, 6];
1290
+ console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
1291
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1292
+ case 5:
1293
+ wsolAssociatedTokenAccount = _f.sent();
1294
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(wsolAssociatedTokenAccount, publicKey, publicKey);
1295
+ instructions.push(closeWsolATAIns);
1296
+ return [2, {
1297
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1298
+ additionalSigners: additionalSigners
1299
+ }];
1300
+ case 6:
1301
+ _f.trys.push([6, 20, , 21]);
1302
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
1303
+ console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
1304
+ wrappedSolAccount = new web3_js_1.Keypair();
1305
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1306
+ case 7:
1307
+ accCreationLamports = (_f.sent());
1308
+ console.log("accCreationLamports:", accCreationLamports);
1309
+ lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
1310
+ _b = anchor_1.BN.bind;
1311
+ return [4, this.provider.connection.getBalance(publicKey)];
1312
+ case 8:
1313
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
1314
+ if (unWrappedSolBalance.lt(amountIn)) {
1315
+ throw "Insufficient SOL Funds";
1316
+ }
1317
+ preInstructions = [
1318
+ web3_js_1.SystemProgram.createAccount({
1319
+ fromPubkey: publicKey,
1320
+ newAccountPubkey: wrappedSolAccount.publicKey,
1321
+ lamports: lamports.toNumber(),
1322
+ space: 165,
1323
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1324
+ }),
1325
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1326
+ ];
1327
+ postInstructions = [
1328
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1329
+ ];
1330
+ additionalSigners.push(wrappedSolAccount);
1331
+ return [3, 12];
1332
+ case 9: return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
1333
+ case 10:
1334
+ if (!(_f.sent())) {
1310
1335
  throw "Insufficient Funds , Token Account doesn't exist";
1311
1336
  }
1312
- _d = anchor_1.BN.bind;
1313
- return [4, this.provider.connection.getTokenAccountBalance(userDispensingCustodyTokenAccount)];
1314
- case 14:
1315
- tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
1337
+ if (!!skipBalanceChecks) return [3, 12];
1338
+ _c = anchor_1.BN.bind;
1339
+ return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
1340
+ case 11:
1341
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
1316
1342
  if (tokenAccountBalance.lt(amountIn)) {
1317
- throw "Insufficient Funds";
1343
+ throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
1318
1344
  }
1319
- _e.label = 15;
1345
+ _f.label = 12;
1346
+ case 12:
1347
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 14];
1348
+ wrappedSolAccount = new web3_js_1.Keypair();
1349
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1350
+ case 13:
1351
+ lamports = (_f.sent());
1352
+ preInstructions = [
1353
+ web3_js_1.SystemProgram.createAccount({
1354
+ fromPubkey: publicKey,
1355
+ newAccountPubkey: wrappedSolAccount.publicKey,
1356
+ lamports: lamports,
1357
+ space: 165,
1358
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1359
+ }),
1360
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1361
+ ];
1362
+ postInstructions = [
1363
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1364
+ ];
1365
+ additionalSigners.push(wrappedSolAccount);
1366
+ return [3, 18];
1367
+ case 14: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey)];
1320
1368
  case 15:
1369
+ userOutputTokenAccount = _f.sent();
1370
+ _d = createUserATA;
1371
+ if (!_d) return [3, 17];
1372
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
1373
+ case 16:
1374
+ _d = !(_f.sent());
1375
+ _f.label = 17;
1376
+ case 17:
1377
+ if (_d) {
1378
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey));
1379
+ }
1380
+ _f.label = 18;
1381
+ case 18:
1382
+ custodyAccountMetas = [];
1383
+ custodyOracleAccountMetas = [];
1384
+ custodyCustomOracles = [];
1385
+ for (_i = 0, _e = poolConfig.custodies; _i < _e.length; _i++) {
1386
+ custody = _e[_i];
1387
+ custodyAccountMetas.push({
1388
+ pubkey: custody.custodyAccount,
1389
+ isSigner: false,
1390
+ isWritable: false,
1391
+ });
1392
+ custodyOracleAccountMetas.push({
1393
+ pubkey: custody.oracleAddress,
1394
+ isSigner: false,
1395
+ isWritable: false,
1396
+ });
1397
+ custodyCustomOracles.push({
1398
+ pubkey: web3_js_1.SystemProgram.programId,
1399
+ isSigner: false,
1400
+ isWritable: false,
1401
+ });
1402
+ }
1321
1403
  params = {
1322
1404
  amountIn: amountIn,
1323
1405
  minAmountOut: minAmountOut,
@@ -1326,141 +1408,207 @@ var PerpetualsClient = (function () {
1326
1408
  .swap(params)
1327
1409
  .accounts({
1328
1410
  owner: publicKey,
1329
- fundingAccount: userDispensingCustodyTokenAccount,
1330
- receivingAccount: userReceivingTokenAccount,
1411
+ fundingAccount: userInputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userInputTokenAccount,
1412
+ receivingAccount: userOutputTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userOutputTokenAccount,
1331
1413
  transferAuthority: poolConfig.transferAuthority,
1332
1414
  perpetuals: poolConfig.perpetuals,
1333
1415
  pool: poolConfig.poolAddress,
1334
- receivingCustody: poolReceivingCustodyConfig.custodyAccount,
1335
- receivingCustodyOracleAccount: poolReceivingCustodyConfig.oracleAddress,
1336
- receivingCustodyTokenAccount: poolReceivingCustodyConfig.tokenAccount,
1337
- dispensingCustody: poolDispensingCustodyConfig.custodyAccount,
1338
- dispensingCustodyOracleAccount: poolDispensingCustodyConfig.oracleAddress,
1339
- dispensingCustodyTokenAccount: poolDispensingCustodyConfig.tokenAccount,
1416
+ receivingCustody: userInputCustodyConfig.custodyAccount,
1417
+ receivingCustodyOracleAccount: userInputCustodyConfig.oracleAddress,
1418
+ receivingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1419
+ receivingCustodyTokenAccount: userInputCustodyConfig.tokenAccount,
1420
+ dispensingCustody: userOutputCustodyConfig.custodyAccount,
1421
+ dispensingCustodyOracleAccount: userOutputCustodyConfig.oracleAddress,
1422
+ dispensingCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1423
+ dispensingCustodyTokenAccount: userOutputCustodyConfig.tokenAccount,
1340
1424
  eventAuthority: this.eventAuthority.publicKey,
1341
1425
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1426
+ program: this.programId,
1342
1427
  })
1428
+ .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
1343
1429
  .instruction()];
1344
- case 16:
1345
- inx = _e.sent();
1430
+ case 19:
1431
+ inx = _f.sent();
1346
1432
  instructions.push(inx);
1347
- if (userReceivingTokenSymbol == 'SOL' && unWrapSol) {
1348
- closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1433
+ if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
1434
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
1349
1435
  instructions.push(closeWsolATAIns);
1350
1436
  }
1351
- return [3, 18];
1352
- case 17:
1353
- err_2 = _e.sent();
1437
+ return [3, 21];
1438
+ case 20:
1439
+ err_2 = _f.sent();
1354
1440
  console.error("perpClient Swap error:: ", err_2);
1355
1441
  throw err_2;
1356
- case 18: return [2, instructions];
1442
+ case 21: return [2, {
1443
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1444
+ additionalSigners: additionalSigners
1445
+ }];
1357
1446
  }
1358
1447
  });
1359
1448
  });
1360
1449
  };
1361
- this.addCollateral = function (collateralWithoutFee, fee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1362
- var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userPayingTokenAccount, collateralWithfee, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, inx;
1363
- return __generator(this, function (_d) {
1364
- switch (_d.label) {
1365
- case 0:
1366
- publicKey = this.provider.wallet.publicKey;
1367
- collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1368
- marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1369
- if (!collateralCustodyConfig || !marketCustodyConfig) {
1370
- throw "payTokenCustody not found";
1371
- }
1372
- instructions = [];
1373
- userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey);
1374
- collateralWithfee = collateralWithoutFee.add(fee);
1375
- if (!(collateralSymbol == 'SOL')) return [3, 7];
1376
- console.log("collateralSymbol === sol", collateralSymbol);
1377
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1378
- case 1:
1379
- wsolAssociatedTokenAccount = _d.sent();
1380
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1381
- case 2:
1382
- wsolATAExist = _d.sent();
1383
- if (!wsolATAExist) {
1384
- console.log("wsol ata does not exist");
1385
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1386
- }
1387
- _a = anchor_1.BN.bind;
1388
- if (!wsolATAExist) return [3, 4];
1389
- return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1390
- case 3:
1391
- _b = (_d.sent()).value.amount;
1392
- return [3, 5];
1393
- case 4:
1394
- _b = 0;
1395
- _d.label = 5;
1396
- case 5:
1397
- wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1398
- if (!wsolBalance.lt(collateralWithfee)) return [3, 7];
1399
- _c = anchor_1.BN.bind;
1400
- return [4, this.provider.connection.getBalance(publicKey)];
1401
- case 6:
1402
- unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _d.sent()]))();
1403
- totalSolBal = unWrappedSolBalance.add(wsolBalance);
1404
- if (totalSolBal.lt(collateralWithfee)) {
1405
- throw "Insufficient SOL Funds";
1406
- }
1407
- conversionAmt = collateralWithfee.sub(wsolBalance);
1408
- instructions.push(web3_js_1.SystemProgram.transfer({
1409
- fromPubkey: publicKey,
1410
- toPubkey: wsolAssociatedTokenAccount,
1411
- lamports: conversionAmt.toNumber(),
1412
- }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1413
- _d.label = 7;
1414
- case 7: return [4, this.program.methods.addCollateral({
1415
- collateralDelta: collateralWithoutFee
1416
- }).accounts({
1417
- owner: publicKey,
1418
- position: positionPubKey,
1419
- fundingAccount: userPayingTokenAccount,
1420
- transferAuthority: poolConfig.transferAuthority,
1421
- perpetuals: poolConfig.perpetuals,
1422
- pool: poolConfig.poolAddress,
1423
- custody: marketCustodyConfig.custodyAccount,
1424
- custodyOracleAccount: marketCustodyConfig.oracleAddress,
1425
- collateralCustody: collateralCustodyConfig.custodyAccount,
1426
- collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1427
- collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1428
- eventAuthority: this.eventAuthority.publicKey,
1429
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1430
- })
1431
- .instruction()];
1432
- case 8:
1433
- inx = _d.sent();
1434
- instructions.push(inx);
1435
- return [2, instructions];
1436
- }
1450
+ this.addCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig, skipBalanceChecks) {
1451
+ if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
1452
+ return __awaiter(_this, void 0, void 0, function () {
1453
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, solBal, tokenAccountBalance, _b, instruction;
1454
+ return __generator(this, function (_c) {
1455
+ switch (_c.label) {
1456
+ case 0:
1457
+ publicKey = this.provider.wallet.publicKey;
1458
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
1459
+ marketCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
1460
+ if (!collateralCustodyConfig || !marketCustodyConfig) {
1461
+ throw "payTokenCustody not found";
1462
+ }
1463
+ userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey);
1464
+ preInstructions = [];
1465
+ instructions = [];
1466
+ postInstructions = [];
1467
+ additionalSigners = [];
1468
+ if (!(collateralSymbol == 'SOL')) return [3, 4];
1469
+ console.log("collateralSymbol === SOL", collateralSymbol);
1470
+ wrappedSolAccount = new web3_js_1.Keypair();
1471
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1472
+ case 1:
1473
+ accCreationLamports = (_c.sent());
1474
+ console.log("accCreationLamports:", accCreationLamports);
1475
+ lamports = collateralWithFee.add(new anchor_1.BN(accCreationLamports));
1476
+ console.log("lamports:", lamports.toNumber());
1477
+ _a = anchor_1.BN.bind;
1478
+ return [4, this.provider.connection.getBalance(publicKey)];
1479
+ case 2:
1480
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _c.sent()]))();
1481
+ if (unWrappedSolBalance.lt(lamports)) {
1482
+ throw "Insufficient SOL Funds";
1483
+ }
1484
+ return [4, this.provider.connection.getBalance(publicKey)];
1485
+ case 3:
1486
+ solBal = _c.sent();
1487
+ console.log("solBal>>:", solBal);
1488
+ preInstructions = [
1489
+ web3_js_1.SystemProgram.createAccount({
1490
+ fromPubkey: publicKey,
1491
+ newAccountPubkey: wrappedSolAccount.publicKey,
1492
+ lamports: lamports.toNumber(),
1493
+ space: 165,
1494
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1495
+ }),
1496
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1497
+ ];
1498
+ postInstructions = [
1499
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1500
+ ];
1501
+ additionalSigners.push(wrappedSolAccount);
1502
+ return [3, 7];
1503
+ case 4: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
1504
+ case 5:
1505
+ if (!(_c.sent())) {
1506
+ throw "Insufficient Funds , token Account doesn't exist";
1507
+ }
1508
+ if (!!skipBalanceChecks) return [3, 7];
1509
+ _b = anchor_1.BN.bind;
1510
+ return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
1511
+ case 6:
1512
+ tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
1513
+ if (tokenAccountBalance.lt(collateralWithFee)) {
1514
+ throw "Insufficient Funds need more ".concat(collateralWithFee.sub(tokenAccountBalance), " tokens");
1515
+ }
1516
+ _c.label = 7;
1517
+ case 7: return [4, this.program.methods.addCollateral({
1518
+ collateralDelta: collateralWithFee
1519
+ }).accounts({
1520
+ owner: publicKey,
1521
+ position: positionPubKey,
1522
+ fundingAccount: collateralSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
1523
+ transferAuthority: poolConfig.transferAuthority,
1524
+ perpetuals: poolConfig.perpetuals,
1525
+ pool: poolConfig.poolAddress,
1526
+ custody: marketCustodyConfig.custodyAccount,
1527
+ custodyOracleAccount: marketCustodyConfig.oracleAddress,
1528
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1529
+ collateralCustody: collateralCustodyConfig.custodyAccount,
1530
+ collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1531
+ collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1532
+ collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1533
+ eventAuthority: this.eventAuthority.publicKey,
1534
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1535
+ program: this.programId,
1536
+ })
1537
+ .instruction()];
1538
+ case 8:
1539
+ instruction = _c.sent();
1540
+ instructions.push(instruction);
1541
+ return [2, {
1542
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1543
+ additionalSigners: additionalSigners
1544
+ }];
1545
+ }
1546
+ });
1437
1547
  });
1438
- }); };
1439
- this.removeCollateral = function (collateralWithoutFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1440
- var publicKey, collateralCustodyConfig, marketCustodyConfig, instructions, userReceivingTokenAccount, ix, closeWsolATAIns;
1441
- return __generator(this, function (_a) {
1442
- switch (_a.label) {
1443
- case 0:
1444
- publicKey = this.provider.wallet.publicKey;
1445
- collateralCustodyConfig = poolConfig.custodies.find(function (i) {
1446
- return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1447
- });
1448
- marketCustodyConfig = poolConfig.custodies.find(function (i) {
1449
- return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1450
- });
1451
- if (!collateralCustodyConfig) {
1452
- throw "payTokenCustody not found";
1453
- }
1454
- instructions = [];
1455
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey);
1456
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1457
- case 1:
1458
- if (!(_a.sent())) {
1459
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
1460
- }
1461
- return [4, this.program.methods
1548
+ };
1549
+ this.removeCollateral = function (collateralWithFee, marketSymbol, collateralSymbol, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA) {
1550
+ if (createUserATA === void 0) { createUserATA = true; }
1551
+ if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1552
+ return __awaiter(_this, void 0, void 0, function () {
1553
+ var publicKey, collateralCustodyConfig, marketCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, instruction, closeWsolATAIns, error_3;
1554
+ return __generator(this, function (_b) {
1555
+ switch (_b.label) {
1556
+ case 0:
1557
+ publicKey = this.provider.wallet.publicKey;
1558
+ collateralCustodyConfig = poolConfig.custodies.find(function (i) {
1559
+ return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
1560
+ });
1561
+ marketCustodyConfig = poolConfig.custodies.find(function (i) {
1562
+ return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey);
1563
+ });
1564
+ if (!collateralCustodyConfig || !marketCustodyConfig) {
1565
+ throw "collateralCustodyConfig/marketCustodyConfig not found";
1566
+ }
1567
+ preInstructions = [];
1568
+ instructions = [];
1569
+ postInstructions = [];
1570
+ additionalSigners = [];
1571
+ _b.label = 1;
1572
+ case 1:
1573
+ _b.trys.push([1, 9, , 10]);
1574
+ if (!(collateralSymbol == 'SOL')) return [3, 3];
1575
+ wrappedSolAccount = new web3_js_1.Keypair();
1576
+ userReceivingTokenAccount = wrappedSolAccount.publicKey;
1577
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1578
+ case 2:
1579
+ lamports = (_b.sent());
1580
+ preInstructions = [
1581
+ web3_js_1.SystemProgram.createAccount({
1582
+ fromPubkey: publicKey,
1583
+ newAccountPubkey: wrappedSolAccount.publicKey,
1584
+ lamports: lamports,
1585
+ space: 165,
1586
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1587
+ }),
1588
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1589
+ ];
1590
+ postInstructions = [
1591
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1592
+ ];
1593
+ additionalSigners.push(wrappedSolAccount);
1594
+ return [3, 7];
1595
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey)];
1596
+ case 4:
1597
+ userReceivingTokenAccount = _b.sent();
1598
+ _a = createUserATA;
1599
+ if (!_a) return [3, 6];
1600
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1601
+ case 5:
1602
+ _a = !(_b.sent());
1603
+ _b.label = 6;
1604
+ case 6:
1605
+ if (_a) {
1606
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
1607
+ }
1608
+ _b.label = 7;
1609
+ case 7: return [4, this.program.methods
1462
1610
  .removeCollateral({
1463
- collateralDelta: collateralWithoutFee,
1611
+ collateralDelta: collateralWithFee,
1464
1612
  })
1465
1613
  .accounts({
1466
1614
  owner: publicKey,
@@ -1471,185 +1619,194 @@ var PerpetualsClient = (function () {
1471
1619
  position: positionPubKey,
1472
1620
  custody: marketCustodyConfig.custodyAccount,
1473
1621
  custodyOracleAccount: marketCustodyConfig.oracleAddress,
1622
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1474
1623
  collateralCustody: collateralCustodyConfig.custodyAccount,
1475
1624
  collateralCustodyOracleAccount: collateralCustodyConfig.oracleAddress,
1625
+ collateralCustodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1476
1626
  collateralCustodyTokenAccount: collateralCustodyConfig.tokenAccount,
1477
1627
  eventAuthority: this.eventAuthority.publicKey,
1478
1628
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1629
+ program: this.programId,
1479
1630
  })
1480
1631
  .instruction()];
1481
- case 2:
1482
- ix = _a.sent();
1483
- instructions.push(ix);
1484
- if (collateralSymbol === 'SOL') {
1485
- closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1486
- instructions.push(closeWsolATAIns);
1487
- }
1488
- return [2, instructions];
1489
- }
1632
+ case 8:
1633
+ instruction = _b.sent();
1634
+ instructions.push(instruction);
1635
+ if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
1636
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1637
+ postInstructions.push(closeWsolATAIns);
1638
+ }
1639
+ return [3, 10];
1640
+ case 9:
1641
+ error_3 = _b.sent();
1642
+ console.error("perpclient removeCollateral error:", error_3);
1643
+ return [3, 10];
1644
+ case 10: return [2, {
1645
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1646
+ additionalSigners: additionalSigners
1647
+ }];
1648
+ }
1649
+ });
1490
1650
  });
1491
- }); };
1492
- this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1493
- var payTokenCustody, publicKey, instructions, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _b, _c, unWrappedSolBalance, _d, totalSolBal, conversionAmt, tokenAccountBalance, _e, inx, err_3;
1494
- return __generator(this, function (_f) {
1495
- switch (_f.label) {
1496
- case 0:
1497
- payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1498
- if (!payTokenCustody) {
1499
- throw "payTokenCustody not found";
1500
- }
1501
- publicKey = this.provider.wallet.publicKey;
1502
- instructions = [];
1503
- _f.label = 1;
1504
- case 1:
1505
- _f.trys.push([1, 17, , 18]);
1506
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustody.mintKey, publicKey)];
1507
- case 2:
1508
- userPayingTokenAccount = _f.sent();
1509
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1510
- case 3:
1511
- lpTokenAccount = _f.sent();
1512
- custodyAccountMetas = [];
1513
- custodyOracleAccountMetas = [];
1514
- custodyCustomOracles = [];
1515
- for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
1516
- custody = _a[_i];
1517
- custodyAccountMetas.push({
1518
- pubkey: custody.custodyAccount,
1519
- isSigner: false,
1520
- isWritable: false,
1521
- });
1522
- custodyOracleAccountMetas.push({
1523
- pubkey: custody.oracleAddress,
1524
- isSigner: false,
1525
- isWritable: false,
1526
- });
1527
- custodyCustomOracles.push({
1528
- pubkey: web3_js_1.SystemProgram.programId,
1529
- isSigner: false,
1530
- isWritable: false,
1531
- });
1532
- }
1533
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
1534
- case 4:
1535
- if (!(_f.sent())) {
1536
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.lpTokenMint));
1537
- }
1538
- if (!(payTokenSymbol == 'SOL')) return [3, 12];
1539
- console.log("payTokenSymbol === sol", payTokenSymbol);
1540
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1541
- case 5:
1542
- wsolAssociatedTokenAccount = _f.sent();
1543
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1544
- case 6:
1545
- wsolATAExist = _f.sent();
1546
- if (!wsolATAExist) {
1547
- console.log("wsol ata does not exist");
1548
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1549
- }
1550
- _b = anchor_1.BN.bind;
1551
- if (!wsolATAExist) return [3, 8];
1552
- return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1553
- case 7:
1554
- _c = (_f.sent()).value.amount;
1555
- return [3, 9];
1556
- case 8:
1557
- _c = 0;
1558
- _f.label = 9;
1559
- case 9:
1560
- wsolBalance = new (_b.apply(anchor_1.BN, [void 0, _c]))();
1561
- if (!wsolBalance.lt(tokenAmountIn)) return [3, 11];
1562
- _d = anchor_1.BN.bind;
1563
- return [4, this.provider.connection.getBalance(publicKey)];
1564
- case 10:
1565
- unWrappedSolBalance = new (_d.apply(anchor_1.BN, [void 0, _f.sent()]))();
1566
- totalSolBal = unWrappedSolBalance.add(wsolBalance);
1567
- if (totalSolBal.lt(tokenAmountIn)) {
1568
- throw "Insufficient SOL Funds";
1569
- }
1570
- conversionAmt = tokenAmountIn.sub(wsolBalance);
1571
- instructions.push(web3_js_1.SystemProgram.transfer({
1572
- fromPubkey: publicKey,
1573
- toPubkey: wsolAssociatedTokenAccount,
1574
- lamports: conversionAmt.toNumber(),
1575
- }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1576
- _f.label = 11;
1577
- case 11: return [3, 15];
1578
- case 12: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
1579
- case 13:
1580
- if (!(_f.sent())) {
1581
- throw "Insufficient Funds , token Account doesn't exist";
1582
- }
1583
- _e = anchor_1.BN.bind;
1584
- return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
1585
- case 14:
1586
- tokenAccountBalance = new (_e.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
1587
- if (tokenAccountBalance.lt(tokenAmountIn)) {
1588
- throw "Insufficient Funds";
1589
- }
1590
- _f.label = 15;
1591
- case 15: return [4, this.program.methods
1592
- .addLiquidity({
1593
- amountIn: tokenAmountIn,
1594
- minLpAmountOut: minLpAmountOut
1595
- })
1596
- .accounts({
1597
- owner: publicKey,
1598
- fundingAccount: userPayingTokenAccount,
1599
- lpTokenAccount: lpTokenAccount,
1600
- transferAuthority: poolConfig.transferAuthority,
1601
- perpetuals: poolConfig.perpetuals,
1602
- pool: poolConfig.poolAddress,
1603
- custody: payTokenCustody.custodyAccount,
1604
- custodyOracleAccount: payTokenCustody.oracleAddress,
1605
- custodyTokenAccount: payTokenCustody.tokenAccount,
1606
- lpTokenMint: poolConfig.lpTokenMint,
1607
- eventAuthority: this.eventAuthority.publicKey,
1608
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1609
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1610
- program: this.programId,
1611
- })
1612
- .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
1613
- .instruction()];
1614
- case 16:
1615
- inx = _f.sent();
1616
- instructions.push(inx);
1617
- return [3, 18];
1618
- case 17:
1619
- err_3 = _f.sent();
1620
- console.error("perpClient addLiquidity error:: ", err_3);
1621
- throw err_3;
1622
- case 18: return [2, instructions];
1623
- }
1651
+ };
1652
+ this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks) {
1653
+ if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
1654
+ return __awaiter(_this, void 0, void 0, function () {
1655
+ var publicKey, payTokenCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, instruction, err_3;
1656
+ return __generator(this, function (_d) {
1657
+ switch (_d.label) {
1658
+ case 0:
1659
+ publicKey = this.provider.wallet.publicKey;
1660
+ payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1661
+ if (!payTokenCustodyConfig) {
1662
+ throw "payTokenCustodyConfig not found";
1663
+ }
1664
+ preInstructions = [];
1665
+ instructions = [];
1666
+ postInstructions = [];
1667
+ additionalSigners = [];
1668
+ _d.label = 1;
1669
+ case 1:
1670
+ _d.trys.push([1, 12, , 13]);
1671
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustodyConfig.mintKey, publicKey)];
1672
+ case 2:
1673
+ userPayingTokenAccount = _d.sent();
1674
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1675
+ case 3:
1676
+ lpTokenAccount = _d.sent();
1677
+ custodyAccountMetas = [];
1678
+ custodyOracleAccountMetas = [];
1679
+ custodyCustomOracles = [];
1680
+ for (_i = 0, _a = poolConfig.custodies; _i < _a.length; _i++) {
1681
+ custody = _a[_i];
1682
+ custodyAccountMetas.push({
1683
+ pubkey: custody.custodyAccount,
1684
+ isSigner: false,
1685
+ isWritable: false,
1686
+ });
1687
+ custodyOracleAccountMetas.push({
1688
+ pubkey: custody.oracleAddress,
1689
+ isSigner: false,
1690
+ isWritable: false,
1691
+ });
1692
+ custodyCustomOracles.push({
1693
+ pubkey: web3_js_1.SystemProgram.programId,
1694
+ isSigner: false,
1695
+ isWritable: false,
1696
+ });
1697
+ }
1698
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
1699
+ case 4:
1700
+ if (!(_d.sent())) {
1701
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.lpTokenMint));
1702
+ }
1703
+ if (!(payTokenSymbol == 'SOL')) return [3, 7];
1704
+ console.log("payTokenSymbol === SOL", payTokenSymbol);
1705
+ wrappedSolAccount = new web3_js_1.Keypair();
1706
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1707
+ case 5:
1708
+ accCreationLamports = (_d.sent());
1709
+ console.log("accCreationLamports:", accCreationLamports);
1710
+ lamports = tokenAmountIn.add(new anchor_1.BN(accCreationLamports));
1711
+ _b = anchor_1.BN.bind;
1712
+ return [4, this.provider.connection.getBalance(publicKey)];
1713
+ case 6:
1714
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _d.sent()]))();
1715
+ if (unWrappedSolBalance.lt(lamports)) {
1716
+ throw "Insufficient SOL Funds";
1717
+ }
1718
+ preInstructions = [
1719
+ web3_js_1.SystemProgram.createAccount({
1720
+ fromPubkey: publicKey,
1721
+ newAccountPubkey: wrappedSolAccount.publicKey,
1722
+ lamports: lamports.toNumber(),
1723
+ space: 165,
1724
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1725
+ }),
1726
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1727
+ ];
1728
+ postInstructions = [
1729
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1730
+ ];
1731
+ additionalSigners.push(wrappedSolAccount);
1732
+ return [3, 10];
1733
+ case 7: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
1734
+ case 8:
1735
+ if (!(_d.sent())) {
1736
+ throw "Insufficient Funds , token Account doesn't exist";
1737
+ }
1738
+ if (!!skipBalanceChecks) return [3, 10];
1739
+ _c = anchor_1.BN.bind;
1740
+ return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
1741
+ case 9:
1742
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_d.sent()).value.amount]))();
1743
+ if (tokenAccountBalance.lt(tokenAmountIn)) {
1744
+ throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
1745
+ }
1746
+ _d.label = 10;
1747
+ case 10: return [4, this.program.methods
1748
+ .addLiquidity({
1749
+ amountIn: tokenAmountIn,
1750
+ minLpAmountOut: minLpAmountOut
1751
+ })
1752
+ .accounts({
1753
+ owner: publicKey,
1754
+ fundingAccount: payTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userPayingTokenAccount,
1755
+ lpTokenAccount: lpTokenAccount,
1756
+ transferAuthority: poolConfig.transferAuthority,
1757
+ perpetuals: poolConfig.perpetuals,
1758
+ pool: poolConfig.poolAddress,
1759
+ custody: payTokenCustodyConfig.custodyAccount,
1760
+ custodyOracleAccount: payTokenCustodyConfig.oracleAddress,
1761
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1762
+ custodyTokenAccount: payTokenCustodyConfig.tokenAccount,
1763
+ lpTokenMint: poolConfig.lpTokenMint,
1764
+ eventAuthority: this.eventAuthority.publicKey,
1765
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1766
+ program: this.programId,
1767
+ })
1768
+ .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
1769
+ .instruction()];
1770
+ case 11:
1771
+ instruction = _d.sent();
1772
+ instructions.push(instruction);
1773
+ return [3, 13];
1774
+ case 12:
1775
+ err_3 = _d.sent();
1776
+ console.error("perpClient addLiquidity error:: ", err_3);
1777
+ throw err_3;
1778
+ case 13: return [2, {
1779
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1780
+ additionalSigners: additionalSigners
1781
+ }];
1782
+ }
1783
+ });
1624
1784
  });
1625
- }); };
1626
- this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA) {
1785
+ };
1786
+ this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA, createUserATA, closeUsersWSOLATA) {
1627
1787
  if (closeLpATA === void 0) { closeLpATA = false; }
1788
+ if (createUserATA === void 0) { createUserATA = true; }
1789
+ if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1628
1790
  return __awaiter(_this, void 0, void 0, function () {
1629
- var recieveTokenCustody, publicKey, instructions, userRecievingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, removeLiquidityTx, closeInx, closeWsolATAIns, err_4;
1630
- return __generator(this, function (_b) {
1631
- switch (_b.label) {
1791
+ var recieveTokenCustodyConfig, publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, custodyCustomOracles, _i, _a, custody, lamports, _b, removeLiquidityTx, closeInx, closeWsolATAIns, err_4;
1792
+ return __generator(this, function (_c) {
1793
+ switch (_c.label) {
1632
1794
  case 0:
1633
- recieveTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey); });
1634
- if (!recieveTokenCustody) {
1795
+ recieveTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey); });
1796
+ if (!recieveTokenCustodyConfig) {
1635
1797
  throw "recieveTokenCustody not found";
1636
1798
  }
1637
1799
  publicKey = this.provider.wallet.publicKey;
1800
+ preInstructions = [];
1638
1801
  instructions = [];
1639
- _b.label = 1;
1802
+ postInstructions = [];
1803
+ additionalSigners = [];
1804
+ _c.label = 1;
1640
1805
  case 1:
1641
- _b.trys.push([1, 6, , 7]);
1642
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(recieveTokenCustody.mintKey, publicKey)];
1643
- case 2:
1644
- userRecievingTokenAccount = _b.sent();
1645
- return [4, (0, utils_1.checkIfAccountExists)(userRecievingTokenAccount, this.provider.connection)];
1646
- case 3:
1647
- if (!(_b.sent())) {
1648
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userRecievingTokenAccount, publicKey, recieveTokenCustody.mintKey));
1649
- }
1806
+ _c.trys.push([1, 10, , 11]);
1650
1807
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1651
- case 4:
1652
- lpTokenAccount = _b.sent();
1808
+ case 2:
1809
+ lpTokenAccount = _c.sent();
1653
1810
  custodyAccountMetas = [];
1654
1811
  custodyOracleAccountMetas = [];
1655
1812
  custodyCustomOracles = [];
@@ -1671,6 +1828,42 @@ var PerpetualsClient = (function () {
1671
1828
  isWritable: false,
1672
1829
  });
1673
1830
  }
1831
+ if (!(recieveTokenSymbol == 'SOL')) return [3, 4];
1832
+ wrappedSolAccount = new web3_js_1.Keypair();
1833
+ userReceivingTokenAccount = wrappedSolAccount.publicKey;
1834
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1835
+ case 3:
1836
+ lamports = (_c.sent());
1837
+ preInstructions = [
1838
+ web3_js_1.SystemProgram.createAccount({
1839
+ fromPubkey: publicKey,
1840
+ newAccountPubkey: wrappedSolAccount.publicKey,
1841
+ lamports: lamports,
1842
+ space: 165,
1843
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
1844
+ }),
1845
+ (0, spl_token_1.createInitializeAccount3Instruction)(wrappedSolAccount.publicKey, spl_token_1.NATIVE_MINT, publicKey),
1846
+ ];
1847
+ postInstructions = [
1848
+ (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1849
+ ];
1850
+ additionalSigners.push(wrappedSolAccount);
1851
+ return [3, 8];
1852
+ case 4: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey, publicKey)];
1853
+ case 5:
1854
+ userReceivingTokenAccount = _c.sent();
1855
+ _b = createUserATA;
1856
+ if (!_b) return [3, 7];
1857
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1858
+ case 6:
1859
+ _b = !(_c.sent());
1860
+ _c.label = 7;
1861
+ case 7:
1862
+ if (_b) {
1863
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey));
1864
+ }
1865
+ _c.label = 8;
1866
+ case 8:
1674
1867
  console.log("liquidityAmountIn", liquidityAmountIn.toString());
1675
1868
  return [4, this.program.methods
1676
1869
  .removeLiquidity({
@@ -1679,39 +1872,42 @@ var PerpetualsClient = (function () {
1679
1872
  })
1680
1873
  .accounts({
1681
1874
  owner: publicKey,
1682
- receivingAccount: userRecievingTokenAccount,
1875
+ receivingAccount: recieveTokenSymbol == 'SOL' ? wrappedSolAccount.publicKey : userReceivingTokenAccount,
1683
1876
  lpTokenAccount: lpTokenAccount,
1684
1877
  transferAuthority: poolConfig.transferAuthority,
1685
1878
  perpetuals: poolConfig.perpetuals,
1686
1879
  pool: poolConfig.poolAddress,
1687
- custody: recieveTokenCustody.custodyAccount,
1688
- custodyOracleAccount: recieveTokenCustody.oracleAddress,
1689
- custodyTokenAccount: recieveTokenCustody.tokenAccount,
1880
+ custody: recieveTokenCustodyConfig.custodyAccount,
1881
+ custodyOracleAccount: recieveTokenCustodyConfig.oracleAddress,
1882
+ custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1883
+ custodyTokenAccount: recieveTokenCustodyConfig.tokenAccount,
1690
1884
  lpTokenMint: poolConfig.lpTokenMint,
1691
1885
  eventAuthority: this.eventAuthority.publicKey,
1692
1886
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1693
- custodyCustomOracleAccount: web3_js_1.SystemProgram.programId,
1694
1887
  program: this.programId,
1695
1888
  })
1696
1889
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), custodyCustomOracles, true))
1697
1890
  .instruction()];
1698
- case 5:
1699
- removeLiquidityTx = _b.sent();
1891
+ case 9:
1892
+ removeLiquidityTx = _c.sent();
1700
1893
  instructions.push(removeLiquidityTx);
1701
1894
  if (closeLpATA) {
1702
1895
  closeInx = (0, spl_token_1.createCloseAccountInstruction)(lpTokenAccount, publicKey, publicKey);
1703
1896
  instructions.push(closeInx);
1704
1897
  }
1705
- if (recieveTokenSymbol == 'SOL') {
1706
- closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userRecievingTokenAccount, publicKey, publicKey);
1707
- instructions.push(closeWsolATAIns);
1898
+ if (recieveTokenSymbol == 'WSOL' && closeUsersWSOLATA) {
1899
+ closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1900
+ postInstructions.push(closeWsolATAIns);
1708
1901
  }
1709
- return [3, 7];
1710
- case 6:
1711
- err_4 = _b.sent();
1902
+ return [3, 11];
1903
+ case 10:
1904
+ err_4 = _c.sent();
1712
1905
  console.log("perpClient removeLiquidity error:: ", err_4);
1713
1906
  throw err_4;
1714
- case 7: return [2, instructions];
1907
+ case 11: return [2, {
1908
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1909
+ additionalSigners: additionalSigners
1910
+ }];
1715
1911
  }
1716
1912
  });
1717
1913
  });