hedge-web3 0.1.1 → 0.1.2

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 +1790 -0
  2. package/lib/index.js.map +1 -0
  3. package/lib/types/src/Constants.d.ts +13 -0
  4. package/lib/types/src/Constants.d.ts.map +1 -0
  5. package/lib/types/src/HedgeDecimal.d.ts +15 -0
  6. package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
  7. package/lib/types/src/idl/idl.d.ts +3 -0
  8. package/lib/types/src/idl/idl.d.ts.map +1 -0
  9. package/lib/types/src/index.d.ts +10 -0
  10. package/lib/types/src/index.d.ts.map +1 -0
  11. package/lib/types/src/instructions/createVault.d.ts +4 -0
  12. package/lib/types/src/instructions/createVault.d.ts.map +1 -0
  13. package/lib/types/src/instructions/depositVault.d.ts +4 -0
  14. package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
  15. package/lib/types/src/instructions/loanVault.d.ts +4 -0
  16. package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
  17. package/lib/types/src/instructions/redeemVault.d.ts +4 -0
  18. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
  19. package/lib/types/src/instructions/repayVault.d.ts +4 -0
  20. package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
  21. package/lib/types/src/instructions/withdrawVault.d.ts +4 -0
  22. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
  23. package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
  24. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
  25. package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
  26. package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
  27. package/lib/types/src/state/LiquidationPosition.d.ts +24 -0
  28. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
  29. package/lib/types/src/state/StakingPool.d.ts +17 -0
  30. package/lib/types/src/state/StakingPool.d.ts.map +1 -0
  31. package/lib/types/src/state/StakingPoolPosition.d.ts +19 -0
  32. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
  33. package/lib/types/src/state/VaultAccount.d.ts +48 -0
  34. package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
  35. package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
  36. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
  37. package/lib/types/tsconfig.base.tsbuildinfo +1 -0
  38. package/package.json +5 -4
  39. package/rollup.config.js +14 -8
  40. package/src/Constants.ts +56 -0
  41. package/src/HedgeDecimal.ts +15 -7
  42. package/src/idl/idl.ts +1474 -0
  43. package/src/index.ts +7 -1
  44. package/src/instructions/createVault.ts +66 -0
  45. package/src/instructions/depositVault.ts +61 -0
  46. package/src/instructions/loanVault.ts +79 -0
  47. package/src/instructions/redeemVault.ts +83 -0
  48. package/src/instructions/repayVault.ts +74 -0
  49. package/src/instructions/withdrawVault.ts +66 -0
  50. package/src/state/LiquidationPoolEra.ts +29 -0
  51. package/src/state/LiquidationPoolState.ts +10 -0
  52. package/src/state/LiquidationPosition.ts +69 -0
  53. package/src/state/StakingPool.ts +39 -0
  54. package/src/state/StakingPoolPosition.ts +40 -0
  55. package/src/{accounts → state}/VaultAccount.ts +5 -5
  56. package/src/state/VaultHistoryEvent.ts +61 -0
  57. package/tsconfig.base.json +3 -3
  58. package/tsconfig.d.json +3 -1
  59. package/tsconfig.json +1 -1
  60. package/typedoc.json +2 -1
  61. package/src/accounts/LiquidationPoolEra.ts +0 -16
  62. package/src/accounts/LiquidationPoolState.ts +0 -22
  63. package/src/accounts/LiquidationPosition.ts +0 -72
  64. package/src/accounts/StakingPool.ts +0 -31
  65. package/src/accounts/StakingPoolPosition.ts +0 -29
  66. package/src/accounts/VaultHistoryEvent.ts +0 -27
package/src/index.ts CHANGED
@@ -3,5 +3,11 @@ export function foo (input: String): void {
3
3
  console.log('Input', input)
4
4
  }
5
5
 
6
- export * from './accounts/VaultAccount'
6
+ export * from './instructions/createVault'
7
+ export * from './instructions/depositVault'
8
+ export * from './instructions/withdrawVault'
9
+ export * from './instructions/loanVault'
10
+ export * from './instructions/repayVault'
11
+ export * from './instructions/redeemVault'
12
+ export * from './state/VaultAccount'
7
13
  export * from './HedgeDecimal'
