hedge-web3 0.1.50 → 0.2.2

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 (77) hide show
  1. package/declarations/Constants.d.ts +2 -1
  2. package/declarations/idl/vault.d.ts +192 -41
  3. package/declarations/index.d.ts +1 -0
  4. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +1 -1
  5. package/declarations/instructions/claimStakingPoolPosition.d.ts +1 -1
  6. package/declarations/instructions/closeClaimedLiquidationPoolPosition.d.ts +5 -0
  7. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
  8. package/declarations/instructions/createStakingPool.d.ts +1 -1
  9. package/declarations/instructions/createVault.d.ts +1 -1
  10. package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
  11. package/declarations/instructions/depositStakingPool.d.ts +1 -1
  12. package/declarations/instructions/depositVault.d.ts +1 -1
  13. package/declarations/instructions/initHedgeFoundation.d.ts +1 -1
  14. package/declarations/instructions/liquidateVault.d.ts +2 -2
  15. package/declarations/instructions/loanVault.d.ts +1 -1
  16. package/declarations/instructions/redeemVault.d.ts +1 -1
  17. package/declarations/instructions/refreshOraclePrice.d.ts +3 -3
  18. package/declarations/instructions/repayVault.d.ts +1 -1
  19. package/declarations/instructions/setHalted.d.ts +1 -1
  20. package/declarations/instructions/updateVaultType.d.ts +4 -1
  21. package/declarations/instructions/withdrawStakingPool.d.ts +1 -1
  22. package/declarations/instructions/withdrawVault.d.ts +1 -1
  23. package/declarations/state/VaultAccount.d.ts +9 -6
  24. package/declarations/utils/getLinkedListAccounts.d.ts +1 -1
  25. package/lib/Constants.js +3 -2
  26. package/lib/idl/vault.js +192 -41
  27. package/lib/index.js +1 -0
  28. package/lib/instructions/claimLiquidationPoolPosition.js +5 -1
  29. package/lib/instructions/claimStakingPoolPosition.js +5 -1
  30. package/lib/instructions/closeClaimedLiquidationPoolPosition.js +38 -0
  31. package/lib/instructions/closeLiquidationPoolPosition.js +5 -1
  32. package/lib/instructions/createStakingPool.js +5 -1
  33. package/lib/instructions/createVault.js +5 -1
  34. package/lib/instructions/depositLiquidationPool.js +5 -1
  35. package/lib/instructions/depositStakingPool.js +5 -1
  36. package/lib/instructions/depositVault.js +12 -8
  37. package/lib/instructions/initHedgeFoundation.js +5 -1
  38. package/lib/instructions/initHedgeFoundationTokens.js +5 -1
  39. package/lib/instructions/liquidateVault.js +10 -8
  40. package/lib/instructions/loanVault.js +9 -6
  41. package/lib/instructions/redeemVault.js +9 -6
  42. package/lib/instructions/refreshOraclePrice.js +8 -3
  43. package/lib/instructions/repayVault.js +9 -6
  44. package/lib/instructions/setHalted.js +5 -1
  45. package/lib/instructions/updateVaultType.js +9 -2
  46. package/lib/instructions/withdrawStakingPool.js +5 -1
  47. package/lib/instructions/withdrawVault.js +9 -6
  48. package/lib/state/VaultAccount.js +28 -32
  49. package/lib/utils/getLinkedListAccounts.js +54 -57
  50. package/package.json +1 -1
  51. package/src/Constants.ts +2 -1
  52. package/src/idl/vault.ts +384 -82
  53. package/src/index.ts +1 -0
  54. package/src/instructions/claimLiquidationPoolPosition.ts +3 -2
  55. package/src/instructions/claimStakingPoolPosition.ts +3 -2
  56. package/src/instructions/closeClaimedLiquidationPoolPosition.ts +31 -0
  57. package/src/instructions/closeLiquidationPoolPosition.ts +3 -2
  58. package/src/instructions/createStakingPool.ts +3 -2
  59. package/src/instructions/createVault.ts +3 -2
  60. package/src/instructions/depositLiquidationPool.ts +3 -2
  61. package/src/instructions/depositStakingPool.ts +3 -2
  62. package/src/instructions/depositVault.ts +11 -11
  63. package/src/instructions/initHedgeFoundation.ts +3 -2
  64. package/src/instructions/initHedgeFoundationTokens.ts +2 -1
  65. package/src/instructions/liquidateVault.ts +11 -10
  66. package/src/instructions/loanVault.ts +11 -9
  67. package/src/instructions/redeemVault.ts +7 -7
  68. package/src/instructions/refreshOraclePrice.ts +9 -6
  69. package/src/instructions/repayVault.ts +7 -7
  70. package/src/instructions/setHalted.ts +3 -2
  71. package/src/instructions/updateVaultType.ts +10 -2
  72. package/src/instructions/withdrawStakingPool.ts +5 -3
  73. package/src/instructions/withdrawVault.ts +7 -7
  74. package/src/state/VaultAccount.ts +37 -32
  75. package/src/state/VaultType.ts +62 -0
  76. package/src/utils/getLinkedListAccounts.ts +61 -60
  77. package/src/utils/sendAndConfirmWithDebug.ts +27 -0
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.depositVaultInstruction = exports.depositVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -16,15 +19,16 @@ const spl_token_1 = require("@solana/spl-token");
16
19
  const web3_js_1 = require("@solana/web3.js");
