hedge-web3 0.1.26 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/declarations/Constants.d.ts +1 -1
  2. package/declarations/idl/vault.d.ts +1056 -768
  3. package/declarations/index.d.ts +2 -0
  4. package/declarations/instructions/depositVault.d.ts +1 -1
  5. package/declarations/instructions/liquidateVault.d.ts +1 -1
  6. package/declarations/instructions/loanVault.d.ts +1 -1
  7. package/declarations/instructions/redeemVault.d.ts +1 -1
  8. package/declarations/instructions/repayVault.d.ts +1 -1
  9. package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
  10. package/declarations/instructions/withdrawVault.d.ts +1 -1
  11. package/declarations/state/VaultAccount.d.ts +8 -0
  12. package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
  13. package/lib/Constants.js +3 -3
  14. package/lib/idl/vault.js +1054 -766
  15. package/lib/index.js +2 -0
  16. package/lib/instructions/createStakingPool.js +2 -2
  17. package/lib/instructions/depositVault.js +14 -7
  18. package/lib/instructions/liquidateVault.js +9 -4
  19. package/lib/instructions/loanVault.js +9 -4
  20. package/lib/instructions/redeemVault.js +7 -2
  21. package/lib/instructions/repayVault.js +9 -4
  22. package/lib/instructions/setVaultTypeStatus.js +38 -0
  23. package/lib/instructions/withdrawVault.js +12 -7
  24. package/lib/state/VaultAccount.js +54 -1
  25. package/lib/utils/Errors.js +2 -2
  26. package/lib/utils/getLinkedListAccounts.js +131 -0
  27. package/package.json +3 -1
  28. package/src/Constants.ts +73 -31
  29. package/src/idl/vault.ts +2075 -1499
  30. package/src/index.ts +3 -0
  31. package/src/instructions/createStakingPool.ts +1 -2
  32. package/src/instructions/depositVault.ts +97 -22
  33. package/src/instructions/liquidateVault.ts +118 -21
  34. package/src/instructions/loanVault.ts +91 -19
  35. package/src/instructions/redeemVault.ts +23 -0
  36. package/src/instructions/repayVault.ts +84 -19
  37. package/src/instructions/setVaultTypeStatus.ts +50 -0
  38. package/src/instructions/withdrawVault.ts +99 -21
  39. package/src/state/StakingPool.ts +0 -4
  40. package/src/state/VaultAccount.ts +86 -10
  41. package/src/utils/Errors.ts +2 -2
  42. package/src/utils/getLinkedListAccounts.ts +156 -0
  43. package/declarations/idl/idl.d.ts +0 -2
  44. package/lib/idl/idl.js +0 -1475
  45. package/src/idl/idl.ts +0 -1474
@@ -1,9 +1,28 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
2
+ import {
3
+ getOrCreateAssociatedTokenAccount,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token'
6
+ import {
7
+ Keypair,
8
+ PublicKey,
9
+ sendAndConfirmTransaction,
10
+ Signer,
11
+ SystemProgram,
12
+ Transaction,
13
+ TransactionInstruction,
14
+ } from '@solana/web3.js'
15
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
16
+ import {
17
+ findAssociatedTokenAddress,
18
+ getHedgeMintPublicKey,
19
+ getPoolPublicKeyForMint,
20
+ getVaultTypeAccountPublicKey,
21
+ getUshMintPublicKey,
22
+ getVaultSystemStatePublicKey,
23
+ } from '../Constants'
5
24
 
6
- export async function repayVault (
25
+ export async function repayVault(
7
26
  program: Program,
8
27
  provider: Provider,
9
28
  payer: Signer,
@@ -14,13 +33,40 @@ export async function repayVault (
14
33
  const ushMintPublickey = await getUshMintPublicKey()
15
34
 
16
35
  // Prep the user to get USH back out at some point
17
- const payerUshAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
36
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
37
+ provider.connection,
38
+ payer,
39
+ ushMintPublickey,
40
+ payer.publicKey
41
+ )
18
42
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
19
43
 
20
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
21
- const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
22
- const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint)
23
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
44
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
45
+ vaultAccount.collateralType
46
+ )
47
+ const vaultTypeAccount = await program.account.vaultType.fetch(
48
+ vaultTypeAccountPublicKey
49
+ )
50
+ const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
51
+ vaultTypeAccountPublicKey,
52
+ vaultTypeAccount.collateralMint
53
+ )
54
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
55
+ vaultPublicKey,
56
+ vaultTypeAccount.collateralMint
57
+ )
58
+
59
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
60
+ await getLinkedListAccounts(
61
+ program,
62
+ provider,
63
+ vaultTypeAccountPublicKey,
64
+ vaultPublicKey,
65
+ 0,
66
+ repayAmount * -1,
67
+ false,
68
+ false
69
+ )
24
70
 