@@ -0,0 +1,66 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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 { getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
+
6
+ import { vaultIdl } from '../idl/idl'
7
+
8
+ export async function createVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ depositAmount: number,
12
+ collateralRatio: number,
13
+ confirmOptions?: ConfirmOptions
14
+ ): Promise<PublicKey> {
15
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
16
+ const USDH = new Token(
17
+ connection,
18
+ usdhMintPublickey,
19
+ TOKEN_PROGRAM_ID,
20
+ payer
21
+ )
22
+
23
+ // Prep the user to get USDH back out at some point
24
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
25
+
26
+ const newVault = Keypair.generate()
27
+ const history = Keypair.generate()
28
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
29
+ const transaction = new Transaction().add(
30
+ createVaultInstruction(
31
+ vaultSystemStatePublicKey,
32
+ payer.publicKey,
33
+ newVault.publicKey,
34
+ history.publicKey,
35
+ depositAmount,
36
+ collateralRatio
37
+ )
38
+ )
39
+ await sendAndConfirmTransaction(connection, transaction, [payer, newVault, history], confirmOptions)
40
+ return newVault.publicKey
41
+ }
42
+
43
+ export function createVaultInstruction (
44
+ vaultSystemStatePublicKey: PublicKey,
45
+ payerPublicKey: PublicKey,
46
+ vaultPublicKey: PublicKey,
47
+ historyPublicKey: PublicKey,
48
+ depositAmount: number,
49
+ collateralRatio: number
50
+ ): TransactionInstruction {
51
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
52
+ const ix = program.instruction.createVault(
53
+ new BN(depositAmount),
54
+ new BN(collateralRatio)
55
+ , {
56
+ accounts: {
57
+ vaultSystemState: vaultSystemStatePublicKey,
58
+ vault: vaultPublicKey,
59
+ history: historyPublicKey,
60
+ payer: payerPublicKey,
61
+ systemProgram: SystemProgram.programId
62
+ },
63
+ signers: []
64
+ })
65
+ return ix
66
+ }
@@ -0,0 +1,61 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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 { getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
+
6
+ import { vaultIdl } from '../idl/idl'
7
+
8
+ export async function depositVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ depositAmount: number,
13
+ confirmOptions?: ConfirmOptions
14
+ ): Promise<PublicKey> {
15
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
16
+ const USDH = new Token(
17
+ connection,
18
+ usdhMintPublickey,
19
+ TOKEN_PROGRAM_ID,
20
+ payer
21
+ )
22
+
23
+ // Prep the user to get USDH back out at some point
24
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
25
+
26
+ const history = Keypair.generate()
27
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
28
+ const transaction = new Transaction().add(
29
+ depositVaultInstruction(
30
+ vaultSystemStatePublicKey,
31
+ payer.publicKey,
32
+ vaultPublicKey,
33
+ history.publicKey,
34
+ depositAmount
35
+ )
36
+ )
37
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
38
+ return vaultPublicKey
39
+ }
40
+
41
+ export function depositVaultInstruction (
42
+ vaultSystemStatePublicKey: PublicKey,
43
+ payerPublicKey: PublicKey,
44
+ vaultPublicKey: PublicKey,
45
+ historyPublicKey: PublicKey,
46
+ depositAmount: number
47
+ ): TransactionInstruction {
48
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
49
+ return program.instruction.depositVault(
50
+ new BN(depositAmount)
51
+ , {
52
+ accounts: {
53
+ vaultSystemState: vaultSystemStatePublicKey,
54
+ vaultAccount: vaultPublicKey,
55
+ history: historyPublicKey,
56
+ vaultOwner: payerPublicKey,
57
+ systemProgram: SystemProgram.programId
58
+ },
59
+ signers: []
60
+ })
61
+ }
@@ -0,0 +1,79 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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'
7
+
8
+ export async function loanVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ loanAmount: number,
13
+ chainlinkOverridePrice?: number,
14
+ confirmOptions?: ConfirmOptions
15
+ ): Promise<PublicKey> {
16
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
17
+ const USDH = new Token(
18
+ connection,
19
+ usdhMintPublickey,
20
+ TOKEN_PROGRAM_ID,
21
+ payer
22
+ )
23
+
24
+ // Prep the user to get USDH back out at some point
25
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
26
+
27
+ const history = Keypair.generate()
28
+ const transaction = new Transaction().add(
29
+ await loanVaultInstruction(
30
+ payer.publicKey,
31
+ payerUsdhAccount.address,
32
+ vaultPublicKey,
33
+ history.publicKey,
34
+ loanAmount,
35
+ chainlinkOverridePrice
36
+ )
37
+ )
38
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
39
+ return vaultPublicKey
40
+ }
41
+
42
+ export async function loanVaultInstruction (
43
+ payerPublicKey: PublicKey,
44
+ ownerUsdhAccount: PublicKey,
45
+ vaultPublickey: PublicKey,
46
+ historyPublicKey: PublicKey,
47
+ loanAmount: number,
48
+ chainlinkOverridePrice?: number
49
+ ): Promise<TransactionInstruction> {
50
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
51
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
52
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
53
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
54
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
55
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
56
+ hedgeStakingPoolPublicKey,
57
+ usdhMintPublickey
58
+ )
59
+
60
+ return program.instruction.loanVault(
61
+ new BN(loanAmount),
62
+ new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
63
+ {
64
+ accounts: {
65
+ vaultSystemState: vaultSystemStatePublicKey,
66
+ vaultAccount: vaultPublickey,
67
+ history: historyPublicKey,
68
+ feePool: hedgeStakingPoolPublicKey,
69
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
70
+ usdhMint: usdhMintPublickey,
71
+ vaultOwner: payerPublicKey,
72
+ ownerUsdhAccount: ownerUsdhAccount,
73
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
74
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
75
+ systemProgram: SystemProgram.programId
76
+ },
77
+ signers: []
78
+ })
79
+ }
@@ -0,0 +1,83 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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'
7
+
8
+ export async function redeemVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ repayAmount: number,
13
+ chainlinkOverridePrice?: number,
14
+ transactionOverrideTime?: number,
15
+ confirmOptions?: ConfirmOptions
16
+ ): Promise<PublicKey> {
17
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
18
+ const USDH = new Token(
19
+ connection,
20
+ usdhMintPublickey,
21
+ TOKEN_PROGRAM_ID,
22
+ payer
23
+ )
24
+
25
+ // Prep the user to get USDH back out at some point
26
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
27
+
28
+ const history = Keypair.generate()
29
+ const transaction = new Transaction().add(
30
+ await redeemVaultInstruction(
31
+ payer.publicKey,
32
+ payerUsdhAccount.address,
33
+ vaultPublicKey,
34
+ history.publicKey,
35
+ repayAmount,
36
+ chainlinkOverridePrice,
37
+ transactionOverrideTime
38
+ )
39
+ )
40
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
41
+ return vaultPublicKey
42
+ }
43
+
44
+ export async function redeemVaultInstruction (
45
+ payerPublicKey: PublicKey,
46
+ ownerUsdhAccount: PublicKey,
47
+ vaultPublickey: PublicKey,
48
+ historyPublicKey: PublicKey,
49
+ repayAmount: number,
50
+ chainlinkOverridePrice?: number,
51
+ transactionOverrideTime?: number
52
+ ): Promise<TransactionInstruction> {
53
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
54
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
55
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
56
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
57
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
58
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
59
+ hedgeStakingPoolPublicKey,
60
+ usdhMintPublickey
61
+ )
62
+
63
+ return program.instruction.redeemVault(
64
+ new BN(repayAmount),
65
+ new BN(chainlinkOverridePrice ?? 150 * LAMPORTS_PER_SOL), // override usd/sol price
66
+ new BN(transactionOverrideTime ?? (Date.now() / 1000)), // override start time
67
+ {
68
+ accounts: {
69
+ vaultSystemState: vaultSystemStatePublicKey,
70
+ vaultAccount: vaultPublickey,
71
+ history: historyPublicKey,
72
+ feePool: hedgeStakingPoolPublicKey,
73
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
74
+ usdhMint: usdhMintPublickey,
75
+ payer: payerPublicKey,
76
+ payerUsdhAccount: ownerUsdhAccount,
77
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
78
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
79
+ systemProgram: SystemProgram.programId
80
+ },
81
+ signers: []
82
+ })
83
+ }
@@ -0,0 +1,74 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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'
7
+
8
+ export async function repayVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ repayAmount: number,
13
+ confirmOptions?: ConfirmOptions
14
+ ): Promise<PublicKey> {
15
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
16
+ const USDH = new Token(
17
+ connection,
18
+ usdhMintPublickey,
19
+ TOKEN_PROGRAM_ID,
20
+ payer
21
+ )
22
+
23
+ // Prep the user to get USDH back out at some point
24
+ const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
25
+
26
+ const history = Keypair.generate()
27
+ const transaction = new Transaction().add(
28
+ await repayVaultInstruction(
29
+ payer.publicKey,
30
+ payerUsdhAccount.address,
31
+ vaultPublicKey,
32
+ history.publicKey,
33
+ repayAmount
34
+ )
35
+ )
36
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
37
+ return vaultPublicKey
38
+ }
39
+
40
+ export async function repayVaultInstruction (
41
+ payerPublicKey: PublicKey,
42
+ ownerUsdhAccount: PublicKey,
43
+ vaultPublickey: PublicKey,
44
+ historyPublicKey: PublicKey,
45
+ repayAmount: number
46
+ ): Promise<TransactionInstruction> {
47
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
48
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
49
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
50
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
51
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
52
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
53
+ hedgeStakingPoolPublicKey,
54
+ usdhMintPublickey
55
+ )
56
+
57
+ return program.instruction.repayVault(
58
+ new BN(repayAmount),
59
+ {
60
+ accounts: {
61
+ vaultSystemState: vaultSystemStatePublicKey,
62
+ vaultAccount: vaultPublickey,
63
+ history: historyPublicKey,
64
+ feePool: hedgeStakingPoolPublicKey,
65
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
66
+ usdhMint: usdhMintPublickey,
67
+ vaultOwner: payerPublicKey,
68
+ ownerUsdhAccount: ownerUsdhAccount,
69
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
70
+ systemProgram: SystemProgram.programId
71
+ },
72
+ signers: []
73
+ })
74
+ }
@@ -0,0 +1,66 @@
1
+ import { BN, Program } from '@project-serum/anchor'
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'
7
+
8
+ export async function withdrawVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ withdrawAmount: number,
13
+ chainlinkOverridePrice?: number,
14
+ confirmOptions?: ConfirmOptions
15
+ ): Promise<PublicKey> {
16
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
17
+ const USDH = new Token(
18
+ connection,
19
+ usdhMintPublickey,
20
+ TOKEN_PROGRAM_ID,
21
+ payer
22
+ )
23
+
24
+ // Prep the user to get USDH back out at some point
25
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
26
+
27
+ const history = Keypair.generate()
28
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
29
+ const transaction = new Transaction().add(
30
+ withdrawVaultInstruction(
31
+ vaultSystemStatePublicKey,
32
+ payer.publicKey,
33
+ vaultPublicKey,
34
+ history.publicKey,
35
+ withdrawAmount,
36
+ chainlinkOverridePrice
37
+ )
38
+ )
39
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
40
+ return vaultPublicKey
41
+ }
42
+
43
+ export function withdrawVaultInstruction (
44
+ vaultSystemStatePublicKey: PublicKey,
45
+ payerPublicKey: PublicKey,
46
+ vaultPublickey: PublicKey,
47
+ historyPublicKey: PublicKey,
48
+ withdrawAmount: number,
49
+ chainlinkOverridePrice?: number
50
+ ): TransactionInstruction {
51
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
52
+ return program.instruction.withdrawVault(
53
+ new BN(withdrawAmount),
54
+ new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
55
+ {
56
+ accounts: {
57
+ vaultSystemState: vaultSystemStatePublicKey,
58
+ vaultAccount: vaultPublickey,
59
+ history: historyPublicKey,
60
+ vaultOwner: payerPublicKey,
61
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
62
+ systemProgram: SystemProgram.programId
63
+ },
64
+ signers: []
65
+ })
66
+ }
@@ -0,0 +1,29 @@
1
+
2
+ import Decimal from 'decimal.js'
3
+ import { DecimalFromU128 } from '..'
4
+
5
+ /**
6
+ * Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
7
+ * created to maintain each users contribution to the pool.
8
+ */
9
+ export class LiquidationPoolEra {
10
+ public totalDeposits: number
11
+
12
+ product: Decimal
13
+
14
+ sum: Decimal
15
+
16
+ hedgeRewardsAccumulator: Decimal
17
+
18
+ hedgeRewardsTimestamp: number
19
+
20
+ constructor (public liquidyPoolEra: any) {
21
+ this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
22
+
23
+ this.product = DecimalFromU128(liquidyPoolEra.productBytes)
24
+ this.sum = DecimalFromU128(liquidyPoolEra.sumBytes)
25
+ this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
26
+
27
+ this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
28
+ }
29
+ }
@@ -0,0 +1,10 @@
1
+
2
+ export class LiquidationPoolState {
3
+ public lifetimeDeposits: number
4
+ public hedgeInitRewardsTimestamp: number
5
+ constructor (public liquidationPoolState: any) {
6
+ this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber()
7
+ this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber()
8
+ // TODO Add the rest that are missing. Do we need them?
9
+ }
10
+ }
@@ -0,0 +1,69 @@
1
+ import { PublicKey } from '@solana/web3.js'
2
+ import Decimal from 'decimal.js'
3
+ import { DecimalFromU128 } from '..'
4
+ import { LiquidationPoolEra } from './LiquidationPoolEra'
5
+ import { LiquidationPoolState } from './LiquidationPoolState'
6
+
7
+ export class LiquidationPosition {
8
+ public publicKey: PublicKey
9
+ public eraPublicKey: PublicKey
10
+ public era: LiquidationPoolEra
11
+ public liquidationPoolState: LiquidationPoolState
12
+
13
+ public ownerAccount: PublicKey
14
+ public deposit: number
15
+ public closedUsdh: number
16
+ public closedSol: number
17
+ public timestampOpened: Date
18
+ public timestampClosed: Date
19
+
20
+ public productSnapshot: Decimal
21
+ public sumSnapshot: Decimal
22
+ public hedgeRewardsSnapshot: Decimal
23
+ public open: boolean
24
+
25
+ constructor (poolPositionInfo: any, key: PublicKey, era: LiquidationPoolEra, liquidationPoolState: LiquidationPoolState) {
26
+ this.publicKey = key
27
+ this.eraPublicKey = poolPositionInfo.era
28
+ this.era = era
29
+ this.liquidationPoolState = liquidationPoolState
30
+ this.ownerAccount = poolPositionInfo.ownerAccount
31
+ this.deposit = poolPositionInfo.deposit.toNumber()
32
+ this.closedUsdh = poolPositionInfo.closedUsdh.toNumber()
33
+ this.closedSol = poolPositionInfo.closedSol.toNumber()
34
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
35
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
36
+
37
+ this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes)
38
+ this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes)
39
+ this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum)
40
+ this.open = poolPositionInfo.state.open !== undefined
41
+ }
42
+
43
+ public getUsdhAvailable (): Decimal {
44
+ return (this.era.product.div(this.productSnapshot)).mul(new Decimal(this.deposit))
45
+ }
46
+
47
+ public getSolAvailable (): Decimal {
48
+ return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal(this.deposit)).floor()
49
+ }
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)
59
+
60
+ // if (this.era.totalDeposits === 0) {
61
+ // return 0
62
+ // }
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
+ // }
69
+ }
@@ -0,0 +1,39 @@
1
+
2
+ import { PublicKey } from '@solana/web3.js'
3
+ import Decimal from 'decimal.js'
4
+ import { DecimalFromU128 } from '..'
5
+
6
+ export class StakingPool {
7
+ public publicKey: PublicKey
8
+ public deposits: number
9
+ lastTransactionTime: number
10
+ startTime: number
11
+ halfLifeInDays: number
12
+ totalHedgeReward: number
13
+
14
+ hedgeRewardAccumulator: Decimal
15
+ usdhFeeAccumulator: Decimal
16
+ solFeeAccumulator: Decimal
17
+ currentRewardsPerDay: Decimal
18
+
19
+ constructor (public poolInfo: any, key: PublicKey) {
20
+ this.publicKey = key
21
+ this.deposits = poolInfo.deposits.toNumber()
22
+ // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
23
+ // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
24
+ this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
25
+ this.startTime = poolInfo.startTime.toNumber()
26
+ this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
27
+ this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber()
28
+
29
+ this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
30
+ this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
31
+ this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
32
+ this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
33
+ }
34
+
35
+ // updateFeeAccumulator () {
36
+ // this.feeAccumulator = (this.totalFeesNow - this.totalFeesPrevious) / this.deposits
37
+ // this.totalFeesPrevious = this.totalFeesNow
38
+ // }
39
+ }
@@ -0,0 +1,40 @@
1
+
2
+ import { PublicKey } from '@solana/web3.js'
3
+ import Decimal from 'decimal.js'
4
+ import { DecimalFromU128 } from '..'
5
+ import { StakingPool } from './StakingPool'
6
+
7
+ export class StakingPoolPosition {
8
+ public publicKey: PublicKey
9
+ public pool: StakingPool
10
+ public owner: PublicKey
11
+ public deposited: number
12
+ public timestampOpened: number
13
+ public timestampClosed: number
14
+ public closedRewardedTokens: number
15
+ public startHedgeRewardAccumulator: Decimal
16
+ public startUsdhFeeAccumulator: Decimal
17
+ public startSolFeeAccumulator: Decimal
18
+ public open: boolean
19
+
20
+ constructor (public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
21
+ this.publicKey = key
22
+ this.pool = stakingPool
23
+ this.owner = poolPositionInfo.owner
24
+ this.deposited = poolPositionInfo.deposited.toNumber()
25
+ this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
26
+ this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
27
+ this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber()
28
+
29
+ this.startHedgeRewardAccumulator = DecimalFromU128(poolPositionInfo.startHedgeRewardAccumulator)
30
+ this.startUsdhFeeAccumulator = DecimalFromU128(poolPositionInfo.startUsdhFeeAccumulator)
31
+ this.startSolFeeAccumulator = DecimalFromU128(poolPositionInfo.startSolFeeAccumulator)
32
+
33
+ this.open = poolPositionInfo.state.open !== undefined
34
+ }
35
+
36
+ // getCurrentUsdhFeeReward () {
37
+ // const feesAccured = (this.pool.usdhFeeAccumulator - this.startUsdhFeeAccumulator) * this.deposited
38
+ // return feesAccured
39
+ // }
40
+ }
@@ -1,5 +1,6 @@
1
1
  import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