17
20
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
18
21
  const Constants_1 = require("../Constants");
22
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
23
+ const token_instructions_1 = require("@project-serum/serum/lib/token-instructions");
19
24
  function depositVault(program, provider, payer, vaultPublicKey, depositAmount, overrideTime) {
20
25
  return __awaiter(this, void 0, void 0, function* () {
21
26
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
22
27
  // Prep the user to get USH back out at some point
23
28
  yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
24
29
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
25
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
26
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
27
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
30
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
31
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
28
32
  const payerTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payer.publicKey, vaultTypeAccountInfo.collateralMint);
29
33
  const vaultAssociatedCollateralAccountPublicKey = yield (0, Constants_1.findAssociatedTokenAddress)(vaultPublicKey, vaultTypeAccountInfo.collateralMint);
30
34
  const history = web3_js_1.Keypair.generate();
@@ -34,8 +38,8 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
34
38
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
35
39
  const transaction = new web3_js_1.Transaction();
36
40
  const signers = [payer, history];
37
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, 0, false, false);
38
- if (vaultAccount.collateralType === 'SOL') {
41
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, depositAmount, 0, false, false);
42
+ if (vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString()) {
39
43
  transaction.add(web3_js_1.SystemProgram.createAccount({
40
44
  fromPubkey: payer.publicKey,
41
45
  lamports: depositAmount + 2.04e6,
@@ -49,15 +53,15 @@ function depositVault(program, provider, payer, vaultPublicKey, depositAmount, o
49
53
  }));
50
54
  signers.push(wrappedSolAccount);
51
55
  }
52
- 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, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime));
53
- if (vaultAccount.collateralType === 'SOL') {
56
+ transaction.add(yield depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString() ? wrappedSolAccount.publicKey : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, vaultTypeAccountInfo.collateralMint, ushMintPublickey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, depositAmount, overrideTime));
57
+ if (vaultTypeAccountInfo.collateralMint.toString() === token_instructions_1.WRAPPED_SOL_MINT.toString()) {
54
58
  transaction.add(serum_1.TokenInstructions.closeAccount({
55
59
  source: wrappedSolAccount.publicKey,
56
60
  destination: payer.publicKey,
57
61
  owner: payer.publicKey,
58
62
  }));
59
63
  }
60
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, signers);
64
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, signers);
61
65
  return vaultPublicKey;
62
66
  });
63
67
  }
@@ -8,17 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.initHedgeFoundationInstruction = exports.initHedgeFoundation = void 0;
13
16
  const spl_token_1 = require("@solana/spl-token");
14
17
  const web3_js_1 = require("@solana/web3.js");
15
18
  const Errors_1 = require("../utils/Errors");
16
19
  const Constants_1 = require("../Constants");
