hedge-web3 0.1.26 → 0.1.29

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.
Files changed (45) hide show
  1. package/declarations/Constants.d.ts +1 -1
  2. package/declarations/idl/vault.d.ts +1056 -768
  3. package/declarations/index.d.ts +2 -0
  4. package/declarations/instructions/depositVault.d.ts +1 -1
  5. package/declarations/instructions/liquidateVault.d.ts +1 -1
  6. package/declarations/instructions/loanVault.d.ts +1 -1
  7. package/declarations/instructions/redeemVault.d.ts +1 -1
  8. package/declarations/instructions/repayVault.d.ts +1 -1
  9. package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
  10. package/declarations/instructions/withdrawVault.d.ts +1 -1
  11. package/declarations/state/VaultAccount.d.ts +8 -0
  12. package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
  13. package/lib/Constants.js +3 -3
  14. package/lib/idl/vault.js +1054 -766
  15. package/lib/index.js +2 -0
  16. package/lib/instructions/createStakingPool.js +2 -2
  17. package/lib/instructions/depositVault.js +14 -7
  18. package/lib/instructions/liquidateVault.js +9 -4
  19. package/lib/instructions/loanVault.js +9 -4
  20. package/lib/instructions/redeemVault.js +7 -2
  21. package/lib/instructions/repayVault.js +9 -4
  22. package/lib/instructions/setVaultTypeStatus.js +38 -0
  23. package/lib/instructions/withdrawVault.js +12 -7
  24. package/lib/state/VaultAccount.js +54 -1
  25. package/lib/utils/Errors.js +2 -2
  26. package/lib/utils/getLinkedListAccounts.js +131 -0
  27. package/package.json +3 -1
  28. package/src/Constants.ts +73 -31
  29. package/src/idl/vault.ts +2075 -1499
  30. package/src/index.ts +3 -0
  31. package/src/instructions/createStakingPool.ts +1 -2
  32. package/src/instructions/depositVault.ts +97 -22
  33. package/src/instructions/liquidateVault.ts +118 -21
  34. package/src/instructions/loanVault.ts +91 -19
  35. package/src/instructions/redeemVault.ts +23 -0
  36. package/src/instructions/repayVault.ts +84 -19
  37. package/src/instructions/setVaultTypeStatus.ts +50 -0
  38. package/src/instructions/withdrawVault.ts +99 -21
  39. package/src/state/StakingPool.ts +0 -4
  40. package/src/state/VaultAccount.ts +86 -10
  41. package/src/utils/Errors.ts +2 -2
  42. package/src/utils/getLinkedListAccounts.ts +156 -0
  43. package/declarations/idl/idl.d.ts +0 -2
  44. package/lib/idl/idl.js +0 -1475
  45. package/src/idl/idl.ts +0 -1474
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ __exportStar(require("./instructions/refreshOraclePrice"), exports);
28
28
  __exportStar(require("./instructions/initHedgeFoundation"), exports);
29
29
  __exportStar(require("./instructions/initHedgeFoundationTokens"), exports);
30
30
  __exportStar(require("./instructions/setHalted"), exports);
31
+ __exportStar(require("./instructions/setVaultTypeStatus"), exports);
31
32
  __exportStar(require("./HedgeDecimal"), exports);
32
33
  __exportStar(require("./Constants"), exports);
33
34
  __exportStar(require("./state/VaultAccount"), exports);
@@ -37,3 +38,4 @@ __exportStar(require("./state/StakingPoolPosition"), exports);
37
38
  __exportStar(require("./state/LiquidationPoolEra"), exports);
38
39
  __exportStar(require("./state/LiquidationPoolState"), exports);
39
40
  __exportStar(require("./state/LiquidationPosition"), exports);
41
+ __exportStar(require("./utils/getLinkedListAccounts"), exports);
@@ -29,10 +29,10 @@ function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, he
29
29
  console.log("createStakingPoolInstruction program ID", program.programId.toString());
30
30
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
31
31
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
32
- const [poolPublickey, poolBump, poolSeedPhrase] = yield (0, Constants_1.getPoolPublicKeyForMint)(mintPublicKey);
32
+ const [poolPublickey, poolBump] = yield (0, Constants_1.getPoolPublicKeyForMint)(mintPublicKey);
33
33
  const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, mintPublicKey);
34
34
  const poolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, ushMintPublickey);
