flash-sdk 1.0.25 → 1.0.27

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.
@@ -68,7 +68,7 @@ var OraclePrice_1 = require("./OraclePrice");
68
68
  var perpetuals_1 = require("./idl/perpetuals");
69
69
  var rpc_1 = require("./utils/rpc");
70
70
  var utils_1 = require("./utils");
71
- var PerpetualsClient = /** @class */ (function () {
71
+ var PerpetualsClient = (function () {
72
72
  function PerpetualsClient(provider, programId, opts) {
73
73
  var _this = this;
74
74
  var _a;
@@ -108,7 +108,7 @@ var PerpetualsClient = /** @class */ (function () {
108
108
  };
109
109
  this.getPerpetuals = function () { return __awaiter(_this, void 0, void 0, function () {
110
110
  return __generator(this, function (_a) {
111
- return [2 /*return*/, this.program.account.perpetuals.fetch(this.perpetuals.publicKey)];
111
+ return [2, this.program.account.perpetuals.fetch(this.perpetuals.publicKey)];
112
112
  });
113
113
  }); };
114
114
  this.getPoolKey = function (name) {
@@ -116,18 +116,17 @@ 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())
120
- return [2 /*return*/, this.program.account.pool.fetch(this.getPoolKey(name))];
119
+ return [2, this.program.account.pool.fetch(this.getPoolKey(name))];
121
120
  });
122
121
  }); };
123
122
  this.getPools = function () { return __awaiter(_this, void 0, void 0, function () {
124
123
  var perpetuals;
125
124
  return __generator(this, function (_a) {
126
125
  switch (_a.label) {
127
- case 0: return [4 /*yield*/, this.getPerpetuals()];
126
+ case 0: return [4, this.getPerpetuals()];
128
127
  case 1:
129
128
  perpetuals = _a.sent();
130
- return [2 /*return*/, this.program.account.pool.fetchMultiple(perpetuals.pools)];
129
+ return [2, this.program.account.pool.fetchMultiple(perpetuals.pools)];
131
130
  }
132
131
  });
133
132
  }); };
@@ -150,8 +149,8 @@ var PerpetualsClient = /** @class */ (function () {
150
149
  this.getCustodyOracleAccountKey = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
151
150
  return __generator(this, function (_a) {
152
151
  switch (_a.label) {
153
- case 0: return [4 /*yield*/, this.getCustody(poolName, tokenMint)];
154
- case 1: return [2 /*return*/, (_a.sent()).oracle.oracleAccount];
152
+ case 0: return [4, this.getCustody(poolName, tokenMint)];
153
+ case 1: return [2, (_a.sent()).oracle.oracleAccount];
155
154
  }
156
155
  });
157
156
  }); };
@@ -163,18 +162,17 @@ var PerpetualsClient = /** @class */ (function () {
163
162
  };
164
163
  this.getCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
165
164
  return __generator(this, function (_a) {
166
- // console.log("custody key :", this.getCustodyKey(poolName, tokenMint).toBase58());
167
- return [2 /*return*/, this.program.account.custody.fetch(this.getCustodyKey(poolName, tokenMint))];
165
+ return [2, this.program.account.custody.fetch(this.getCustodyKey(poolName, tokenMint))];
168
166
  });
169
167
  }); };
170
168
  this.getCustodies = function (poolName) { return __awaiter(_this, void 0, void 0, function () {
171
169
  var pool;
172
170
  return __generator(this, function (_a) {
173
171
  switch (_a.label) {
174
- case 0: return [4 /*yield*/, this.getPool(poolName)];
172
+ case 0: return [4, this.getPool(poolName)];
175
173
  case 1:
176
174
  pool = _a.sent();
177
- return [2 /*return*/, this.program.account.custody.fetchMultiple(pool.custodies)];
175
+ return [2, this.program.account.custody.fetchMultiple(pool.custodies)];
178
176
  }
179
177
  });
180
178
  }); };
@@ -182,10 +180,10 @@ var PerpetualsClient = /** @class */ (function () {
182
180
  var pool, custodies, custodyMetas, _i, _a, custody, _b, custodies_1, custody;
183
181
  return __generator(this, function (_c) {
184
182
  switch (_c.label) {
185
- case 0: return [4 /*yield*/, this.getPool(poolName)];
183
+ case 0: return [4, this.getPool(poolName)];
186
184
  case 1:
187
185
  pool = _c.sent();
188
- return [4 /*yield*/, this.program.account.custody.fetchMultiple(pool.custodies)];
186
+ return [4, this.program.account.custody.fetchMultiple(pool.custodies)];
189
187
  case 2:
190
188
  custodies = _c.sent();
191
189
  custodyMetas = [];
@@ -205,13 +203,13 @@ var PerpetualsClient = /** @class */ (function () {
205
203
  pubkey: custody === null || custody === void 0 ? void 0 : custody.oracle.oracleAccount,
206
204
  });
207
205
  }
208
- return [2 /*return*/, custodyMetas];
206
+ return [2, custodyMetas];
209
207
  }
210
208
  });
211
209
  }); };
212
210
  this.getMultisig = function () { return __awaiter(_this, void 0, void 0, function () {
213
211
  return __generator(this, function (_a) {
214
- return [2 /*return*/, this.program.account.multisig.fetch(this.multisig.publicKey)];
212
+ return [2, this.program.account.multisig.fetch(this.multisig.publicKey)];
215
213
  });
216
214
  }); };
217
215
  this.getPositionKey = function (wallet, poolName, tokenMint, side) {
@@ -226,12 +224,12 @@ var PerpetualsClient = /** @class */ (function () {
226
224
  };
227
225
  this.getPosition = function (postionKey) { return __awaiter(_this, void 0, void 0, function () {
228
226
  return __generator(this, function (_a) {
229
- return [2 /*return*/, this.program.account.position.fetch(postionKey)];
227
+ return [2, this.program.account.position.fetch(postionKey)];
230
228
  });
231
229
  }); };
232
230
  this.getUserPosition = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
233
231
  return __generator(this, function (_a) {
234
- return [2 /*return*/, this.program.account.position.fetch(this.getPositionKey(wallet, poolName, tokenMint, side))];
232
+ return [2, this.program.account.position.fetch(this.getPositionKey(wallet, poolName, tokenMint, side))];
235
233
  });
236
234
  }); };