25
71
  const history = Keypair.generate()
26
72
  const transaction = new Transaction().add(
@@ -33,15 +79,23 @@ export async function repayVault (
33
79
  history.publicKey,
34
80
  vaultTypeAccountPublicKey,
35
81
  vaultTypeAssociatedTokenAccount,
82
+ oldSmallerPublicKey,
83
+ newSmallerPublicKey,
84
+ newLargerPublicKey,
36
85
  repayAmount,
37
86
  overrideTime
38
87
  )
39
88
  )
40
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
89
+ await sendAndConfirmTransaction(
90
+ provider.connection,
91
+ transaction,
92
+ [payer, history],
93
+ provider.opts
94
+ )
41
95
  return vaultPublicKey
42
96
  }
43
97
 
44
- export async function repayVaultInstruction (
98
+ export async function repayVaultInstruction(
45
99
  program: Program,
46
100
  payerPublicKey: PublicKey,
47
101
  ownerUshAccount: PublicKey,
@@ -50,17 +104,23 @@ export async function repayVaultInstruction (
50
104
  historyPublicKey: PublicKey,
51
105
  vaultTypeAccount: PublicKey,
52
106
  vaultTypeAssociatedTokenAccount: PublicKey,
107
+ oldSmallerPublicKey: PublicKey,
108
+ newSmallerPublicKey: PublicKey,
109
+ newLargerPublicKey: PublicKey,
53
110
  repayAmount: number,
54
111
  overrideTime?: number
55
112
  ): Promise<TransactionInstruction> {
56
113
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
57
114
  const ushMintPublickey = await getUshMintPublicKey()
58
115
  const hedgeMintPublickey = await getHedgeMintPublicKey()
59
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
60
- const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
61
- hedgeStakingPoolPublicKey,
62
- ushMintPublickey
116
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
117
+ hedgeMintPublickey
63
118
  )
119
+ const hedgeStakingPoolAssociatedUshTokenAccount =
120
+ await findAssociatedTokenAddress(
121
+ hedgeStakingPoolPublicKey,
122
+ ushMintPublickey
123
+ )
64
124
 
65
125
  return program.instruction.repayVault(
66
126
  new BN(repayAmount),
@@ -74,13 +134,18 @@ export async function repayVaultInstruction (
74
134
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
75
135
  history: historyPublicKey,
76
136
  feePool: hedgeStakingPoolPublicKey,
77
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
137
+ feePoolAssociatedUshTokenAccount:
138
+ hedgeStakingPoolAssociatedUshTokenAccount,
78
139
  ushMint: ushMintPublickey,
79
140
  vaultOwner: payerPublicKey,
141
+ oldSmallerVaultInfo: oldSmallerPublicKey,
142
+ newSmallerVaultInfo: newSmallerPublicKey,
143
+ newLargerVaultInfo: newLargerPublicKey,
80
144
  ownerUshAccount: ownerUshAccount,
81
145
  tokenProgram: TOKEN_PROGRAM_ID,
82
- systemProgram: SystemProgram.programId
146
+ systemProgram: SystemProgram.programId,
83
147
  },
84
- signers: []
85
- })
148
+ signers: [],
149
+ }
150
+ )
86
151
  }