35
- return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor_1.BN(hedgeTokensToBeMinted), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), {
35
+ return program.instruction.createStakingPool(poolBump, new anchor_1.BN(hedgeTokensToBeMinted), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), {
36
36
  accounts: {
37
37
  signer: payerPublicKey,
38
38
  vaultSystemState: vaultSystemStatePublicKey,
@@ -14,6 +14,7 @@ const anchor_1 = require("@project-serum/anchor");
14
14
  const serum_1 = require("@project-serum/serum");
15
15
  const spl_token_1 = require("@solana/spl-token");
16
16
  const web3_js_1 = require("@solana/web3.js");
17
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
18
  const Constants_1 = require("../Constants");
18
19
  function depositVault(program, provider, payer, vaultPublicKey, depositAmount, overrideTime) {
19
20
  return __awaiter(this, void 0, void 0, function* () {
@@ -33,26 +34,29 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
33
34
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
34
35
  const transaction = new web3_js_1.Transaction();
35
36
  const signers = [payer, history];
37
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, 0, false, false);
36
38
  if (vaultAccount.collateralType === 'SOL') {
37
39
  transaction.add(web3_js_1.SystemProgram.createAccount({
38
40
  fromPubkey: payer.publicKey,
39
41
  lamports: depositAmount + 2.04e6,
40
42
  newAccountPubkey: wrappedSolAccount.publicKey,
41
43
  programId: spl_token_1.TOKEN_PROGRAM_ID,
42
- space: 165
44
+ space: 165,
43
45
  }), serum_1.TokenInstructions.initializeAccount({
44
46
  account: wrappedSolAccount.publicKey,
45
47
  mint: serum_1.TokenInstructions.WRAPPED_SOL_MINT,
46
- owner: payer.publicKey
48
+ owner: payer.publicKey,
47
49
  }));
48
50
  signers.push(wrappedSolAccount);
49
51
  }
50
- transaction.add(yield depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultAccount.collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, vaultTypeAccountInfo.collateralMint, ushMintPublickey, depositAmount, overrideTime));
52
+ transaction.add(yield depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultAccount.collateralType === 'SOL'
53
+ ? wrappedSolAccount.publicKey
54
+ : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, vaultTypeAccountInfo.collateralMint, ushMintPublickey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime));
51
55
  if (vaultAccount.collateralType === 'SOL') {
52
56
  transaction.add(serum_1.TokenInstructions.closeAccount({
53
57
  source: wrappedSolAccount.publicKey,
54
58
  destination: payer.publicKey,
55
- owner: payer.publicKey
59
+ owner: payer.publicKey,
56
60
  }));
57
61
  }
58
62
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, signers, provider.opts);
@@ -60,7 +64,7 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
60
64
  });
61
65
  }
62
66
  exports.depositVault = depositVault;
63
- function depositVaultInstruction(program, vaultSystemStatePublicKey, vaultOwner, vaultOwnerTokenAccount, vaultPublicKey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, ushMintPublickey, depositAmount, overrideTime) {
67
+ function depositVaultInstruction(program, vaultSystemStatePublicKey, vaultOwner, vaultOwnerTokenAccount, vaultPublicKey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, ushMintPublickey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime) {
64
68
  return __awaiter(this, void 0, void 0, function* () {
65
69
  return program.instruction.depositVault(new anchor_1.BN(depositAmount), new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
66
70
  {
@@ -77,10 +81,13 @@ function depositVaultInstruction(program, vaultSystemStatePublicKey, vaultOwner,
77
81
  vaultOwner: vaultOwner,
78
82
  vaultOwnerTokenAccount: vaultOwnerTokenAccount,
79
83
  ushMint: ushMintPublickey,
84
+ oldSmallerVaultInfo: oldSmallerPublicKey,
85
+ newSmallerVaultInfo: newSmallerPublicKey,
86
+ newLargerVaultInfo: newLargerPublicKey,
80
87
  systemProgram: web3_js_1.SystemProgram.programId,
81
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
88
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
82
89
  },
83
- signers: []
90
+ signers: [],
84
91
  });
85
92
  });
86
93
  }
@@ -13,6 +13,7 @@ exports.liquidateVaultInstruction = exports.liquidateVault = void 0;
13
13
  const anchor_1 = require("@project-serum/anchor");
14
14
  const spl_token_1 = require("@solana/spl-token");
15
15
  const web3_js_1 = require("@solana/web3.js");
16
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
16
17
  const Constants_1 = require("../Constants");
17
18
  function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime) {
18
19
  return __awaiter(this, void 0, void 0, function* () {
@@ -31,16 +32,17 @@ function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime)
31
32
  const poolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, liquidationPoolStatePublicKey, true);
32
33
  const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultTypeAccountPublicKey, true);