237
235
  this.getUserPositions = function (wallet, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
@@ -242,10 +240,10 @@ var PerpetualsClient = /** @class */ (function () {
242
240
  switch (_b.label) {
243
241
  case 0:
244
242
  positionKeys = __spreadArray(__spreadArray([], poolConfig.getNonStableTokens().map(function (mint) { return _this.getPositionKey(wallet, poolConfig.poolName, mint, 'long'); }), true), poolConfig.getNonStableTokens().map(function (mint) { return _this.getPositionKey(wallet, poolConfig.poolName, mint, 'short'); }), true);
245
- return [4 /*yield*/, this.provider.connection.getMultipleAccountsInfo(positionKeys)];
243
+ return [4, this.provider.connection.getMultipleAccountsInfo(positionKeys)];
246
244
  case 1:
247
245
  positionsDatas = (_a = (_b.sent())) !== null && _a !== void 0 ? _a : [];
248
- return [2 /*return*/, positionsDatas
246
+ return [2, positionsDatas
249
247
  .map(function (p, i) { return ({ pubkey: positionKeys[i], data: p }); })
250
248
  .filter(function (f) { return f.data !== null; })
251
249
  .map(function (k) { return (__assign({ pubkey: k.pubkey }, _this.program.account.position.coder.accounts.decode('position', k.data.data))); })];
@@ -261,12 +259,12 @@ var PerpetualsClient = /** @class */ (function () {
261
259
  poolKey = this.getPoolKey(poolName);
262
260
  custodyKey = this.getCustodyKey(poolName, tokenMint);
263
261
  data = (0, bs58_1.encode)(Buffer.concat([poolKey.toBuffer(), custodyKey.toBuffer()]));
264
- return [4 /*yield*/, this.provider.connection.getProgramAccounts(this.program.programId, {
262
+ return [4, this.provider.connection.getProgramAccounts(this.program.programId, {
265
263
  filters: [{ dataSize: 200 }, { memcmp: { bytes: data, offset: 40 } }],
266
264
  })];
267
265
  case 1:
268
266
  positions = _a.sent();
269
- return [2 /*return*/, Promise.all(positions.map(function (position) {
267
+ return [2, Promise.all(positions.map(function (position) {
270
268
  return _this.program.account.position.fetch(position.pubkey);
271
269
  }))];
272
270
  }
@@ -274,7 +272,7 @@ var PerpetualsClient = /** @class */ (function () {
274
272
  }); };
275
273
  this.getAllPositions = function () { return __awaiter(_this, void 0, void 0, function () {
276
274
  return __generator(this, function (_a) {
277
- return [2 /*return*/, this.program.account.position.all()];
275
+ return [2, this.program.account.position.all()];
278
276
  });
279
277
  }); };
280
278
  this.getAccountDiscriminator = function (name) {
@@ -293,8 +291,6 @@ var PerpetualsClient = /** @class */ (function () {
293
291
  this.prettyPrint = function (object) {
294
292
  console.log(JSON.stringify(object, null, 2));
295
293
  };
296
- ///////
297
- // instructions
298
294
  this.init = function (admins, config) { return __awaiter(_this, void 0, void 0, function () {
299
295
  var perpetualsProgramData, adminMetas, _i, admins_1, admin;
300
296
  return __generator(this, function (_a) {
@@ -310,7 +306,7 @@ var PerpetualsClient = /** @class */ (function () {
310
306
  pubkey: admin,
311
307
  });
312
308
  }
313
- return [4 /*yield*/, this.program.methods
309
+ return [4, this.program.methods
314
310
  .init(config)
315
311
  .accounts({
316
312
  upgradeAuthority: this.provider.wallet.publicKey,
@@ -330,7 +326,7 @@ var PerpetualsClient = /** @class */ (function () {
330
326
  })];
331
327
  case 1:
332
328
  _a.sent();
333
- return [2 /*return*/];
329
+ return [2];
334
330
  }
335
331
  });
336
332
  }); };
@@ -351,7 +347,7 @@ var PerpetualsClient = /** @class */ (function () {
351
347
  _a.label = 1;
352
348
  case 1:
353
349
  _a.trys.push([1, 3, , 4]);
354
- return [4 /*yield*/, this.program.methods
350
+ return [4, this.program.methods
355
351
  .setAdminSigners({
356
352
  minSignatures: minSignatures,
357
353
  })
@@ -360,26 +356,24 @@ var PerpetualsClient = /** @class */ (function () {
360
356
  multisig: this.multisig.publicKey,
361
357
  })
362
358
  .remainingAccounts(adminMetas)
363
- // .signers([this.admin])
364
359
  .rpc()];
365
360
  case 2:
366
361
  _a.sent();
367
- return [3 /*break*/, 4];
362
+ return [3, 4];
368
363
  case 3:
369
364
  err_1 = _a.sent();
370
- // @ts-ignore
371
365
  if (this.printErrors) {
372
366
  console.error("setAdminSigners err:", err_1);
373
367
  }
374
368
  throw err_1;
375
- case 4: return [2 /*return*/];
369
+ case 4: return [2];
376
370
  }
377
371
  });
378
372
  }); };
379
373
  this.addPool = function (name) { return __awaiter(_this, void 0, void 0, function () {
380
374
  return __generator(this, function (_a) {
381
375
  switch (_a.label) {
382
- case 0: return [4 /*yield*/, this.program.methods
376
+ case 0: return [4, this.program.methods
383
377
  .addPool({ name: name })
384
378
  .accounts({
385
379
  admin: this.provider.wallet.publicKey,
@@ -392,7 +386,6 @@ var PerpetualsClient = /** @class */ (function () {
392
386
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
393
387
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
394
388
  })
395
- // .signers([this.admin])
396
389
  .rpc()
397
390
  .catch(function (err) {
398
391
  console.error(err);
@@ -400,14 +393,14 @@ var PerpetualsClient = /** @class */ (function () {
400
393
  })];
401
394
  case 1:
402
395
  _a.sent();
403
- return [2 /*return*/];
396
+ return [2];
404
397
  }
405
398
  });
406
399
  }); };
407
400
  this.removePool = function (name) { return __awaiter(_this, void 0, void 0, function () {
408
401
  return __generator(this, function (_a) {
409
402
  switch (_a.label) {
410
- case 0: return [4 /*yield*/, this.program.methods
403
+ case 0: return [4, this.program.methods
411
404
  .removePool({})
412
405
  .accounts({
413
406
  admin: this.admin,
@@ -417,7 +410,6 @@ var PerpetualsClient = /** @class */ (function () {
417
410
  pool: this.getPoolKey(name),
418
411
  systemProgram: web3_js_1.SystemProgram.programId,
419
412
  })
420
- // .signers([this.admin])
421
413
  .rpc()
422
414
  .catch(function (err) {
423
415
  console.error(err);
@@ -425,7 +417,7 @@ var PerpetualsClient = /** @class */ (function () {
425
417
  })];
426
418
  case 1:
427
419
  _a.sent();
428
- return [2 /*return*/];
420
+ return [2];
429
421
  }
430
422
  });
431
423
  }); };
@@ -435,8 +427,7 @@ var PerpetualsClient = /** @class */ (function () {
435
427
  switch (_a.label) {
436
428
  case 0:
437
429
  _a.trys.push([0, 2, , 3]);
438
- return [4 /*yield*/, this.program.methods
439
- // @ts-ignore
430
+ return [4, this.program.methods
440
431
  .addCustody({
441
432
  isStable: isStable,
442
433
  oracle: oracle,
@@ -459,7 +450,6 @@ var PerpetualsClient = /** @class */ (function () {
459
450
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
460
451
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
461
452
  })
462
- // .signers([this.admin])
463
453
  .rpc()
464
454
  .catch(function (err) {
465
455
  console.error(err);
@@ -468,12 +458,12 @@ var PerpetualsClient = /** @class */ (function () {
468
458
  case 1:
469
459
  trx_id = _a.sent();
470
460
  console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
471
- return [3 /*break*/, 3];
461
+ return [3, 3];
472
462
  case 2:
473
463
  error_1 = _a.sent();
474
464
  console.error("cli error :", error_1);
475
- return [3 /*break*/, 3];
476
- case 3: return [2 /*return*/];
465
+ return [3, 3];
466
+ case 3: return [2];
477
467
  }
478
468
  });
479
469
  }); };
@@ -481,8 +471,7 @@ var PerpetualsClient = /** @class */ (function () {
481
471
  var trx_id;
482
472
  return __generator(this, function (_a) {
483
473
  switch (_a.label) {
484
- case 0: return [4 /*yield*/, this.program.methods
485
- //@ts-ignore
474
+ case 0: return [4, this.program.methods
486
475
  .testingEditCustody({
487
476
  isStable: isStable,
488
477
  oracle: oracle,
@@ -505,7 +494,6 @@ var PerpetualsClient = /** @class */ (function () {
505
494
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
506
495
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
507
496
  })
508
- // .signers([this.admin])
509
497
  .rpc()
510
498
  .catch(function (err) {
511
499
  console.error(err);
@@ -514,14 +502,14 @@ var PerpetualsClient = /** @class */ (function () {
514
502
  case 1:
515
503
  trx_id = _a.sent();
516
504
  console.log("trx_id:", "https://explorer.solana.com/tx/".concat(trx_id, "?cluster=devnet"));
517
- return [2 /*return*/];
505
+ return [2];
518
506
  }
519
507
  });
520
508
  }); };
521
509
  this.removeCustody = function (poolName, tokenMint, ratios) { return __awaiter(_this, void 0, void 0, function () {
522
510
  return __generator(this, function (_a) {
523
511
  switch (_a.label) {
524
- case 0: return [4 /*yield*/, this.program.methods
512
+ case 0: return [4, this.program.methods
525
513
  .removeCustody({ ratios: ratios })
526
514
  .accounts({
527
515
  admin: this.admin,
@@ -534,7 +522,6 @@ var PerpetualsClient = /** @class */ (function () {
534
522
  systemProgram: web3_js_1.SystemProgram.programId,
535
523
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
536
524
  })
537
- // .signers([this.admin])
538
525
  .rpc()
539
526
  .catch(function (err) {
540
527
  console.error(err);
@@ -542,14 +529,14 @@ var PerpetualsClient = /** @class */ (function () {
542
529
  })];
543
530
  case 1:
544
531
  _a.sent();
545
- return [2 /*return*/];
532
+ return [2];
546
533
  }
547
534
  });
548
535
  }); };
549
536
  this.upgradeCustody = function (poolName, tokenMint) { return __awaiter(_this, void 0, void 0, function () {
550
537
  return __generator(this, function (_a) {
551
538
  switch (_a.label) {
552
- case 0: return [4 /*yield*/, this.program.methods
539
+ case 0: return [4, this.program.methods
553
540
  .upgradeCustody({})
554
541
  .accounts({
555
542
  admin: this.admin,
@@ -558,7 +545,6 @@ var PerpetualsClient = /** @class */ (function () {
558
545
  custody: this.getCustodyKey(poolName, tokenMint),
559
546
  systemProgram: web3_js_1.SystemProgram.programId,
560
547
  })
561
- // .signers([this.admin])
562
548
  .rpc()
563
549
  .catch(function (err) {
564
550
  console.error(err);
@@ -566,7 +552,7 @@ var PerpetualsClient = /** @class */ (function () {
566
552
  })];
567
553
  case 1:
568
554
  _a.sent();
569
- return [2 /*return*/];
555
+ return [2];
570
556
  }
571
557
  });
572
558
  }); };
@@ -589,8 +575,8 @@ var PerpetualsClient = /** @class */ (function () {
589
575
  position: this.getPositionKey(wallet, poolName, tokenMint, side),
590
576
  custody: this.getCustodyKey(poolName, tokenMint)
591
577
  };
592
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
593
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
578
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
579
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
594
580
  _c.custodyTokenAccount = this.getCustodyTokenAccountKey(poolName, tokenMint),
595
581
  _c.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
596
582
  _c)])
@@ -599,7 +585,7 @@ var PerpetualsClient = /** @class */ (function () {
599
585
  console.error(err);
600
586
  throw err;
601
587
  })];
602
- case 2: return [2 /*return*/, _d.sent()];
588
+ case 2: return [2, _d.sent()];
603
589
  }
604
590
  });
605
591
  }); };
@@ -615,20 +601,19 @@ var PerpetualsClient = /** @class */ (function () {
615
601
  }))
616
602
  .accounts;
617
603
  _c = {
618
- // signer: this.provider.wallet.publicKey,
619
604
  perpetuals: this.perpetuals.publicKey,
620
605
  pool: this.getPoolKey(poolName),
621
606
  custody: this.getCustodyKey(poolName, tokenMint)
622
607
  };
623
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
624
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
608
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
609
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
625
610
  _c)])
626
611
  .view()
627
612
  .catch(function (err) {
628
613
  console.error(err);
629
614
  throw err;
630
615
  })];
631
- case 2: return [2 /*return*/, _d.sent()];
616
+ case 2: return [2, _d.sent()];
632
617
  }
633
618
  });
634
619
  }); };
@@ -648,20 +633,20 @@ var PerpetualsClient = /** @class */ (function () {
648
633
  pool: this.getPoolKey(poolName),
649
634
  custody: this.getCustodyKey(poolName, tokenMint)
650
635
  };
651
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
636
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
652
637
  case 1:
653
638
  _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
654
639
  _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
655
640
  _e)]))
656
641
  .remainingAccounts;
657
- return [4 /*yield*/, this.getCustodyMetas(poolName)];
658
- case 2: return [4 /*yield*/, _d.apply(_a, [_f.sent()])
642
+ return [4, this.getCustodyMetas(poolName)];
643
+ case 2: return [4, _d.apply(_a, [_f.sent()])
659
644
  .view()
660
645
  .catch(function (err) {
661
646
  console.error(err);
662
647
  throw err;
663
648
  })];
664
- case 3: return [2 /*return*/, _f.sent()];
649
+ case 3: return [2, _f.sent()];
665
650
  }
666
651
  });
667
652
  }); };
@@ -681,20 +666,20 @@ var PerpetualsClient = /** @class */ (function () {
681
666
  pool: this.getPoolKey(poolName),
682
667
  custody: this.getCustodyKey(poolName, tokenMint)
683
668
  };
684
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
669
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
685
670
  case 1:
686
671
  _d = (_a = _c.apply(_b, [(_e.custodyOracleAccount = _f.sent(),
687
672
  _e.lpTokenMint = this.getPoolLpTokenKey(poolName),
688
673
  _e)]))
689
674
  .remainingAccounts;
690
- return [4 /*yield*/, this.getCustodyMetas(poolName)];
691
- case 2: return [4 /*yield*/, _d.apply(_a, [_f.sent()])
675
+ return [4, this.getCustodyMetas(poolName)];
676
+ case 2: return [4, _d.apply(_a, [_f.sent()])
692
677
  .view()
693
678
  .catch(function (err) {
694
679
  console.error(err);
695
680
  throw err;
696
681
  })];
697
- case 3: return [2 /*return*/, _f.sent()];
682
+ case 3: return [2, _f.sent()];
698
683
  }
699
684
  });
700
685
  }); };
@@ -705,7 +690,6 @@ var PerpetualsClient = /** @class */ (function () {
705
690
  switch (_d.label) {
706
691
  case 0:
707
692
  _b = (_a = this.program.methods
708
- //@ts-ignore
709
693
  .getEntryPriceAndFee({
710
694
  collateral: collateral,
711
695
  size: size,
@@ -713,26 +697,19 @@ var PerpetualsClient = /** @class */ (function () {
713
697
  }))
714
698
  .accounts;
715
699
  _c = {
716
- // signer: this.provider.wallet.publicKey,
717
700
  perpetuals: this.perpetuals.publicKey,
718
701
  pool: this.getPoolKey(poolName),
719
702
  custody: this.getCustodyKey(poolName, tokenMint)
720
703
  };
721
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
722
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
704
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
705
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
723
706
  _c)])
724
707
  .view()
725
708
  .catch(function (err) {
726
709
  console.error(err);
727
710
  throw err;
728
711
  })];
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())
734
- //@ts-ignore
735
- return [2 /*return*/, _d.sent()];
712
+ case 2: return [2, _d.sent()];
736
713
  }
737
714
  });
738
715
  }); };
@@ -751,15 +728,15 @@ var PerpetualsClient = /** @class */ (function () {
751
728
  position: this.getPositionKey(wallet, poolName, tokenMint, side),
752
729
  custody: this.getCustodyKey(poolName, tokenMint)
753
730
  };
754
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
755
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
731
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
732
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
756
733
  _c)])
757
734
  .view()
758
735
  .catch(function (err) {
759
736
  console.error(err);
760
737
  throw err;
761
738
  })];
762
- case 2: return [2 /*return*/, _d.sent()];
739
+ case 2: return [2, _d.sent()];
763
740
  }
764
741
  });
765
742
  }); };