20
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
17
21
  function initHedgeFoundation(program, provider, payer) {
18
22
  return __awaiter(this, void 0, void 0, function* () {
19
23
  const poolEra = web3_js_1.Keypair.generate();
20
24
  const transaction = new web3_js_1.Transaction().add(yield initHedgeFoundationInstruction(program, poolEra.publicKey, payer.publicKey));
21
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, poolEra]).catch(Errors_1.parseAnchorErrors);
25
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, poolEra]).catch(Errors_1.parseAnchorErrors);
22
26
  return payer.publicKey;
23
27
  });
24
28
  }
@@ -8,17 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.initHedgeFoundationTokensInstruction = exports.initHedgeFoundationTokens = void 0;
13
16
  const spl_token_1 = require("@solana/spl-token");
14
17
  const web3_js_1 = require("@solana/web3.js");
15
18
  const Errors_1 = require("../utils/Errors");
16
19
  const Constants_1 = require("../Constants");
20
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
17
21
  function initHedgeFoundationTokens(program, provider, payer) {
18
22
  return __awaiter(this, void 0, void 0, function* () {
19
23
  const poolEra = web3_js_1.Keypair.generate();
20
24
  const transaction = new web3_js_1.Transaction().add(yield initHedgeFoundationTokensInstruction(program, payer.publicKey));
21
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
25
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
22
26
  return payer.publicKey;
23
27
  });
24
28
  }
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.liquidateVaultInstruction = exports.liquidateVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -15,11 +18,11 @@ const spl_token_1 = require("@solana/spl-token");
15
18
  const web3_js_1 = require("@solana/web3.js");
16
19
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
20
  const Constants_1 = require("../Constants");
21
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
18
22
  function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime) {
19
23
  return __awaiter(this, void 0, void 0, function* () {
20
24
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
21
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
22
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
23
26
  const collateralMint = vaultTypeAccountInfo.collateralMint;
24
27
  const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
25
28
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -30,24 +33,23 @@ function liquidateVault(program, provider, payer, vaultPublicKey, overrideTime)
30
33
  const feePoolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, hedgeStakingPoolPublicKey, true);
31
34
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultPublicKey, true);
32
35
  const poolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, liquidationPoolStatePublicKey, true);
33
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultTypeAccountPublicKey, true);
36
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, vaultAccount.vaultType, true);
34
37
  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);
38
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, 0, false, true);
36
39
  const history = web3_js_1.Keypair.generate();
37
40
  const newEra = web3_js_1.Keypair.generate();
38
41
  const transaction = new web3_js_1.Transaction();
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));
40
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history, newEra]);
42
+ 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.vaultType, overrideTime));
43
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history, newEra]);
41
44
  return vaultPublicKey;
42
45
  });
43
46
  }
44
47
  exports.liquidateVault = liquidateVault;
45
- function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, collateralType, overrideTime) {
48
+ function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, hedgeStakingPoolAssociatedUshTokenAccount, collateralMint, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaultTypeAccount, overrideTime) {
46
49
  return __awaiter(this, void 0, void 0, function* () {
47
50
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
48
51
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
49
52
  const liquidationPoolUshAccountPublickey = yield (0, Constants_1.getLiquidationPoolUshAccountPublicKey)();
50
- const vaultTypeAccount = yield (0, Constants_1.getVaultTypeAccountPublicKey)(collateralType);
51
53
  return yield program.methods
52
54
  .liquidateVault(new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
53
55
  )
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.loanVaultInstruction = exports.loanVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -15,19 +18,19 @@ const spl_token_1 = require("@solana/spl-token");
15
18
  const web3_js_1 = require("@solana/web3.js");
16
19
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
20
  const Constants_1 = require("../Constants");
21
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
18
22
  function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrideTime) {
19
23
  return __awaiter(this, void 0, void 0, function* () {
20
24
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
21
25
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
22
26
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
23
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
24
- const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultTypeAccountPublicKey, true);
27
+ const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
28
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultAccount.vaultType, true);
26
29
  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);
30
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, loanAmount, false, false);
28
31
  const history = web3_js_1.Keypair.generate();
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));
30
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
32
+ const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime));
33
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
31
34
  return vaultPublicKey;