33
34
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, hedgeStakingPoolPublicKey, true);
35
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, 0, false, true);
34
36
  const history = web3_js_1.Keypair.generate();
35
37
  const newEra = web3_js_1.Keypair.generate();
36
38
  const transaction = new web3_js_1.Transaction();
37
- transaction.add(yield liquidateVaultInstruction(program, payer.publicKey, payerAssociatedTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, liquidationPoolStatePublicKey, poolStateInfo.currentEra, poolAssociatedTokenAccount.address, history.publicKey, newEra.publicKey, hedgeStakingPoolPublicKey, feePoolAssociatedTokenAccount.address, hedgeStakingPoolAssociatedUshTokenAccount.address, collateralMint, vaultTypeAssociatedTokenAccount.address, vaultAccount.collateralType, overrideTime));
39
+ transaction.add(yield liquidateVaultInstruction(program, payer.publicKey, payerAssociatedTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, liquidationPoolStatePublicKey, poolStateInfo.currentEra, poolAssociatedTokenAccount.address, history.publicKey, newEra.publicKey, hedgeStakingPoolPublicKey, feePoolAssociatedTokenAccount.address, hedgeStakingPoolAssociatedUshTokenAccount.address, collateralMint, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaultAccount.collateralType, overrideTime));
38
40
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history, newEra], provider.opts);
39
41
  return vaultPublicKey;
40
42
  });
41
43
  }
42
44
  exports.liquidateVault = liquidateVault;
43
- function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, collateralType, overrideTime) {
45
+ function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, collateralType, overrideTime) {
44
46
  return __awaiter(this, void 0, void 0, function* () {
45
47
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
46
48
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -66,12 +68,15 @@ function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedToken
66
68
  feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
67
69
  liquidationPoolUshAccount: liquidationPoolUshAccountPublickey,
68
70
  newEra: newEraPublicKey,
71
+ oldSmallerVaultInfo: oldSmallerPublicKey,
72
+ newSmallerVaultInfo: newSmallerPublicKey,
73
+ newLargerVaultInfo: newLargerPublicKey,
69
74
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
70
75
  systemProgram: web3_js_1.SystemProgram.programId,
71
76
  associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
72
- rent: web3_js_1.SYSVAR_RENT_PUBKEY
77
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
73
78
  },
74
- signers: []
79
+ signers: [],
75
80
  };
76
81
  return program.instruction.liquidateVault(new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
77
82
  payload);
@@ -13,6 +13,7 @@ exports.loanVaultInstruction = exports.loanVault = void 0;
13
13
  const anchor_1 = require("@project-serum/anchor");
14
14
  const spl_token_1 = require("@solana/spl-token");
15
15
  const web3_js_1 = require("@solana/web3.js");
16
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
16
17
  const Constants_1 = require("../Constants");
17
18
  function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrideTime) {
18
19
  return __awaiter(this, void 0, void 0, function* () {
@@ -23,14 +24,15 @@ function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrid
23
24
  const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
24
25
  const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultTypeAccountPublicKey, true);
25
26
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true);
27
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, loanAmount, false, false);
26
28
  const history = web3_js_1.Keypair.generate();
27
- const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, loanAmount, overrideTime));
29
+ const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime));
28
30
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history], provider.opts);
29
31
  return vaultPublicKey;
30
32
  });
31
33
  }
32
34
  exports.loanVault = loanVault;
33
- function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, loanAmount, overrideTime) {
35
+ function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime) {
34
36
  return __awaiter(this, void 0, void 0, function* () {
35
37
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
36
38
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -51,10 +53,13 @@ function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPub
51
53
  ushMint: ushMintPublickey,
52
54
  vaultOwner: payerPublicKey,
53
55
  ownerUshAccount: ownerUshAccount,
56
+ oldSmallerVaultInfo: oldSmallerPublicKey,
57
+ newSmallerVaultInfo: newSmallerPublicKey,
58
+ newLargerVaultInfo: newLargerPublicKey,
54
59
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
55
- systemProgram: web3_js_1.SystemProgram.programId
60
+ systemProgram: web3_js_1.SystemProgram.programId,
56
61
  },
57
- signers: []
62
+ signers: [],
58
63
  });