@@ -784,15 +761,15 @@ var PerpetualsClient = /** @class */ (function () {
784
761
  position: this.getPositionKey(wallet, poolName, tokenMint, side),
785
762
  custody: this.getCustodyKey(poolName, tokenMint)
786
763
  };
787
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
788
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
764
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
765
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
789
766
  _c)])
790
767
  .view()
791
768
  .catch(function (err) {
792
769
  console.error(err);
793
770
  throw err;
794
771
  })];
795
- case 2: return [2 /*return*/, _d.sent()];
772
+ case 2: return [2, _d.sent()];
796
773
  }
797
774
  });
798
775
  });
@@ -818,24 +795,22 @@ var PerpetualsClient = /** @class */ (function () {
818
795
  position: this.getPositionKey(wallet, poolName, tokenMint, side),
819
796
  custody: this.getCustodyKey(poolName, tokenMint)
820
797
  };
821
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
822
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
798
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
799
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
823
800
  _c)])
824
801
  .rpc()
825
802
  .catch(function (err) {
826
803
  console.error(err);
827
804
  throw err;
828
805
  })];
829
- case 2: return [2 /*return*/, _d.sent()];
806
+ case 2: return [2, _d.sent()];
830
807
  }
831
808
  });
832
809
  });
833
810
  };
834
- // TODO: currently just static, need to write
835
811
  this.getLiquidationPrice = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime, addCollateral, removeCollateral) {
836
812
  if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
837
813
  if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
838
- // const positionAccount = PositionAccount.from(postionKey, postionData);
839
814
  var x;
840
815
  if ((0, types_1.isVariant)(postionData.side, 'long')) {
841
816
  x = tokenPrice.price.mul(new anchor_1.BN(90)).div(new anchor_1.BN(90));
@@ -860,15 +835,15 @@ var PerpetualsClient = /** @class */ (function () {
860
835
  position: this.getPositionKey(wallet, poolName, tokenMint, side),
861
836
  custody: this.getCustodyKey(poolName, tokenMint)
862
837
  };
863
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
864
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
838
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
839
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
865
840
  _c)])
