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,58 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import {
3
+ Keypair,
4
+ PublicKey,
5
+ sendAndConfirmTransaction,
6
+ Signer,
7
+ SystemProgram,
8
+ SYSVAR_RENT_PUBKEY,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import {
13
+ findAssociatedTokenAddress,
14
+ findVaultAddress,
15
+ getVaultTypeAccountPublicKey,
16
+ getUshMintPublicKey,
17
+ getVaultSystemStatePublicKey,
18
+ getPoolPublicKeyForMint,
19
+ getHedgeMintPublicKey,
20
+ } from '../Constants'
21
+
22
+ import { v4 as uuidv4 } from 'uuid'
23
+ import { parseAnchorErrors } from '../utils/Errors'
24
+ import { Vault } from 'idl/vault'
25
+
26
+ export async function setVaultTypeStatus(
27
+ program: Program<Vault>,
28
+ provider: Provider,
29
+ payer: Signer,
30
+ vaultTypeAccount: PublicKey,
31
+ deprecated: boolean
32
+ ): Promise<PublicKey> {
33
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
34
+
35
+ const transaction = new Transaction().add(
36
+ await setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated)
37
+ )
38
+
39
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
40
+ return vaultSystemStatePublicKey
41
+ }
42
+
43
+ export async function setVaultTypeStatusInstruction(
44
+ program: Program<Vault>,
45
+ vaultSystemStatePublicKey: PublicKey,
46
+ payerPublicKey: PublicKey,
47
+ vaultTypeAccount: PublicKey,
48
+ deprecated: boolean
49
+ ): Promise<TransactionInstruction> {
50
+ return await program.methods
51
+ .setVaultTypeStatus(deprecated)
52
+ .accounts({
53
+ payer: payerPublicKey,
54
+ vaultSystemState: vaultSystemStatePublicKey,
55
+ vaultType: vaultTypeAccount,
56
+ })
57
+ .instruction()
58
+ }
@@ -1,11 +1,27 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ SYSVAR_RENT_PUBKEY,
10
+ Transaction,
11
+ TransactionInstruction,
12
+ } from '@solana/web3.js'
4
13
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
14
+ import {
15
+ findAssociatedTokenAddress,
16
+ getHedgeMintPublicKey,
17
+ getPoolPublicKeyForMint,
18
+ getUshMintPublicKey,
19
+ getVaultSystemStatePublicKey,
20
+ } from '../Constants'
21
+ import { Vault } from 'idl/vault'
6
22
 
7
- export async function withdrawStakingPool (
8
- program: Program,
23
+ export async function withdrawStakingPool(
24
+ program: Program<Vault>,
9
25
  provider: Provider,
10
26
  payer: Signer,
11
27
  poolPositionPublicKey: PublicKey,
@@ -22,12 +38,12 @@ export async function withdrawStakingPool (
22
38
  overrideStartTime
23
39
  )
24
40
  )
25
- await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
41
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
26
42
  return poolPosition.publicKey
27
43
  }
28
44
 
