flash-sdk 1.0.1 → 1.0.3

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.
@@ -1,8 +1,8 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { Assets, FeesStats, Custody, Fees, PricingParams, TradeStats, Permissions, BorrowRateParams, OracleParams, VolumeStats, PositionStats, BorrowRateState } from "./types";
2
+ import { Assets, FeesStats, Custody, Fees, PricingParams, TradeStats, Permissions, BorrowRateParams, OracleParams, VolumeStats, PositionStats, BorrowRateState, Side } from "./types";
3
3
  import { PositionAccount } from "./PositionAccount";
4
4
  import BN from "bn.js";
5
- export declare class CustodyAccount {
5
+ export declare class CustodyAccount implements Custody {
6
6
  publicKey: PublicKey;
7
7
  pool: PublicKey;
8
8
  mint: PublicKey;
@@ -21,9 +21,12 @@ export declare class CustodyAccount {
21
21
  longPositions: PositionStats;
22
22
  shortPositions: PositionStats;
23
23
  borrowRateState: BorrowRateState;
24
+ bump: number;
25
+ tokenAccountBump: number;
24
26
  constructor(publicKey: PublicKey, parseData: Custody);
25
27
  static from(publicKey: PublicKey, parseData: Custody): CustodyAccount;
26
28
  updateCustodyData(custody: Custody): void;
27
29
  getCumulativeInterest(curtime: BN): BN;
28
30
  getInterestAmountUsd(position: PositionAccount, curtime: BN): BN;
31
+ getCollectivePosition(side: Side): PositionAccount;
29
32
  }
@@ -15,6 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.CustodyAccount = void 0;
18
+ var types_1 = require("./types");
18
19
  var bn_js_1 = __importDefault(require("bn.js"));
19
20
  var constants_1 = require("./constants");
20
21
  var utils_1 = require("./utils");
@@ -59,6 +60,35 @@ var CustodyAccount = /** @class */ (function () {
59
60
  return (position_interest.mul(position.sizeUsd))
60
61
  .div(new bn_js_1.default(constants_1.RATE_POWER));
61
62
  };
63
+ CustodyAccount.prototype.getCollectivePosition = function (side) {
64
+ var stats;
65
+ if ((0, types_1.isVariant)(side, 'long')) {
66
+ stats = this.longPositions;
67
+ }
68
+ else {
69
+ stats = this.shortPositions;
70
+ }
71
+ ;
72
+ // if (!stats.openPositions.isNeg()) {
73
+ // const obj: Position = {
74
+ // side,
75
+ // price:
76
+ // stats.weighted_price.div(stats.total_quantity)
77
+ // size_usd: stats.size_usd,
78
+ // collateral_usd: stats.collateral_usd,
79
+ // unrealized_loss_usd: stats.cumulative_interest_usd,
80
+ // cumulative_interest_snapshot: stats.cumulative_interest_snapshot,
81
+ // locked_amount: stats.locked_amount,
82
+ // ..Position::default()
83
+ // }
84
+ // return new PositionAccount(
85
+ // new PublicKey('dgfgfg'),
86
+ // )
87
+ // } else {
88
+ // Ok(Position::default())
89
+ // }
90
+ throw Error("Incomplete");
91
+ };
62
92
  return CustodyAccount;
63
93
  }());
64
94
  exports.CustodyAccount = CustodyAccount;
@@ -1,4 +1,3 @@
1
- /// <reference types="bn.js" />
2
1
  import { BN } from "@coral-xyz/anchor";
3
2
  export declare class OraclePrice {
4
3
  price: BN;
@@ -1,5 +1,5 @@
1
1
  import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
2
- import { PublicKey, Keypair } from "@solana/web3.js";
2
+ import { PublicKey } from "@solana/web3.js";
3
3
  import { PoolAccount } from "./PoolAccount";
4
4
  import { BorrowRateParams, Fees, OracleParams, Permissions, Position, PositionSide, PricingParams, TokenRatios } from "./types";
5
5
  import { OraclePrice } from "./OraclePrice";
@@ -8,7 +8,8 @@ import { Perpetuals } from "./target/types/perpetuals";
8
8
  export declare class PerpetualsClient {
9
9
  provider: AnchorProvider;
10
10
  program: Program<Perpetuals>;
11
- admin: Keypair;
11
+ admin: PublicKey;
12
+ programId: PublicKey;
12
13
  multisig: {
13
14
  publicKey: PublicKey;
14
15
  bump: number;
@@ -21,7 +22,7 @@ export declare class PerpetualsClient {
21
22
  publicKey: PublicKey;
22
23
  bump: number;
23
24
  };
24
- constructor(clusterUrl: string, adminKey: string);
25
+ constructor(provider: AnchorProvider, programId: PublicKey);
25
26
  findProgramAddress: (label: string, extraSeeds?: any) => {
26
27
  publicKey: PublicKey;
27
28
  bump: number;
@@ -42,11 +42,31 @@ var web3_js_1 = require("@solana/web3.js");
42
42
  var spl_token_1 = require("@solana/spl-token");
43
43
  var js_sha256_1 = require("js-sha256");
44
44
  var bs58_1 = require("bs58");
45
- var fs_1 = require("fs");
46
45
  var PositionAccount_1 = require("./PositionAccount");
47
- // import { Perpetuals } from "../../target/types/perpetuals"
46
+ var perpetuals_1 = require("./target/types/perpetuals");
47
+ /* USEAGE
48
+
49
+ UI ---
50
+ provider = from phatom
51
+
52
+ client = new PerpetualsClient(provider, user.pubkey , programId);
53
+
54
+ BOT cli --------
55
+
56
+ provider = await getProvider(new DefaultWallet(DEFAULT_PERPS_USER));
57
+
58
+ AnchorProvider.local(clusterUrl, {
59
+ commitment: "confirmed",
60
+ preflightCommitment: "confirmed",
61
+ skipPreflight: true
62
+ });
63
+ process.env["ANCHOR_WALLET"] = adminKeyPath;
64
+
65
+ client = new PerpetualsClient(provider, DEFAULT_PERPS_USER.pubkey , programId);
66
+
67
+ */
48
68
  var PerpetualsClient = /** @class */ (function () {
49
- function PerpetualsClient(clusterUrl, adminKey) {
69
+ function PerpetualsClient(provider, programId) {
50
70
  var _this = this;
51
71
  this.findProgramAddress = function (label, extraSeeds) {
52
72
  if (extraSeeds === void 0) { extraSeeds = null; }
@@ -335,11 +355,11 @@ var PerpetualsClient = /** @class */ (function () {
335
355
  minSignatures: minSignatures,
336
356
  })
337
357
  .accounts({
338
- admin: this.admin.publicKey,
358
+ admin: this.admin,
339
359
  multisig: this.multisig.publicKey,
340
360
  })
341
361
  .remainingAccounts(adminMetas)
342
- .signers([this.admin])
362
+ // .signers([this.admin])
343
363
  .rpc()];
344
364
  case 2:
345
365
  _a.sent();
@@ -361,7 +381,7 @@ var PerpetualsClient = /** @class */ (function () {
361
381
  case 0: return [4 /*yield*/, this.program.methods
362
382
  .addPool({ name: name })
363
383
  .accounts({
364
- admin: this.admin.publicKey,
384
+ admin: this.provider.wallet.publicKey,
365
385
  multisig: this.multisig.publicKey,
366
386
  transferAuthority: this.authority.publicKey,
367
387
  perpetuals: this.perpetuals.publicKey,
@@ -371,7 +391,7 @@ var PerpetualsClient = /** @class */ (function () {
371
391
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
372
392
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
373
393
  })
374
- .signers([this.admin])
394
+ // .signers([this.admin])
375
395
  .rpc()
376
396
  .catch(function (err) {
377
397
  console.error(err);
@@ -389,14 +409,14 @@ var PerpetualsClient = /** @class */ (function () {
389
409
  case 0: return [4 /*yield*/, this.program.methods
390
410
  .removePool({})
391
411
  .accounts({
392
- admin: this.admin.publicKey,
412
+ admin: this.admin,
393
413
  multisig: this.multisig.publicKey,
394
414
  transferAuthority: this.authority.publicKey,
395
415
  perpetuals: this.perpetuals.publicKey,
396
416
  pool: this.getPoolKey(name),
397
417
  systemProgram: web3_js_1.SystemProgram.programId,
398
418
  })
399
- .signers([this.admin])
419
+ // .signers([this.admin])
400
420
  .rpc()
401
421
  .catch(function (err) {
402
422
  console.error(err);
@@ -430,7 +450,7 @@ var PerpetualsClient = /** @class */ (function () {
430
450
  ratios: ratios,
431
451
  })
432
452
  .accounts({
433
- admin: this.admin.publicKey,
453
+ admin: this.admin,
434
454
  multisig: this.multisig.publicKey,
435
455
  transferAuthority: this.authority.publicKey,
436
456
  perpetuals: this.perpetuals.publicKey,
@@ -442,7 +462,7 @@ var PerpetualsClient = /** @class */ (function () {
442
462
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
443
463
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
444
464
  })
445
- .signers([this.admin])
465
+ // .signers([this.admin])
446
466
  .rpc()
447
467
  .catch(function (err) {
448
468
  console.error(err);
@@ -479,7 +499,7 @@ var PerpetualsClient = /** @class */ (function () {
479
499
  ratios: ratios,
480
500
  })
481
501
  .accounts({
482
- admin: this.admin.publicKey,
502
+ admin: this.admin,
483
503
  multisig: this.multisig.publicKey,
484
504
  transferAuthority: this.authority.publicKey,
485
505
  perpetuals: this.perpetuals.publicKey,
@@ -491,7 +511,7 @@ var PerpetualsClient = /** @class */ (function () {
491
511
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
492
512
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
493
513
  })
494
- .signers([this.admin])
514
+ // .signers([this.admin])
495
515
  .rpc()
496
516
  .catch(function (err) {
497
517
  console.error(err);
@@ -510,7 +530,7 @@ var PerpetualsClient = /** @class */ (function () {
510
530
  case 0: return [4 /*yield*/, this.program.methods
511
531
  .removeCustody({ ratios: ratios })
512
532
  .accounts({
513
- admin: this.admin.publicKey,
533
+ admin: this.admin,
514
534
  multisig: this.multisig.publicKey,
515
535
  transferAuthority: this.authority.publicKey,
516
536
  perpetuals: this.perpetuals.publicKey,
@@ -520,7 +540,7 @@ var PerpetualsClient = /** @class */ (function () {
520
540
  systemProgram: web3_js_1.SystemProgram.programId,
521
541
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
522
542
  })
523
- .signers([this.admin])
543
+ // .signers([this.admin])
524
544
  .rpc()
525
545
  .catch(function (err) {
526
546
  console.error(err);
@@ -538,13 +558,13 @@ var PerpetualsClient = /** @class */ (function () {
538
558
  case 0: return [4 /*yield*/, this.program.methods
539
559
  .upgradeCustody({})
540
560
  .accounts({
541
- admin: this.admin.publicKey,
561
+ admin: this.admin,
542
562
  multisig: this.multisig.publicKey,
543
563
  pool: this.getPoolKey(poolName),
544
564
  custody: this.getCustodyKey(poolName, tokenMint),
545
565
  systemProgram: web3_js_1.SystemProgram.programId,
546
566
  })
547
- .signers([this.admin])
567
+ // .signers([this.admin])
548
568
  .rpc()
549
569
  .catch(function (err) {
550
570
  console.error(err);
@@ -993,14 +1013,25 @@ var PerpetualsClient = /** @class */ (function () {
993
1013
  }
994
1014
  });
995
1015
  }); };
996
- this.provider = anchor_1.AnchorProvider.local(clusterUrl, {
997
- commitment: "confirmed",
998
- preflightCommitment: "confirmed",
999
- skipPreflight: true
1000
- });
1001
- (0, anchor_1.setProvider)(this.provider);
1002
- this.program = anchor_1.workspace.Perpetuals;
1003
- this.admin = web3_js_1.Keypair.fromSecretKey(new Uint8Array(JSON.parse((0, fs_1.readFileSync)(adminKey).toString())));
1016
+ // this.provider = AnchorProvider.local(clusterUrl, {
1017
+ // commitment: "confirmed",
1018
+ // preflightCommitment: "confirmed",
1019
+ // skipPreflight: true
1020
+ // });
1021
+ // setProvider(this.provider);
1022
+ this.provider = provider;
1023
+ (0, anchor_1.setProvider)(provider);
1024
+ // const idl = JSON.parse( fs.readFileSync("./target/idl/perpetuals.json", "utf8"));
1025
+ // const idl = JSON.parse(IDL);
1026
+ // const program = new anchor.Program(idl, programId, provider);
1027
+ this.program = new anchor_1.Program(perpetuals_1.IDL, programId);
1028
+ //this.program = workspace.Perpetuals as Program<Perpetuals>;
1029
+ console.log("client constructor programID : ", this.program.programId.toBase58());
1030
+ // this.admin = Keypair.fromSecretKey(
1031
+ // new Uint8Array(JSON.parse(readFileSync(adminKey).toString()))
1032
+ // );
1033
+ this.admin = this.provider.wallet.publicKey;
1034
+ console.log("admin:", this.admin.toBase58());
1004
1035
  this.multisig = this.findProgramAddress("multisig");
1005
1036
  this.authority = this.findProgramAddress("transfer_authority");
1006
1037
  this.perpetuals = this.findProgramAddress("perpetuals");
@@ -1,11 +1,10 @@
1
- /// <reference types="bn.js" />
2
1
  import { BN } from "@coral-xyz/anchor";
3
- import { Pool, Side, TokenRatios } from "./types";
2
+ import { AumCalcMode, Pool, Side, TokenRatios } from "./types";
4
3
  import { PublicKey } from "@solana/web3.js";
5
4
  import { CustodyAccount } from "./CustodyAccount";
6
5
  import { OraclePrice } from "./OraclePrice";
7
6
  import { PositionAccount } from "./PositionAccount";
8
- export declare class PoolAccount {
7
+ export declare class PoolAccount implements Pool {
9
8
  publicKey: PublicKey;
10
9
  name: string;
11
10
  custodies: PublicKey[];
@@ -31,4 +30,5 @@ export declare class PoolAccount {
31
30
  loss: BN;
32
31
  exitFee: BN;
33
32
  };
33
+ getAssetsUnderManagementUsd(token_prices: OraclePrice[], token_ema_prices: OraclePrice[], custodies: CustodyAccount[], aum_calc_mode: AumCalcMode): BN;
34
34
  }
@@ -20,9 +20,6 @@ var PoolAccount = /** @class */ (function () {
20
20
  PoolAccount.prototype.getTokenId = function (custodyKey) {
21
21
  return this.custodies.findIndex(function (i) { return i.toBase58() == custodyKey.toBase58(); });
22
22
  };
23
- // loadlpData(lpTokenInfo : Mint){
24
- // this.lpTokenInfo = lpTokenInfo
25
- // }
26
23
  PoolAccount.prototype.getAddLiquidityFee = function (tokenId, amount, custody, tokenPrice) {
27
24
  return this.getFee(tokenId, custody.fees.addLiquidity, amount, new anchor_1.BN(0), custody, tokenPrice);
28
25
  };
@@ -243,6 +240,66 @@ var PoolAccount = /** @class */ (function () {
243
240
  }
244
241
  }
245
242
  }; //getPnlUsd
243
+ PoolAccount.prototype.getAssetsUnderManagementUsd = function (token_prices, token_ema_prices, custodies, aum_calc_mode) {
244
+ var pool_amount_usd = constants_1.BN_ZERO;
245
+ for (var index = 0; index < this.custodies.length; index++) {
246
+ if (token_prices.length != this.custodies.length || token_prices.length != token_ema_prices.length) {
247
+ throw Error("token prices length incorrect");
248
+ }
249
+ var aum_token_price = void 0;
250
+ // switch unable to match enum
251
+ if ((0, types_1.isVariant)(aum_calc_mode, "last")) {
252
+ aum_token_price = token_prices[index];
253
+ }
254
+ else if ((0, types_1.isVariant)(aum_calc_mode, "ema")) {
255
+ aum_token_price = token_ema_prices[index];
256
+ }
257
+ else if ((0, types_1.isVariant)(aum_calc_mode, "min")) {
258
+ if (token_prices[index].cmp(token_ema_prices[index])) {
259
+ aum_token_price = token_prices[index];
260
+ }
261
+ else {
262
+ aum_token_price = token_ema_prices[index];
263
+ }
264
+ }
265
+ else if ((0, types_1.isVariant)(aum_calc_mode, "max")) {
266
+ if (token_ema_prices[index].cmp(token_prices[index])) {
267
+ aum_token_price = token_prices[index];
268
+ }
269
+ else {
270
+ aum_token_price = token_ema_prices[index];
271
+ }
272
+ }
273
+ var token_amount_usd = aum_token_price.getAssetAmountUsd(custodies[index].assets.owned, custodies[index].decimals);
274
+ pool_amount_usd = pool_amount_usd.add(token_amount_usd);
275
+ // if (custodies[index].pricing.useUnrealizedPnlInAum) {
276
+ // // compute aggregate unrealized pnl
277
+ // let (long_profit, long_loss, _) = this.getPnlUsd(
278
+ // &custodies[index].get_collective_position(Side::Long)?,
279
+ // &token_price,
280
+ // &token_ema_price,
281
+ // &custodies[index],
282
+ // curtime,
283
+ // false,
284
+ // )?;
285
+ // let (short_profit, short_loss, _) = self.get_pnl_usd(
286
+ // &custodies[index].get_collective_position(Side::Short)?,
287
+ // &token_price,
288
+ // &token_ema_price,
289
+ // &custodies[index],
290
+ // curtime,
291
+ // false,
292
+ // )?;
293
+ // // adjust pool amount by collective profit/loss
294
+ // pool_amount_usd = math::checked_add(pool_amount_usd, long_profit as u128)?;
295
+ // pool_amount_usd = math::checked_add(pool_amount_usd, short_profit as u128)?;
296
+ // pool_amount_usd = pool_amount_usd.saturating_sub(long_loss as u128);
297
+ // pool_amount_usd = pool_amount_usd.saturating_sub(short_loss as u128);
298
+ // }
299
+ throw Error("Incomplete");
300
+ }
301
+ return pool_amount_usd;
302
+ };
246
303
  return PoolAccount;
247
304
  }()); // Pool
248
305
  exports.PoolAccount = PoolAccount;
@@ -1,13 +1,33 @@
1
- /// <reference types="bn.js" />
2
1
  import { BN } from "@coral-xyz/anchor";
3
2
  import { Mint } from "@solana/spl-token";
4
- import { Pool } from "./types";
5
3
  import { CustodyAccount } from "./CustodyAccount";
6
4
  import { PoolConfig } from "./PoolConfig";
5
+ import { PoolAccount } from "./PoolAccount";
7
6
  export declare class PoolDisplayData {
8
7
  poolConfig: PoolConfig;
9
- poolData: Pool;
8
+ pool: PoolAccount;
10
9
  lpTokenInfo: Mint;
11
10
  custodies: CustodyAccount[];
12
11
  totalPoolValueUsd: BN;
12
+ constructor(poolConfig: PoolConfig, pool: PoolAccount, lpTokenInfo: Mint, custodies: CustodyAccount[]);
13
+ loadCustodies(custodies: CustodyAccount[]): void;
14
+ loadPoolData(pool: PoolAccount): void;
15
+ loadlpData(lpTokenInfo: Mint): void;
16
+ getLpStats(prices: any): {
17
+ lpTokenSupply: BN;
18
+ decimals: number;
19
+ totalPoolValue: BN;
20
+ price: BN;
21
+ stableCoinPercentage: BN;
22
+ marketCap: BN;
23
+ };
24
+ getOiLongUI(): BN;
25
+ getOiShortUI(): BN;
26
+ getCustodyDetails(prices: any): any[];
27
+ getPoolStats(): {
28
+ totalFees: BN;
29
+ totalVolume: BN;
30
+ currentLongPositionsUsd: BN;
31
+ currentShortPositionsUsd: BN;
32
+ };
13
33
  }
@@ -1,9 +1,152 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PoolDisplayData = void 0;
4
+ var constants_1 = require("./constants");
5
+ var anchor_1 = require("@coral-xyz/anchor");
6
+ var utils_1 = require("./utils");
4
7
  var PoolDisplayData = /** @class */ (function () {
5
- function PoolDisplayData() {
8
+ function PoolDisplayData(poolConfig, pool, lpTokenInfo, custodies) {
9
+ this.poolConfig = poolConfig;
10
+ this.pool = pool;
11
+ this.lpTokenInfo = lpTokenInfo;
12
+ this.custodies = custodies;
13
+ this.totalPoolValueUsd = new anchor_1.BN(-1); // -1 meaning unset
6
14
  }
15
+ PoolDisplayData.prototype.loadCustodies = function (custodies) {
16
+ this.custodies = custodies;
17
+ };
18
+ PoolDisplayData.prototype.loadPoolData = function (pool) {
19
+ this.pool = pool;
20
+ };
21
+ PoolDisplayData.prototype.loadlpData = function (lpTokenInfo) {
22
+ this.lpTokenInfo = lpTokenInfo;
23
+ };
24
+ // TODO :: replace this with PoolAccount.getAssetsUnderManagementUsd()
25
+ // should take pnl's into account
26
+ PoolDisplayData.prototype.getLpStats = function (prices) {
27
+ var stableCoinAmount = new anchor_1.BN(0);
28
+ var totalPoolValueUsd = new anchor_1.BN(0);
29
+ var _loop_1 = function (custody) {
30
+ var custodyData = this_1.custodies.find(function (t) { return t.mint.toBase58() === custody.mintKey.toBase58(); });
31
+ if (custodyData) {
32
+ if (custodyData.isStable) {
33
+ stableCoinAmount = stableCoinAmount.add(custodyData.assets.owned);
34
+ }
35
+ var priceBN = new anchor_1.BN(prices.get(custody.symbol) * Math.pow(10, constants_1.PRICE_DECIMALS)); // so always keep prices with 6 decimals
36
+ var custodyValue = priceBN.mul(custodyData.assets.owned).div(new anchor_1.BN(Math.pow(10, custody.decimals)));
37
+ totalPoolValueUsd = totalPoolValueUsd.add(custodyValue);
38
+ }
39
+ };
40
+ var this_1 = this;
41
+ for (var _i = 0, _a = this.poolConfig.custodies; _i < _a.length; _i++) {
42
+ var custody = _a[_i];
43
+ _loop_1(custody);
44
+ }
45
+ // console.log("totalPoolValueUsd.toNumber():",totalPoolValueUsd.toString())
46
+ // console.log("stableCoinAmount.toNumber():",stableCoinAmount.toString())
47
+ // if(this.lpTokenInfo.supply.toString() =='0' || totalPoolValueUsd.toString()=='0'){
48
+ // console.error("supply or amt cannot be zero")
49
+ // throw "supply or amt cannot be zero";
50
+ // }
51
+ this.totalPoolValueUsd = totalPoolValueUsd;
52
+ var lpPrice = totalPoolValueUsd.div(new anchor_1.BN(this.lpTokenInfo.supply.toString() === '0' ? 1 : this.lpTokenInfo.supply.toString()));
53
+ return {
54
+ lpTokenSupply: new anchor_1.BN(this.lpTokenInfo.supply.toString()),
55
+ decimals: this.poolConfig.lpDecimals,
56
+ totalPoolValue: totalPoolValueUsd,
57
+ price: lpPrice,
58
+ stableCoinPercentage: totalPoolValueUsd.toNumber() != 0 ? stableCoinAmount.mul(new anchor_1.BN(constants_1.PERCENTAGE_DECIMALS)).div(totalPoolValueUsd) : new anchor_1.BN(1),
59
+ marketCap: lpPrice.mul(new anchor_1.BN(this.lpTokenInfo.supply.toString())),
60
+ // totalStaked : BN,
61
+ };
62
+ };
63
+ PoolDisplayData.prototype.getOiLongUI = function () {
64
+ var totalAmount = new anchor_1.BN('0');
65
+ this.custodies.forEach(function (i) {
66
+ totalAmount = totalAmount.add(i.tradeStats.oiLongUsd);
67
+ });
68
+ return totalAmount;
69
+ };
70
+ PoolDisplayData.prototype.getOiShortUI = function () {
71
+ var totalAmount = new anchor_1.BN('0');
72
+ this.custodies.forEach(function (i) {
73
+ totalAmount = totalAmount.add(i.tradeStats.oiShortUsd);
74
+ });
75
+ return totalAmount;
76
+ };
77
+ // handle decimal and this should accept a list of prices probs map or object
78
+ PoolDisplayData.prototype.getCustodyDetails = function (prices) {
79
+ var custodyDetails = [];
80
+ var _loop_2 = function (i) {
81
+ var custody = this_2.poolConfig.custodies[i];
82
+ if (!custody)
83
+ return "continue";
84
+ // console.log('this.pool :>> ', this.pool);
85
+ // const token = this.pool.tokens.find(t => t.custody.toBase58() === custody.custodyAccount.toBase58());
86
+ var tokenRatio = this_2.pool.ratios[i];
87
+ var custodyData = this_2.custodies.find(function (t) { return t.mint.toBase58() === custody.mintKey.toBase58(); });
88
+ var priceBN = new anchor_1.BN(prices.get(custody.symbol) * Math.pow(10, 6)); // so always keep prices with 6 decimals
89
+ if (this_2.totalPoolValueUsd.toString() == "-1") {
90
+ console.error("call getLpStats first");
91
+ throw "call getLpStats first";
92
+ }
93
+ // if(this.totalPoolValueUsd.toString()=='0'){
94
+ // console.error("call getLpStats first , totalPoolValueUsd ZERO")
95
+ // return defaultData.custodyDetails;
96
+ // }
97
+ // console.log("this.totalPoolValueUsd:",this.totalPoolValueUsd.toString())
98
+ if (custodyData && tokenRatio) {
99
+ custodyDetails.push({
100
+ symbol: custody.symbol,
101
+ price: new anchor_1.BN(prices.get(custody.symbol)),
102
+ targetWeight: tokenRatio.target,
103
+ currentWeight: this_2.totalPoolValueUsd.toNumber() ?
104
+ (custodyData.assets.owned.mul(priceBN)).mul(new anchor_1.BN(Math.pow(10, constants_1.PERCENTAGE_DECIMALS))).div(this_2.totalPoolValueUsd).div(new anchor_1.BN(Math.pow(10, custody.decimals)))
105
+ : new anchor_1.BN(0),
106
+ utilization: custodyData.assets.owned.toNumber() ?
107
+ (0, utils_1.toUiDecimals)(custodyData.assets.locked.mul(new anchor_1.BN(Math.pow(10, constants_1.PERCENTAGE_DECIMALS))).div(custodyData.assets.owned), constants_1.PERCENTAGE_DECIMALS, 2)
108
+ : '0',
109
+ // assetsAmountUi : (custodyData.assets.owned.toNumber() / 10**(custody.decimals)).toFixed(4),
110
+ assetsAmountUi: (0, utils_1.toUiDecimals)(custodyData.assets.owned, custody.decimals, 4, true),
111
+ // totalUsdAmountUi : ((custodyData.assets.owned.mul(priceBN)).div(new BN(10**(custody.decimals))).toNumber() / 10**6).toFixed(4),
112
+ totalUsdAmountUi: (0, utils_1.toUiDecimals)((custodyData.assets.owned.mul(priceBN)), custody.decimals + constants_1.PRICE_DECIMALS, 2, true),
113
+ });
114
+ }
115
+ };
116
+ var this_2 = this;
117
+ for (var i = 0; i < this.poolConfig.custodies.length; i++) {
118
+ _loop_2(i);
119
+ }
120
+ return custodyDetails;
121
+ };
122
+ PoolDisplayData.prototype.getPoolStats = function () {
123
+ var totalFees = new anchor_1.BN(0);
124
+ var totalVolume = new anchor_1.BN(0);
125
+ var currentLongPositionsUsd = new anchor_1.BN(0);
126
+ var currentShortPositionsUsd = new anchor_1.BN(0);
127
+ var _loop_3 = function (custody) {
128
+ var custodyData = this_3.custodies.find(function (t) { return t.mint.toBase58() === custody.mintKey.toBase58(); });
129
+ if (custodyData) {
130
+ var custodyFeeTotal = Object.values(custodyData.collectedFees).reduce(function (a, b) { return a.add(b); }, new anchor_1.BN(0));
131
+ totalFees = totalFees.add(custodyFeeTotal);
132
+ var custodyVolume = Object.values(custodyData.volumeStats).reduce(function (a, b) { return a.add(b); }, new anchor_1.BN(0));
133
+ totalVolume = totalVolume.add(custodyVolume);
134
+ currentLongPositionsUsd = currentLongPositionsUsd.add(custodyData.tradeStats.oiLongUsd);
135
+ currentShortPositionsUsd = currentShortPositionsUsd.add(custodyData.tradeStats.oiShortUsd);
136
+ }
137
+ };
138
+ var this_3 = this;
139
+ for (var _i = 0, _a = this.poolConfig.custodies; _i < _a.length; _i++) {
140
+ var custody = _a[_i];
141
+ _loop_3(custody);
142
+ }
143
+ return {
144
+ totalFees: totalFees,
145
+ totalVolume: totalVolume,
146
+ currentLongPositionsUsd: currentLongPositionsUsd,
147
+ currentShortPositionsUsd: currentShortPositionsUsd
148
+ };
149
+ };
7
150
  return PoolDisplayData;
8
- }()); // Pool
151
+ }()); // PoolDisplayData
9
152
  exports.PoolDisplayData = PoolDisplayData;
@@ -1,8 +1,7 @@
1
- /// <reference types="bn.js" />
2
1
  import { BN } from "@coral-xyz/anchor";
3
2
  import { PublicKey } from "@solana/web3.js";
4
3
  import { Position, Side } from "./types";
5
- export declare class PositionAccount {
4
+ export declare class PositionAccount implements Position {
6
5
  publicKey: PublicKey;
7
6
  owner: PublicKey;
8
7
  pool: PublicKey;
@@ -18,6 +17,7 @@ export declare class PositionAccount {
18
17
  cumulativeInterestSnapshot: BN;
19
18
  lockedAmount: BN;
20
19
  collateralAmount: BN;
20
+ bump: number;
21
21
  constructor(publicKey: PublicKey, parseData: Position);
22
22
  static from(publicKey: PublicKey, parseData: Position): PositionAccount;
23
23
  updatePositionData(position: Position): void;
@@ -50,7 +50,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.PositionAccount = void 0;
51
51
  var anchor_1 = require("@coral-xyz/anchor");
52
52
  var PositionAccount = /** @class */ (function () {
53
- // extra
54
53
  function PositionAccount(publicKey, parseData) {
55
54
  this.publicKey = publicKey;
56
55
  Object.assign(this, parseData);
@@ -1,16 +1,15 @@
1
- /// <reference types="bn.js" />
2
- /// <reference types="node" />
3
1
  import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
4
- import { PublicKey, Keypair } from "@solana/web3.js";
2
+ import { PublicKey } from "@solana/web3.js";
5
3
  import { PoolAccount } from "./PoolAccount";
6
4
  import { BorrowRateParams, Fees, OracleParams, Permissions, Position, PositionSide, PricingParams, TokenRatios } from "./types";
7
5
  import { OraclePrice } from "./OraclePrice";
8
- import CustodyAccount from "./CustodyAccount";
9
- import { Perpetuals } from "../../target/types/perpetuals";
6
+ import { CustodyAccount } from "./CustodyAccount";
7
+ import { Perpetuals } from "./target/types/perpetuals";
10
8
  export declare class PerpetualsClient {
11
9
  provider: AnchorProvider;
12
10
  program: Program<Perpetuals>;
13
- admin: Keypair;
11
+ admin: PublicKey;
12
+ programId: PublicKey;
14
13
  multisig: {
15
14
  publicKey: PublicKey;
16
15
  bump: number;
@@ -23,7 +22,7 @@ export declare class PerpetualsClient {
23
22
  publicKey: PublicKey;
24
23
  bump: number;
25
24
  };
26
- constructor(clusterUrl: string, adminKey: string);
25
+ constructor(provider: AnchorProvider, programId: PublicKey);
27
26
  findProgramAddress: (label: string, extraSeeds?: any) => {
28
27
  publicKey: PublicKey;
29
28
  bump: number;
@@ -1937,4 +1936,3 @@ export declare class PerpetualsClient {
1937
1936
  getSwapAmountAndFees: (poolName: string, tokenMintIn: PublicKey, tokenMintOut: PublicKey, amountIn: BN) => Promise<any>;
1938
1937
  getAum: (poolName: string) => Promise<any>;
1939
1938
  }
1940
- //# sourceMappingURL=PerpetualsClient.d.ts.map