866
841
  .view()
867
842
  .catch(function (err) {
868
843
  console.error(err);
869
844
  throw err;
870
845
  })];
871
- case 2: return [2 /*return*/, _d.sent()];
846
+ case 2: return [2, _d.sent()];
872
847
  }
873
848
  });
874
849
  }); };
@@ -888,17 +863,15 @@ var PerpetualsClient = /** @class */ (function () {
888
863
  position: pos,
889
864
  custody: this.getCustodyKey(poolName, tokenMint)
890
865
  };
891
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
892
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
866
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
867
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
893
868
  _c)])
894
869
  .view()
895
870
  .catch(function (err) {
896
871
  console.error(err);
897
872
  throw err;
898
873
  })];
899
- case 2:
900
- // console.log("pos:", pos.toBase58())
901
- return [2 /*return*/, _d.sent()];
874
+ case 2: return [2, _d.sent()];
902
875
  }
903
876
  });
904
877
  }); };
@@ -918,24 +891,20 @@ var PerpetualsClient = /** @class */ (function () {
918
891
  position: pos,
919
892
  custody: this.getCustodyKey(poolName, tokenMint)
920
893
  };
921
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
922
- case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
894
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMint)];
895
+ case 1: return [4, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
923
896
  _c)])
924
897
  .rpc()
925
898
  .catch(function (err) {
926
899
  console.error(err);
927
900
  throw err;
928
901
  })];
929
- case 2:
930
- // console.log("pos:", pos.toBase58())
931
- return [2 /*return*/, _d.sent()];
902
+ case 2: return [2, _d.sent()];
932
903
  }
933
904
  });
934
905
  }); };
935
906
  this.getPnl = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime) {
936
907
  var positionAccount = PositionAccount_1.PositionAccount.from(postionKey, postionData);
937
- // console.log("positionAccount:",positionAccount);
938
- // console.log("side :", postionData.side, (isVariant(postionData.side, 'long')))
939
908
  var _a = poolAccount.getPnlUsd(positionAccount, tokenPrice, tokenEmaPrice, custodyAccount, currentTime, false), profit = _a.profit, loss = _a.loss, exitFee = _a.exitFee;
940
909
  return {
941
910
  profit: profit,
@@ -958,19 +927,19 @@ var PerpetualsClient = /** @class */ (function () {
958
927
  pool: this.getPoolKey(poolName),
959
928
  receivingCustody: this.getCustodyKey(poolName, tokenMintIn)
960
929
  };
961
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMintIn)];
930
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMintIn)];
962
931
  case 1:
963
932
  _c.receivingCustodyOracleAccount = _d.sent(),
964
933
  _c.dispensingCustody = this.getCustodyKey(poolName, tokenMintOut);
965
- return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMintOut)];
966
- case 2: return [4 /*yield*/, _b.apply(_a, [(_c.dispensingCustodyOracleAccount = _d.sent(),
934
+ return [4, this.getCustodyOracleAccountKey(poolName, tokenMintOut)];
935
+ case 2: return [4, _b.apply(_a, [(_c.dispensingCustodyOracleAccount = _d.sent(),
967
936
  _c)])
968
937
  .view()
969
938
  .catch(function (err) {
970
939
  console.error(err);
971
940
  throw err;
972
941
  })];
973
- case 3: return [2 /*return*/, _d.sent()];
942
+ case 3: return [2, _d.sent()];
974
943
  }
975
944
  });
976
945
  }); };
@@ -986,14 +955,14 @@ var PerpetualsClient = /** @class */ (function () {
986
955
  pool: this.getPoolKey(poolName),
987
956
  }))
988
957
  .remainingAccounts;
989
- return [4 /*yield*/, this.getCustodyMetas(poolName)];
990
- case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])
958
+ return [4, this.getCustodyMetas(poolName)];
959
+ case 1: return [4, _b.apply(_a, [_c.sent()])
991
960
  .view()
992
961
  .catch(function (err) {
993
962
  console.error(err);
994
963
  throw err;
995
964
  })];
996
- case 2: return [2 /*return*/, _c.sent()];
965
+ case 2: return [2, _c.sent()];
997
966
  }
998
967
  });
999
968
  }); };
@@ -1009,26 +978,20 @@ var PerpetualsClient = /** @class */ (function () {
1009
978
  pool: this.getPoolKey(poolName),
1010
979
  }))
1011
980
  .remainingAccounts;
1012
- return [4 /*yield*/, this.getCustodyMetas(poolName)];
1013
- case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])
981
+ return [4, this.getCustodyMetas(poolName)];
982
+ case 1: return [4, _b.apply(_a, [_c.sent()])
1014
983
  .rpc()
1015
984
  .catch(function (err) {
1016
985
  console.error(err);
1017
986
  throw err;
1018
987
  })];
1019
- case 2: return [2 /*return*/, _c.sent()];
988
+ case 2: return [2, _c.sent()];
1020
989
  }
1021
990
  });
1022
991
  }); };
1023
992
  this.getAumSdk = function (poolAccount, token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime) {
1024
- // console.log("poolAccount:",poolAccount);
1025
993
  return poolAccount.getAssetsUnderManagementUsd(token_prices, token_ema_prices, custodies, aum_calc_mode, currentTime);
1026
994
  };
