hedge-web3 0.1.27 → 0.1.31

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