hedge-web3 0.1.29 → 0.1.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. package/declarations/idl/vault.d.ts +99 -99
  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 +4 -4
  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 +99 -99
  24. package/lib/index.js +1 -0
  25. package/lib/instructions/claimLiquidationPoolPosition.js +22 -24
  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 -32
  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 +19 -19
  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 +2 -2
  50. package/src/idl/vault.ts +198 -198
  51. package/src/index.ts +1 -0
  52. package/src/instructions/claimLiquidationPoolPosition.ts +42 -34
  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 +80 -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 +42 -33
  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
  78. package/tsconfig.json +1 -1
@@ -1,4 +1,3 @@
1
-
2
1
  import { PublicKey } from '@solana/web3.js'
3
2
  import Decimal from 'decimal.js'
4
3
  import { DecimalFromU128 } from '../HedgeDecimal'
@@ -15,11 +14,9 @@ export class StakingPool {
15
14
  ushFeeAccumulator: Decimal
16
15
  collateralFeeAccumulator: [Decimal]
17
16
 
18
- constructor (public poolInfo: any, publicKey: PublicKey) {
17
+ constructor(public poolInfo: any, publicKey: PublicKey) {
19
18
  this.publicKey = publicKey
20
19
  this.deposits = poolInfo.deposits.toNumber()
21
- // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
22
- // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
23
20
  this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
24
21
  this.startTime = poolInfo.startTime.toNumber()
25
22
  this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
@@ -27,8 +24,8 @@ export class StakingPool {
27
24
 
28
25
  this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
29
26
  this.ushFeeAccumulator = DecimalFromU128(poolInfo.ushFeeAccumulator)
30
- this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum: any) => { return DecimalFromU128(sum) })
31
- // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
27
+ this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum: any) => {
28
+ return DecimalFromU128(sum)
29
+ })
32
30
  }
33
-
34
31
  }
@@ -1,4 +1,3 @@
1
-
2
1
  import { PublicKey } from '@solana/web3.js'
3
2
  import Decimal from 'decimal.js'
4
3
  import { DecimalFromU128 } from '../HedgeDecimal'
@@ -17,7 +16,7 @@ export class StakingPoolPosition {
17
16
  public startSolFeeAccumulator: Decimal
18
17
  public open: boolean
19
18
 
20
- constructor (public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
19
+ constructor(public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
21
20
  this.publicKey = key
22
21
  this.pool = stakingPool
23
22
  this.owner = poolPositionInfo.owner
@@ -33,7 +32,7 @@ export class StakingPoolPosition {
33
32
  this.open = poolPositionInfo.state.open !== undefined
34
33
  }
35
34
 
36
- public getCurrentUshFeeReward (): Decimal {
35
+ public getCurrentUshFeeReward(): Decimal {
37
36
  return this.pool.ushFeeAccumulator.minus(this.startUshFeeAccumulator).times(new Decimal(this.deposited))
38
37
  }
39
38
  }
@@ -40,12 +40,8 @@ export class VaultAccount {
40
40
  this.pdaSalt = vault.pdaSalt
41
41
  this.deposited = vault.deposited.toNumber()
42
42
  this.denormalizedDebt = vault.denormalizedDebt.toNumber()
43
- this.debtProductSnapshotBytes = DecimalFromU128(
44
- vault.debtProductSnapshotBytes.toString()
45
- )
46
- this.collateralAccumulatorSnapshotBytes = DecimalFromU128(
47
- vault.collateralAccumulatorSnapshotBytes.toString()
48
- )
43
+ this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
44
+ this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
49
45
  this.collateralType = vault.collateralType
50
46
 
51
47
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]
@@ -90,13 +86,8 @@ export class VaultAccount {
90
86
  .substring(this.publicKey.toString().length - 6)}`
91
87
  }
92
88
 
93
- public addDebt(
94
- newNormalizedDebt: Decimal,
95
- vaultTypeCompoundedInterest: Decimal
96
- ) {
97
- const denormalizedNewDebt = newNormalizedDebt.div(
98
- vaultTypeCompoundedInterest
99
- )
89
+ public addDebt(newNormalizedDebt: Decimal, vaultTypeCompoundedInterest: Decimal) {
90
+ const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest)
100
91
  this.denormalizedDebt += denormalizedNewDebt.toNumber()
101
92
  }
102
93
  public addDeposit(depositAmount: number) {
@@ -114,14 +105,10 @@ export class VaultAccount {
114
105
  this.vaultStatus = 'liquidated'
115
106
  }
116
107
 
117
- public redistribution(vaultTypeAccuntData: any) {
118
- const debtProductCurrent = DecimalFromU128(
119
- vaultTypeAccuntData.debtRedistributionProduct
120
- )
108
+ public updateDebtAndCollateral(vaultTypeAccuntData: any) {
109
+ const debtProductCurrent = DecimalFromU128(vaultTypeAccuntData.debtRedistributionProduct)
121
110
 
122
- const collateralAccumulatorCurrent = DecimalFromU128(
123
- vaultTypeAccuntData.collateralRedistributionAccumulator
124
- )
111
+ const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccuntData.collateralRedistributionAccumulator)
125
112
 
126
113
  this.denormalizedDebt = debtProductCurrent
127
114
  .div(this.debtProductSnapshotBytes)
@@ -130,10 +117,7 @@ export class VaultAccount {
130
117
  .toNumber()
131
118
 
132
119
  const extraCollateralDeposited =
133
- this.denormalizedDebt *
134
- collateralAccumulatorCurrent
135
- .sub(this.collateralAccumulatorSnapshotBytes)
136
- .toNumber()
120
+ this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
137
121
  this.deposited += extraCollateralDeposited
138
122
 
139
123
  this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent
@@ -151,9 +135,6 @@ export class VaultAccount {
151
135
  }
152
136
  return `Vault(${this.vaultNumber}): ${this.publicKey
153
137
  .toString()
154
- .substring(
155
- 0,
156
- 6
157
- )}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
138
+ .substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
158
139
  }
159
140
  }
@@ -1,4 +1,3 @@
1
-
2
1
  import { PublicKey } from '@solana/web3.js'
3
2
  import Decimal from 'decimal.js'
4
3
  import { DecimalFromU128 } from '../HedgeDecimal'
@@ -18,7 +17,7 @@ export class VaultHistoryEvent {
18
17
  timestamp: PublicKey
19
18
  action: VaultHistoryAction
20
19
 
21
- constructor (public account: any, publicKey: PublicKey) {
20
+ constructor(public account: any, publicKey: PublicKey) {
22
21
  this.publicKey = publicKey
23
22
  this.actorAccount = account.actorAccount
24
23
 
@@ -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
package/tsconfig.json CHANGED
@@ -18,4 +18,4 @@
18
18
  "outDir": "./lib",
19
19
  },
20
20
  "include": ["src"]
21
- }
21
+ }