32
35
  });
33
36
  }
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.redeemVaultInstruction = exports.redeemVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -16,21 +19,21 @@ const spl_token_1 = require("@solana/spl-token");
16
19
  const web3_js_1 = require("@solana/web3.js");
17
20
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
18
21
  const Constants_1 = require("../Constants");
22
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
23
  function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, transactionOverrideTime) {
20
24
  return __awaiter(this, void 0, void 0, function* () {
21
25
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
22
26
  // Prep the user to get USH back out at some point
23
27
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
24
28
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
25
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
26
- const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
27
- const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true);
29
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
30
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
28
31
  const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true);
29
32
  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);
33
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, 0, true, false);
31
34
  const history = web3_js_1.Keypair.generate();
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));
33
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
35
+ const transaction = new web3_js_1.Transaction().add(yield redeemVaultInstruction(program, payer.publicKey, payerUshAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, redeemAmount, transactionOverrideTime));
36
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
34
37
  return vaultPublicKey;
35
38
  });
36
39
  }
@@ -8,15 +8,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.Cluster = exports.refreshOraclePriceInstruction = exports.refreshOraclePrice = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
14
17
  const web3_js_1 = require("@solana/web3.js");
18
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
15
19
  const Constants_1 = require("../Constants");
16
20
  function refreshOraclePrice(program, provider, payer, collateralType, network, overridePrice, overrideTime) {
17
21
  return __awaiter(this, void 0, void 0, function* () {
18
22
  const transaction = new web3_js_1.Transaction().add(yield refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime));
19
- return yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]);
23
+ return yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
20
24
  });
21
25
  }
22
26
  exports.refreshOraclePrice = refreshOraclePrice;
@@ -32,10 +36,11 @@ function refreshOraclePriceInstruction(program, collateralType, network, overrid
32
36
  .accounts({
33
37
  oracleInfoAccount: oracleInfoAccount,
34
38
  vaultTypeAccount: vaultTypeAccount,
35
- oracleChainlink: chainlinkAccunts[network],
39
+ oracleChainlink: chainlinkAccounts[network],
36
40
  oraclePyth: pythAccounts[network],
37
41
  oracleSwitchboard: switchboardAccounts[network],
38
42
  systemProgram: web3_js_1.SystemProgram.programId,
43
+ chainlinkProgram: Constants_1.CHAINLINK_PROGRAM_ID,
39
44
  })
40
45
  .instruction();
41
46
  });
@@ -52,7 +57,7 @@ const pythAccounts = {
52
57
  Devnet: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
53
58
  MainnetBeta: new web3_js_1.PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG'),
54
59
  };
55
- const chainlinkAccunts = {
60
+ const chainlinkAccounts = {
56
61
  Testing: web3_js_1.SystemProgram.programId,
57
62
  Devnet: new web3_js_1.PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
58
63
  MainnetBeta: web3_js_1.SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.repayVaultInstruction = exports.repayVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -15,20 +18,20 @@ const spl_token_1 = require("@solana/spl-token");
15
18
  const web3_js_1 = require("@solana/web3.js");
16
19
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
17
20
  const Constants_1 = require("../Constants");
21
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
18
22
  function repayVault(program, provider, payer, vaultPublicKey, repayAmount, overrideTime) {
19
23
  return __awaiter(this, void 0, void 0, function* () {
20
24
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
21
25
  // Prep the user to get USH back out at some point
22
26
  const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
23
27
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
24
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
25
- const vaultTypeAccount = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
26
- const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint);
28
+ const vaultTypeAccount = yield program.account.vaultType.fetch(vaultAccount.vaultType);
29
+ const vaultTypeAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultAccount.vaultType, vaultTypeAccount.collateralMint);
27
30
  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);
31
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, 0, repayAmount * -1, false, false);
29
32
  const history = web3_js_1.Keypair.generate();
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));
31
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
33
+ const transaction = new web3_js_1.Transaction().add(yield repayVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, repayAmount, overrideTime));
34
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
32
35
  return vaultPublicKey;