59
64
  });
60
65
  }
@@ -14,6 +14,7 @@ const anchor_1 = require("@project-serum/anchor");
14
14
  const spl_token_1 = require("@solana/spl-token");
15
15
  // import { TokenInstructions } from '@project-serum/serum'
16
16
  const web3_js_1 = require("@solana/web3.js");
17
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
18
  const Constants_1 = require("../Constants");
18
19
  function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, transactionOverrideTime) {
19
20
  return __awaiter(this, void 0, void 0, function* () {
@@ -26,14 +27,15 @@ function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, tra
26
27
  const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
27
28
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true);
28
29
  const payerTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
30
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, 0, true, false);
29
31
  const history = web3_js_1.Keypair.generate();
30
- const transaction = new web3_js_1.Transaction().add(yield redeemVaultInstruction(program, payer.publicKey, payerUshAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, redeemAmount, transactionOverrideTime));
32
+ const transaction = new web3_js_1.Transaction().add(yield redeemVaultInstruction(program, payer.publicKey, payerUshAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime));
31
33
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history], provider.opts);
32
34
  return vaultPublicKey;
33
35
  });
34
36
  }
35
37
  exports.redeemVault = redeemVault;
36
- function redeemVaultInstruction(program, payerPublicKey, payerUshAccount, destinationTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, redeemAmount, transactionOverrideTime) {
38
+ function redeemVaultInstruction(program, payerPublicKey, payerUshAccount, destinationTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime) {
37
39
  return __awaiter(this, void 0, void 0, function* () {
38
40
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
39
41
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -55,6 +57,9 @@ function redeemVaultInstruction(program, payerPublicKey, payerUshAccount, destin
55
57
  payer: payerPublicKey,
56
58
  payerUshAccount: payerUshAccount,
57
59
  destinationTokenAccount: destinationTokenAccount,
60
+ oldSmallerVaultInfo: oldSmallerPublicKey,
61
+ newSmallerVaultInfo: newSmallerPublicKey,
62
+ newLargerVaultInfo: newLargerPublicKey,
58
63
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
59
64
  systemProgram: web3_js_1.SystemProgram.programId
60
65
  },
@@ -13,6 +13,7 @@ exports.repayVaultInstruction = exports.repayVault = void 0;
13
13
  const anchor_1 = require("@project-serum/anchor");
14
14
  const spl_token_1 = require("@solana/spl-token");
15
15
  const web3_js_1 = require("@solana/web3.js");
16
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
16
17
  const Constants_1 = require("../Constants");
17
18
  function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overrideTime) {
18
19
  return __awaiter(this, void 0, void 0, function* () {
@@ -24,14 +25,15 @@ function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overr
24
25
  const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
26
  const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint);
26
27
  const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccount.collateralMint);
28
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, 0, repayAmount * -1, false, false);
27
29
  const history = web3_js_1.Keypair.generate();
28
- const transaction = new web3_js_1.Transaction().add(yield repayVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount, repayAmount, overrideTime));
30
+ const transaction = new web3_js_1.Transaction().add(yield repayVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, repayAmount, overrideTime));
29
31
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history], provider.opts);
30
32
  return vaultPublicKey;
31
33
  });
32
34
  }
33
35
  exports.repayVault = repayVault;
34
- function repayVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, repayAmount, overrideTime) {
36
+ function repayVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, repayAmount, overrideTime) {
35
37
  return __awaiter(this, void 0, void 0, function* () {
36
38
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
37
39
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -51,11 +53,14 @@ function repayVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPu
51
53
  feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
52
54
  ushMint: ushMintPublickey,
53
55
  vaultOwner: payerPublicKey,
56
+ oldSmallerVaultInfo: oldSmallerPublicKey,
57
+ newSmallerVaultInfo: newSmallerPublicKey,
58
+ newLargerVaultInfo: newLargerPublicKey,
54
59
  ownerUshAccount: ownerUshAccount,
55
60
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
56
- systemProgram: web3_js_1.SystemProgram.programId
61
+ systemProgram: web3_js_1.SystemProgram.programId,
57
62
  },
58
- signers: []
63
+ signers: [],
59
64
  });
60
65
  });
61
66
  }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.setVaultTypeStatusInstruction = exports.setVaultTypeStatus = void 0;