1027
- // TODO: handle SOL wrapping to WSOL and create a ATA - DONE
1028
- // TODO: Balance checks - DONE
1029
- // TODO: ATA check - else create - DONE
1030
- // Create WSOL Token account and not ATA and close it in end
1031
- // TODO: close other Accounts - NOT NEEDED
1032
995
  this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateralWithfee, fee, size, side, poolConfig, createUserWSOLATA, skipBalanceChecks) {
1033
996
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1034
997
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
@@ -1040,7 +1003,7 @@ var PerpetualsClient = /** @class */ (function () {
1040
1003
  console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1041
1004
  publicKey = this.provider.wallet.publicKey;
1042
1005
  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)];
1006
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1044
1007
  case 1:
1045
1008
  userCustodyTokenAccount = _b.sent();
1046
1009
  preInstructions = [];
@@ -1050,10 +1013,10 @@ var PerpetualsClient = /** @class */ (function () {
1050
1013
  _b.label = 2;
1051
1014
  case 2:
1052
1015
  _b.trys.push([2, 10, , 11]);
1053
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 4];
1016
+ if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3, 4];
1054
1017
  console.log("payTokenSymbol === sol", payTokenSymbol);
1055
1018
  wrappedSolAccount = new web3_js_1.Keypair();
1056
- return [4 /*yield*/, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1019
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1057
1020
  case 3:
1058
1021
  accCreationLamports = (_b.sent());
1059
1022
  console.log("accCreationLamports:", accCreationLamports);
@@ -1072,20 +1035,19 @@ var PerpetualsClient = /** @class */ (function () {
1072
1035
  (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1073
1036
  ];
1074
1037
  additionalSigners.push(wrappedSolAccount);
1075
- return [3 /*break*/, 8];
1038
+ return [3, 8];
1076
1039
  case 4:
1077
- if (!(createUserWSOLATA == false)) return [3 /*break*/, 5];
1040
+ if (!(createUserWSOLATA == false)) return [3, 5];
1078
1041
  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)];
1042
+ return [3, 8];
1043
+ case 5: return [4, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1081
1044
  case 6:
1082
- // for other tokens check if ATA and balance
1083
1045
  if (!(_b.sent())) {
1084
1046
  throw "Insufficient Funds , token Account doesn't exist";
1085
1047
  }
1086
- if (!!skipBalanceChecks) return [3 /*break*/, 8];
1048
+ if (!!skipBalanceChecks) return [3, 8];
1087
1049
  _a = anchor_1.BN.bind;
1088
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1050
+ return [4, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1089
1051
  case 7:
1090
1052
  tokenAccountBalance = new (_a.apply(anchor_1.BN, [void 0, (_b.sent()).value.amount]))();
1091
1053
  if (tokenAccountBalance.lt(collateralWithfee)) {
@@ -1106,7 +1068,7 @@ var PerpetualsClient = /** @class */ (function () {
1106
1068
  size: size,
1107
1069
  side: side,
1108
1070
  };
1109
- return [4 /*yield*/, this.program.methods
1071
+ return [4, this.program.methods
1110
1072
  .openPosition(params)
1111
1073
  .accounts({
1112
1074
  owner: publicKey,
@@ -1124,12 +1086,12 @@ var PerpetualsClient = /** @class */ (function () {
1124
1086
  case 9:
1125
1087
  instruction = _b.sent();
1126
1088
  instructions.push(instruction);
1127
- return [3 /*break*/, 11];
1089
+ return [3, 11];
1128
1090
  case 10:
1129
1091
  error_2 = _b.sent();
1130
1092
  console.log("perpClient openPosition error:", error_2);
1131
- return [3 /*break*/, 11];
1132
- case 11: return [2 /*return*/, {
1093
+ return [3, 11];
1094
+ case 11: return [2, {
1133
1095
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1134
1096
  additionalSigners: additionalSigners
1135
1097
  }];
@@ -1137,13 +1099,7 @@ var PerpetualsClient = /** @class */ (function () {
1137
1099
  });
1138
1100
  });
1139
1101
  };
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
- ) {
1102
+ this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, createUserATA, closeUsersWSOLATA) {
1147
1103
  if (createUserATA === void 0) { createUserATA = true; }
1148
1104
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
1149
1105
  return __awaiter(_this, void 0, void 0, function () {
@@ -1160,10 +1116,10 @@ var PerpetualsClient = /** @class */ (function () {
1160
1116
  _b.label = 1;
1161
1117
  case 1:
1162
1118
  _b.trys.push([1, 9, , 10]);
1163
- if (!(receivingTokenSymbol == 'SOL')) return [3 /*break*/, 3];
1119
+ if (!(receivingTokenSymbol == 'SOL')) return [3, 3];
1164
1120
  wrappedSolAccount = new web3_js_1.Keypair();
1165
1121
  userReceivingTokenAccount = wrappedSolAccount.publicKey;
1166
- return [4 /*yield*/, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1122
+ return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
1167
1123
  case 2:
1168
1124
  lamports = (_b.sent());
1169
1125
  preInstructions = [
@@ -1180,13 +1136,13 @@ var PerpetualsClient = /** @class */ (function () {
1180
1136
  (0, spl_token_1.createCloseAccountInstruction)(wrappedSolAccount.publicKey, publicKey, publicKey),
1181
1137
  ];
1182
1138
  additionalSigners.push(wrappedSolAccount);
1183
- return [3 /*break*/, 7];
1184
- case 3: return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1139
+ return [3, 7];
1140
+ case 3: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1185
1141
  case 4:
1186
1142
  userReceivingTokenAccount = _b.sent();
1187
1143
  _a = createUserATA;
1188
- if (!_a) return [3 /*break*/, 6];
1189
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1144
+ if (!_a) return [3, 6];
1145
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1190
1146
  case 5:
1191
1147
  _a = !(_b.sent());
1192
1148
  _b.label = 6;
@@ -1207,7 +1163,7 @@ var PerpetualsClient = /** @class */ (function () {
1207
1163
  params = {
1208
1164
  price: priceAfterSlippage,
1209
1165
  };
1210
- return [4 /*yield*/, this.program.methods
1166
+ return [4, this.program.methods
1211
1167
  .closePosition(params)
1212
1168
  .accounts({
1213
1169
  owner: publicKey,
@@ -1228,12 +1184,12 @@ var PerpetualsClient = /** @class */ (function () {
1228
1184
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1229
1185
  postInstructions.push(closeWsolATAIns);
1230
1186
  }
1231
- return [3 /*break*/, 10];
1187
+ return [3, 10];
1232
1188
  case 9:
1233
1189
  error_3 = _b.sent();
1234
1190
  console.error("perpclient closePosition error:", error_3);
1235
- return [3 /*break*/, 10];
1236
- case 10: return [2 /*return*/, {
1191
+ return [3, 10];
1192
+ case 10: return [2, {
1237
1193
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
1238
1194
  additionalSigners: additionalSigners
1239
1195
  }];
@@ -1241,13 +1197,7 @@ var PerpetualsClient = /** @class */ (function () {
1241
1197
  });
1242
1198
  });
1243
1199
  };
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
1250
- ) {
1200
+ this.openPositionOld = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA) {
1251
1201
  if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
1252
1202
  return __awaiter(_this, void 0, void 0, function () {
1253
1203
  var publicKey, payTokenCustody, userCustodyTokenAccount, instructions, wsolAssociatedTokenAccount, wsolATAExist, collateralWithfee, wsolBalance, _a, _b, unWrappedSolBalance, _c, totalSolBal, conversionAmt, tokenAccountBalance, _d, positionAccount, params, instruction, error_4;
@@ -1257,17 +1207,17 @@ var PerpetualsClient = /** @class */ (function () {
1257
1207
  console.log("open position :::", payTokenSymbol, poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey.toBase58());
1258
1208
  publicKey = this.provider.wallet.publicKey;
1259
1209
  payTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
1260
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1210
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey, publicKey)];
1261
1211
  case 1:
1262
1212
  userCustodyTokenAccount = _e.sent();
1263
1213
  instructions = [];
1264
1214
  _e.label = 2;
1265
1215
  case 2:
1266
1216
  _e.trys.push([2, 15, , 16]);
1267
- if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3 /*break*/, 9];
1217
+ if (!(payTokenSymbol == 'SOL' && createUserWSOLATA)) return [3, 9];
1268
1218
  console.log("payTokenSymbol === sol", payTokenSymbol);
1269
1219
  wsolAssociatedTokenAccount = userCustodyTokenAccount;
1270
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1220
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1271
1221
  case 3:
1272
1222
  wsolATAExist = _e.sent();
1273
1223
  if (!wsolATAExist) {
@@ -1276,26 +1226,23 @@ var PerpetualsClient = /** @class */ (function () {
1276
1226
  }
1277
1227
  collateralWithfee = collateral.add(fee);
1278
1228
  _a = anchor_1.BN.bind;
1279
- if (!wsolATAExist) return [3 /*break*/, 5];
1280
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1229
+ if (!wsolATAExist) return [3, 5];
1230
+ return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1281
1231
  case 4:
1282
1232
  _b = (_e.sent()).value.amount;
1283
- return [3 /*break*/, 6];
1233
+ return [3, 6];
1284
1234
  case 5:
1285
1235
  _b = 0;
1286
1236
  _e.label = 6;
1287
1237
  case 6:
1288
1238
  wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1289
- if (!wsolBalance.lt(collateralWithfee)) return [3 /*break*/, 8];
1239
+ if (!wsolBalance.lt(collateralWithfee)) return [3, 8];
1290
1240
  console.log("WSOL balance insufficient \n so Convert SOL to WSOL");
1291
1241
  _c = anchor_1.BN.bind;
1292
- return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
1242
+ return [4, this.provider.connection.getBalance(publicKey)];
1293
1243
  case 7:
1294
1244
  unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
1295
1245
  totalSolBal = unWrappedSolBalance.add(wsolBalance);
1296
- // const rentExemptSolNeedforATA = new BN(0.02 * LAMPORTS_PER_SOL);
1297
- // console.log("rentExemptSolNeedforATA:",rentExemptSolNeedforATA.toString())
1298
- //TODOD :: add(rentExemptSolNeedforATA)
1299
1246
  if (totalSolBal.lt(collateralWithfee)) {
1300
1247
  throw "Insufficient SOL Funds";
1301
1248
  }
@@ -1307,22 +1254,21 @@ var PerpetualsClient = /** @class */ (function () {
1307
1254
  instructions.push(web3_js_1.SystemProgram.transfer({
1308
1255
  fromPubkey: publicKey,
1309
1256
  toPubkey: wsolAssociatedTokenAccount,
1310
- lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
1257
+ lamports: conversionAmt.toNumber(),
1311
1258
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1312
1259
  _e.label = 8;
1313
- case 8: return [3 /*break*/, 13];
1260
+ case 8: return [3, 13];
1314
1261
  case 9:
1315
- if (!(createUserWSOLATA == false)) return [3 /*break*/, 10];
1262
+ if (!(createUserWSOLATA == false)) return [3, 10];
1316
1263
  console.log("skip WSOL checks and creation ,since createUserWSOLATA == false");
1317
- return [3 /*break*/, 13];
1318
- case 10: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1264
+ return [3, 13];
1265
+ case 10: return [4, (0, utils_1.checkIfAccountExists)(userCustodyTokenAccount, this.provider.connection)];
1319
1266
  case 11:
1320
- // for other tokens check if ATA and balance
1321
1267
  if (!(_e.sent())) {
1322
1268
  throw "Insufficient Funds , token Account doesn't exist";
1323
1269
  }
1324
1270
  _d = anchor_1.BN.bind;
1325
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1271
+ return [4, this.provider.connection.getTokenAccountBalance(userCustodyTokenAccount)];
1326
1272
  case 12:
1327
1273
  tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
1328
1274
  if (tokenAccountBalance.lt(collateral.add(fee))) {
@@ -1343,7 +1289,7 @@ var PerpetualsClient = /** @class */ (function () {
1343
1289
  size: size,
1344
1290
  side: side,
1345
1291
  };
1346
- return [4 /*yield*/, this.program.methods
1292
+ return [4, this.program.methods
1347
1293
  .openPosition(params)
1348
1294
  .accounts({
1349
1295
  owner: publicKey,
@@ -1361,22 +1307,16 @@ var PerpetualsClient = /** @class */ (function () {
1361
1307
  case 14:
1362
1308
  instruction = _e.sent();
1363
1309
  instructions.push(instruction);
1364
- return [3 /*break*/, 16];
1310
+ return [3, 16];
1365
1311
  case 15:
1366
1312
  error_4 = _e.sent();
1367
1313
  console.log("perpClient openPosition error:", error_4);
1368
- return [3 /*break*/, 16];
1369
- case 16: return [2 /*return*/, instructions];
1314
+ return [3, 16];
1315
+ case 16: return [2, instructions];
1370
1316
  }
1371
1317
  });
1372
1318
  });
1373
1319
  };
1374
- // ==== OLD
1375
- // TODO: handle SOL wrapping to WSOL and create a ATA - NOT NEEDED
1376
- // TODO : Balance checks - NOT NEEDED
1377
- // TODO: ATA check - else create - DONE
1378
- // TODO: for close Accounts - DONE BY ANCHOR
1379
- // TODO : if out token WSOL -> unwrap to SOL - DONE
1380
1320
  this.closePositionOld = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1381
1321
  var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_5;
1382
1322
  return __generator(this, function (_a) {
@@ -1384,14 +1324,14 @@ var PerpetualsClient = /** @class */ (function () {
1384
1324
  case 0:
1385
1325
  console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
1386
1326
  publicKey = this.provider.wallet.publicKey;
1387
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1327
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
1388
1328
  case 1:
1389
1329
  userReceivingTokenAccount = _a.sent();
1390
1330
  instructions = [];
1391
1331
  _a.label = 2;
1392
1332
  case 2:
1393
1333
  _a.trys.push([2, 5, , 6]);
1394
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1334
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1395
1335
  case 3:
1396
1336
  if (!(_a.sent())) {
1397
1337
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
@@ -1408,7 +1348,7 @@ var PerpetualsClient = /** @class */ (function () {
1408
1348
  params = {
1409
1349
  price: priceAfterSlippage,
1410
1350
  };
1411
- return [4 /*yield*/, this.program.methods
1351
+ return [4, this.program.methods
1412
1352
  .closePosition(params)
1413
1353
  .accounts({
1414
1354
  owner: publicKey,
@@ -1425,25 +1365,19 @@ var PerpetualsClient = /** @class */ (function () {
1425
1365
  case 4:
1426
1366
  instruction = _a.sent();
1427
1367
  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
1368
  if (receivingTokenSymbol == 'SOL') {
1430
1369
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1431
1370
  instructions.push(closeWsolATAIns);
1432
1371
  }
1433
- return [3 /*break*/, 6];
1372
+ return [3, 6];
1434
1373
  case 5:
1435
1374
  error_5 = _a.sent();
1436
1375
  console.error("perpclient closePosition error:", error_5);
1437
- return [3 /*break*/, 6];
1438
- case 6: return [2 /*return*/, instructions];
1376
+ return [3, 6];
1377
+ case 6: return [2, instructions];
1439
1378
  }
1440
1379
  });
1441
1380
  }); };
1442
- // TODO: ATA check - else create - DONE
1443
- // TODO: handle SOL wrapping to WSOL and create a ATA - DONE
1444
- // TODO : Balance checks - NOT NEEDED
1445
- // TODO: for close Accounts - DONE BY ANCHOR
1446
- // TODO : if out token WSOL -> unwrap to SOL - DONE
1447
1381
  this.swap = function (poolReceivingTokenSymbol, poolDispensingTokenSymbol, amountIn, minAmountOut, poolConfig, unWrapSol) {
1448
1382
  if (unWrapSol === void 0) { unWrapSol = false; }
1449
1383
  return __awaiter(_this, void 0, void 0, function () {
@@ -1466,23 +1400,23 @@ var PerpetualsClient = /** @class */ (function () {
1466
1400
  _e.label = 1;
1467
1401
  case 1:
1468
1402
  _e.trys.push([1, 17, , 18]);
1469
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolDispensingCustodyConfig.mintKey, publicKey)];
1403
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolDispensingCustodyConfig.mintKey, publicKey)];
1470
1404
  case 2:
1471
1405
  userReceivingTokenAccount = _e.sent();
1472
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1406
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1473
1407
  case 3:
1474
1408
  if (!(_e.sent())) {
1475
1409
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolDispensingCustodyConfig.mintKey));
1476
1410
  }
1477
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolReceivingCustodyConfig.mintKey, publicKey)];
1411
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolReceivingCustodyConfig.mintKey, publicKey)];
1478
1412
  case 4:
1479
1413
  userDispensingCustodyTokenAccount = _e.sent();
1480
- if (!(userDispensingTokenSymbol == 'SOL')) return [3 /*break*/, 12];
1414
+ if (!(userDispensingTokenSymbol == 'SOL')) return [3, 12];
1481
1415
  console.log("userDispensingTokenSymbol === sol", userDispensingTokenSymbol);
1482
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1416
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1483
1417
  case 5:
1484
1418
  wsolAssociatedTokenAccount = _e.sent();
1485
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1419
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1486
1420
  case 6:
1487
1421
  wsolATAExist = _e.sent();
1488
1422
  if (!wsolATAExist) {
@@ -1490,19 +1424,19 @@ var PerpetualsClient = /** @class */ (function () {
1490
1424
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1491
1425
  }
1492
1426
  _a = anchor_1.BN.bind;
1493
- if (!wsolATAExist) return [3 /*break*/, 8];
1494
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1427
+ if (!wsolATAExist) return [3, 8];
1428
+ return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1495
1429
  case 7:
1496
1430
  _b = (_e.sent()).value.amount;
1497
- return [3 /*break*/, 9];
1431
+ return [3, 9];
1498
1432
  case 8:
1499
1433
  _b = 0;
1500
1434
  _e.label = 9;
1501
1435
  case 9:
1502
1436
  wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1503
- if (!wsolBalance.lt(amountIn)) return [3 /*break*/, 11];
1437
+ if (!wsolBalance.lt(amountIn)) return [3, 11];
1504
1438
  _c = anchor_1.BN.bind;
1505
- return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
1439
+ return [4, this.provider.connection.getBalance(publicKey)];
1506
1440
  case 10:
1507
1441
  unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _e.sent()]))();
1508
1442
  totalSolBal = unWrappedSolBalance.add(wsolBalance);
@@ -1513,17 +1447,17 @@ var PerpetualsClient = /** @class */ (function () {
1513
1447
  instructions.push(web3_js_1.SystemProgram.transfer({
1514
1448
  fromPubkey: publicKey,
1515
1449
  toPubkey: wsolAssociatedTokenAccount,
1516
- lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER ?
1450
+ lamports: conversionAmt.toNumber(),
1517
1451
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1518
1452
  _e.label = 11;
1519
- case 11: return [3 /*break*/, 15];
1520
- case 12: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userDispensingCustodyTokenAccount, this.provider.connection)];
1453
+ case 11: return [3, 15];
1454
+ case 12: return [4, (0, utils_1.checkIfAccountExists)(userDispensingCustodyTokenAccount, this.provider.connection)];
1521
1455
  case 13:
1522
1456
  if (!(_e.sent())) {
1523
1457
  throw "Insufficient Funds , Token Account doesn't exist";
1524
1458
  }
1525
1459
  _d = anchor_1.BN.bind;
1526
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userDispensingCustodyTokenAccount)];
1460
+ return [4, this.provider.connection.getTokenAccountBalance(userDispensingCustodyTokenAccount)];
1527
1461
  case 14:
1528
1462
  tokenAccountBalance = new (_d.apply(anchor_1.BN, [void 0, (_e.sent()).value.amount]))();
1529
1463
  if (tokenAccountBalance.lt(amountIn)) {
@@ -1535,7 +1469,7 @@ var PerpetualsClient = /** @class */ (function () {
1535
1469
  amountIn: amountIn,
1536
1470
  minAmountOut: minAmountOut,
1537
1471
  };
1538
- return [4 /*yield*/, this.program.methods
1472
+ return [4, this.program.methods
1539
1473
  .swap(params)
1540
1474
  .accounts({
1541
1475
  owner: publicKey,
@@ -1556,17 +1490,16 @@ var PerpetualsClient = /** @class */ (function () {
1556
1490
  case 16:
1557
1491
  inx = _e.sent();
1558
1492
  instructions.push(inx);
1559
- // SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
1560
1493
  if (userReceivingTokenSymbol == 'SOL' && unWrapSol) {
1561
1494
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1562
1495
  instructions.push(closeWsolATAIns);
1563
1496
  }
1564
- return [3 /*break*/, 18];
1497
+ return [3, 18];
1565
1498
  case 17:
1566
1499
  err_2 = _e.sent();
1567
1500
  console.error("perpClient Swap error:: ", err_2);
1568
1501
  throw err_2;
1569
- case 18: return [2 /*return*/, instructions];
1502
+ case 18: return [2, instructions];
1570
1503
  }
1571
1504
  });
1572
1505
  });
@@ -1584,12 +1517,12 @@ var PerpetualsClient = /** @class */ (function () {
1584
1517
  instructions = [];
1585
1518
  userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(custody.mintKey, publicKey);
1586
1519
  collateralWithfee = collateralWithoutFee.add(fee);
1587
- if (!(collateralSymbol == 'SOL')) return [3 /*break*/, 7];
1520
+ if (!(collateralSymbol == 'SOL')) return [3, 7];
1588
1521
  console.log("collateralSymbol === sol", collateralSymbol);
1589
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1522
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1590
1523
  case 1:
1591
1524
  wsolAssociatedTokenAccount = _d.sent();
1592
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1525
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1593
1526
  case 2:
1594
1527
  wsolATAExist = _d.sent();
1595
1528
  if (!wsolATAExist) {
@@ -1597,19 +1530,19 @@ var PerpetualsClient = /** @class */ (function () {
1597
1530
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1598
1531
  }
1599
1532
  _a = anchor_1.BN.bind;
1600
- if (!wsolATAExist) return [3 /*break*/, 4];
1601
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1533
+ if (!wsolATAExist) return [3, 4];
1534
+ return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1602
1535
  case 3:
1603
1536
  _b = (_d.sent()).value.amount;
1604
- return [3 /*break*/, 5];
1537
+ return [3, 5];
1605
1538
  case 4:
1606
1539
  _b = 0;
1607
1540
  _d.label = 5;
1608
1541
  case 5:
1609
1542
  wsolBalance = new (_a.apply(anchor_1.BN, [void 0, _b]))();
1610
- if (!wsolBalance.lt(collateralWithfee)) return [3 /*break*/, 7];
1543
+ if (!wsolBalance.lt(collateralWithfee)) return [3, 7];
1611
1544
  _c = anchor_1.BN.bind;
1612
- return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
1545
+ return [4, this.provider.connection.getBalance(publicKey)];
1613
1546
  case 6:
1614
1547
  unWrappedSolBalance = new (_c.apply(anchor_1.BN, [void 0, _d.sent()]))();
1615
1548
  totalSolBal = unWrappedSolBalance.add(wsolBalance);
@@ -1620,10 +1553,10 @@ var PerpetualsClient = /** @class */ (function () {
1620
1553
  instructions.push(web3_js_1.SystemProgram.transfer({
1621
1554
  fromPubkey: publicKey,
1622
1555
  toPubkey: wsolAssociatedTokenAccount,
1623
- lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
1556
+ lamports: conversionAmt.toNumber(),
1624
1557
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1625
1558
  _d.label = 7;
1626
- case 7: return [4 /*yield*/, this.program.methods.addCollateral({
1559
+ case 7: return [4, this.program.methods.addCollateral({
1627
1560
  collateral: collateralWithoutFee
1628
1561
  }).accounts({
1629
1562
  owner: publicKey,
@@ -1641,7 +1574,7 @@ var PerpetualsClient = /** @class */ (function () {
1641
1574
  case 8:
1642
1575
  inx = _d.sent();
1643
1576
  instructions.push(inx);
1644
- return [2 /*return*/, instructions];
1577
+ return [2, instructions];
1645
1578
  }
1646
1579
  });
1647
1580
  }); };
@@ -1659,12 +1592,12 @@ var PerpetualsClient = /** @class */ (function () {
1659
1592
  }
1660
1593
  instructions = [];
1661
1594
  userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey);
1662
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1595
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
1663
1596
  case 1:
1664
1597
  if (!(_a.sent())) {
1665
1598
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
1666
1599
  }
1667
- return [4 /*yield*/, this.program.methods
1600
+ return [4, this.program.methods
1668
1601
  .removeCollateral({
1669
1602
  collateralUsd: collateralWithoutFee,
1670
1603
  })
@@ -1688,16 +1621,11 @@ var PerpetualsClient = /** @class */ (function () {
1688
1621
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
1689
1622
  instructions.push(closeWsolATAIns);
1690
1623
  }
1691
- return [2 /*return*/, instructions];
1624
+ return [2, instructions];
1692
1625
  }
1693
1626
  });
1694
1627
  }); };
1695
- // TODO: handle SOL wrapping to WSOL and create a ATA - DONE
1696
- // TODO :Balance checks - DONE
1697
- // TODO: ATA check - else create - DONE
1698
- // TODO: for close Accounts - NOT NEEDED
1699
- this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, // give this value based on slippage
1700
- poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1628
+ this.addLiquidity = function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
1701
1629
  var payTokenCustody, publicKey, instructions, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody, wsolAssociatedTokenAccount, wsolATAExist, wsolBalance, _b, _c, unWrappedSolBalance, _d, totalSolBal, conversionAmt, tokenAccountBalance, _e, inx, err_3;
1702
1630
  return __generator(this, function (_f) {
1703
1631
  switch (_f.label) {
@@ -1711,10 +1639,10 @@ var PerpetualsClient = /** @class */ (function () {
1711
1639
  _f.label = 1;
1712
1640
  case 1:
1713
1641
  _f.trys.push([1, 17, , 18]);
1714
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustody.mintKey, publicKey)];
1642
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(payTokenCustody.mintKey, publicKey)];
1715
1643
  case 2:
1716
1644
  userPayingTokenAccount = _f.sent();
1717
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1645
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1718
1646
  case 3:
1719
1647
  lpTokenAccount = _f.sent();
1720
1648
  custodyAccountMetas = [];
@@ -1732,17 +1660,17 @@ var PerpetualsClient = /** @class */ (function () {
1732
1660
  isWritable: false,
1733
1661
  });
1734
1662
  }
1735
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
1663
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
1736
1664
  case 4:
1737
1665
  if (!(_f.sent())) {
1738
1666
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.lpTokenMint));
1739
1667
  }
1740
- if (!(payTokenSymbol == 'SOL')) return [3 /*break*/, 12];
1668
+ if (!(payTokenSymbol == 'SOL')) return [3, 12];
1741
1669
  console.log("payTokenSymbol === sol", payTokenSymbol);
1742
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1670
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey)];
1743
1671
  case 5:
1744
1672
  wsolAssociatedTokenAccount = _f.sent();
1745
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1673
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
1746
1674
  case 6:
1747
1675
  wsolATAExist = _f.sent();
1748
1676
  if (!wsolATAExist) {
@@ -1750,19 +1678,19 @@ var PerpetualsClient = /** @class */ (function () {
1750
1678
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
1751
1679
  }
1752
1680
  _b = anchor_1.BN.bind;
1753
- if (!wsolATAExist) return [3 /*break*/, 8];
1754
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1681
+ if (!wsolATAExist) return [3, 8];
1682
+ return [4, this.provider.connection.getTokenAccountBalance(wsolAssociatedTokenAccount)];
1755
1683
  case 7:
1756
1684
  _c = (_f.sent()).value.amount;
1757
- return [3 /*break*/, 9];
1685
+ return [3, 9];
1758
1686
  case 8:
1759
1687
  _c = 0;
1760
1688
  _f.label = 9;
1761
1689
  case 9:
1762
1690
  wsolBalance = new (_b.apply(anchor_1.BN, [void 0, _c]))();
1763
- if (!wsolBalance.lt(tokenAmountIn)) return [3 /*break*/, 11];
1691
+ if (!wsolBalance.lt(tokenAmountIn)) return [3, 11];
1764
1692
  _d = anchor_1.BN.bind;
1765
- return [4 /*yield*/, this.provider.connection.getBalance(publicKey)];
1693
+ return [4, this.provider.connection.getBalance(publicKey)];
1766
1694
  case 10:
1767
1695
  unWrappedSolBalance = new (_d.apply(anchor_1.BN, [void 0, _f.sent()]))();
1768
1696
  totalSolBal = unWrappedSolBalance.add(wsolBalance);
@@ -1773,24 +1701,24 @@ var PerpetualsClient = /** @class */ (function () {
1773
1701
  instructions.push(web3_js_1.SystemProgram.transfer({
1774
1702
  fromPubkey: publicKey,
1775
1703
  toPubkey: wsolAssociatedTokenAccount,
1776
- lamports: conversionAmt.toNumber(), // IS IT SAFE TO PUT AS NUMBER
1704
+ lamports: conversionAmt.toNumber(),
1777
1705
  }), (0, spl_token_1.createSyncNativeInstruction)(wsolAssociatedTokenAccount));
1778
1706
  _f.label = 11;
1779
- case 11: return [3 /*break*/, 15];
1780
- case 12: return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
1707
+ case 11: return [3, 15];
1708
+ case 12: return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
1781
1709
  case 13:
1782
1710
  if (!(_f.sent())) {
1783
1711
  throw "Insufficient Funds , token Account doesn't exist";
1784
1712
  }
1785
1713
  _e = anchor_1.BN.bind;
1786
- return [4 /*yield*/, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
1714
+ return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
1787
1715
  case 14:
1788
1716
  tokenAccountBalance = new (_e.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
1789
1717
  if (tokenAccountBalance.lt(tokenAmountIn)) {
1790
1718
  throw "Insufficient Funds";
1791
1719
  }
1792
1720
  _f.label = 15;
1793
- case 15: return [4 /*yield*/, this.program.methods
1721
+ case 15: return [4, this.program.methods
1794
1722
  .addLiquidity({
1795
1723
  amountIn: tokenAmountIn,
1796
1724
  minLpAmountOut: minLpAmountOut
@@ -1813,22 +1741,16 @@ var PerpetualsClient = /** @class */ (function () {
1813
1741
  case 16:
1814
1742
  inx = _f.sent();
1815
1743
  instructions.push(inx);
1816
- return [3 /*break*/, 18];
1744
+ return [3, 18];
1817
1745
  case 17:
1818
1746
  err_3 = _f.sent();
1819
1747
  console.error("perpClient addLiquidity error:: ", err_3);
1820
1748
  throw err_3;
1821
- case 18: return [2 /*return*/, instructions];
1749
+ case 18: return [2, instructions];
1822
1750
  }
1823
1751
  });
1824
1752
  }); };
1825
- // TODO: handle SOL wrapping to WSOL and create a ATA - NOT NEEDED
1826
- // TODO :Balance checks - DONE
1827
- // TODO: ATA check - else create - DONE
1828
- // TODO: for LP close Accounts - DONE
1829
- // TODO : if out token WSOL -> unwrap to SOL - DONE
1830
- this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, // give this value based on slippage
1831
- poolConfig, closeLpATA) {
1753
+ this.removeLiquidity = function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA) {
1832
1754
  if (closeLpATA === void 0) { closeLpATA = false; }
1833
1755
  return __awaiter(_this, void 0, void 0, function () {
1834
1756
  var recieveTokenCustody, publicKey, instructions, userRecievingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody, removeLiquidityTx, closeInx, closeWsolATAIns, err_4;
@@ -1844,15 +1766,15 @@ var PerpetualsClient = /** @class */ (function () {
1844
1766
  _b.label = 1;
1845
1767
  case 1:
1846
1768
  _b.trys.push([1, 6, , 7]);
1847
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(recieveTokenCustody.mintKey, publicKey)];
1769
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(recieveTokenCustody.mintKey, publicKey)];
1848
1770
  case 2:
1849
1771
  userRecievingTokenAccount = _b.sent();
1850
- return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userRecievingTokenAccount, this.provider.connection)];
1772
+ return [4, (0, utils_1.checkIfAccountExists)(userRecievingTokenAccount, this.provider.connection)];
1851
1773
  case 3:
1852
1774
  if (!(_b.sent())) {
1853
1775
  instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userRecievingTokenAccount, publicKey, recieveTokenCustody.mintKey));
1854
1776
  }
1855
- return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1777
+ return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.lpTokenMint, publicKey)];
1856
1778
  case 4:
1857
1779
  lpTokenAccount = _b.sent();
1858
1780
  custodyAccountMetas = [];
@@ -1871,7 +1793,7 @@ var PerpetualsClient = /** @class */ (function () {
1871
1793
  });
1872
1794
  }
1873
1795
  console.log("liquidityAmountIn", liquidityAmountIn.toString());
1874
- return [4 /*yield*/, this.program.methods
1796
+ return [4, this.program.methods
1875
1797
  .removeLiquidity({
1876
1798
  lpAmountIn: liquidityAmountIn,
1877
1799
  minAmountOut: minTokenAmountOut
@@ -1898,41 +1820,25 @@ var PerpetualsClient = /** @class */ (function () {
1898
1820
  closeInx = (0, spl_token_1.createCloseAccountInstruction)(lpTokenAccount, publicKey, publicKey);
1899
1821
  instructions.push(closeInx);
1900
1822
  }
1901
- // SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
1902
1823
  if (recieveTokenSymbol == 'SOL') {
1903
1824
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userRecievingTokenAccount, publicKey, publicKey);
1904
1825
  instructions.push(closeWsolATAIns);
1905
1826
  }
1906
- return [3 /*break*/, 7];
1827
+ return [3, 7];
1907
1828
  case 6:
1908
1829
  err_4 = _b.sent();
1909
1830
  console.log("perpClient removeLiquidity error:: ", err_4);
1910
1831
  throw err_4;
1911
- case 7: return [2 /*return*/, instructions];
1832
+ case 7: return [2, instructions];
1912
1833
  }
1913
1834
  });
1914
1835
  });
1915
1836
  };
1916
- // this.provider = AnchorProvider.local(clusterUrl, {
1917
- // commitment: "confirmed",
1918
- // preflightCommitment: "confirmed",
1919
- // skipPreflight: true
1920
- // });
1921
- // setProvider(this.provider);
1922
1837
  this.provider = provider;
1923
1838
  (0, anchor_1.setProvider)(provider);
1924
- // const idl = JSON.parse( fs.readFileSync("./target/idl/perpetuals.json", "utf8"));
1925
- // const idl = JSON.parse(IDL);
1926
- // const program = new anchor.Program(idl, programId, provider);
1927
1839
  this.program = new anchor_1.Program(perpetuals_1.IDL, programId);
1928
1840
  this.programId = programId;
1929
- //this.program = workspace.Perpetuals as Program<Perpetuals>;
1930
- // console.log("client constructor programID : ", this.program.programId.toBase58());
1931
- // this.admin = Keypair.fromSecretKey(
1932
- // new Uint8Array(JSON.parse(readFileSync(adminKey).toString()))
1933
- // );
1934
1841
  this.admin = this.provider.wallet.publicKey;
1935
- // console.log("admin:", this.admin.toBase58())
1936
1842
  this.multisig = this.findProgramAddress("multisig");
1937
1843
  this.authority = this.findProgramAddress("transfer_authority");
1938
1844
  this.perpetuals = this.findProgramAddress("perpetuals");
@@ -1949,8 +1855,8 @@ var PerpetualsClient = /** @class */ (function () {
1949
1855
  return __awaiter(this, void 0, void 0, function () {
1950
1856
  return __generator(this, function (_b) {
1951
1857
  switch (_b.label) {
1952
- case 0: return [4 /*yield*/, (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))];
1953
- case 1: return [2 /*return*/, _b.sent()];
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()];
1954
1860
  }
1955
1861
  });
1956
1862
  });