33
36
  });
34
37
  }
@@ -8,16 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.setHaltedInstruction = exports.setHalted = void 0;
13
16
  const web3_js_1 = require("@solana/web3.js");
14
17
  const Constants_1 = require("../Constants");
15
18
  const Errors_1 = require("../utils/Errors");
19
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
16
20
  function setHalted(program, provider, payer, halted) {
17
21
  return __awaiter(this, void 0, void 0, function* () {
18
22
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
19
23
  const transaction = new web3_js_1.Transaction().add(yield setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted));
20
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
24
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
21
25
  return vaultSystemStatePublicKey;
22
26
  });
23
27
  }
@@ -8,21 +8,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.updateVaultTypeStatusInstruction = exports.updateVaultType = void 0;
13
16
  const web3_js_1 = require("@solana/web3.js");
14
17
  const Constants_1 = require("../Constants");
18
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
15
19
  function updateVaultType(program, provider, payer, vaultTypeAccount, oracleInfoAccount, config) {
16
20
  return __awaiter(this, void 0, void 0, function* () {
17
21
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
18
22
  const transaction = new web3_js_1.Transaction().add(yield updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, oracleInfoAccount, config));
19
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]);
23
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
20
24
  return vaultSystemStatePublicKey;
21
25
  });
22
26
  }
23
27
  exports.updateVaultType = updateVaultType;
24
28
  function updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultTypeAccount, oracleInfoAccount, vaultTypeConfig) {
25
- var _a, _b, _c, _d, _e, _f, _g;
29
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
26
30
  return __awaiter(this, void 0, void 0, function* () {
27
31
  const config = {
28
32
  maxDebtExtended: (_a = vaultTypeConfig.maxDebtExtended) !== null && _a !== void 0 ? _a : null,
@@ -32,6 +36,9 @@ function updateVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, pa
32
36
  oraclePyth: (_e = vaultTypeConfig.oraclePyth) !== null && _e !== void 0 ? _e : null,
33
37
  oracleSwitchboard: (_f = vaultTypeConfig.oracleSwitchboard) !== null && _f !== void 0 ? _f : null,
34
38
  deprecated: (_g = vaultTypeConfig.deprecated) !== null && _g !== void 0 ? _g : null,
39
+ priorityPyth: (_h = vaultTypeConfig.priorityPyth) !== null && _h !== void 0 ? _h : null,
40
+ priorityChainlink: (_j = vaultTypeConfig.priorityChainlink) !== null && _j !== void 0 ? _j : null,
41
+ prioritySwitchboard: (_k = vaultTypeConfig.prioritySwitchboard) !== null && _k !== void 0 ? _k : null,
35
42
  };
36
43
  return yield program.methods
37
44
  .updateVaultType(config)
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.withdrawStakingPoolInstruction = exports.withdrawStakingPool = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -15,11 +18,12 @@ const spl_token_1 = require("@solana/spl-token");
15
18
  const web3_js_1 = require("@solana/web3.js");
16
19
  const Errors_1 = require("../utils/Errors");
17
20
  const Constants_1 = require("../Constants");
21
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
18
22
  function withdrawStakingPool(program, provider, payer, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
19
23
  return __awaiter(this, void 0, void 0, function* () {
20
24
  const poolPosition = web3_js_1.Keypair.generate();
21
25
  const transaction = new web3_js_1.Transaction().add(yield withdrawStakingPoolInstruction(program, payer.publicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime));
22
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
26
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
23
27
  return poolPosition.publicKey;
24
28
  });
25
29
  }
@@ -8,6 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.withdrawVaultInstruction = exports.withdrawVault = void 0;
13
16
  const anchor_1 = require("@project-serum/anchor");
@@ -16,6 +19,7 @@ const serum_1 = require("@project-serum/serum");
16
19
  const web3_js_1 = require("@solana/web3.js");
17
20
  const Constants_1 = require("../Constants");
18
21
  const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
22
+ const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
19
23
  function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount, overrideTime) {
20
24
  return __awaiter(this, void 0, void 0, function* () {
21
25
  const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
@@ -24,16 +28,15 @@ function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount,
24
28
  const history = web3_js_1.Keypair.generate();
25
29
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
26
30
  const vaultAccount = yield program.account.vault.fetch(vaultPublicKey);
27
- const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(vaultAccount.collateralType);
28
31
  const vaultAssociatedCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, serum_1.TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, 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);
32
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultAccount.vaultType);
33
+ const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultAccount.vaultType, true);
31
34
  const destinationTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey);
32
35
  const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
33
36
  const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
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));
36
- yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer, history]);
37
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, provider, vaultAccount.vaultType, vaultPublicKey, withdrawAmount * -1, 0, false, false);
38
+ const transaction = new web3_js_1.Transaction().add(yield withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount.address, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, hedgeStakingPoolPublicKey, hedgeStakingPoolAssociatedUshTokenAccount, ushMintPublickey, history.publicKey, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, withdrawAmount, overrideTime));
39
+ yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
37
40
  return vaultPublicKey;