13
+ const web3_js_1 = require("@solana/web3.js");
14
+ const Constants_1 = require("../Constants");
15
+ const Errors_1 = require("../utils/Errors");
16
+ function setVaultTypeStatus(program, provider, payer, vaultTypeAccount, deprecated) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
19
+ const transaction = new web3_js_1.Transaction().add(yield setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated));
20
+ yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer], provider === null || provider === void 0 ? void 0 : provider.opts).catch(Errors_1.parseAnchorErrors);
21
+ return vaultSystemStatePublicKey;
22
+ });
23
+ }
24
+ exports.setVaultTypeStatus = setVaultTypeStatus;
25
+ function setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultTypeAccount, deprecated) {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ const ix = program.instruction.setVaultTypeStatus(deprecated, {
28
+ accounts: {
29
+ payer: payerPublicKey,
30
+ vaultSystemState: vaultSystemStatePublicKey,
31
+ vaultType: vaultTypeAccount,
32
+ },
33
+ signers: []
34
+ });
35
+ return ix;
36
+ });
37
+ }
38
+ exports.setVaultTypeStatusInstruction = setVaultTypeStatusInstruction;
@@ -15,6 +15,7 @@ const spl_token_1 = require("@solana/spl-token");
15
15
  const serum_1 = require("@project-serum/serum");
16
16
  const web3_js_1 = require("@solana/web3.js");
17
17
  const Constants_1 = require("../Constants");
18
+ const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
18
19
  function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, overrideTime) {
19
20
  return __awaiter(this, void 0, void 0, function* () {
20
21
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -23,20 +24,21 @@ function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount,
23
24
  const history = web3_js_1.Keypair.generate();
24
25
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
25
26
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
26
- const vaultTypeAccount = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
27
+ const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
27
28
  const vaultAssociatedCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, serum_1.TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true);
28
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccount);
29
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccount, true);
29
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
30
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
30
31
  const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
31
32
  const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
32
33
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
33
- const transaction = new web3_js_1.Transaction().add(yield withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount.address, vaultTypeAccount, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMintPublickey, history.publicKey, withdrawAmount, overrideTime));
34
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, withdrawAmount * -1, 0, false, false);
35
+ const transaction = new web3_js_1.Transaction().add(yield withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount.address, vaultTypeAccountPublicKey, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMintPublickey, history.publicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime));
34
36
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history], provider.opts);
35
37
  return vaultPublicKey;
36
38
  });
37
39
  }
38
40
  exports.withdrawVault = withdrawVault;
39
- function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMint, historyPublicKey, withdrawAmount, overrideTime) {
41
+ function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMint, historyPublicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime) {
40
42
  return __awaiter(this, void 0, void 0, function* () {
41
43
  return program.instruction.withdrawVault(new anchor_1.BN(withdrawAmount), new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
42
44
  {
@@ -52,10 +54,13 @@ function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPubli
52
54
  history: historyPublicKey,
53
55
  vaultOwner: payerPublicKey,
54
56
  destinationTokenAccount: destinationTokenAccount,
57
+ oldSmallerVaultInfo: oldSmallerPublicKey,
58
+ newSmallerVaultInfo: newSmallerPublicKey,
59
+ newLargerVaultInfo: newLargerPublicKey,
55
60
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
56
- systemProgram: web3_js_1.SystemProgram.programId
61
+ systemProgram: web3_js_1.SystemProgram.programId,
57
62
  },
58
- signers: []
63
+ signers: [],
59
64
  });
60
65
  });
61
66
  }
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.VaultAccount = void 0;
4
7
  const web3_js_1 = require("@solana/web3.js");
8
+ const decimal_js_1 = __importDefault(require("decimal.js"));
5
9
  const HedgeDecimal_1 = require("../HedgeDecimal");
6
10
  /**
7
11
  * A class that represents an on-chian vault.
@@ -10,6 +14,7 @@ class VaultAccount {
10
14
  constructor(vault, publicKey) {
11
15
  this.publicKey = publicKey;
12
16
  this.vaultOwner = vault.vaultOwner;
17
+ this.vaultNumber = vault.vaultNumber.toNumber();
13
18
  this.pdaSalt = vault.pdaSalt;
14
19
  this.deposited = vault.deposited.toNumber();
15
20
  this.denormalizedDebt = vault.denormalizedDebt.toNumber();
@@ -49,7 +54,55 @@ class VaultAccount {
49
54
  * @returns example: `1b6ca...azy71s`
50
55
  */