2
- import { HedgeDecimal } from '../HedgeDecimal'
2
+ import Decimal from 'decimal.js'
3
+ import { DecimalFromU128 } from '../HedgeDecimal'
3
4
 
4
5
  /**
5
6
  * A class that represents an on-chian vault.
@@ -18,7 +19,7 @@ export class VaultAccount {
18
19
  deposited: number
19
20
 
20
21
  /** The minimum collateral ratio this vault must maintain to not be subject to liquidation. */
21
- minCollateralRatio: HedgeDecimal
22
+ minCollateralRatio: Decimal
22
23
 
23
24
  /** The SOL/USD price at which this vault is subject to liquidation */
24
25
  liquidationPrice: number
@@ -26,13 +27,12 @@ export class VaultAccount {
26
27
  /** Current State of the vautl ("Open", "Closed", "Liquidated") */
27
28
  vaultStatus: string
28
29
 
29
- constructor (vault: any
30
- , publicKey: PublicKey) {
30
+ constructor (vault: any, publicKey: PublicKey) {
31
31
  this.publicKey = publicKey
32
32
  this.vaultOwner = vault.vaultOwner
33
33
  this.debt = vault.debt.toNumber()
34
34
  this.deposited = vault.deposited.toNumber()
35
- this.minCollateralRatio = vault.minCollateralRatio.toNumber()
35
+ this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio)
36
36
  this.liquidationPrice = vault.liquidationPrice.toNumber()
37
37
  this.minCollateralRatio = vault.minCollateralRatio.toNumber()
38
38
  this.vaultStatus = Object.keys(vault.vaultStatus)[0]