@@ -0,0 +1,50 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
3
+ import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
4
+
5
+ import { v4 as uuidv4 } from 'uuid'
6
+ import { parseAnchorErrors } from '../utils/Errors'
7
+
8
+ export async function setVaultTypeStatus (
9
+ program: Program,
10
+ provider: Provider,
11
+ payer: Signer,
12
+ vaultTypeAccount: PublicKey,
13
+ deprecated: boolean
14
+ ): Promise<PublicKey> {
15
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
16
+
17
+
18
+ const transaction = new Transaction().add(
19
+ await setVaultTypeStatusInstruction(
20
+ program,
21
+ vaultSystemStatePublicKey,
22
+ payer.publicKey,
23
+ vaultTypeAccount,
24
+ deprecated,
25
+ )
26
+ )
27
+
28
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider?.opts).catch(parseAnchorErrors)
29
+ return vaultSystemStatePublicKey
30
+ }
31
+
32
+ export async function setVaultTypeStatusInstruction(
33
+ program: Program,
34
+ vaultSystemStatePublicKey: PublicKey,
35
+ payerPublicKey: PublicKey,
36
+ vaultTypeAccount: PublicKey,
37
+ deprecated: boolean
38
+ ): Promise<TransactionInstruction> {
39
+ const ix = program.instruction.setVaultTypeStatus(
40
+ deprecated,
41
+ {
42
+ accounts: {
43
+ payer: payerPublicKey,
44
+ vaultSystemState: vaultSystemStatePublicKey,
45
+ vaultType: vaultTypeAccount,
46
+ },
47
+ signers: []
48
+ })
49
+ return ix
50
+ }
@@ -1,10 +1,29 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
2
+ import {
3
+ getOrCreateAssociatedTokenAccount,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token'
3
6
  import { TokenInstructions } from '@project-serum/serum'
4
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
7
+ import {
8
+ Keypair,
9
+ PublicKey,
10
+ sendAndConfirmTransaction,
11
+ Signer,
12
+ SystemProgram,
13
+ Transaction,
14
+ TransactionInstruction,
15
+ } from '@solana/web3.js'
16
+ import {
17
+ findAssociatedTokenAddress,
18
+ getVaultTypeAccountPublicKey,
19
+ getUshMintPublicKey,
20
+ getVaultSystemStatePublicKey,
21
+ getPoolPublicKeyForMint,
22
+ getHedgeMintPublicKey,
23
+ } from '../Constants'
24
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
6
25
 
7
- export async function withdrawVault (
26
+ export async function withdrawVault(
8
27
  program: Program,
9
28
  provider: Provider,
10
29
  payer: Signer,
@@ -15,23 +34,66 @@ export async function withdrawVault (
15
34
  const ushMintPublickey = await getUshMintPublicKey()
16
35
 
17
36
  // Prep the user to get USH back out at some point
18
- await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
37
+ await getOrCreateAssociatedTokenAccount(
38
+ provider.connection,
39
+ payer,
40
+ ushMintPublickey,
41
+ payer.publicKey
42
+ )
19
43
 
20
44
  const history = Keypair.generate()
21
45
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
22
46
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
23
- const vaultTypeAccount = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
24
- const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true)
47
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
48
+ vaultAccount.collateralType
49
+ )
50
+ const vaultAssociatedCollateralAccount =
51
+ await getOrCreateAssociatedTokenAccount(
52
+ provider.connection,
53
+ payer,
54
+ TokenInstructions.WRAPPED_SOL_MINT,
55
+ vaultPublicKey,
56
+ true
57
+ )
25
58
 
26
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
27
- const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccount, true)
59
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(
60
+ vaultTypeAccountPublicKey
61
+ )
62
+ const vaultTypeAssociatedTokenAccount =
63
+ await getOrCreateAssociatedTokenAccount(
64
+ provider.connection,
65
+ payer,
66
+ vaultTypeAccountInfo.collateralMint,
67
+ vaultTypeAccountPublicKey,
68
+ true
69
+ )
28
70
 
29
- const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
30
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
31
- const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
32
- hedgeStakingPoolPublicKey,
33
- ushMintPublickey
71
+ const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
72
+ provider.connection,
73
+ payer,
74
+ vaultTypeAccountInfo.collateralMint,
75
+ payer.publicKey
34
76
  )
77
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
78
+ await getHedgeMintPublicKey()
79
+ )
80
+ const hedgeStakingPoolAssociatedUshTokenAccount =
81
+ await findAssociatedTokenAddress(
82
+ hedgeStakingPoolPublicKey,
83
+ ushMintPublickey
84
+ )
85
+
86
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
87
+ await getLinkedListAccounts(
88
+ program,
89
+ provider,
90
+ vaultTypeAccountPublicKey,
91
+ vaultPublicKey,
92
+ withdrawAmount * -1,
93
+ 0,
94
+ false,
95
+ false
96
+ )
35
97
 
36
98
  const transaction = new Transaction().add(
37
99
  await withdrawVaultInstruction(
@@ -41,21 +103,29 @@ export async function withdrawVault (
41
103
  destinationTokenAccount.address,
42
104
  vaultPublicKey,
43
105
  vaultAssociatedCollateralAccount.address,
44
- vaultTypeAccount,
106
+ vaultTypeAccountPublicKey,
45
107
  vaultTypeAssociatedTokenAccount.address,
46
108
  hedgeStakingPoolPublicKey,
47
109
  hedgeStakingPoolAssociatedUshTokenAccount,
48
110
  ushMintPublickey,
49
111
  history.publicKey,
112
+ oldSmallerPublicKey,
113
+ newSmallerPublicKey,
114
+ newLargerPublicKey,
50
115
  withdrawAmount,
51
116
  overrideTime
52
117
  )
53
118
  )
54
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
119
+ await sendAndConfirmTransaction(
120
+ provider.connection,
121
+ transaction,
122
+ [payer, history],
123
+ provider.opts
124
+ )
55
125
  return vaultPublicKey
56
126
  }
