hedge-web3 0.1.28 → 0.1.32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. package/declarations/idl/vault.d.ts +277 -126
  2. package/declarations/index.d.ts +1 -0
  3. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
  4. package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
  5. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
  6. package/declarations/instructions/createStakingPool.d.ts +3 -2
  7. package/declarations/instructions/createVault.d.ts +6 -5
  8. package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
  9. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  10. package/declarations/instructions/depositVault.d.ts +3 -2
  11. package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
  12. package/declarations/instructions/liquidateVault.d.ts +3 -2
  13. package/declarations/instructions/loanVault.d.ts +3 -2
  14. package/declarations/instructions/redeemVault.d.ts +3 -2
  15. package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
  16. package/declarations/instructions/repayVault.d.ts +3 -2
  17. package/declarations/instructions/setHalted.d.ts +3 -2
  18. package/declarations/instructions/setVaultTypeStatus.d.ts +3 -2
  19. package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
  20. package/declarations/instructions/withdrawVault.d.ts +3 -2
  21. package/declarations/state/VaultAccount.d.ts +1 -1
  22. package/declarations/utils/getLinkedListAccounts.d.ts +3 -1
  23. package/lib/idl/vault.js +277 -126
  24. package/lib/index.js +1 -0
  25. package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
  26. package/lib/instructions/claimStakingPoolPosition.js +19 -19
  27. package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
  28. package/lib/instructions/createStakingPool.js +17 -18
  29. package/lib/instructions/createVault.js +28 -31
  30. package/lib/instructions/depositLiquidationPool.js +17 -18
  31. package/lib/instructions/depositStakingPool.js +16 -18
  32. package/lib/instructions/depositVault.js +25 -27
  33. package/lib/instructions/initHedgeFoundation.js +17 -19
  34. package/lib/instructions/initHedgeFoundationTokens.js +15 -15
  35. package/lib/instructions/liquidateVault.js +32 -33
  36. package/lib/instructions/loanVault.js +23 -23
  37. package/lib/instructions/redeemVault.js +24 -24
  38. package/lib/instructions/refreshOraclePrice.js +17 -17
  39. package/lib/instructions/repayVault.js +23 -23
  40. package/lib/instructions/setHalted.js +8 -9
  41. package/lib/instructions/setVaultTypeStatus.js +9 -10
  42. package/lib/instructions/withdrawStakingPool.js +22 -24
  43. package/lib/instructions/withdrawVault.js +23 -23
  44. package/lib/state/LiquidationPoolEra.js +3 -1
  45. package/lib/state/LiquidationPosition.js +0 -7
  46. package/lib/state/StakingPool.js +3 -4
  47. package/lib/state/VaultAccount.js +2 -5
  48. package/lib/utils/getLinkedListAccounts.js +24 -16
  49. package/package.json +1 -1
  50. package/src/idl/vault.ts +554 -252
  51. package/src/index.ts +1 -0
  52. package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
  53. package/src/instructions/claimStakingPoolPosition.ts +45 -25
  54. package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
  55. package/src/instructions/createStakingPool.ts +37 -35
  56. package/src/instructions/createVault.ts +81 -125
  57. package/src/instructions/depositLiquidationPool.ts +45 -26
  58. package/src/instructions/depositStakingPool.ts +32 -24
  59. package/src/instructions/depositVault.ts +57 -86
  60. package/src/instructions/initHedgeFoundation.ts +42 -43
  61. package/src/instructions/initHedgeFoundationTokens.ts +38 -39
  62. package/src/instructions/liquidateVault.ts +42 -65
  63. package/src/instructions/loanVault.ts +51 -69
  64. package/src/instructions/redeemVault.ts +83 -47
  65. package/src/instructions/refreshOraclePrice.ts +41 -32
  66. package/src/instructions/repayVault.ts +45 -65
  67. package/src/instructions/setHalted.ts +32 -24
  68. package/src/instructions/setVaultTypeStatus.ts +32 -24
  69. package/src/instructions/withdrawStakingPool.ts +44 -30
  70. package/src/instructions/withdrawVault.ts +58 -82
  71. package/src/state/LiquidationPoolEra.ts +4 -3
  72. package/src/state/LiquidationPosition.ts +0 -27
  73. package/src/state/StakingPool.ts +4 -7
  74. package/src/state/StakingPoolPosition.ts +2 -3
  75. package/src/state/VaultAccount.ts +9 -28
  76. package/src/state/VaultHistoryEvent.ts +1 -2
  77. package/src/utils/getLinkedListAccounts.ts +31 -30