29
- export async function withdrawStakingPoolInstruction (
30
- program: Program,
45
+ export async function withdrawStakingPoolInstruction(
46
+ program: Program<Vault>,
31
47
  payerPublicKey: PublicKey,
32
48
  poolPositionPublicKey: PublicKey,
33
49
  stakedTokenMintPublicKey: PublicKey,
@@ -44,28 +60,26 @@ export async function withdrawStakingPoolInstruction (
44
60
  const payerAssociatedUshAccount = await findAssociatedTokenAddress(payerPublicKey, ushMintPublickey)
45
61
  const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
46
62
 
47
- return program.instruction.withdrawStakingPool(
48
- new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
49
- {
50
- accounts: {
51
- payer: payerPublicKey,
52
- vaultSystemState: vaultSystemStatePublicKey,
53
- pool: poolPublickey,
54
- poolPosition: poolPositionPublicKey,
55
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
56
- poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
57
- payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
58
- payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
59
- payerAssociatedUshAccount: payerAssociatedUshAccount,
60
- communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
61
- hedgeMint: hedgeMintPublickey,
62
- stakedTokenMint: stakedTokenMintPublicKey,
63
- ushMint: ushMintPublickey,
64
- rent: SYSVAR_RENT_PUBKEY,
65
- tokenProgram: TOKEN_PROGRAM_ID,
66
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
67
- systemProgram: SystemProgram.programId
68
- },
69
- signers: []
63
+ return await program.methods
64
+ .withdrawStakingPool(
65
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
66
+ )
67
+ .accounts({
68
+ payer: payerPublicKey,
69
+ vaultSystemState: vaultSystemStatePublicKey,
70
+ pool: poolPublickey,
71
+ poolPosition: poolPositionPublicKey,
72
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
73
+ poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
74
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
75
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
76
+ payerAssociatedUshAccount: payerAssociatedUshAccount,
77
+ communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
78
+ hedgeMint: hedgeMintPublickey,
79
+ stakedTokenMint: stakedTokenMintPublicKey,
80
+ ushMint: ushMintPublickey,
81
+ tokenProgram: TOKEN_PROGRAM_ID,
82
+ systemProgram: SystemProgram.programId,
70
83
  })
84
+ .instruction()
71
85
  }
@@ -1,11 +1,28 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  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'
4
+ import {
5
+ Keypair,
6
+ PublicKey,
7
+ sendAndConfirmTransaction,
8
+ Signer,
9
+ SystemProgram,
10
+ Transaction,
11
+ TransactionInstruction,
12
+ } from '@solana/web3.js'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getVaultTypeAccountPublicKey,
16
+ getUshMintPublicKey,
17
+ getVaultSystemStatePublicKey,
18
+ getPoolPublicKeyForMint,
19
+ getHedgeMintPublicKey,
20
+ } from '../Constants'
21
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
22
+ import { Vault } from 'idl/vault'
6
23
 
7
- export async function withdrawVault (
8
- program: Program,
24
+ export async function withdrawVault(
25
+ program: Program<Vault>,
9
26
  provider: Provider,
10
27
  payer: Signer,
11
28
  vaultPublicKey: PublicKey,
@@ -20,19 +37,47 @@ export async function withdrawVault (
20
37
  const history = Keypair.generate()
21
38
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
22
39
  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)
40
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
41
+ const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
42
+ provider.connection,
43
+ payer,
44
+ TokenInstructions.WRAPPED_SOL_MINT,
45
+ vaultPublicKey,
46
+ true
47
+ )
25
48
 
26
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
27
- const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccount, true)
49
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
50
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
51
+ provider.connection,
52
+ payer,
53
+ vaultTypeAccountInfo.collateralMint,
54
+ vaultTypeAccountPublicKey,
55
+ true
56
+ )
28
57
 
29
- const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
58
+ const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
59
+ provider.connection,
60
+ payer,
61
+ vaultTypeAccountInfo.collateralMint,
62
+ payer.publicKey
63
+ )
30
64
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
31
65
  const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
32
66
  hedgeStakingPoolPublicKey,
33
67
  ushMintPublickey
34
68
  )
35
69
 
70
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
71
+ program,
72
+ provider,
73
+ vaultTypeAccountPublicKey,
74
+ vaultPublicKey,
75
+ withdrawAmount * -1,
76
+ 0,
77
+ false,
78
+ false
79
+ )
80
+
36
81
  const transaction = new Transaction().add(
37
82
  await withdrawVaultInstruction(
38
83
  program,
@@ -41,22 +86,25 @@ export async function withdrawVault (
41
86
  destinationTokenAccount.address,
42
87
  vaultPublicKey,
43
88
  vaultAssociatedCollateralAccount.address,
44
- vaultTypeAccount,
89
+ vaultTypeAccountPublicKey,
45
90
  vaultTypeAssociatedTokenAccount.address,
46
91
  hedgeStakingPoolPublicKey,
47
92
  hedgeStakingPoolAssociatedUshTokenAccount,
48
93
  ushMintPublickey,
49
94
  history.publicKey,
95
+ oldSmallerPublicKey,
96
+ newSmallerPublicKey,
97
+ newLargerPublicKey,
50
98
  withdrawAmount,
51
99
  overrideTime
52
100
  )
53
101
  )
54
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
102
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
55
103
  return vaultPublicKey
56
104
  }
57
105
 
