hedge-web3 0.2.3 → 0.2.9

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 (37) hide show
  1. package/declarations/instructions/createVault.d.ts +3 -2
  2. package/declarations/instructions/depositLiquidationPool.d.ts +4 -3
  3. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  4. package/declarations/instructions/depositVault.d.ts +3 -2
  5. package/declarations/instructions/loanVault.d.ts +3 -2
  6. package/declarations/instructions/redeemVault.d.ts +3 -2
  7. package/declarations/instructions/repayVault.d.ts +3 -2
  8. package/declarations/instructions/setVaultTypeStatus.d.ts +2 -3
  9. package/declarations/instructions/withdrawVault.d.ts +3 -2
  10. package/declarations/state/VaultAccount.d.ts +33 -10
  11. package/declarations/utils/getLinkedListAccounts.d.ts +17 -2
  12. package/lib/instructions/createVault.js +2 -2
  13. package/lib/instructions/depositLiquidationPool.js +2 -2
  14. package/lib/instructions/depositStakingPool.js +2 -2
  15. package/lib/instructions/depositVault.js +3 -3
  16. package/lib/instructions/liquidateVault.js +1 -1
  17. package/lib/instructions/loanVault.js +3 -3
  18. package/lib/instructions/redeemVault.js +3 -3
  19. package/lib/instructions/refreshOraclePrice.js +2 -2
  20. package/lib/instructions/repayVault.js +3 -3
  21. package/lib/instructions/setVaultTypeStatus.js +10 -9
  22. package/lib/instructions/withdrawVault.js +3 -3
  23. package/lib/state/VaultAccount.js +76 -33
  24. package/lib/utils/getLinkedListAccounts.js +29 -13
  25. package/package.json +1 -1
  26. package/src/instructions/createVault.ts +3 -3
  27. package/src/instructions/depositLiquidationPool.ts +4 -4
  28. package/src/instructions/depositStakingPool.ts +3 -3
  29. package/src/instructions/depositVault.ts +7 -6
  30. package/src/instructions/liquidateVault.ts +4 -3
  31. package/src/instructions/loanVault.ts +7 -6
  32. package/src/instructions/redeemVault.ts +7 -6
  33. package/src/instructions/refreshOraclePrice.ts +2 -2
  34. package/src/instructions/repayVault.ts +7 -6
  35. package/src/instructions/withdrawVault.ts +7 -6
  36. package/src/state/VaultAccount.ts +84 -39
  37. package/src/utils/getLinkedListAccounts.ts +34 -16
@@ -27,12 +27,18 @@ const web3_js_1 = require("@solana/web3.js");
27
27
  const decimal_js_1 = __importDefault(require("decimal.js"));
28
28
  const HedgeDecimal_1 = require("../HedgeDecimal");
29
29
  const borsh = __importStar(require("@project-serum/borsh"));
30
+ const bn_js_1 = __importDefault(require("bn.js"));
30
31
  /**
31
32
  * A class that represents an on-chian vault.
32
33
  */
