hedge-web3 0.1.3 → 0.1.9

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 (66) hide show
  1. package/lib/index.js +415 -73
  2. package/lib/index.js.map +1 -1
  3. package/lib/types/src/Constants.d.ts +4 -3
  4. package/lib/types/src/Constants.d.ts.map +1 -1
  5. package/lib/types/src/StakingPools.d.ts +4 -0
  6. package/lib/types/src/StakingPools.d.ts.map +1 -0
  7. package/lib/types/src/Vaults.d.ts +9 -0
  8. package/lib/types/src/Vaults.d.ts.map +1 -0
  9. package/lib/types/src/index.d.ts +13 -1
  10. package/lib/types/src/index.d.ts.map +1 -1
  11. package/lib/types/src/instructions/createStakingPool.d.ts +5 -0
  12. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
  13. package/lib/types/src/instructions/createVault.d.ts +4 -3
  14. package/lib/types/src/instructions/createVault.d.ts.map +1 -1
  15. package/lib/types/src/instructions/depositStakingPool.d.ts +5 -0
  16. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
  17. package/lib/types/src/instructions/depositVault.d.ts +4 -3
  18. package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
  19. package/lib/types/src/instructions/liquidateVault.d.ts +5 -0
  20. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
  21. package/lib/types/src/instructions/loanVault.d.ts +4 -3
  22. package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
  23. package/lib/types/src/instructions/redeemVault.d.ts +4 -3
  24. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
  25. package/lib/types/src/instructions/refreshOraclePrice.d.ts +5 -0
  26. package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -0
  27. package/lib/types/src/instructions/repayVault.d.ts +4 -3
  28. package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
  29. package/lib/types/src/instructions/withdrawStakingPool.d.ts +5 -0
  30. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
  31. package/lib/types/src/instructions/withdrawVault.d.ts +4 -3
  32. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
  33. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
  34. package/lib/types/src/state/LiquidationPosition.d.ts +1 -0
  35. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
  36. package/lib/types/src/state/StakingPool.d.ts +1 -2
  37. package/lib/types/src/state/StakingPool.d.ts.map +1 -1
  38. package/lib/types/src/state/StakingPoolPosition.d.ts +1 -0
  39. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -1
  40. package/lib/types/src/state/VaultAccount.d.ts.map +1 -1
  41. package/lib/types/src/utils/Errors.d.ts +2 -0
  42. package/lib/types/src/utils/Errors.d.ts.map +1 -0
  43. package/lib/types/tsconfig.base.tsbuildinfo +1 -1
  44. package/package.json +2 -2
  45. package/src/Constants.ts +8 -9
  46. package/src/StakingPools.ts +3 -0
  47. package/src/Vaults.ts +8 -0
  48. package/src/index.ts +15 -1
  49. package/src/instructions/createStakingPool.ts +64 -0
  50. package/src/instructions/createVault.ts +10 -11
  51. package/src/instructions/depositStakingPool.ts +60 -0
  52. package/src/instructions/depositVault.ts +10 -11
  53. package/src/instructions/liquidateVault.ts +66 -0
  54. package/src/instructions/loanVault.ts +14 -18
  55. package/src/instructions/redeemVault.ts +10 -11
  56. package/src/instructions/refreshOraclePrice.ts +45 -0
  57. package/src/instructions/repayVault.ts +10 -11
  58. package/src/instructions/withdrawStakingPool.ts +69 -0
  59. package/src/instructions/withdrawVault.ts +10 -11
  60. package/src/state/LiquidationPoolEra.ts +5 -5
  61. package/src/state/LiquidationPosition.ts +26 -18
  62. package/src/state/StakingPool.ts +4 -5
  63. package/src/state/StakingPoolPosition.ts +4 -5
  64. package/src/state/VaultAccount.ts +0 -1
  65. package/src/state/VaultHistoryEvent.ts +1 -1
  66. package/src/utils/Errors.ts +11 -0
