hedge-web3 0.1.13 → 0.1.14

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 (54) hide show
  1. package/lib/index.js +373 -147
  2. package/lib/index.js.map +1 -1
  3. package/lib/types/src/Constants.d.ts +7 -6
  4. package/lib/types/src/Constants.d.ts.map +1 -1
  5. package/lib/types/src/index.d.ts +3 -0
  6. package/lib/types/src/index.d.ts.map +1 -1
  7. package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts +5 -0
  8. package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +1 -0
  9. package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts +5 -0
  10. package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +1 -0
  11. package/lib/types/src/instructions/createVault.d.ts +2 -2
  12. package/lib/types/src/instructions/createVault.d.ts.map +1 -1
  13. package/lib/types/src/instructions/depositLiquidationPool.d.ts +5 -0
  14. package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +1 -0
  15. package/lib/types/src/instructions/depositVault.d.ts +1 -1
  16. package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
  17. package/lib/types/src/instructions/liquidateVault.d.ts +2 -2
  18. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
  19. package/lib/types/src/instructions/loanVault.d.ts +1 -1
  20. package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
  21. package/lib/types/src/instructions/redeemVault.d.ts +2 -2
  22. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
  23. package/lib/types/src/instructions/refreshOraclePrice.d.ts +2 -2
  24. package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -1
  25. package/lib/types/src/instructions/repayVault.d.ts +1 -1
  26. package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
  27. package/lib/types/src/instructions/withdrawVault.d.ts +1 -1
  28. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
  29. package/lib/types/src/state/LiquidationPoolEra.d.ts +1 -1
  30. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
  31. package/lib/types/src/state/LiquidationPosition.d.ts +8 -9
  32. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
  33. package/lib/types/tsconfig.base.tsbuildinfo +1 -1
  34. package/package.json +6 -2
  35. package/rollup.config.js +3 -1
  36. package/src/Constants.ts +21 -17
  37. package/src/idl/idl.ts +13 -13
  38. package/src/index.ts +3 -0
  39. package/src/instructions/claimLiquidationPoolPosition.ts +68 -0
  40. package/src/instructions/closeLiquidationPoolPosition.ts +88 -0
  41. package/src/instructions/createStakingPool.ts +4 -4
  42. package/src/instructions/createVault.ts +75 -14
  43. package/src/instructions/depositLiquidationPool.ts +70 -0
  44. package/src/instructions/depositStakingPool.ts +2 -2
  45. package/src/instructions/depositVault.ts +68 -12
  46. package/src/instructions/liquidateVault.ts +90 -35
  47. package/src/instructions/loanVault.ts +19 -7
  48. package/src/instructions/redeemVault.ts +41 -15
  49. package/src/instructions/refreshOraclePrice.ts +5 -2
  50. package/src/instructions/repayVault.ts +20 -8
  51. package/src/instructions/withdrawStakingPool.ts +6 -6
  52. package/src/instructions/withdrawVault.ts +28 -5
  53. package/src/state/LiquidationPoolEra.ts +2 -8
  54. package/src/state/LiquidationPosition.ts +38 -39
@@ -1,17 +1,18 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ // import { TokenInstructions } from '@project-serum/serum'
3
4
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getSolCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
6
 
6
7
  export async function redeemVault (
7
8
  program: Program,
8
9
  provider: Provider,
9
10
  payer: Signer,
10
11
  vaultPublicKey: PublicKey,
11
- repayAmount: number,
12
+ redeemAmount: number,
12
13
  transactionOverrideTime?: number
13
14
  ): Promise<PublicKey> {
14
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
15
+ const usdhMintPublickey = await getUsdhMintPublicKey()
15
16
  const USDH = new Token(
16
17
  provider.connection,
17
18
  usdhMintPublickey,
@@ -22,15 +23,35 @@ export async function redeemVault (
22
23
  // Prep the user to get USDH back out at some point
23
24
  const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
24
25
 
26
+ const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
27
+ const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
28
+ const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
29
+ const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccountInfo.collateralMint)
30
+
31
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint)
32
+
33
+ const token = new Token(
34
+ provider.connection,
35
+ collateralStateAccountInfo.collateralMint,
36
+ TOKEN_PROGRAM_ID,
37
+ payer
38
+ )
39
+
40
+ const payerTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey)
41
+
25
42
  const history = Keypair.generate()