38
41
  });
39
42
  }
@@ -32,11 +32,7 @@ const borsh = __importStar(require("@project-serum/borsh"));
32
32
  */
33
33
  class VaultAccount {
34
34
  constructor(vault, publicKey) {
35
- var _a;
36
- /** The deposited collateral of the vault (in SOL Lamports). */
37
- this.deposited = new decimal_js_1.default(0);
38
- /** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
39
- this.denormalizedDebt = new decimal_js_1.default(0);
35
+ var _a, _b, _c;
40
36
  /** Debt redistribution snapshot */
41
37
  this.debtProductSnapshotBytes = new decimal_js_1.default(0);
42
38
  /** Collateral redistribution snapshot' */
@@ -47,23 +43,20 @@ class VaultAccount {
47
43
  this.vaultOwner = vault.vaultOwner;
48
44
  this.vaultNumber = (_a = vault.vaultNumber) === null || _a === void 0 ? void 0 : _a.toNumber();
49
45
  this.pdaSalt = vault.pdaSalt;
50
- if (vault.deposited) {
51
- this.deposited = new decimal_js_1.default(vault.deposited.toString());
52
- }
53
- if (vault.denormalizedDebt) {
54
- this.denormalizedDebt = new decimal_js_1.default(vault.denormalizedDebt.toString());
55
- }
46
+ this.deposited = (_b = vault.deposited) === null || _b === void 0 ? void 0 : _b.toNumber();
47
+ this.denormalizedDebt = (_c = vault.denormalizedDebt) === null || _c === void 0 ? void 0 : _c.toNumber();
56
48
  if (vault.debtProductSnapshotBytes) {
57
49
  this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
58
50
  }
59
51
  if (vault.collateralAccumulatorSnapshotBytes) {
60
52
  this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
61
53
  }
62
- this.collateralType = vault.collateralType;
54
+ this.vaultTypeName = vault.vaultTypeName;
63
55
  this.nextVaultToRedeem = vault.nextVaultToRedeem;
64
56
  if (vault.vaultStatus) {
65
57
  this.vaultStatus = Object.keys(vault.vaultStatus)[0];
66
58
  }
59
+ this.vaultType = vault.vaultType;
67
60
  }
68
61
  /**
69
62
  * Check if some `PublicKey` is the owner
@@ -80,7 +73,7 @@ class VaultAccount {
80
73
  * @returns collateral value in SOL
81
74
  */
82
75
  inSol() {
83
- return this.deposited.div(web3_js_1.LAMPORTS_PER_SOL).toNumber();
76
+ return this.deposited / web3_js_1.LAMPORTS_PER_SOL;
84
77
  }
85
78
  /**
86
79
  * Get the debt value in USH
@@ -88,7 +81,7 @@ class VaultAccount {
88
81
  * @returns debt value in USH
89
82
  */
90
83
  inUsd() {
91
- return this.denormalizedDebt.div(web3_js_1.LAMPORTS_PER_SOL).toNumber();
84
+ return this.denormalizedDebt / web3_js_1.LAMPORTS_PER_SOL;
92
85
  }
93
86
  /**
94
87
  * Pretty print the vault publickey for easy display
@@ -100,34 +93,37 @@ class VaultAccount {
100
93
  .toString()
101
94
  .substring(this.publicKey.toString().length - 6)}`;
102
95
  }
103
- addDebt(newNormalizedDebt, vaultTypeCompoundedInterest) {
104
- const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest);
105
- this.denormalizedDebt = denormalizedNewDebt.add(new decimal_js_1.default(this.denormalizedDebt)).floor();
106
- // this.denormalizedDebt = parseFloat(this.denormalizedDebt.toFixed(0))
96
+ addDebt(additionalDebt, vaultTypeAccount) {
97
+ let loanFee = new decimal_js_1.default(0);
98
+ if (additionalDebt.isPositive()) {
99
+ loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebt);
100
+ }
101
+ const totalNormalizedLoan = additionalDebt.add(loanFee);
102
+ const denormalizedNewDebt = totalNormalizedLoan.div(new decimal_js_1.default(vaultTypeAccount.cumulativeRate.toString()));
103
+ this.denormalizedDebt = denormalizedNewDebt.add(new decimal_js_1.default(this.denormalizedDebt)).floor().toNumber();
107
104
  }
108
105
  addDeposit(depositAmount) {
109
- this.deposited = this.deposited.add(depositAmount);
106
+ this.deposited += depositAmount;
110
107
  }
111
108
  redeem() {
112
109
  // TODO - Calculate actual redeem amount and adust correctly
113
- this.denormalizedDebt = new decimal_js_1.default(0);
110
+ this.denormalizedDebt = 0;
114
111
  this.vaultStatus = 'initialized';
115
112
  }
116
113
  liquidate() {
117
114
  // TODO - Calculate actual liquidate amount and adust correctly
118
- this.denormalizedDebt = new decimal_js_1.default(0);
115
+ this.denormalizedDebt = 0;
119
116
  this.vaultStatus = 'liquidated';
120
117
  }
121
- updateDebtAndCollateral(vaultTypeAccuntData) {
122
- const debtProductCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.debtRedistributionProduct);
123
- const collateralAccumulatorCurrent = (0, HedgeDecimal_1.DecimalFromU128)(vaultTypeAccuntData.collateralRedistributionAccumulator);
124
- this.denormalizedDebt = debtProductCurrent
118
+ updateDebtAndCollateral(vaultTypeAccountData) {
119
+ this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
125
120
  .div(this.debtProductSnapshotBytes)
126
- .mul(new decimal_js_1.default(this.denormalizedDebt));
127
- const extraCollateralDeposited = this.denormalizedDebt.mul(collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes));
128
- this.deposited = this.deposited.add(extraCollateralDeposited);
129
- this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent;
130
- this.debtProductSnapshotBytes = debtProductCurrent;
121
+ .mul(new decimal_js_1.default(this.denormalizedDebt))
122
+ .toNumber();
123
+ const extraCollateralDeposited = this.denormalizedDebt * vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber();
124
+ this.deposited += extraCollateralDeposited;
125
+ this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator;
126
+ this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct;
131
127
  }
132
128
  toString(highlight) {
133
129
  let arrow = '';
@@ -135,14 +131,14 @@ class VaultAccount {
135
131
  arrow = ' <----!!';
136
132
  }
137
133
  let collateralRatio = 'Infinite';
138
- if (this.denormalizedDebt.greaterThan(0)) {
134
+ if (this.denormalizedDebt > 0) {
139
135
  collateralRatio = new decimal_js_1.default(this.deposited).div(new decimal_js_1.default(this.denormalizedDebt)).toString();
140
136
  }
141
137
  let nextVault = 'None';
142
138
  if (this.nextVaultToRedeem) {
143
139
  nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
144
140
  }
145
- return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} NextVault: ${nextVault} ${arrow} `;
141
+ return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `;
146
142
  }
147
143
  /**
148
144
  * Creates a VaultAccount from a slice of data