51
56
  toDisplayString() {
52
- return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`;
57
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey
58
+ .toString()
59
+ .substring(this.publicKey.toString().length - 6)}`;
60
+ }
61
+ addDebt(newNormalizedDebt, vaultTypeCompoundedInterest) {
62
+ const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest);
63
+ this.denormalizedDebt += denormalizedNewDebt.toNumber();
64
+ }
65
+ addDeposit(depositAmount) {
66
+ this.deposited += depositAmount;
67
+ }
68
+ redeem() {
69
+ // TODO - Calculate actual redeem amount and adust correctly
70
+ this.denormalizedDebt = 0;
71
+ this.vaultStatus = 'initialized';
72
+ }
73
+ liquidate() {
74
+ // TODO - Calculate actual liquidate amount and adust correctly
75
+ this.denormalizedDebt = 0;
76
+ this.vaultStatus = 'liquidated';
77
+ }
78
+ redistribution(vaultTypeAccuntData) {
79
+ const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.debtRedistributionProduct);
80
+ const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.collateralRedistributionAccumulator);
81
+ this.denormalizedDebt = debtProductCurrent
82
+ .div(this.debtProductSnapshotBytes)
83
+ .mul(new decimal_js_1.default(this.denormalizedDebt))
84
+ // .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
85
+ .toNumber();
86
+ const extraCollateralDeposited = this.denormalizedDebt *
87
+ collateralAccumulatorCurrent
88
+ .sub(this.collateralAccumulatorSnapshotBytes)
89
+ .toNumber();
90
+ this.deposited += extraCollateralDeposited;
91
+ this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent;
92
+ this.debtProductSnapshotBytes = debtProductCurrent;
93
+ }
94
+ toString(highlight) {
95
+ let arrow = '';
96
+ if (this.publicKey.toString() === highlight.toString()) {
97
+ arrow = ' <----';
98
+ }
99
+ let collateralRatio = 'Infinite';
100
+ if (this.denormalizedDebt > 0) {
101
+ collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8);
102
+ }
103
+ return `Vault(${this.vaultNumber}): ${this.publicKey
104
+ .toString()
105
+ .substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`;
53
106
  }
54
107
  }
55
108
  exports.VaultAccount = VaultAccount;
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseAnchorErrors = void 0;
4
4
  const anchor_1 = require("@project-serum/anchor");