58
- export async function withdrawVaultInstruction (
59
- program: Program,
106
+ export async function withdrawVaultInstruction(
107
+ program: Program<Vault>,
60
108
  vaultSystemStatePublicKey: PublicKey,
61
109
  payerPublicKey: PublicKey,
62
110
  destinationTokenAccount: PublicKey,
@@ -68,28 +116,34 @@ export async function withdrawVaultInstruction (
68
116
  hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
69
117
  ushMint: PublicKey,
70
118
  historyPublicKey: PublicKey,
119
+ oldSmallerPublicKey: PublicKey,
120
+ newSmallerPublicKey: PublicKey,
121
+ newLargerPublicKey: PublicKey,
71
122
  withdrawAmount: number,
72
123
  overrideTime?: number
73
124
  ): Promise<TransactionInstruction> {
74
- return program.instruction.withdrawVault(
75
- new BN(withdrawAmount),
76
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
77
- {
78
- accounts: {
79
- vaultSystemState: vaultSystemStatePublicKey,
80
- vaultTypeAccount: vaultTypeAccount,
81
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
82
- vault: vaultPublickey,
83
- vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
84
- feePool: hedgeStakingPoolPublicKey,
85
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
86
- ushMint: ushMint,
87
- history: historyPublicKey,
88
- vaultOwner: payerPublicKey,
89
- destinationTokenAccount: destinationTokenAccount,
90
- tokenProgram: TOKEN_PROGRAM_ID,
91
- systemProgram: SystemProgram.programId
92
- },
93
- signers: []
125
+ return await program.methods
126
+ .withdrawVault(
127
+ new BN(withdrawAmount),
128
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
129
+ )
130
+ .accounts({
131
+ vaultSystemState: vaultSystemStatePublicKey,
132
+ vaultTypeAccount: vaultTypeAccount,
133
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
134
+ vault: vaultPublickey,
135
+ vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
136
+ feePool: hedgeStakingPoolPublicKey,
137
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
138
+ ushMint: ushMint,
139
+ history: historyPublicKey,
140
+ vaultOwner: payerPublicKey,
141
+ destinationTokenAccount: destinationTokenAccount,
142
+ oldSmallerVaultInfo: oldSmallerPublicKey,
143
+ newSmallerVaultInfo: newSmallerPublicKey,
144
+ newLargerVaultInfo: newLargerPublicKey,
145
+ tokenProgram: TOKEN_PROGRAM_ID,
146
+ systemProgram: SystemProgram.programId,
94
147
  })
148
+ .instruction()
95
149
  }
@@ -1,4 +1,3 @@
1
-
2
1
  import Decimal from 'decimal.js'
3
2
  import { DecimalFromU128 } from '../HedgeDecimal'
4
3
 
@@ -13,10 +12,12 @@ export class LiquidationPoolEra {
13
12
  public hedgeRewardsAccumulator: Decimal
14
13
  public hedgeRewardsTimestamp: number
15
14
 
16
- constructor (public liquidyPoolEra: any) {
15
+ constructor(public liquidyPoolEra: any) {
17
16
  this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
18
17
  this.product = DecimalFromU128(liquidyPoolEra.productBytes)
19
- this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => { return DecimalFromU128(sumBytes) })
18
+ this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => {
19
+ return DecimalFromU128(sumBytes)
20
+ })
20
21
  this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
21
22
  this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
22
23
  }
@@ -47,31 +47,4 @@ export class LiquidationPosition {
47
47
  public getTokensAvailable (era: LiquidationPoolEra, index: number): Decimal {
48
48
  return era.sum[index].minus(this.sumSnapshotsEntry[index]).div(this.productSnapshotEntry).mul(new Decimal(this.deposit)).floor()
49
49
  }
50
-
51
- // public getHedgeAvailable (): Decimal {
52
- // const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
53
- // const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
54
- // LiquidationPoolTotalSupply,
55
- // this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
56
- // this.era.hedgeRewardsTimestamp * 1000,
57
- // Date.now(),
58
- // 365 * 1000)
59
-
60
- // if (this.era.totalDeposits === 0) {
61
- // return new Decimal(0)
62
- // }
63
-
64
- // const rewardsPerToken = this.era.product.mul(new Decimal(hedgeRewardsSinceLastUpdate / this.era.totalDeposits))
65
- // const newAccumulator = rewardsPerToken.add(new Decimal(this.era.hedgeRewardsAccumulator))
66
- // const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal(this.deposit)).div(new Decimal(this.productSnapshot))
67
- // return hedgeAvailable
68
- // }
69
50
  }
70
-
71
- // function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
72
- // const timeInOffsetStart = timeIn - birthTime
73
- // const timeOutOffsetStart = timeOut - birthTime
74
- // const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
75
- // const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
76
- // return awardedTokens
77
- // }
@@ -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
  }
@@ -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,9 +33,10 @@ 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()
@@ -49,8 +53,7 @@ export class VaultAccount {
49
53
  * @param publicKey the publicKey to check against the vault owner
50
54
  * @returns true if publicKey matches the owner publicKey
51
55
  */
52
- public isOwnedBy (publicKey: PublicKey): boolean {
53
-
56
+ public isOwnedBy(publicKey: PublicKey): boolean {
54
57
  return publicKey && publicKey.toString() === this.vaultOwner.toString()
55
58
  }
56
59
 
@@ -59,7 +62,7 @@ export class VaultAccount {
59
62
  *
60
63
  * @returns collateral value in SOL
61
64
  */
62
- public inSol (): number {
65
+ public inSol(): number {
63
66
  return this.deposited / LAMPORTS_PER_SOL
64
67
  }
65
68
 
@@ -68,7 +71,7 @@ export class VaultAccount {
68
71
  *
69
72
  * @returns debt value in USH
70
73
  */
71
- public inUsd (): number {
74
+ public inUsd(): number {
72
75
  return this.denormalizedDebt / LAMPORTS_PER_SOL
73
76
  }
74
77
 
@@ -77,7 +80,61 @@ export class VaultAccount {
77
80
  *
78
81
  * @returns example: `1b6ca...azy71s`
79
82
  */
80
- public toDisplayString (): string {
81
- return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`
83
+ public toDisplayString(): string {
84
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey
85
+ .toString()
86
+ .substring(this.publicKey.toString().length - 6)}`
87
+ }
88
+
89
+ public addDebt(newNormalizedDebt: Decimal, vaultTypeCompoundedInterest: Decimal) {
90
+ const denormalizedNewDebt = newNormalizedDebt.div(vaultTypeCompoundedInterest)
91
+ this.denormalizedDebt += denormalizedNewDebt.toNumber()
92
+ }
93
+ public addDeposit(depositAmount: number) {
94
+ this.deposited += depositAmount
95
+ }
96
+
97
+ public redeem() {
98
+ // TODO - Calculate actual redeem amount and adust correctly
99
+ this.denormalizedDebt = 0
100
+ this.vaultStatus = 'initialized'
101
+ }
102
+ public liquidate() {
103
+ // TODO - Calculate actual liquidate amount and adust correctly
104
+ this.denormalizedDebt = 0
105
+ this.vaultStatus = 'liquidated'
106
+ }
107
+
108
+ public updateDebtAndCollateral(vaultTypeAccuntData: any) {
109
+ const debtProductCurrent = DecimalFromU128(vaultTypeAccuntData.debtRedistributionProduct)
110
+
111
+ const collateralAccumulatorCurrent = DecimalFromU128(vaultTypeAccuntData.collateralRedistributionAccumulator)
112
+
113
+ this.denormalizedDebt = debtProductCurrent
114
+ .div(this.debtProductSnapshotBytes)
115
+ .mul(new Decimal(this.denormalizedDebt))
116
+ // .add(new Decimal(vaultTypeAccuntData.debtRedistributionError))
117
+ .toNumber()
118
+
119
+ const extraCollateralDeposited =
120
+ this.denormalizedDebt * collateralAccumulatorCurrent.sub(this.collateralAccumulatorSnapshotBytes).toNumber()
121
+ this.deposited += extraCollateralDeposited
122
+
123
+ this.collateralAccumulatorSnapshotBytes = collateralAccumulatorCurrent
124
+ this.debtProductSnapshotBytes = debtProductCurrent
125
+ }
126
+
127
+ public toString(highlight: PublicKey): string {
128
+ let arrow = ''
129
+ if (this.publicKey.toString() === highlight.toString()) {
130
+ arrow = ' <----'
131
+ }
132
+ let collateralRatio = 'Infinite'
133
+ if (this.denormalizedDebt > 0) {
134
+ collateralRatio = (this.deposited / this.denormalizedDebt).toFixed(8)
135
+ }
136
+ return `Vault(${this.vaultNumber}): ${this.publicKey
137
+ .toString()
138
+ .substring(0, 6)}. Debt: ${this.inUsd()} Collat: ${collateralRatio} ${arrow}`
82
139
  }
83
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