@@ -1,27 +1,24 @@
1
1
  import { Program, Provider } from '@project-serum/anchor'
2
2
  import { PublicKey, Signer } from '@solana/web3.js'
3
3
  import _ from 'underscore'
4
- import {
5
- getVaultSystemStatePublicKey,
6
- getVaultTypeOracleAccountPublicKey,
7
- } from '../Constants'
4
+ import { getVaultSystemStatePublicKey, getVaultTypeOracleAccountPublicKey } from '../Constants'
8
5
 
9
6
  import { DecimalFromU128 } from '../HedgeDecimal'
10
7
  import { VaultAccount } from '../state/VaultAccount'
11
8
  import Decimal from 'decimal.js'
9
+ import { Vault } from 'idl/vault'
12
10
  export async function getLinkedListAccounts(
13
- program: Program,
11
+ program: Program<Vault>,
14
12
  provider: Provider,
15
13
  vaultTypeAccountPublicKey: PublicKey,
16
14
  vaultPublicKey: PublicKey,
17
15
  depositAmount: number,
18
16
  loanAmount: number,
19
17
  redeem: boolean,
20
- liquidate: boolean
21
- ): Promise<[PublicKey, PublicKey, PublicKey]> {
22
- const vaultTypeAccount = await program.account.vaultType.fetch(
23
- vaultTypeAccountPublicKey
24
- )
18
+ liquidate: boolean,
19
+ cachedVaults?: VaultAccount[]
20
+ ): Promise<[PublicKey, PublicKey, PublicKey, VaultAccount[]]> {
21
+ const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
25
22
 
26
23
  // Default for null is the vault itself, so set them all to this vault
27
24
  let oldSmallerPublicKey = vaultPublicKey
@@ -32,21 +29,27 @@ export async function getLinkedListAccounts(
32
29
  const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
33
30
 
34
31
  // 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
- })
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
+ }
50
53
 
51
54
  // Filter out the account that are not the same vault type
52
55
  vaults = _.filter(vaults, (vault) => {
@@ -89,9 +92,8 @@ export async function getLinkedListAccounts(
89
92
  // Now that we know it's def in the list, iterate the list and update
90
93
  // this vault with the opeation we're going to apply
91
94
  const newNormalizedDebt = new Decimal(loanAmount)
92
- const vaultTypeCompoundedInterest = DecimalFromU128(
93
- vaultTypeAccount.cumulativeRate.toString()
94
- )
95
+ const vaultTypeCompoundedInterest = DecimalFromU128(vaultTypeAccount.cumulativeRate.toString())
96
+ vaults[indexBefore].updateDebtAndCollateral(vaultTypeAccount)
95
97
  vaults[indexBefore].addDebt(newNormalizedDebt, vaultTypeCompoundedInterest)
96
98
  vaults[indexBefore].addDeposit(depositAmount)
97
99
 
@@ -101,7 +103,6 @@ export async function getLinkedListAccounts(
101
103
  if (redeem) {
102
104
  vaults[indexBefore].redeem()
103
105
  }
104
- vaults[indexBefore].redistribution(vaultTypeAccount)
105
106
 
106
107
  if (vaults[indexBefore].denormalizedDebt === 0) {
107
108
  vaults.splice(indexBefore, 1)
@@ -141,7 +142,7 @@ export async function getLinkedListAccounts(
141
142
  // console.log('newSmallerPublicKey', newSmallerPublicKey.toString())
142
143
  // console.log('newLargerPublicKey', newLargerPublicKey.toString())
143
144
 
144
- return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey]
145
+ return [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, vaults]
145
146
  }
146
147
 
147
148
  // Sort function we can use to sort the vaults