hedge-web3 0.2.7 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/declarations/Constants.d.ts +47 -0
  2. package/declarations/idl/pyth.d.ts +80 -0
  3. package/declarations/instructions/createVault.d.ts +3 -2
  4. package/declarations/instructions/depositLiquidationPool.d.ts +4 -3
  5. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  6. package/declarations/instructions/depositVault.d.ts +3 -2
  7. package/declarations/instructions/loanVault.d.ts +3 -2
  8. package/declarations/instructions/redeemVault.d.ts +3 -2
  9. package/declarations/instructions/repayVault.d.ts +3 -2
  10. package/declarations/instructions/withdrawVault.d.ts +3 -2
  11. package/declarations/state/VaultAccount.d.ts +33 -10
  12. package/declarations/utils/getLinkedListAccounts.d.ts +17 -2
  13. package/lib/Constants.js +47 -0
  14. package/lib/idl/pyth.js +82 -0
  15. package/lib/instructions/createVault.js +2 -2
  16. package/lib/instructions/depositLiquidationPool.js +2 -2
  17. package/lib/instructions/depositStakingPool.js +2 -2
  18. package/lib/instructions/depositVault.js +3 -3
  19. package/lib/instructions/liquidateVault.js +1 -1
  20. package/lib/instructions/loanVault.js +3 -3
  21. package/lib/instructions/redeemVault.js +3 -3
  22. package/lib/instructions/refreshOraclePrice.js +6 -5
  23. package/lib/instructions/repayVault.js +3 -3
  24. package/lib/instructions/withdrawVault.js +3 -3
  25. package/lib/state/VaultAccount.js +69 -30
  26. package/lib/utils/getLinkedListAccounts.js +26 -10
  27. package/package.json +1 -1
  28. package/src/Constants.ts +50 -4
  29. package/src/idl/pyth.ts +159 -0
  30. package/src/instructions/createVault.ts +3 -3
  31. package/src/instructions/depositLiquidationPool.ts +4 -4
  32. package/src/instructions/depositStakingPool.ts +3 -3
  33. package/src/instructions/depositVault.ts +7 -6
  34. package/src/instructions/liquidateVault.ts +4 -3
  35. package/src/instructions/loanVault.ts +7 -6
  36. package/src/instructions/redeemVault.ts +7 -6
  37. package/src/instructions/refreshOraclePrice.ts +6 -5
  38. package/src/instructions/repayVault.ts +7 -6
  39. package/src/instructions/withdrawVault.ts +7 -6
  40. package/src/state/VaultAccount.ts +79 -36
  41. package/src/utils/getLinkedListAccounts.ts +31 -13