@@ -1,21 +1,18 @@
1
- import { BN, Program } from '@project-serum/anchor'
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { ConfirmOptions, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { CHAINLINK_SOL_USD_PUBLICKEY, findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
-
6
- import { vaultIdl } from '../idl/idl'
3
+ import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getSolCollateralPriceAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
7
5
 
8
6
  export async function loanVault (
9
- connection: Connection,
7
+ program: Program,
8
+ provider: Provider,
10
9
  payer: Signer,
11
10
  vaultPublicKey: PublicKey,
12
- loanAmount: number,
13
- chainlinkOverridePrice?: number,
14
- confirmOptions?: ConfirmOptions
11
+ loanAmount: number
15
12
  ): Promise<PublicKey> {
16
13
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
17
14
  const USDH = new Token(
18
- connection,
15
+ provider.connection,
19
16
  usdhMintPublickey,
20
17
  TOKEN_PROGRAM_ID,
21
18
  payer
@@ -27,27 +24,26 @@ export async function loanVault (
27
24
  const history = Keypair.generate()
28
25
  const transaction = new Transaction().add(
29
26
  await loanVaultInstruction(
27
+ program,
30
28
  payer.publicKey,
31
29
  payerUsdhAccount.address,
32
30
  vaultPublicKey,
33
31
  history.publicKey,
34
- loanAmount,
35
- chainlinkOverridePrice
32
+ loanAmount
36
33
  )
37
34
  )
38
- await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
35
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
39
36
  return vaultPublicKey
40
37
  }
41
38
 
42
39
  export async function loanVaultInstruction (
40
+ program: Program,
43
41
  payerPublicKey: PublicKey,
44
42
  ownerUsdhAccount: PublicKey,
45
43
  vaultPublickey: PublicKey,
46
44
  historyPublicKey: PublicKey,
47
- loanAmount: number,
48
- chainlinkOverridePrice?: number
45
+ loanAmount: number
49
46
  ): Promise<TransactionInstruction> {
50
- const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
51
47
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
52
48
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
53
49
  const [hedgeMintPublickey] = await getHedgeMintPublicKey()
@@ -56,10 +52,10 @@ export async function loanVaultInstruction (
56
52
  hedgeStakingPoolPublicKey,
57
53
  usdhMintPublickey
58
54
  )
55
+ const solPriceAccount = await getSolCollateralPriceAccountPublicKey()
59
56
 
60
57
  return program.instruction.loanVault(
61
58
  new BN(loanAmount),
62
- new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
63
59
  {
64
60
  accounts: {
65
61
  vaultSystemState: vaultSystemStatePublicKey,
@@ -70,7 +66,7 @@ export async function loanVaultInstruction (
70
66
  usdhMint: usdhMintPublickey,
71
67
  vaultOwner: payerPublicKey,
72
68
  ownerUsdhAccount: ownerUsdhAccount,
73
- chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
69
+ priceForCollateral: solPriceAccount,
74
70
  splTokenProgramInfo: TOKEN_PROGRAM_ID,
75
71
  systemProgram: SystemProgram.programId
76
72
  },
@@ -1,22 +1,20 @@
1
- import { BN, Program } from '@project-serum/anchor'
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { ConfirmOptions, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { CHAINLINK_SOL_USD_PUBLICKEY, findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
-
6
- import { vaultIdl } from '../idl/idl'
3
+ import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { CHAINLINK_SOL_USD_PUBLICKEY, findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
7
5
 
8
6
  export async function redeemVault (
9
- connection: Connection,
7
+ program: Program,
8
+ provider: Provider,
10
9
  payer: Signer,
11
10
  vaultPublicKey: PublicKey,
12
11
  repayAmount: number,
13
12
  chainlinkOverridePrice?: number,
14
- transactionOverrideTime?: number,
15
- confirmOptions?: ConfirmOptions
13
+ transactionOverrideTime?: number
16
14
  ): Promise<PublicKey> {
17
15
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
18
16
  const USDH = new Token(
19
- connection,
17
+ provider.connection,
20
18
  usdhMintPublickey,
21
19
  TOKEN_PROGRAM_ID,
22
20
  payer
@@ -28,6 +26,7 @@ export async function redeemVault (
28
26
  const history = Keypair.generate()
29
27
  const transaction = new Transaction().add(
30
28
  await redeemVaultInstruction(
29
+ program,
31
30
  payer.publicKey,
32
31
  payerUsdhAccount.address,
33
32
  vaultPublicKey,
@@ -37,11 +36,12 @@ export async function redeemVault (
37
36
  transactionOverrideTime
38
37
  )
39
38
  )
40
- await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
39
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
41
40
  return vaultPublicKey
42
41
  }
43
42
 
44
43
  export async function redeemVaultInstruction (
44
+ program: Program,
45
45
  payerPublicKey: PublicKey,
46
46
  ownerUsdhAccount: PublicKey,
47
47
  vaultPublickey: PublicKey,
@@ -50,7 +50,6 @@ export async function redeemVaultInstruction (
50
50
  chainlinkOverridePrice?: number,
51
51
  transactionOverrideTime?: number
52
52
  ): Promise<TransactionInstruction> {
53
- const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
54
53
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
55
54
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
56
55
  const [hedgeMintPublickey] = await getHedgeMintPublicKey()
@@ -0,0 +1,45 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
3
+ import { HEDGE_PROGRAM_PUBLICKEY } from '../Constants'
4
+
5
+ export async function refreshOraclePrice (
6
+ program: Program,
7
+ provider: Provider,
8
+ payer: Signer,
9
+ overridePrice?: number,
10
+ overrideTime?: number
11
+ ): Promise<string> {
12
+ const transaction = new Transaction().add(
13
+ await refreshOraclePriceInstruction(
14
+ program,
15
+ overridePrice,
16
+ overrideTime
17
+ )
18
+ )
19
+ return await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts)
20
+ }
21
+
22
+ export async function refreshOraclePriceInstruction (
23
+ program: Program,
24
+ overridePrice?: number,
25
+ overrideTime?: number
26
+ ): Promise<TransactionInstruction> {
27
+ const enc = new TextEncoder()
28
+ const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
29
+ const [collateralPriceAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Price')], HEDGE_PROGRAM_PUBLICKEY)
30
+
31
+ return program.instruction.refreshOraclePrice(
32
+ new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
33
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
34
+ {
35
+ accounts: {
36
+ oracleInfoAccount: oracleInfoAccount,
37
+ collateralPriceAccount: collateralPriceAccount,
38
+ oracleChainlink: SystemProgram.programId,
39
+ oraclePyth: SystemProgram.programId,
40
+ oracleSwitchboard: SystemProgram.programId,
41
+ systemProgram: SystemProgram.programId
42
+ },
43
+ signers: []
44
+ })
45
+ }
@@ -1,20 +1,18 @@
1
- import { BN, Program } from '@project-serum/anchor'
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { ConfirmOptions, Connection, Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
-
6
- import { vaultIdl } from '../idl/idl'
3
+ import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
7
5
 
8
6
  export async function repayVault (
9
- connection: Connection,
7
+ program: Program,
8
+ provider: Provider,
10
9
  payer: Signer,
11
10
  vaultPublicKey: PublicKey,
12
- repayAmount: number,
13
- confirmOptions?: ConfirmOptions
11
+ repayAmount: number
14
12
  ): Promise<PublicKey> {
15
13
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
16
14
  const USDH = new Token(
17
- connection,
15
+ provider.connection,
18
16
  usdhMintPublickey,
19
17
  TOKEN_PROGRAM_ID,
20
18
  payer
@@ -26,6 +24,7 @@ export async function repayVault (
26
24
  const history = Keypair.generate()
27
25
  const transaction = new Transaction().add(
28
26
  await repayVaultInstruction(
27
+ program,
29
28
  payer.publicKey,
30
29
  payerUsdhAccount.address,
31
30
  vaultPublicKey,
@@ -33,18 +32,18 @@ export async function repayVault (
33
32
  repayAmount
34
33
  )
35
34
  )
36
- await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
35
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
37
36
  return vaultPublicKey
38
37
  }
39
38
 
40
39
  export async function repayVaultInstruction (
40
+ program: Program,
41
41
  payerPublicKey: PublicKey,
42
42
  ownerUsdhAccount: PublicKey,
43
43
  vaultPublickey: PublicKey,
44
44
  historyPublicKey: PublicKey,
45
45
  repayAmount: number
46
46
  ): Promise<TransactionInstruction> {
47
- const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
48
47
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
49
48
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
50
49
  const [hedgeMintPublickey] = await getHedgeMintPublicKey()
@@ -0,0 +1,69 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
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'
4
+ import { parseAnchorErrors } from '../utils/Errors'
5
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
+
7
+ export async function withdrawStakingPool (
8
+ program: Program,
9
+ provider: Provider,
10
+ payer: Signer,
11
+ poolPositionPublicKey: PublicKey,
12
+ stakedTokenMintPublicKey: PublicKey,
13
+ overrideStartTime?: number
14
+ ): Promise<PublicKey> {
15
+ const poolPosition = Keypair.generate()
16
+ const transaction = new Transaction().add(
17
+ await withdrawStakingPoolInstruction(
18
+ program,
19
+ payer.publicKey,
20
+ poolPositionPublicKey,
21
+ stakedTokenMintPublicKey,
22
+ overrideStartTime
23
+ )
24
+ )
25
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
26
+ return poolPosition.publicKey
27
+ }
28
+
29
+ export async function withdrawStakingPoolInstruction (
30
+ program: Program,
31
+ payerPublicKey: PublicKey,
32
+ poolPositionPublicKey: PublicKey,
33
+ stakedTokenMintPublicKey: PublicKey,
34
+ overrideStartTime?: number
35
+ ): Promise<TransactionInstruction> {
36
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
37
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
38
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
39
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
40
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
41
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
42
+ const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
43
+ const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
44
+ const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey)
45
+
46
+ return program.instruction.withdrawStakingPool(
47
+ new BN(overrideStartTime ?? Date.now() / 1000), // override current time
48
+ {
49
+ accounts: {
50
+ payer: payerPublicKey,
51
+ vaultSystemState: vaultSystemStatePublicKey,
52
+ pool: poolPublickey,
53
+ poolPosition: poolPositionPublicKey,
54
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
55
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
56
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
57
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
58
+ payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
59
+ hedgeMint: hedgeMintPublickey,
60
+ stakedTokenMint: stakedTokenMintPublicKey,
61
+ usdhMint: usdhMintPublickey,
62
+ rent: SYSVAR_RENT_PUBKEY,
63
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
64
+ splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
65
+ systemProgram: SystemProgram.programId
66
+ },
67
+ signers: []
68
+ })
69
+ }
@@ -1,21 +1,19 @@
1
- import { BN, Program } from '@project-serum/anchor'
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { ConfirmOptions, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { CHAINLINK_SOL_USD_PUBLICKEY, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
-
6
- import { vaultIdl } from '../idl/idl'
3
+ import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { CHAINLINK_SOL_USD_PUBLICKEY, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
7
5
 
8
6
  export async function withdrawVault (
9
- connection: Connection,
7
+ program: Program,
8
+ provider: Provider,
10
9
  payer: Signer,
11
10
  vaultPublicKey: PublicKey,
12
11
  withdrawAmount: number,
13
- chainlinkOverridePrice?: number,
14
- confirmOptions?: ConfirmOptions
12
+ chainlinkOverridePrice?: number
15
13
  ): Promise<PublicKey> {
16
14
  const [usdhMintPublickey] = await getUsdhMintPublicKey()
17
15
  const USDH = new Token(
18
- connection,
16
+ provider.connection,
19
17
  usdhMintPublickey,
20
18
  TOKEN_PROGRAM_ID,
21
19
  payer
@@ -28,6 +26,7 @@ export async function withdrawVault (
28
26
  const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
29
27
  const transaction = new Transaction().add(
30
28
  withdrawVaultInstruction(
29
+ program,
31
30
  vaultSystemStatePublicKey,
32
31
  payer.publicKey,
33
32
  vaultPublicKey,
@@ -36,11 +35,12 @@ export async function withdrawVault (
36
35
  chainlinkOverridePrice
37
36
  )
38
37
  )
39
- await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
38
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
40
39
  return vaultPublicKey
41
40
  }
42
41
 
43
42
  export function withdrawVaultInstruction (
43
+ program: Program,
44
44
  vaultSystemStatePublicKey: PublicKey,
45
45
  payerPublicKey: PublicKey,
46
46
  vaultPublickey: PublicKey,
@@ -48,7 +48,6 @@ export function withdrawVaultInstruction (
48
48
  withdrawAmount: number,
49
49
  chainlinkOverridePrice?: number
50
50
  ): TransactionInstruction {
51
- const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
52
51
  return program.instruction.withdrawVault(
53
52
  new BN(withdrawAmount),
54
53
  new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
@@ -1,6 +1,6 @@
1
1
 
2
2
  import Decimal from 'decimal.js'
3
- import { DecimalFromU128 } from '..'
3
+ import { DecimalFromU128 } from '../HedgeDecimal'
4
4
 
5
5
  /**
6
6
  * Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
@@ -9,13 +9,13 @@ import { DecimalFromU128 } from '..'
9
9
  export class LiquidationPoolEra {
10
10
  public totalDeposits: number
11
11
 
12
- product: Decimal
12
+ public product: Decimal
13
13
 
14
- sum: Decimal
14
+ public sum: Decimal
15
15
 
16
- hedgeRewardsAccumulator: Decimal
16
+ public hedgeRewardsAccumulator: Decimal
17
17
 
18
- hedgeRewardsTimestamp: number
18
+ public hedgeRewardsTimestamp: number
19
19
 
20
20
  constructor (public liquidyPoolEra: any) {
21
21
  this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
@@ -1,6 +1,6 @@
1
- import { PublicKey } from '@solana/web3.js'
1
+ import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
2
2
  import Decimal from 'decimal.js'
3
- import { DecimalFromU128 } from '..'
3
+ import { DecimalFromU128 } from '../HedgeDecimal'
4
4
  import { LiquidationPoolEra } from './LiquidationPoolEra'
5
5
  import { LiquidationPoolState } from './LiquidationPoolState'
6
6
 
@@ -48,22 +48,30 @@ export class LiquidationPosition {
48
48
  return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal(this.deposit)).floor()
49
49
  }
50
50
 
51
- // getHedgeAvailable () {
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)
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
59
 
60
- // if (this.era.totalDeposits === 0) {
61
- // return 0
62
- // }
60
+ if (this.era.totalDeposits === 0) {
61
+ return new Decimal(0)
62
+ }
63
63
 
64
- // const rewardsPerToken = hedgeRewardsSinceLastUpdate / this.era.totalDeposits * this.era.product
65
- // const newAccumulator = this.era.hedgeRewardsAccumulator + rewardsPerToken
66
- // const hedgeAvailable = (newAccumulator - this.hedgeRewardsSnapshot) * this.deposit / this.productSnapshot
67
- // return hedgeAvailable
68
- // }
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
+ }
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
69
77
  }
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { PublicKey } from '@solana/web3.js'
3
3
  import Decimal from 'decimal.js'
4
- import { DecimalFromU128 } from '..'
4
+ import { DecimalFromU128 } from '../HedgeDecimal'
5
5
 
6
6
  export class StakingPool {
7
7
  public publicKey: PublicKey
@@ -14,10 +14,9 @@ export class StakingPool {
14
14
  hedgeRewardAccumulator: Decimal
15
15
  usdhFeeAccumulator: Decimal
16
16
  solFeeAccumulator: Decimal
17
- currentRewardsPerDay: Decimal
18
17
 
19
- constructor (public poolInfo: any, key: PublicKey) {
20
- this.publicKey = key
18
+ constructor (public poolInfo: any, publicKey: PublicKey) {
19
+ this.publicKey = publicKey
21
20
  this.deposits = poolInfo.deposits.toNumber()
22
21
  // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
23
22
  // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
@@ -29,7 +28,7 @@ export class StakingPool {
29
28
  this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
30
29
  this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
31
30
  this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
32
- this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
31
+ // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
33
32
  }
34
33
 
35
34
  // updateFeeAccumulator () {
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { PublicKey } from '@solana/web3.js'
3
3
  import Decimal from 'decimal.js'
4
- import { DecimalFromU128 } from '..'
4
+ import { DecimalFromU128 } from '../HedgeDecimal'
5
5
  import { StakingPool } from './StakingPool'
6
6
 
7
7
  export class StakingPoolPosition {
@@ -33,8 +33,7 @@ export class StakingPoolPosition {
33
33
  this.open = poolPositionInfo.state.open !== undefined
34
34
  }
35
35
 
36
- // getCurrentUsdhFeeReward () {
37
- // const feesAccured = (this.pool.usdhFeeAccumulator - this.startUsdhFeeAccumulator) * this.deposited
38
- // return feesAccured
39
- // }
36
+ public getCurrentUsdhFeeReward (): Decimal {
37
+ return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal(this.deposited))
38
+ }
40
39
  }
@@ -34,7 +34,6 @@ export class VaultAccount {
34
34
  this.deposited = vault.deposited.toNumber()
35
35
  this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio)
36
36
  this.liquidationPrice = vault.liquidationPrice.toNumber()
37
- this.minCollateralRatio = vault.minCollateralRatio.toNumber()
38
37
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]
39
38
  }
40
39
 
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { PublicKey } from '@solana/web3.js'
3
3
  import Decimal from 'decimal.js'
4
- import { DecimalFromU128 } from '..'
4
+ import { DecimalFromU128 } from '../HedgeDecimal'
5
5
 
6
6
  export class VaultHistoryEvent {
7
7
  publicKey: PublicKey
@@ -0,0 +1,11 @@
1
+ import { parseIdlErrors, ProgramError } from '@project-serum/anchor'
2
+ import { vaultIdl } from '../idl/idl'
3
+
4
+ export function parseAnchorErrors (error: any): void {
5
+ const idlErrors = parseIdlErrors(vaultIdl)
6
+ const parsedError = ProgramError.parse(error, idlErrors)
7
+ if (parsedError !== null) {
8
+ throw parsedError
9
+ }
10
+ throw error
11
+ }