hedge-web3 0.1.26 → 0.1.29

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,156 @@
1
+ import { Program, Provider } from '@project-serum/anchor'
2
+ import { PublicKey, Signer } from '@solana/web3.js'
3
+ import _ from 'underscore'
4
+ import {
5
+ getVaultSystemStatePublicKey,
6
+ getVaultTypeOracleAccountPublicKey,
7
+ } from '../Constants'
8
+
9
+ import { DecimalFromU128 } from '../HedgeDecimal'
10
+ import { VaultAccount } from '../state/VaultAccount'
11
+ import Decimal from 'decimal.js'
12
+ export async function getLinkedListAccounts(
13
+ program: Program,
14
+ provider: Provider,
15
+ vaultTypeAccountPublicKey: PublicKey,
16
+ vaultPublicKey: PublicKey,
17
+ depositAmount: number,
18
+ loanAmount: number,
19
+ redeem: boolean,
20
+ liquidate: boolean
21
+ ): Promise<[PublicKey, PublicKey, PublicKey]> {
22
+ const vaultTypeAccount = await program.account.vaultType.fetch(
23
+ vaultTypeAccountPublicKey
24
+ )
25
+
26
+ // Default for null is the vault itself, so set them all to this vault
27
+ let oldSmallerPublicKey = vaultPublicKey
28
+ let newSmallerPublicKey = vaultPublicKey
29
+ let newLargerPublicKey = vaultPublicKey
30
+
31
+ const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
32
+ const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
33
+
34
+ // Load all the vaults
35
+ let allVaults =
36
+ (await program.account.vault
37
+ .all([
38
+ // {
39
+ // memcmp: { bytes: bs58.encode(inputCollateralType), offset: 8 + 32 + 8 },
40
+ // },
41
+ ])
42
+ .catch((error) => {
43
+ console.log('error', error)
44
+ })) || []
45
+
46
+ // Load them into our account objects
47
+ let vaults = allVaults.map((vault) => {
48
+ return new VaultAccount(vault.account, vault.publicKey)
49
+ })
50
+
51
+ // Filter out the account that are not the same vault type
52
+ vaults = _.filter(vaults, (vault) => {
53
+ return vault.collateralType === vaultTypeAccount.collateralType
54
+ })
55
+
56
+ // Filter out the accounts that are not open
57
+ vaults = _.filter(vaults, (vault) => {
58
+ return vault.vaultStatus === 'open'
59
+ })
60
+
61
+ // Sort them
62
+ vaults.sort(sortVaults)
63
+
64
+ // Find the location of the vault before the operation
65
+ let indexBefore = -1
66
+ vaults.forEach((vault, index) => {
67
+ if (vault.publicKey.toString() === vaultPublicKey.toString()) {
68
+ indexBefore = index
69
+ }
70
+ })
71
+
72
+ // If we found it before, set the old smaller vault the one to the left
73
+ if (indexBefore > 0) {
74
+ oldSmallerPublicKey = vaults[indexBefore - 1].publicKey
75
+ }
76
+
77
+ // Pretty print all the vaults before the operation
78
+ // console.log('Sorted open vaults before')
79
+ // vaults.forEach((vault) => {
80
+ // console.log(vault.toString(vaultPublicKey))
81
+ // })
82
+
83
+ // If it wasn't in the list, add it now
84
+ if (indexBefore < 0) {
85
+ vaults.push(thisVault)
86
+ indexBefore = vaults.length - 1
87
+ }
88
+
89
+ // Now that we know it's def in the list, iterate the list and update
90
+ // this vault with the opeation we're going to apply
91
+ const newNormalizedDebt = new Decimal(loanAmount)
92
+ const vaultTypeCompoundedInterest = DecimalFromU128(
93
+ vaultTypeAccount.cumulativeRate.toString()
94
+ )
95
+ vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
96
+ vaults[indexBefore].addDeposit(depositAmount)
97
+
98
+ if (liquidate) {
99
+ vaults[indexBefore].liquidate()
100
+ }
101
+ if (redeem) {
102
+ vaults[indexBefore].redeem()
103
+ }
104
+ vaults[indexBefore].redistribution(vaultTypeAccount)
105
+
106
+ if (vaults[indexBefore].denormalizedDebt === 0) {
107
+ vaults.splice(indexBefore, 1)
108
+ }
109
+
110
+ // Sort it again since we've changed one vault
111
+ vaults = vaults.sort(sortVaults)
112
+
113
+ // Pretty print the list again
114
+ // console.log('Sorted open vaults with new debt added')
115
+ // vaults.forEach((vault) => {
116
+ // console.log(vault.toString(vaultPublicKey))
117
+ // })
118
+
119
+ // Search for the vaults new position
120
+ let indexAfter = -1
121
+ vaults.forEach((vault, index) => {
122
+ if (vault.publicKey.toString() === vaultPublicKey.toString()) {
123
+ indexAfter = index
124
+ }
125
+ })
126
+
127
+ // Print where it moved from / to
128
+ // console.log('Index Before', indexBefore)
129
+ // console.log('Index After', indexAfter)
130
+
131
+ // Save references to the new left and right
132
+ if (indexAfter > 0) {
133
+ newSmallerPublicKey = vaults[indexAfter - 1].publicKey
134
+ }
135
+ if (indexAfter < vaults.length - 1 && indexAfter >= 0) {
136
+ newLargerPublicKey = vaults[indexAfter + 1].publicKey
137
+ }
138
+
139
+ // Print out the new left/right
140
+ // console.log('oldSmallerPublicKey', oldSmallerPublicKey.toString())
141
+ // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
142
+ // console.log('newLargerPublicKey', newLargerPublicKey.toString())
143
+
144
+ return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey]
145
+ }
146
+
147
+ // Sort function we can use to sort the vaults
148
+ // Sorted by collateral ratio. If two are the same, newer vault first
149
+ function sortVaults(a: VaultAccount, b: VaultAccount) {
150
+ const aRatio = a.deposited / a.denormalizedDebt
151
+ const bRatio = b.deposited / b.denormalizedDebt
152
+ if (aRatio === bRatio) {
153
+ return b.vaultNumber - a.vaultNumber
154
+ }
155
+ return aRatio - bRatio
156
+ }
@@ -1,2 +0,0 @@
1
- import { Idl } from '@project-serum/anchor';
2
- export declare const vaultIdl: Idl;