@@ -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({
@@ -44,6 +44,7 @@ export async function refreshOraclePriceInstruction(
44
44
  [enc.encode(collateralType), enc.encode('State')],
45
45
  HEDGE_PROGRAM_PUBLICKEY
46
46
  )
47
+ const oracleInfo = await program.account.oracleInfoForCollateralType.fetch(oracleInfoAccount)
47
48
 
48
49
  return await program.methods
49
50
  .refreshOraclePrice(
@@ -53,9 +54,9 @@ export async function refreshOraclePriceInstruction(
53
54
  .accounts({
54
55
  oracleInfoAccount: oracleInfoAccount,
55
56
  vaultTypeAccount: vaultTypeAccount,
56
- oracleChainlink: chainlinkAccounts[network],
57
- oraclePyth: pythAccounts[network],
58
- oracleSwitchboard: switchboardAccounts[network],
57
+ oracleChainlink: oracleInfo.oracleChainlink,
58
+ oraclePyth: oracleInfo.oraclePyth,
59
+ oracleSwitchboard: oracleInfo.oracleSwitchboard,
59
60
  systemProgram: SystemProgram.programId,
60
61
  chainlinkProgram: CHAINLINK_PROGRAM_ID,
61
62
  })
@@ -75,12 +76,12 @@ const pythAccounts = {
75
76
  }
76
77
  const chainlinkAccounts = {
77
78
  Testing: SystemProgram.programId,
78
- Devnet: new PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
79
+ Devnet: new PublicKey('HgTtcbcmp5BeThax5AU8vg4VwK79qAvAKKFMs8txMLW6'),
79
80
  MainnetBeta: SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
80
81
  }
81
82
  const switchboardAccounts = {
82
83
  Testing: SystemProgram.programId,
83
84
  // Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
84
- Devnet: new PublicKey('DpoK8Zz69APV9ntjuY9C4LZCxANYMV56M2cbXEdkjxME'),
85
+ Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
85
86
  MainnetBeta: SystemProgram.programId, // Switchboard V2 NOT ON MAINNET YET
86
87
  }
@@ -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({
@@ -20,10 +20,10 @@ export class VaultAccount {
20
20
  pdaSalt: string
21
21
 
22
22
  /** The deposited collateral of the vault (in SOL Lamports). */
23
- deposited: number
23
+ deposited = new BN(0)
24
24
 
25
25
  /** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
26
- denormalizedDebt: number
26
+ denormalizedDebt = new BN(0)
27
27
 
28
28
  /** The ordered number of when this vault was created. */
29
29
  vaultNumber = 0
@@ -50,8 +50,10 @@ export class VaultAccount {
50
50
  this.publicKey = publicKey
51
51
  this.vaultOwner = vault.vaultOwner
52
52
  this.pdaSalt = vault.pdaSalt
53
- this.deposited = vault.deposited?.toNumber()
54
- this.denormalizedDebt = vault.denormalizedDebt?.toNumber()
53
+
54
+ this.deposited = vault.deposited
55
+ this.denormalizedDebt = vault.denormalizedDebt
56
+
55
57
  if (vault.vaultNumber) {
56
58
  this.vaultNumber = vault.vaultNumber.toNumber()
57
59
  }
@@ -80,14 +82,15 @@ export class VaultAccount {
80
82
  return publicKey && publicKey.toString() === this.vaultOwner.toString()
81
83
  }
82
84
 
83
- /**
84
- * Get the collateral value in SOL
85
- *
86
- * @returns collateral value in SOL
87
- */
88
- public inSol(): number {
89
- return this.deposited / LAMPORTS_PER_SOL
90
- }
85
+ // /**
86
+ // * Get the collateral value in SOL
87
+ // *
88
+ // * @returns collateral value in SOL
89
+ // */
90
+ // public inSol(): number {
91
+ // This should not be LAMPORTS_PER_SOL. Should be collateral units like Ray
92
+ // return new Decimal(this.deposited.toString()).div(LAMPORTS_PER_SOL).toNumber()
93
+ // }
91
94
 
92
95
  /**
93
96
  * Get the debt value in USH
@@ -95,7 +98,7 @@ export class VaultAccount {
95
98
  * @returns debt value in USH
96
99
  */
97
100
  public inUsd(): number {
98
- return this.denormalizedDebt / LAMPORTS_PER_SOL
101
+ return new Decimal(this.denormalizedDebt.toString()).div(LAMPORTS_PER_SOL).toNumber()
99
102
  }
100
103
 
101
104
  /**
@@ -109,42 +112,82 @@ export class VaultAccount {
109
112
  .substring(this.publicKey.toString().length - 6)}`
110
113
  }
111
114
 
112
- public addDebt(additionalDebt: Decimal, vaultTypeAccount: VaultType) {
113
- let loanFee = new Decimal(0)
114
- if (additionalDebt.isPositive()){
115
- loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebt)
116
- }
115
+ /**
116
+ * Add additional debt to the vault.
117
+ *
118
+ * @param {BN} additionalDebt - Additional normalized debt to add in (USH) lamports.
119
+ * @param {VaultType} vaultTypeAccount - Vault's vaultType
120
+ *
121
+ */
122
+ public addDebt(additionalDebt: BN, vaultTypeAccount: VaultType) {
123
+ const additionalDebtAsDecimal = new Decimal(additionalDebt.toString())
124
+ // Calculate the fee on the loan
125
+ const loanFee = vaultTypeAccount.loanInitFee.mul(additionalDebtAsDecimal)
117
126
 
118
- const totalNormalizedLoan = additionalDebt.add(loanFee)
119
- const denormalizedNewDebt = totalNormalizedLoan.div(new Decimal(vaultTypeAccount.cumulativeRate.toString()))
120
- this.denormalizedDebt = denormalizedNewDebt.add(new Decimal(this.denormalizedDebt)).floor().toNumber()
127
+ // TODO: There's a chance this needs to be .Floor()
128
+ const totalNormalizedLoan = additionalDebtAsDecimal.add(loanFee)
129
+
130
+ const denormalizedNewDebt = new BN(totalNormalizedLoan.div(vaultTypeAccount.cumulativeRate).floor().toString())
131
+ this.denormalizedDebt = this.denormalizedDebt.add(denormalizedNewDebt)
121
132
  }
122
133
 
123
- public addDeposit(depositAmount: number) {
124
- this.deposited += depositAmount
134
+ /**
135
+ * Repay debt on a vault
136
+ *
137
+ * @param {BN} repayAmount - Normalized debt to repay in (USH) lamports.
138
+ * @param {VaultType} vaultTypeAccount - Vault's vaultType
139
+ *
140
+ */
141
+ public repayDebt(repayAmount: BN, vaultTypeAccount: VaultType) {
142
+ const denormalizedRepayment = new Decimal(repayAmount.toString()).div(vaultTypeAccount.cumulativeRate).floor()
143
+
144
+ this.denormalizedDebt = this.denormalizedDebt.sub(new BN(denormalizedRepayment.toString()))
145
+ }
146
+
147
+ /**
148
+ * Deposit Collateral
149
+ *
150
+ * @param {BN} depositAmount - Amount to deposit in (CollateralMint) lamports
151
+ *
152
+ */
153
+ public depositCollateral(depositAmount: BN) {
154
+ this.deposited = this.deposited.add(depositAmount)
155
+ }
156
+ /**
157
+ * Withdraw Collateral
158
+ *
159
+ * @param {BN} withdrawAmount - Amount to withdraw in (CollateralMint) lamports
160
+ *
161
+ */
162
+ public withdrawCollateral(withdrawAmount: BN) {
163
+ this.deposited = this.deposited.sub(withdrawAmount)
125
164
  }
126
165
 
127
166
  public redeem() {
128
167
  // TODO - Calculate actual redeem amount and adust correctly
129
- this.denormalizedDebt = 0
168
+ this.denormalizedDebt = new BN(0)
130
169
  this.vaultStatus = 'initialized'
131
170
  }
132
171
  public liquidate() {
133
172
  // TODO - Calculate actual liquidate amount and adust correctly
134
- this.denormalizedDebt = 0
173
+ this.denormalizedDebt = new BN(0)
135
174
  this.vaultStatus = 'liquidated'
136
175
  }
137
176
 
138
177
  public updateDebtAndCollateral(vaultTypeAccountData: VaultType) {
139
-
140
- this.denormalizedDebt = vaultTypeAccountData.debtRedistributionProduct
178
+ this.denormalizedDebt = new BN(
179
+ vaultTypeAccountData.debtRedistributionProduct
141
180
  .div(this.debtProductSnapshotBytes)
142
- .mul(new Decimal(this.denormalizedDebt))
143
- .toNumber()
181
+ .mul(new Decimal(this.denormalizedDebt.toString()))
182
+ .floor()
183
+ .toString()
184
+ )
144
185
 
145
186
  const extraCollateralDeposited =
146
- this.denormalizedDebt * vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
147
- this.deposited += extraCollateralDeposited
187
+ new Decimal(this.denormalizedDebt.toString()).mul(
188
+ vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
189
+ ).floor()
190
+ this.deposited = this.deposited.add(new BN(extraCollateralDeposited.toString()))
148
191
 
149
192
  this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator
150
193
  this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct
@@ -156,8 +199,8 @@ export class VaultAccount {
156
199
  arrow = ' <----!!'
157
200
  }
158
201
  let collateralRatio = 'Infinite'
159
- if (this.denormalizedDebt > 0) {
160
- collateralRatio = new Decimal(this.deposited).div(new Decimal(this.denormalizedDebt)).toString()
202
+ if (this.denormalizedDebt.gt(new BN(0))) {
203
+ collateralRatio = new Decimal(this.deposited.toString()).div(new Decimal(this.denormalizedDebt.toString())).toString()
161
204
  }
162
205
 
163
206
  let nextVault = 'None'
@@ -165,9 +208,9 @@ export class VaultAccount {
165
208
  nextVault = this.nextVaultToRedeem.toString().substring(0, 6)
166
209
  }
167
210
 
168
- return `${this.publicKey.toString().substring(0, 6)}. Debt: ${
169
- this.denormalizedDebt
170
- } Collat: ${this.deposited} Ratio: ${collateralRatio} ${arrow} `
211
+ return `${this.publicKey.toString().substring(0, 6)}. Debt: ${this.denormalizedDebt} Collat: ${
212
+ this.deposited
213
+ } Ratio: ${collateralRatio} ${arrow} `
171
214
  }
172
215
  /**
173
216
  * Creates a VaultAccount from a slice of data
@@ -1,4 +1,4 @@
1
- import { Program, Provider } from '@project-serum/anchor'
1
+ import { Program, Provider, BN } from '@project-serum/anchor'
2
2
  import { PublicKey, Signer } from '@solana/web3.js'
3
3
  import _ from 'underscore'
4
4
  import { getVaultSystemStatePublicKey, getVaultTypeOracleAccountPublicKey, HEDGE_PROGRAM_PUBLICKEY } from '../Constants'
@@ -12,13 +12,28 @@ import * as borsh from '@project-serum/borsh'
12
12
  import base58 from 'bs58'
13
13
  import VaultType from '../state/VaultType'
14
14
 
15
+ /**
16
+ * Get the accounts the left and right for re-inserting in the linked list
17
+ *
18
+ * @param {Program<Vault>} program - Anchor program <Vault ILD>
19
+ * @param {PublicKey} vaultTypeAccountPublicKey - Vault Type Account PublicKey
20
+ * @param {PublicKey} vaultPublicKey - Vault Account PublicKey
21
+ * @param {BN} depositAmount - Amount that will be deposited into vault with instruction
22
+ * @param {BN} withdrawAmount - Amount that will be withdrawn from vault with instruction
23
+ * @param {BN} loanAmount - Amount that will be deposited into vault with transaction (sans fees)
24
+ * @param {BN} repayAmount - Amount that will be repaid into vault with transaction
25
+ * @param {boolean} redeem - True if vault is going to be redeemed fully
26
+ * @param {boolean} liquidate - True if vault is going to be liquidated fully
27
+ * @param {VaultAccount[]} cachedVaults - Optional list of cached vaults. Saves a request to the on-chain data.
28
+ */
15
29
  export async function getLinkedListAccounts(
16
30
  program: Program<Vault>,
17
- provider: Provider,
18
31
  vaultTypeAccountPublicKey: PublicKey,
19
32
  vaultPublicKey: PublicKey,
20
- depositAmount: number,
21
- loanAmount: number,
33
+ depositAmount: BN,
34
+ withdrawAmount: BN,
35
+ loanAmount: BN,
36
+ repayAmount: BN,
22
37
  redeem: boolean,
23
38
  liquidate: boolean,
24
39
  cachedVaults?: VaultAccount[]
@@ -58,7 +73,7 @@ export async function getLinkedListAccounts(
58
73
 
59
74
  // Remove any vaults with no debt or collateral
60
75
  vaults = _.filter(vaults, (vault) => {
61
- return vault.denormalizedDebt > 0 && vault.deposited > 0
76
+ return vault.denormalizedDebt.gt(new BN(0)) && vault.deposited.gt(new BN(0))
62
77
  })
63
78
 
64
79
  // Sort them
@@ -117,10 +132,13 @@ export async function getLinkedListAccounts(
117
132
 
118
133
  // Now that we know it's def in the list, iterate the list and update
119
134
  // this vault with the operation we're going to apply
120
- const newNormalizedDebt = new Decimal(loanAmount)
121
135
  vaults[indexBefore].updateDebtAndCollateral(vaultType)
122
- vaults[indexBefore].addDebt(newNormalizedDebt, vaultType)
123
- vaults[indexBefore].addDeposit(depositAmount)
136
+
137
+ vaults[indexBefore].addDebt(loanAmount, vaultType)
138
+ vaults[indexBefore].repayDebt(repayAmount, vaultType)
139
+
140
+ vaults[indexBefore].depositCollateral(depositAmount)
141
+ vaults[indexBefore].withdrawCollateral(withdrawAmount)
124
142
 
125
143
  if (liquidate) {
126
144
  vaults[indexBefore].liquidate()
@@ -129,7 +147,7 @@ export async function getLinkedListAccounts(
129
147
  vaults[indexBefore].redeem()
130
148
  }
131
149
 
132
- if (vaults[indexBefore].denormalizedDebt === 0) {
150
+ if (vaults[indexBefore].denormalizedDebt.isZero()) {
133
151
  vaults.splice(indexBefore, 1)
134
152
  }
135
153
 
@@ -180,12 +198,12 @@ export async function getLinkedListAccounts(
180
198
  // Sort function we can use to sort the vaults
181
199
  // Sorted by collateral ratio. If two are the same, newer vault first
182
200
  function sortVaults(a: VaultAccount, b: VaultAccount) {
183
- const aRatio = a.deposited / a.denormalizedDebt
184
- const bRatio = b.deposited / b.denormalizedDebt
185
- if (aRatio === bRatio) {
201
+ const aRatio = new Decimal(a.deposited.toString()).div(new Decimal(a.denormalizedDebt.toString()))
202
+ const bRatio = new Decimal(b.deposited.toString()).div(new Decimal(b.denormalizedDebt.toString()))
203
+ if (aRatio.equals(bRatio)) {
186
204
  return a.publicKey.toString() > b.publicKey.toString() ? 1 : -1
187
205
  }
188
- return aRatio - bRatio
206
+ return aRatio.greaterThan(bRatio) ? 1 : -1
189
207
  }
190
208
 
191
209
  async function getMiniVaults(program: Program<Vault>, vaultTypePublicKey: PublicKey) {