5
- const idl_1 = require("../idl/idl");
5
+ const vault_1 = require("../idl/vault");
6
6
  function parseAnchorErrors(error) {
7
- const idlErrors = (0, anchor_1.parseIdlErrors)(idl_1.vaultIdl);
7
+ const idlErrors = (0, anchor_1.parseIdlErrors)(vault_1.IDL);
8
8
  const parsedError = anchor_1.ProgramError.parse(error, idlErrors);
9
9
  if (parsedError !== null) {
10
10
  throw parsedError;
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.getLinkedListAccounts = void 0;
16
+ const underscore_1 = __importDefault(require("underscore"));
17
+ const HedgeDecimal_1 = require("../HedgeDecimal");
18
+ const VaultAccount_1 = require("../state/VaultAccount");
19
+ const decimal_js_1 = __importDefault(require("decimal.js"));
20
+ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
23
+ // Default for null is the vault itself, so set them all to this vault
24
+ let oldSmallerPublicKey = vaultPublicKey;
25
+ let newSmallerPublicKey = vaultPublicKey;
26
+ let newLargerPublicKey = vaultPublicKey;
27
+ const thisVaultData = yield program.account.vault.fetch(vaultPublicKey);
28
+ const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
29
+ // Load all the vaults
30
+ let allVaults = (yield program.account.vault
31
+ .all([
32
+ // {
33
+ // memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
34
+ // },
35
+ ])
36
+ .catch((error) => {
37
+ console.log('error', error);
38
+ })) || [];
39
+ // Load them into our account objects
40
+ let vaults = allVaults.map((vault) => {
41
+ return new VaultAccount_1.VaultAccount(vault.account, vault.publicKey);
42
+ });
43
+ // Filter out the account that are not the same vault type
44
+ vaults = underscore_1.default.filter(vaults, (vault) => {
45
+ return vault.collateralType === vaultTypeAccount.collateralType;
46
+ });
47
+ // Filter out the accounts that are not open
48
+ vaults = underscore_1.default.filter(vaults, (vault) => {
49
+ return vault.vaultStatus === 'open';
50
+ });
51
+ // Sort them
52
+ vaults.sort(sortVaults);
53
+ // Find the location of the vault before the operation
54
+ let indexBefore = -1;
55
+ vaults.forEach((vault, index) => {
56
+ if (vault.publicKey.toString() === vaultPublicKey.toString()) {
57
+ indexBefore = index;
58
+ }
59
+ });
60
+ // If we found it before, set the old smaller vault the one to the left
61
+ if (indexBefore > 0) {
62
+ oldSmallerPublicKey = vaults[indexBefore - 1].publicKey;
63
+ }
64
+ // Pretty print all the vaults before the operation
65
+ // console.log('Sorted open vaults before')
66
+ // vaults.forEach((vault) => {
67
+ // console.log(vault.toString(vaultPublicKey))
68
+ // })
69
+ // If it wasn't in the list, add it now
70
+ if (indexBefore < 0) {
71
+ vaults.push(thisVault);
72
+ indexBefore = vaults.length - 1;
73
+ }
74
+ // Now that we know it's def in the list, iterate the list and update
75
+ // this vault with the opeation we're going to apply
76
+ const newNormalizedDebt = new decimal_js_1.default(loanAmount);
77
+ const vaultTypeCompoundedInterest = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccount.cumulativeRate.toString());
78
+ vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest);
79
+ vaults[indexBefore].addDeposit(depositAmount);
80
+ if (liquidate) {
81
+ vaults[indexBefore].liquidate();
82
+ }
83
+ if (redeem) {
84
+ vaults[indexBefore].redeem();
85
+ }
86
+ vaults[indexBefore].redistribution(vaultTypeAccount);
87
+ if (vaults[indexBefore].denormalizedDebt === 0) {
88
+ vaults.splice(indexBefore, 1);
89
+ }
90
+ // Sort it again since we've changed one vault
91
+ vaults = vaults.sort(sortVaults);
92
+ // Pretty print the list again
93
+ // console.log('Sorted open vaults with new debt added')
94
+ // vaults.forEach((vault) => {
95
+ // console.log(vault.toString(vaultPublicKey))
96
+ // })
97
+ // Search for the vaults new position
98
+ let indexAfter = -1;
99
+ vaults.forEach((vault, index) => {
100
+ if (vault.publicKey.toString() === vaultPublicKey.toString()) {
101
+ indexAfter = index;
102
+ }
103
+ });
104
+ // Print where it moved from / to
105
+ // console.log('Index Before', indexBefore)
106
+ // console.log('Index After', indexAfter)
107
+ // Save references to the new left and right
108
+ if (indexAfter > 0) {
109
+ newSmallerPublicKey = vaults[indexAfter - 1].publicKey;
110
+ }
111
+ if (indexAfter < vaults.length - 1 && indexAfter >= 0) {
112
+ newLargerPublicKey = vaults[indexAfter + 1].publicKey;
113
+ }
114
+ // Print out the new left/right
115
+ // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
116
+ // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
117
+ // console.log('newLargerPublicKey', newLargerPublicKey.toString())
118
+ return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey];
119
+ });
120
+ }
121
+ exports.getLinkedListAccounts = getLinkedListAccounts;
122
+ // Sort function we can use to sort the vaults
123
+ // Sorted by collateral ratio. If two are the same, newer vault first
124
+ function sortVaults(a, b) {
125
+ const aRatio = a.deposited / a.denormalizedDebt;
126
+ const bRatio = b.deposited / b.denormalizedDebt;
127
+ if (aRatio === bRatio) {
128
+ return b.vaultNumber - a.vaultNumber;
129
+ }
130
+ return aRatio - bRatio;
131
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.26",
3
+ "version": "0.1.29",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
@@ -21,6 +21,7 @@
21
21
  "@solana/spl-token": "^0.2.0",
22
22
  "@solana/web3.js": "^1.37.0",
23
23
  "@types/bn.js": "^5.1.0",
24
+ "@types/underscore": "^1.11.4",
24
25
  "@types/uuid": "^8.3.4",
25
26
  "bn.js": "^5.2.0",
26
27
  "decimal.js": "^10.3.1",
@@ -28,6 +29,7 @@
28
29
  "ts-standard": "^11.0.0",
29
30
  "typedoc": "^0.22.10",
30
31
  "typescript": "^4.5.5",
32
+ "underscore": "^1.13.2",
31
33
  "uuid": "^8.3.2"
32
34
  }
33
35
  }