57
127
 
58
- export async function withdrawVaultInstruction (
128
+ export async function withdrawVaultInstruction(
59
129
  program: Program,
60
130
  vaultSystemStatePublicKey: PublicKey,
61
131
  payerPublicKey: PublicKey,
@@ -68,6 +138,9 @@ export async function withdrawVaultInstruction (
68
138
  hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
69
139
  ushMint: PublicKey,
70
140
  historyPublicKey: PublicKey,
141
+ oldSmallerPublicKey: PublicKey,
142
+ newSmallerPublicKey: PublicKey,
143
+ newLargerPublicKey: PublicKey,
71
144
  withdrawAmount: number,
72
145
  overrideTime?: number
73
146
  ): Promise<TransactionInstruction> {
@@ -82,14 +155,19 @@ export async function withdrawVaultInstruction (
82
155
  vault: vaultPublickey,
83
156
  vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
84
157
  feePool: hedgeStakingPoolPublicKey,
85
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
158
+ feePoolAssociatedUshTokenAccount:
159
+ hedgeStakingPoolAssociatedUshTokenAccount,
86
160
  ushMint: ushMint,
87
161
  history: historyPublicKey,
88
162
  vaultOwner: payerPublicKey,
89
163
  destinationTokenAccount: destinationTokenAccount,
164
+ oldSmallerVaultInfo: oldSmallerPublicKey,
165
+ newSmallerVaultInfo: newSmallerPublicKey,
166
+ newLargerVaultInfo: newLargerPublicKey,
90
167
  tokenProgram: TOKEN_PROGRAM_ID,
91
- systemProgram: SystemProgram.programId
168
+ systemProgram: SystemProgram.programId,
92
169
  },
93
- signers: []
94
- })
170
+ signers: [],
171
+ }
172
+ )
95
173
  }
@@ -31,8 +31,4 @@ export class StakingPool {
31
31
  // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
32
32
  }
33
33
 
34
- // updateFeeAccumulator () {
35
- // this.feeAccumulator = (this.totalFeesNow - this.totalFeesPrevious) / this.deposits
36
- // this.totalFeesPrevious = this.totalFeesNow
37
- // }
38
34
  }