33
34
  class VaultAccount {
34
35
  constructor(vault, publicKey) {
35
- var _a, _b, _c;
36
+ /** The deposited collateral of the vault (in SOL Lamports). */
37
+ this.deposited = new bn_js_1.default(0);
38
+ /** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
39
+ this.denormalizedDebt = new bn_js_1.default(0);
40
+ /** The ordered number of when this vault was created. */
41
+ this.vaultNumber = 0;
36
42
  /** Debt redistribution snapshot */
37
43
  this.debtProductSnapshotBytes = new decimal_js_1.default(0);
38
44
  /** Collateral redistribution snapshot' */
@@ -41,10 +47,12 @@ class VaultAccount {
41
47
  this.vaultStatus = '';
42
48
  this.publicKey = publicKey;
43
49
  this.vaultOwner = vault.vaultOwner;
44
- this.vaultNumber = (_a = vault.vaultNumber) === null || _a === void 0 ? void 0 : _a.toNumber();
45
50
  this.pdaSalt = vault.pdaSalt;
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();
51
+ this.deposited = vault.deposited;
52
+ this.denormalizedDebt = vault.denormalizedDebt;
53
+ if (vault.vaultNumber) {
54
+ this.vaultNumber = vault.vaultNumber.toNumber();
55
+ }
48
56
  if (vault.debtProductSnapshotBytes) {
49
57
  this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
50
58
  }
@@ -67,21 +75,22 @@ class VaultAccount {
67
75
  isOwnedBy(publicKey) {
68
76
  return publicKey && publicKey.toString() === this.vaultOwner.toString();
69
77
  }
70
- /**
71
- * Get the collateral value in SOL
72
- *
73
- * @returns collateral value in SOL
74
- */
75
- inSol() {
76
- return this.deposited / web3_js_1.LAMPORTS_PER_SOL;
77
- }
78
+ // /**
79
+ // * Get the collateral value in SOL
80
+ // *
81
+ // * @returns collateral value in SOL
82
+ // */
83
+ // public inSol(): number {
84
+ // This should not be LAMPORTS_PER_SOL. Should be collateral units like Ray
85
+ // return new Decimal(this.deposited.toString()).div(LAMPORTS_PER_SOL).toNumber()
86
+ // }
78
87
  /**
79
88
  * Get the debt value in USH
80
89
  *
81
90
  * @returns debt value in USH
82
91
  */
83
92
  inUsd() {
84
- return this.denormalizedDebt / web3_js_1.LAMPORTS_PER_SOL;
93
+ return new decimal_js_1.default(this.denormalizedDebt.toString()).div(web3_js_1.LAMPORTS_PER_SOL).toNumber();
85
94
  }
86
95
  /**
87
96
  * Pretty print the vault publickey for easy display
@@ -93,35 +102,69 @@ class VaultAccount {
93
102
  .toString()
94
103
  .substring(this.publicKey.toString().length - 6)}`;
95
104
  }
105
+ /**
106
+ * Add additional debt to the vault.
107
+ *
108
+ * @param {BN} additionalDebt - Additional normalized debt to add in (USH) lamports.
109
+ * @param {VaultType} vaultTypeAccount - Vault's vaultType
110
+ *
111
+ */
96
112
  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();
113
+ const additionalDebtAsDecimal = new decimal_js_1.default(additionalDebt.toString());
114
+ // Calculate the fee on the loan
115
+ const loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebtAsDecimal);
116
+ // TODO: There's a chance this needs to be .Floor()
117
+ const totalNormalizedLoan = additionalDebtAsDecimal.add(loanFee);
118
+ const denormalizedNewDebt = new bn_js_1.default(totalNormalizedLoan.div(vaultTypeAccount.cumulativeRate).floor().toString());
119
+ this.denormalizedDebt = this.denormalizedDebt.add(denormalizedNewDebt);
120
+ }
121
+ /**
122
+ * Repay debt on a vault
123
+ *
124
+ * @param {BN} repayAmount - Normalized debt to repay in (USH) lamports.
125
+ * @param {VaultType} vaultTypeAccount - Vault's vaultType
126
+ *
127
+ */
128
+ repayDebt(repayAmount, vaultTypeAccount) {
129
+ const denormalizedRepayment = new decimal_js_1.default(repayAmount.toString()).div(vaultTypeAccount.cumulativeRate).floor();
130
+ this.denormalizedDebt = this.denormalizedDebt.sub(new bn_js_1.default(denormalizedRepayment.toString()));
131
+ }
132
+ /**
133
+ * Deposit Collateral
134
+ *
135
+ * @param {BN} depositAmount - Amount to deposit in (CollateralMint) lamports
136
+ *
137
+ */
138
+ depositCollateral(depositAmount) {
139
+ this.deposited = this.deposited.add(depositAmount);
104
140
  }
105
- addDeposit(depositAmount) {
106
- this.deposited += depositAmount;
141
+ /**
142
+ * Withdraw Collateral
143
+ *
144
+ * @param {BN} withdrawAmount - Amount to withdraw in (CollateralMint) lamports
145
+ *
146
+ */
147
+ withdrawCollateral(withdrawAmount) {
148
+ this.deposited = this.deposited.sub(withdrawAmount);
107
149
  }
108
150
  redeem() {
109
151
  // TODO - Calculate actual redeem amount and adust correctly
110
- this.denormalizedDebt = 0;
152
+ this.denormalizedDebt = new bn_js_1.default(0);
111
153
  this.vaultStatus = 'initialized';
112
154
  }
113
155
  liquidate() {
114
156
  // TODO - Calculate actual liquidate amount and adust correctly
115
- this.denormalizedDebt = 0;
157
+ this.denormalizedDebt = new bn_js_1.default(0);
116
158
  this.vaultStatus = 'liquidated';
117
159
  }
118
160
  updateDebtAndCollateral(vaultTypeAccountData) {
119
- this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
161
+ this.denormalizedDebt = new bn_js_1.default(vaultTypeAccountData.debtRedistributionProduct
120
162
  .div(this.debtProductSnapshotBytes)
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;
163
+ .mul(new decimal_js_1.default(this.denormalizedDebt.toString()))
164
+ .floor()
165
+ .toString());
166
+ const extraCollateralDeposited = new decimal_js_1.default(this.denormalizedDebt.toString()).mul(vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()).floor();
167
+ this.deposited = this.deposited.add(new bn_js_1.default(extraCollateralDeposited.toString()));
125
168
  this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator;
126
169
  this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct;
127
170
  }
@@ -131,14 +174,14 @@ class VaultAccount {
131
174
  arrow = ' <----!!';
132
175
  }
133
176
  let collateralRatio = 'Infinite';
134
- if (this.denormalizedDebt > 0) {
135
- collateralRatio = new decimal_js_1.default(this.deposited).div(new decimal_js_1.default(this.denormalizedDebt)).toString();
177
+ if (this.denormalizedDebt.gt(new bn_js_1.default(0))) {
178
+ collateralRatio = new decimal_js_1.default(this.deposited.toString()).div(new decimal_js_1.default(this.denormalizedDebt.toString())).toString();
136
179
  }
137
180
  let nextVault = 'None';
138
181
  if (this.nextVaultToRedeem) {
139
182
  nextVault = this.nextVaultToRedeem.toString().substring(0, 6);
140
183
  }
141
- return `Vault(${this.vaultNumber}): ${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `;
184
+ return `${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `;
142
185
  }
143
186
  /**
144
187
  * Creates a VaultAccount from a slice of data
@@ -153,7 +196,7 @@ class VaultAccount {
153
196
  * @returns a new VaultAccount
154
197
  */
155
198
  static FromMiniSlice(data, pubkey) {
156
- const props = [borsh.u64('vaultNumber'), borsh.u64('deposited'), borsh.u64('denormalizedDebt')];
199
+ const props = [borsh.u64('deposited'), borsh.u64('denormalizedDebt')];
157
200
  const miniVaultLayout = borsh.struct(props, 'minVaultLayout');
158
201
  const decodedData = miniVaultLayout.decode(data);
159
202
  return new VaultAccount(decodedData, pubkey);
@@ -13,13 +13,28 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.getLinkedListAccounts = void 0;
16
+ const anchor_1 = require("@project-serum/anchor");
16
17
  const underscore_1 = __importDefault(require("underscore"));
17
18
  const Constants_1 = require("../Constants");
18
19
  const VaultAccount_1 = require("../state/VaultAccount");
19
20
  const decimal_js_1 = __importDefault(require("decimal.js"));
20
21
  const bs58_1 = __importDefault(require("bs58"));
21
22
  const VaultType_1 = __importDefault(require("../state/VaultType"));
22
- function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, loanAmount, redeem, liquidate, cachedVaults) {
23
+ /**
24
+ * Get the accounts the left and right for re-inserting in the linked list
25
+ *
26
+ * @param {Program<Vault>} program - Anchor program <Vault ILD>
27
+ * @param {PublicKey} vaultTypeAccountPublicKey - Vault Type Account PublicKey
28
+ * @param {PublicKey} vaultPublicKey - Vault Account PublicKey
29
+ * @param {BN} depositAmount - Amount that will be deposited into vault with instruction
30
+ * @param {BN} withdrawAmount - Amount that will be withdrawn from vault with instruction
31
+ * @param {BN} loanAmount - Amount that will be deposited into vault with transaction (sans fees)
32
+ * @param {BN} repayAmount - Amount that will be repaid into vault with transaction
33
+ * @param {boolean} redeem - True if vault is going to be redeemed fully
34
+ * @param {boolean} liquidate - True if vault is going to be liquidated fully
35
+ * @param {VaultAccount[]} cachedVaults - Optional list of cached vaults. Saves a request to the on-chain data.
36
+ */
37
+ function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKey, depositAmount, withdrawAmount, loanAmount, repayAmount, redeem, liquidate, cachedVaults) {
23
38
  return __awaiter(this, void 0, void 0, function* () {
24
39
  const vaultTypeRaw = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
25
40
  const vaultType = new VaultType_1.default(vaultTypeRaw, vaultTypeAccountPublicKey);
@@ -50,7 +65,7 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
50
65
  // })
51
66
  // Remove any vaults with no debt or collateral
52
67
  vaults = underscore_1.default.filter(vaults, (vault) => {
53
- return vault.denormalizedDebt > 0 && vault.deposited > 0;
68
+ return vault.denormalizedDebt.gt(new anchor_1.BN(0)) && vault.deposited.gt(new anchor_1.BN(0));
54
69
  });
55
70
  // Sort them
56
71
  vaults.sort(sortVaults);
@@ -101,17 +116,18 @@ function getLinkedListAccounts(program, provider, vaultTypeAccountPublicKey, vau
101
116
  }
102
117
  // Now that we know it's def in the list, iterate the list and update
103
118
  // this vault with the operation we're going to apply
104
- const newNormalizedDebt = new decimal_js_1.default(loanAmount);
105
119
  vaults[indexBefore].updateDebtAndCollateral(vaultType);
106
- vaults[indexBefore].addDebt(newNormalizedDebt, vaultType);
107
- vaults[indexBefore].addDeposit(depositAmount);
120
+ vaults[indexBefore].addDebt(loanAmount, vaultType);
121
+ vaults[indexBefore].repayDebt(repayAmount, vaultType);
122
+ vaults[indexBefore].depositCollateral(depositAmount);
123
+ vaults[indexBefore].withdrawCollateral(withdrawAmount);
108
124
  if (liquidate) {
109
125
  vaults[indexBefore].liquidate();
110
126
  }
111
127
  if (redeem) {
112
128
  vaults[indexBefore].redeem();
113
129
  }
114
- if (vaults[indexBefore].denormalizedDebt === 0) {
130
+ if (vaults[indexBefore].denormalizedDebt.isZero()) {
115
131
  vaults.splice(indexBefore, 1);
116
132
  }
117
133
  // Sort it again since we've changed one vault
@@ -154,12 +170,12 @@ exports.getLinkedListAccounts = getLinkedListAccounts;
154
170
  // Sort function we can use to sort the vaults
155
171
  // Sorted by collateral ratio. If two are the same, newer vault first
156
172
  function sortVaults(a, b) {
157
- const aRatio = a.deposited / a.denormalizedDebt;
158
- const bRatio = b.deposited / b.denormalizedDebt;
159
- if (aRatio === bRatio) {
160
- return b.vaultNumber - a.vaultNumber;
173
+ const aRatio = new decimal_js_1.default(a.deposited.toString()).div(new decimal_js_1.default(a.denormalizedDebt.toString()));
174
+ const bRatio = new decimal_js_1.default(b.deposited.toString()).div(new decimal_js_1.default(b.denormalizedDebt.toString()));
175
+ if (aRatio.equals(bRatio)) {
176
+ return a.publicKey.toString() > b.publicKey.toString() ? 1 : -1;
161
177
  }
162
- return aRatio - bRatio;
178
+ return aRatio.greaterThan(bRatio) ? 1 : -1;
163
179
  }
164
180
  function getMiniVaults(program, vaultTypePublicKey) {
165
181
  return __awaiter(this, void 0, void 0, function* () {
@@ -189,8 +205,8 @@ function getMiniVaults(program, vaultTypePublicKey) {
189
205
  // Slice the data only to grab the 3 u64's of size 8 bytes each
190
206
  dataSlice: {
191
207
  // See programs/hedge-vault/src/account_data/vault.rs for struct layout
192
- offset: 8 + 32,
193
- length: 24,
208
+ offset: 8 + 32 + 8,
209
+ length: 16,
194
210
  },
195
211
  });
196
212
  return allAccounts.map((vaultData) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.2.3",
3
+ "version": "0.2.9",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "types": "declarations/index.d.ts",
@@ -93,7 +93,7 @@ export async function createVault(
93
93
  vaultTypeAccountInfo.collateralMint,
94
94
  history.publicKey,
95
95
  ushMintPublickey,
96
- depositAmount,
96
+ new BN(depositAmount),
97
97
  overrideTime
98
98
  )
99
99
  )
@@ -187,7 +187,7 @@ export async function buildCreateVaultTransaction(
187
187
  vaultTypeAccountInfo.collateralMint,
188
188
  history.publicKey,
189
189
  ushMintPublickey,
190
- depositAmount,
190
+ new BN(depositAmount),
191
191
  overrideTime
192
192
  )
193
193
  )
@@ -221,7 +221,7 @@ export async function createVaultInstruction(
221
221
  collateralMint: PublicKey,
222
222
  historyPublicKey: PublicKey,
223
223
  ushMintPublickey: PublicKey,
224
- depositAmount: number,
224
+ depositAmount: BN,
225
225
  overrideTime?: number
226
226
  ): Promise<TransactionInstruction> {
227
227
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -24,7 +24,7 @@ export async function depositLiquidationPool(
24
24
  program: Program<Vault>,
25
25
  provider: Provider,
26
26
  payer: Signer,
27
- depositAmount: number,
27
+ depositAmount: BN,
28
28
  overrideStartTime?: number
29
29
  ): Promise<PublicKey> {
30
30
  const ushMintPublickey = await getUshMintPublicKey()
@@ -42,7 +42,7 @@ export async function depositLiquidationPool(
42
42
  payer.publicKey,
43
43
  payerUshAccount.address,
44
44
  poolPosition.publicKey,
45
- depositAmount,
45
+ new BN(depositAmount),
46
46
  overrideStartTime
47
47
  )
48
48
  )
@@ -55,7 +55,7 @@ export async function depositLiquidationPoolInstruction(
55
55
  payerPublicKey: PublicKey,
56
56
  payerUshAccount: PublicKey,
57
57
  poolPositionPublicKey: PublicKey,
58
- depositAmount: number,
58
+ depositAmount: BN,
59
59
  overrideStartTime?: number
60
60
  ): Promise<TransactionInstruction> {
61
61
  const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
@@ -68,7 +68,7 @@ export async function depositLiquidationPoolInstruction(
68
68
 
69
69
  return await program.methods
70
70
  .depositLiquidationPool(
71
- new BN(depositAmount),
71
+ depositAmount,
72
72
  new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
73
73
  )
74
74
  .accounts({
@@ -30,7 +30,7 @@ export async function depositStakingPool(
30
30
  payer.publicKey,
31
31
  poolPosition.publicKey,
32
32
  mintPublicKey,
33
- depositAmount,
33
+ new BN(depositAmount),
34
34
  overrideStartTime
35
35
  )
36
36
  )
@@ -43,7 +43,7 @@ export async function depositStakingPoolInstruction(
43
43
  payerPublicKey: PublicKey,
44
44
  poolPositionPublicKey: PublicKey,
45
45
  stakedTokenMintPublicKey: PublicKey,
46
- depositAmount: number,
46
+ depositAmount: BN,
47
47
  overrideStartTime?: number
48
48
  ): Promise<TransactionInstruction> {
49
49
  const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
@@ -53,7 +53,7 @@ export async function depositStakingPoolInstruction(
53
53
 
54
54
  return await program.methods
55
55
  .depositStakingPool(
56
- new BN(depositAmount),
56
+ depositAmount,
57
57
  new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
58
58
  )
59
59
  .accounts({
@@ -67,11 +67,12 @@ export async function depositVault(
67
67
  const signers = [payer, history]
68
68
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
69
69
  program,
70
- provider,
71
70
  vaultAccount.vaultType,
72
71
  vaultPublicKey,
73
- depositAmount,
74
- 0,
72
+ new BN(depositAmount),
73
+ new BN(0),
74
+ new BN(0),
75
+ new BN(0),
75
76
  false,
76
77
  false
77
78
  )
@@ -111,7 +112,7 @@ export async function depositVault(
111
112
  oldSmallerPublicKey,
112
113
  newSmallerPublicKey,
113
114
  newLargerPublicKey,
114
- depositAmount,
115
+ new BN(depositAmount),
115
116
  overrideTime
116
117
  )
117
118
  )
@@ -146,12 +147,12 @@ export async function depositVaultInstruction(
146
147
  oldSmallerPublicKey: PublicKey,
147
148
  newSmallerPublicKey: PublicKey,
148
149
  newLargerPublicKey: PublicKey,
149
- depositAmount: number,
150
+ depositAmount: BN,
150
151
  overrideTime?: number
151
152
  ): Promise<TransactionInstruction> {
152
153
  return await program.methods
153
154
  .depositVault(
154
- new BN(depositAmount),
155
+ depositAmount,
155
156
  new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
156
157
  )
157
158
  .accounts({
@@ -87,11 +87,12 @@ export async function liquidateVault(
87
87
 
88
88
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
89
89
  program,
90
- provider,
91
90
  vaultAccount.vaultType,
92
91
  vaultPublicKey,
93
- 0,
94
- 0,
92
+ new BN(0),
93
+ new BN(0),
94
+ new BN(0),
95
+ new BN(0),
95
96
  false,
96
97
  true
97
98
  )
@@ -59,11 +59,12 @@ export async function loanVault(
59
59
  )
60
60
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
61
61
  program,
62
- provider,
63
62
  vaultAccount.vaultType,
64
63
  vaultPublicKey,
65
- 0,
66
- loanAmount,
64
+ new BN(0),
65
+ new BN(0),
66
+ new BN(loanAmount),
67
+ new BN(0),
67
68
  false,
68
69
  false
69
70
  )
@@ -82,7 +83,7 @@ export async function loanVault(
82
83
  oldSmallerPublicKey,
83
84
  newSmallerPublicKey,
84
85
  newLargerPublicKey,
85
- loanAmount,
86
+ new BN(loanAmount),
86
87
  overrideTime
87
88
  )
88
89
  )
@@ -102,7 +103,7 @@ export async function loanVaultInstruction(
102
103
  oldSmallerPublicKey: PublicKey,
103
104
  newSmallerPublicKey: PublicKey,
104
105
  newLargerPublicKey: PublicKey,
105
- loanAmount: number,
106
+ loanAmount: BN,
106
107
  overrideTime?: number
107
108
  ): Promise<TransactionInstruction> {
108
109
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -116,7 +117,7 @@ export async function loanVaultInstruction(
116
117
 
117
118
  return await program.methods
118
119
  .loanVault(
119
- new BN(loanAmount),
120
+ loanAmount,
120
121
  new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
121
122
  )
122
123
  .accounts({
@@ -67,11 +67,12 @@ export async function redeemVault(
67
67
 
68
68
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
69
69
  program,
70
- provider,
71
70
  vaultAccount.vaultType,
72
71
  vaultPublicKey,
73
- 0,
74
- 0,
72
+ new BN(0),
73
+ new BN(0),
74
+ new BN(0),
75
+ new BN(0),
75
76
  true,
76
77
  false
77
78
  )
@@ -91,7 +92,7 @@ export async function redeemVault(
91
92
  oldSmallerPublicKey,
92
93
  newSmallerPublicKey,
93
94
  newLargerPublicKey,
94
- redeemAmount,
95
+ new BN(redeemAmount),
95
96
  transactionOverrideTime
96
97
  )
97
98
  )
@@ -112,7 +113,7 @@ export async function redeemVaultInstruction(
112
113
  oldSmallerPublicKey: PublicKey,
113
114
  newSmallerPublicKey: PublicKey,
114
115
  newLargerPublicKey: PublicKey,
115
- redeemAmount: number,
116
+ redeemAmount: BN,
116
117
  transactionOverrideTime?: number
117
118
  ): Promise<TransactionInstruction> {
118
119
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -125,7 +126,7 @@ export async function redeemVaultInstruction(
125
126
  )
126
127
  return await program.methods
127
128
  .redeemVault(
128
- new BN(redeemAmount),
129
+ redeemAmount,
129
130
  new BN(transactionOverrideTime ?? Date.now() / 1000) // override start time
130
131
  )
131
132
  .accounts({
@@ -75,12 +75,12 @@ const pythAccounts = {
75
75
  }
76
76
  const chainlinkAccounts = {
77
77
  Testing: SystemProgram.programId,
78
- Devnet: new PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
78
+ Devnet: new PublicKey('HgTtcbcmp5BeThax5AU8vg4VwK79qAvAKKFMs8txMLW6'),
79
79
  MainnetBeta: SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
80
80
  }
81
81
  const switchboardAccounts = {
82
82
  Testing: SystemProgram.programId,
83
83
  // Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
84
- Devnet: new PublicKey('DpoK8Zz69APV9ntjuY9C4LZCxANYMV56M2cbXEdkjxME'),
84
+ Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
85
85
  MainnetBeta: SystemProgram.programId, // Switchboard V2 NOT ON MAINNET YET
86
86
  }
@@ -49,11 +49,12 @@ export async function repayVault(
49
49
 
50
50
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
51
51
  program,
52
- provider,
53
52
  vaultAccount.vaultType,
54
53
  vaultPublicKey,
55
- 0,
56
- repayAmount * -1,
54
+ new BN(0),
55
+ new BN(0),
56
+ new BN(0),
57
+ new BN(Math.abs(repayAmount)),
57
58
  false,
58
59
  false
59
60
  )
@@ -72,7 +73,7 @@ export async function repayVault(
72
73
  oldSmallerPublicKey,
73
74
  newSmallerPublicKey,
74
75
  newLargerPublicKey,
75
- repayAmount,
76
+ new BN(repayAmount),
76
77
  overrideTime
77
78
  )
78
79
  )
@@ -92,7 +93,7 @@ export async function repayVaultInstruction(
92
93
  oldSmallerPublicKey: PublicKey,
93
94
  newSmallerPublicKey: PublicKey,
94
95
  newLargerPublicKey: PublicKey,
95
- repayAmount: number,
96
+ repayAmount: BN,
96
97
  overrideTime?: number
97
98
  ): Promise<TransactionInstruction> {
98
99
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -106,7 +107,7 @@ export async function repayVaultInstruction(
106
107
 
107
108
  return await program.methods
108
109
  .repayVault(
109
- new BN(repayAmount),
110
+ repayAmount,
110
111
  new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
111
112
  )
112
113
  .accounts({
@@ -69,11 +69,12 @@ export async function withdrawVault(
69
69
 
70
70
  const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
71
71
  program,
72
- provider,
73
72
  vaultAccount.vaultType,
74
73
  vaultPublicKey,
75
- withdrawAmount * -1,
76
- 0,
74
+ new BN(0),
75
+ new BN(Math.abs(withdrawAmount)),
76
+ new BN(0),
77
+ new BN(0),
77
78
  false,
78
79
  false
79
80
  )
@@ -95,7 +96,7 @@ export async function withdrawVault(
95
96
  oldSmallerPublicKey,
96
97
  newSmallerPublicKey,
97
98
  newLargerPublicKey,
98
- withdrawAmount,
99
+ new BN(withdrawAmount),
99
100
  overrideTime
100
101
  )
101
102
  )
@@ -119,12 +120,12 @@ export async function withdrawVaultInstruction(
119
120
  oldSmallerPublicKey: PublicKey,
120
121
  newSmallerPublicKey: PublicKey,
121
122
  newLargerPublicKey: PublicKey,
122
- withdrawAmount: number,
123
+ withdrawAmount: BN,
123
124
  overrideTime?: number
124
125
  ): Promise<TransactionInstruction> {
125
126
  return await program.methods
126
127
  .withdrawVault(
127
- new BN(withdrawAmount),
128
+ withdrawAmount,
128
129
  new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
129
130
  )
130
131
  .accounts({