26
43
  const transaction = new Transaction().add(
27
44
  await redeemVaultInstruction(
28
45
  program,
29
46
  payer.publicKey,
30
47
  payerUsdhAccount.address,
48
+ payerTokenAccount.address,
31
49
  vaultPublicKey,
50
+ vaultAssociatedTokenAccount,
32
51
  history.publicKey,
33
- repayAmount,
52
+ collateralStateAccountPublicKey,
53
+ collateralAssociatedTokenAccount,
54
+ redeemAmount,
34
55
  transactionOverrideTime
35
56
  )
36
57
  )
@@ -41,37 +62,42 @@ export async function redeemVault (
41
62
  export async function redeemVaultInstruction (
42
63
  program: Program,
43
64
  payerPublicKey: PublicKey,
44
- ownerUsdhAccount: PublicKey,
65
+ payerUsdhAccount: PublicKey,
66
+ destinationTokenAccount: PublicKey,
45
67
  vaultPublickey: PublicKey,
68
+ vaultAssociatedTokenAccount: PublicKey,
46
69
  historyPublicKey: PublicKey,
47
- repayAmount: number,
70
+ collateralStateAccount: PublicKey,
71
+ collateralAssociatedTokenAccount: PublicKey,
72
+ redeemAmount: number,
48
73
  transactionOverrideTime?: number
49
74
  ): Promise<TransactionInstruction> {
50
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
51
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
52
- const [hedgeMintPublickey] = await getHedgeMintPublicKey()
75
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
76
+ const usdhMintPublickey = await getUsdhMintPublicKey()
77
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
53
78
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
54
79
  const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
55
80
  hedgeStakingPoolPublicKey,
56
81
  usdhMintPublickey
57
82
  )
58
- const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
59
-
60
83
  return program.instruction.redeemVault(
61
- new BN(repayAmount),
84
+ new BN(redeemAmount),
62
85
  new BN(transactionOverrideTime ?? (Date.now() / 1000)), // override start time
63
86
  {
64
87
  accounts: {
65
88
  vaultSystemState: vaultSystemStatePublicKey,
66
89
  collateralStateAccount: collateralStateAccount,
67
- vaultAccount: vaultPublickey,
90
+ collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
91
+ vault: vaultPublickey,
92
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
68
93
  history: historyPublicKey,
69
94
  feePool: hedgeStakingPoolPublicKey,
70
95
  feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
71
96
  usdhMint: usdhMintPublickey,
72
97
  payer: payerPublicKey,
73
- payerUsdhAccount: ownerUsdhAccount,
74
- splTokenProgramInfo: TOKEN_PROGRAM_ID,
98
+ payerUsdhAccount: payerUsdhAccount,
99
+ destinationTokenAccount: destinationTokenAccount,
100
+ tokenProgram: TOKEN_PROGRAM_ID,
75
101
  systemProgram: SystemProgram.programId
76
102
  },
77
103
  signers: []
@@ -6,6 +6,7 @@ export async function refreshOraclePrice (
6
6
  program: Program,
7
7
  provider: Provider,
8
8
  payer: Signer,
9
+ collateralType: string,
9
10
  network: Cluster,
10
11
  overridePrice?: number,
11
12
  overrideTime?: number
@@ -13,6 +14,7 @@ export async function refreshOraclePrice (
13
14
  const transaction = new Transaction().add(
14
15
  await refreshOraclePriceInstruction(
15
16
  program,
17
+ collateralType,
16
18
  network,
17
19
  overridePrice,
18
20
  overrideTime
@@ -23,13 +25,14 @@ export async function refreshOraclePrice (
23
25
 
24
26
  export async function refreshOraclePriceInstruction (
25
27
  program: Program,
28
+ collateralType: string,
26
29
  network: Cluster,
27
30
  overridePrice?: number,
28
31
  overrideTime?: number
29
32
  ): Promise<TransactionInstruction> {
30
33
  const enc = new TextEncoder()
31
- const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
32
- const [collateralStateAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
34
+ const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
35
+ const [collateralStateAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
33
36
 
34
37
  return program.instruction.refreshOraclePrice(
35
38
  new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
@@ -1,7 +1,7 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getSolCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
4
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
5
 
6
6
  export async function repayVault (
7
7
  program: Program,
@@ -10,7 +10,7 @@ export async function repayVault (
10
10
  vaultPublicKey: PublicKey,
11
11
  repayAmount: number
12
12
  ): Promise<PublicKey> {
13
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
13
+ const usdhMintPublickey = await getUsdhMintPublicKey()
14
14
  const USDH = new Token(
15
15
  provider.connection,
16
16
  usdhMintPublickey,
@@ -20,6 +20,12 @@ export async function repayVault (
20
20
 
21
21
  // Prep the user to get USDH back out at some point
22
22
  const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
23
+ const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
24
+
25
+ const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
26
+ const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
27
+ const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint)
28
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint)
23
29
 
24
30
  const history = Keypair.generate()
25
31
  const transaction = new Transaction().add(
@@ -28,7 +34,10 @@ export async function repayVault (
28
34
  payer.publicKey,
29
35
  payerUsdhAccount.address,
30
36
  vaultPublicKey,
37
+ vaultAssociatedTokenAccount,
31
38
  history.publicKey,
39
+ collateralStateAccountPublicKey,
40
+ collateralAssociatedTokenAccount,
32
41
  repayAmount
33
42
  )
34
43
  )
@@ -41,34 +50,37 @@ export async function repayVaultInstruction (
41
50
  payerPublicKey: PublicKey,
42
51
  ownerUsdhAccount: PublicKey,
43
52
  vaultPublickey: PublicKey,
53
+ vaultAssociatedTokenAccount: PublicKey,
44
54
  historyPublicKey: PublicKey,
55
+ collateralStateAccount: PublicKey,
56
+ collateralAssociatedTokenAccount: PublicKey,
45
57
  repayAmount: number
46
58
  ): Promise<TransactionInstruction> {
47
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
48
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
49
- const [hedgeMintPublickey] = await getHedgeMintPublicKey()
59
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
60
+ const usdhMintPublickey = await getUsdhMintPublicKey()
61
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
50
62
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
51
63
  const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
52
64
  hedgeStakingPoolPublicKey,
53
65
  usdhMintPublickey
54
66
  )
55
67
 
56
- const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
57
-
58
68
  return program.instruction.repayVault(
59
69
  new BN(repayAmount),
60
70
  {
61
71
  accounts: {
62
72
  vaultSystemState: vaultSystemStatePublicKey,
63
73
  collateralStateAccount: collateralStateAccount,
74
+ collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
64
75
  vaultAccount: vaultPublickey,
76
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
65
77
  history: historyPublicKey,
66
78
  feePool: hedgeStakingPoolPublicKey,
67
79
  feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
68
80
  usdhMint: usdhMintPublickey,
69
81
  vaultOwner: payerPublicKey,
70
82
  ownerUsdhAccount: ownerUsdhAccount,
71
- splTokenProgramInfo: TOKEN_PROGRAM_ID,
83
+ tokenProgram: TOKEN_PROGRAM_ID,
72
84
  systemProgram: SystemProgram.programId
73
85
  },
74
86
  signers: []
@@ -33,16 +33,16 @@ export async function withdrawStakingPoolInstruction (
33
33
  stakedTokenMintPublicKey: PublicKey,
34
34
  overrideStartTime?: number
35
35
  ): Promise<TransactionInstruction> {
36
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
37
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
38
- const [hedgeMintPublickey] = await getHedgeMintPublicKey()
36
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
37
+ const usdhMintPublickey = await getUsdhMintPublicKey()
38
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
39
39
  const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
40
40
  const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
41
41
  const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
42
42
  const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
43
43
  const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
44
44
  const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey)
45
- const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey,hedgeMintPublickey)
45
+ const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
46
46
 
47
47
  return program.instruction.withdrawStakingPool(
48
48
  new BN(overrideStartTime ?? Date.now() / 1000), // override current time
@@ -62,8 +62,8 @@ export async function withdrawStakingPoolInstruction (
62
62
  stakedTokenMint: stakedTokenMintPublicKey,
63
63
  usdhMint: usdhMintPublickey,
64
64
  rent: SYSVAR_RENT_PUBKEY,
65
- splTokenProgramInfo: TOKEN_PROGRAM_ID,
66
- splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
65
+ tokenProgram: TOKEN_PROGRAM_ID,
66
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
67
67
  systemProgram: SystemProgram.programId
68
68
  },
69
69
  signers: []
@@ -1,7 +1,8 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { TokenInstructions } from '@project-serum/serum'
3
4
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { getSolCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
6
 
6
7
  export async function withdrawVault (
7
8
  program: Program,
@@ -10,7 +11,7 @@ export async function withdrawVault (
10
11
  vaultPublicKey: PublicKey,
11
12
  withdrawAmount: number
12
13
  ): Promise<PublicKey> {
13
- const [usdhMintPublickey] = await getUsdhMintPublicKey()
14
+ const usdhMintPublickey = await getUsdhMintPublicKey()
14
15
  const USDH = new Token(
15
16
  provider.connection,
16
17
  usdhMintPublickey,
@@ -22,13 +23,28 @@ export async function withdrawVault (
22
23
  await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
23
24
 
24
25
  const history = Keypair.generate()
25
- const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
26
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
27
+
28
+ const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
29
+ const collateralStateAccount = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
30
+ const vaultAssociatedCollateralAccount = await findAssociatedTokenAddress(vaultPublicKey, TokenInstructions.WRAPPED_SOL_MINT)
31
+ const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccount)
32
+ const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccount, collateralStateAccountInfo.collateralMint)
33
+
34
+ const token = new Token(provider.connection, collateralStateAccountInfo.collateralMint, TOKEN_PROGRAM_ID, payer)
35
+
36
+ const destinationTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey)
37
+
26
38
  const transaction = new Transaction().add(
27
39
  await withdrawVaultInstruction(
28
40
  program,
29
41
  vaultSystemStatePublicKey,
30
42
  payer.publicKey,
43
+ destinationTokenAccount.address,
31
44
  vaultPublicKey,
45
+ vaultAssociatedCollateralAccount,
46
+ collateralStateAccount,
47
+ collateralAssociatedTokenAccount,
32
48
  history.publicKey,
33
49
  withdrawAmount
34
50
  )
@@ -41,20 +57,27 @@ export async function withdrawVaultInstruction (
41
57
  program: Program,
42
58
  vaultSystemStatePublicKey: PublicKey,
43
59
  payerPublicKey: PublicKey,
60
+ destinationTokenAccount: PublicKey,
44
61
  vaultPublickey: PublicKey,
62
+ vaultAssociatedCollateralPublicKey: PublicKey,
63
+ collateralStateAccount: PublicKey,
64
+ collateralAssociatedTokenAccount: PublicKey,
45
65
  historyPublicKey: PublicKey,
46
66
  withdrawAmount: number
47
67
  ): Promise<TransactionInstruction> {
48
- const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
49
68
  return program.instruction.withdrawVault(
50
69
  new BN(withdrawAmount),
51
70
  {
52
71
  accounts: {
53
72
  vaultSystemState: vaultSystemStatePublicKey,
54
73
  collateralStateAccount: collateralStateAccount,
55
- vaultAccount: vaultPublickey,
74
+ collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
75
+ vault: vaultPublickey,
76
+ vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
56
77
  history: historyPublicKey,
57
78
  vaultOwner: payerPublicKey,
79
+ destinationTokenAccount: destinationTokenAccount,
80
+ tokenProgram: TOKEN_PROGRAM_ID,
58
81
  systemProgram: SystemProgram.programId
59
82
  },
60
83
  signers: []
@@ -8,22 +8,16 @@ import { DecimalFromU128 } from '../HedgeDecimal'
8
8
  */
9
9
  export class LiquidationPoolEra {
10
10
  public totalDeposits: number
11
-
12
11
  public product: Decimal
13
-
14
- public sum: Decimal
15
-
12
+ public sum: [Decimal]
16
13
  public hedgeRewardsAccumulator: Decimal
17
-
18
14
  public hedgeRewardsTimestamp: number
19
15
 
20
16
  constructor (public liquidyPoolEra: any) {
21
17
  this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
22
-
23
18
  this.product = DecimalFromU128(liquidyPoolEra.productBytes)
24
- this.sum = DecimalFromU128(liquidyPoolEra.sumBytes)
19
+ this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => { return DecimalFromU128(sumBytes) })
25
20
  this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
26
-
27
21
  this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
28
22
  }
29
23
  }
@@ -1,4 +1,4 @@
1
- import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
1
+ import { PublicKey } from '@solana/web3.js'
2
2
  import Decimal from 'decimal.js'
3
3
  import { DecimalFromU128 } from '../HedgeDecimal'
4
4
  import { LiquidationPoolEra } from './LiquidationPoolEra'
@@ -7,71 +7,70 @@ import { LiquidationPoolState } from './LiquidationPoolState'
7
7
  export class LiquidationPosition {
8
8
  public publicKey: PublicKey
9
9
  public eraPublicKey: PublicKey
10
- public era: LiquidationPoolEra
11
- public liquidationPoolState: LiquidationPoolState
10
+ public liquidationPoolState?: LiquidationPoolState
12
11
 
13
12
  public ownerAccount: PublicKey
14
13
  public deposit: number
15
14
  public closedUsdh: number
16
- public closedSol: number
17
15
  public timestampOpened: Date
18
16
  public timestampClosed: Date
19
17
 
20
- public productSnapshot: Decimal
21
- public sumSnapshot: Decimal
18
+ public productSnapshotEntry: Decimal
19
+ public productSnapshotClosed: Decimal
20
+ public sumSnapshotsEntry: [Decimal]
21
+ public sumSnapshotsClosed: [Decimal]
22
22
  public hedgeRewardsSnapshot: Decimal
23
23
  public open: boolean
24
24
 
25
- constructor (poolPositionInfo: any, key: PublicKey, era: LiquidationPoolEra, liquidationPoolState: LiquidationPoolState) {
25
+ constructor (poolPositionInfo: any, key: PublicKey) {
26
26
  this.publicKey = key
27
27
  this.eraPublicKey = poolPositionInfo.era
28
- this.era = era
29
- this.liquidationPoolState = liquidationPoolState
30
28
  this.ownerAccount = poolPositionInfo.ownerAccount
31
29
  this.deposit = poolPositionInfo.deposit.toNumber()
32
30
  this.closedUsdh = poolPositionInfo.closedUsdh.toNumber()
33
- this.closedSol = poolPositionInfo.closedSol.toNumber()
34
31
  this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
35
32
  this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
36
33
 
37
- this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes)
38
- this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes)
34
+ this.productSnapshotEntry = DecimalFromU128(poolPositionInfo.productSnapshotEntry)
35
+ this.productSnapshotClosed = DecimalFromU128(poolPositionInfo.productSnapshotClosed)
36
+ this.sumSnapshotsEntry = poolPositionInfo.sumSnapshotsEntry.map((sum: any) => { return DecimalFromU128(sum) })
37
+ this.sumSnapshotsClosed = poolPositionInfo.sumSnapshotsClosed.map((sum: any) => { return DecimalFromU128(sum) })
39
38
  this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum)
40
39
  this.open = poolPositionInfo.state.open !== undefined
41
40
  }
42
41
 
43
- public getUsdhAvailable (): Decimal {
44
- return (this.era.product.div(this.productSnapshot)).mul(new Decimal(this.deposit))
42
+ public getUsdhAvailable (era: LiquidationPoolEra): Decimal {
43
+ return (era.product.div(this.productSnapshotEntry)).mul(new Decimal(this.deposit))
45
44
  }
46
45
 
47
- public getSolAvailable (): Decimal {
48
- return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal(this.deposit)).floor()
46
+ public getSolAvailable (era: LiquidationPoolEra): Decimal {
47
+ return era.sum[0].minus(this.sumSnapshotsEntry[0]).div(this.productSnapshotEntry).mul(new Decimal(this.deposit)).floor()
49
48
  }
50
49
 
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)
50
+ // public getHedgeAvailable (): Decimal {
51
+ // const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
52
+ // const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
53
+ // LiquidationPoolTotalSupply,
54
+ // this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
55
+ // this.era.hedgeRewardsTimestamp * 1000,
56
+ // Date.now(),
57
+ // 365 * 1000)
59
58
 
60
- if (this.era.totalDeposits === 0) {
61
- return new Decimal(0)
62
- }
59
+ // if (this.era.totalDeposits === 0) {
60
+ // return new Decimal(0)
61
+ // }
63
62
 
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
- }
63
+ // const rewardsPerToken = this.era.product.mul(new Decimal(hedgeRewardsSinceLastUpdate / this.era.totalDeposits))
64
+ // const newAccumulator = rewardsPerToken.add(new Decimal(this.era.hedgeRewardsAccumulator))
65
+ // const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal(this.deposit)).div(new Decimal(this.productSnapshot))
66
+ // return hedgeAvailable
67
+ // }
69
68
  }
70
69
 
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
- }
70
+ // function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
71
+ // const timeInOffsetStart = timeIn - birthTime
72
+ // const timeOutOffsetStart = timeOut - birthTime
73
+ // const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
74
+ // const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
75
+ // return awardedTokens
76
+ // }