@@ -14,13 +14,16 @@ export class VaultAccount {
14
14
 
15
15
  /** The public key of the vault owner. */
16
16
  pdaSalt: string
17
-
17
+
18
18
  /** The deposited collateral of the vault (in SOL Lamports). */
19
19
  deposited: number
20
20
 
21
21
  /** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
22
22
  denormalizedDebt: number
23
23
 
24
+ /** The ordered number of when this vault was created. */
25
+ vaultNumber: number
26
+
24
27
  debtProductSnapshotBytes: Decimal
25
28
 
26
29
  collateralAccumulatorSnapshotBytes: Decimal
@@ -30,14 +33,19 @@ export class VaultAccount {
30
33
  /** Current State of the vautl ("Open", "Closed", "Liquidated") */
31
34
  vaultStatus: string
32
35
 
33
- constructor (vault: any, publicKey: PublicKey) {
36
+ constructor(vault: any, publicKey: PublicKey) {
34
37
  this.publicKey = publicKey
35
38
  this.vaultOwner = vault.vaultOwner
39
+ this.vaultNumber = vault.vaultNumber.toNumber()
36
40
  this.pdaSalt = vault.pdaSalt
37
41
  this.deposited = vault.deposited.toNumber()
38
42
  this.denormalizedDebt = vault.denormalizedDebt.toNumber()
39
- this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
40
- this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
43
+ this.debtProductSnapshotBytes = DecimalFromU128(
44
+ vault.debtProductSnapshotBytes.toString()
45
+ )
46
+ this.collateralAccumulatorSnapshotBytes = DecimalFromU128(
47
+ vault.collateralAccumulatorSnapshotBytes.toString()
48
+ )
41
49
  this.collateralType = vault.collateralType
42
50
 
43
51
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]
@@ -49,8 +57,7 @@ export class VaultAccount {
49
57
  * @param publicKey the publicKey to check against the vault owner
50
58
  * @returns true if publicKey matches the owner publicKey
51
59
  */
52
- public isOwnedBy (publicKey: PublicKey): boolean {
53
-
60
+ public isOwnedBy(publicKey: PublicKey): boolean {
54
61
  return publicKey && publicKey.toString() === this.vaultOwner.toString()
55
62
  }
56
63
 
@@ -59,7 +66,7 @@ export class VaultAccount {
59
66
  *
60
67
  * @returns collateral value in SOL
61
68
  */
62
- public inSol (): number {
69
+ public inSol(): number {
63
70
  return this.deposited / LAMPORTS_PER_SOL
64
71
  }
65
72
 
@@ -68,7 +75,7 @@ export class VaultAccount {
68
75
  *
69
76
  * @returns debt value in USH
70
77
  */
71
- public inUsd (): number {
78
+ public inUsd(): number {
72
79
  return this.denormalizedDebt / LAMPORTS_PER_SOL
73
80
  }
74
81
 
@@ -77,7 +84,76 @@ export class VaultAccount {
77
84
  *
78
85
  * @returns example: `1b6ca...azy71s`
79
86
  */
80
- public toDisplayString (): string {
81
- return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`
87
+ public toDisplayString(): string {
88
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey
89
+ .toString()
90
+ .substring(this.publicKey.toString().length - 6)}`
91
+ }
92
+
93
+ public addDebt(
94
+ newNormalizedDebt: Decimal,
95
+ vaultTypeCompoundedInterest: Decimal
96
+ ) {
97
+ const denormalizedNewDebt = newNormalizedDebt.div(
98
+ vaultTypeCompoundedInterest
99
+ )
100
+ this.denormalizedDebt += denormalizedNewDebt.toNumber()
101
+ }
102
+ public addDeposit(depositAmount: number) {
103
+ this.deposited += depositAmount
104
+ }
105
+
106
+ public redeem() {
107
+ // TODO - Calculate actual redeem amount and adust correctly
108
+ this.denormalizedDebt = 0
109
+ this.vaultStatus = 'initialized'
110
+ }
111
+ public liquidate() {
112
+ // TODO - Calculate actual liquidate amount and adust correctly
113
+ this.denormalizedDebt = 0
114
+ this.vaultStatus = 'liquidated'
115
+ }
116
+
117
+ public redistribution(vaultTypeAccuntData: any) {
118
+ const debtProductCurrent = DecimalFromU128(
119
+ vaultTypeAccuntData.debtRedistributionProduct
120
+ )
121
+
122
+ const collateralAccumulatorCurrent = DecimalFromU128(
123
+ vaultTypeAccuntData.collateralRedistributionAccumulator
124
+ )
125
+
126
+ this.denormalizedDebt = debtProductCurrent
127
+ .div(this.debtProductSnapshotBytes)
128
+ .mul(new Decimal(this.denormalizedDebt))
129
+ // .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
130
+ .toNumber()
131
+
132
+ const extraCollateralDeposited =
133
+ this.denormalizedDebt *
134
+ collateralAccumulatorCurrent
135
+ .sub(this.collateralAccumulatorSnapshotBytes)
136
+ .toNumber()
137
+ this.deposited += extraCollateralDeposited
138
+
139
+ this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent
140
+ this.debtProductSnapshotBytes = debtProductCurrent
141
+ }
142
+
143
+ public toString(highlight: PublicKey): string {
144
+ let arrow = ''
145
+ if (this.publicKey.toString() === highlight.toString()) {
146
+ arrow = ' <----'
147
+ }
148
+ let collateralRatio = 'Infinite'
149
+ if (this.denormalizedDebt > 0) {
150
+ collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8)
151
+ }
152
+ return `Vault(${this.vaultNumber}): ${this.publicKey
153
+ .toString()
154
+ .substring(
155
+ 0,
156
+ 6
157
+ )}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
82
158
  }
83
159
  }
@@ -1,8 +1,8 @@
1
1
  import { parseIdlErrors, ProgramError } from '@project-serum/anchor'
2
- import { vaultIdl } from '../idl/idl'
2
+ import { IDL } from '../idl/vault'
3
3
 
4
4
  export function parseAnchorErrors (error: any): void {
5
- const idlErrors = parseIdlErrors(vaultIdl)
5
+ const idlErrors = parseIdlErrors(IDL)
6
6
  const parsedError = ProgramError.parse(error, idlErrors)
7
7
  if (parsedError !== null) {
8
8
  throw parsedError