hedge-web3 0.1.0 → 0.1.4

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 (90) hide show
  1. package/.github/workflows/npm-publish.yml +34 -0
  2. package/README.md +44 -0
  3. package/lib/index.js +1976 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/src/Constants.d.ts +13 -0
  6. package/lib/types/src/Constants.d.ts.map +1 -0
  7. package/lib/types/src/HedgeDecimal.d.ts +15 -0
  8. package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
  9. package/lib/types/src/StakingPools.d.ts +4 -0
  10. package/lib/types/src/StakingPools.d.ts.map +1 -0
  11. package/lib/types/src/Vaults.d.ts +9 -0
  12. package/lib/types/src/Vaults.d.ts.map +1 -0
  13. package/lib/types/src/idl/idl.d.ts +3 -0
  14. package/lib/types/src/idl/idl.d.ts.map +1 -0
  15. package/lib/types/src/index.d.ts +5 -0
  16. package/lib/types/src/index.d.ts.map +1 -0
  17. package/lib/types/src/instructions/createStakingPool.d.ts +4 -0
  18. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
  19. package/lib/types/src/instructions/createVault.d.ts +4 -0
  20. package/lib/types/src/instructions/createVault.d.ts.map +1 -0
  21. package/lib/types/src/instructions/depositStakingPool.d.ts +4 -0
  22. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
  23. package/lib/types/src/instructions/depositVault.d.ts +4 -0
  24. package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
  25. package/lib/types/src/instructions/liquidateVault.d.ts +4 -0
  26. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
  27. package/lib/types/src/instructions/loanVault.d.ts +4 -0
  28. package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
  29. package/lib/types/src/instructions/redeemVault.d.ts +4 -0
  30. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
  31. package/lib/types/src/instructions/repayVault.d.ts +4 -0
  32. package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
  33. package/lib/types/src/instructions/withdrawStakingPool.d.ts +4 -0
  34. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
  35. package/lib/types/src/instructions/withdrawVault.d.ts +4 -0
  36. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
  37. package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
  38. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
  39. package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
  40. package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
  41. package/lib/types/src/state/LiquidationPosition.d.ts +24 -0
  42. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
  43. package/lib/types/src/state/StakingPool.d.ts +17 -0
  44. package/lib/types/src/state/StakingPool.d.ts.map +1 -0
  45. package/lib/types/src/state/StakingPoolPosition.d.ts +19 -0
  46. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
  47. package/lib/types/src/state/VaultAccount.d.ts +48 -0
  48. package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
  49. package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
  50. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
  51. package/lib/types/src/utils/Errors.d.ts +2 -0
  52. package/lib/types/src/utils/Errors.d.ts.map +1 -0
  53. package/lib/types/tsconfig.base.tsbuildinfo +1 -0
  54. package/package.json +6 -6
  55. package/rollup.config.js +14 -8
  56. package/src/Constants.ts +56 -0
  57. package/src/HedgeDecimal.ts +15 -7
  58. package/src/StakingPools.ts +3 -0
  59. package/src/Vaults.ts +8 -0
  60. package/src/idl/idl.ts +1474 -0
  61. package/src/index.ts +3 -5
  62. package/src/instructions/createStakingPool.ts +65 -0
  63. package/src/instructions/createVault.ts +66 -0
  64. package/src/instructions/depositStakingPool.ts +61 -0
  65. package/src/instructions/depositVault.ts +61 -0
  66. package/src/instructions/liquidateVault.ts +67 -0
  67. package/src/instructions/loanVault.ts +79 -0
  68. package/src/instructions/redeemVault.ts +83 -0
  69. package/src/instructions/repayVault.ts +74 -0
  70. package/src/instructions/withdrawStakingPool.ts +70 -0
  71. package/src/instructions/withdrawVault.ts +66 -0
  72. package/src/state/LiquidationPoolEra.ts +29 -0
  73. package/src/state/LiquidationPoolState.ts +10 -0
  74. package/src/state/LiquidationPosition.ts +69 -0
  75. package/src/state/StakingPool.ts +39 -0
  76. package/src/state/StakingPoolPosition.ts +40 -0
  77. package/src/state/VaultAccount.ts +77 -0
  78. package/src/state/VaultHistoryEvent.ts +61 -0
  79. package/src/types/buffer-layout/index.d.ts +88 -0
  80. package/src/utils/Errors.ts +11 -0
  81. package/tsconfig.base.json +3 -3
  82. package/tsconfig.d.json +3 -1
  83. package/tsconfig.json +1 -1
  84. package/src/accounts/LiquidationPoolEra.ts +0 -16
  85. package/src/accounts/LiquidationPoolState.ts +0 -22
  86. package/src/accounts/LiquidationPosition.ts +0 -72
  87. package/src/accounts/StakingPool.ts +0 -31
  88. package/src/accounts/StakingPoolPosition.ts +0 -29
  89. package/src/accounts/VaultAccount.ts +0 -40
  90. package/src/accounts/VaultHistoryEvent.ts +0 -27
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
1
 
2
- export function foo (input: String): void {
3
- console.log('Input', input)
4
- }
5
-
6
- export * from './accounts/VaultAccount'
2
+ export * as Vaults from './Vaults'
3
+ export * as StakingPool from './StakingPools'
7
4
  export * from './HedgeDecimal'
5
+ export * as Constants from './Constants'
@@ -0,0 +1,65 @@
1
+ import { BN, Program } from '@project-serum/anchor'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { ConfirmOptions, Connection, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { parseAnchorErrors } from '../utils/Errors'
5
+ import { findAssociatedTokenAddress, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
6
+
7
+ import { vaultIdl } from '../idl/idl'
8
+
9
+ export async function createStakingPool (
10
+ connection: Connection,
11
+ payer: Signer,
12
+ mintPublicKey: PublicKey,
13
+ hedgeTokensToBeMinted: number,
14
+ overrideStartTime?: number,
15
+ confirmOptions?: ConfirmOptions
16
+ ): Promise<PublicKey> {
17
+ const transaction = new Transaction().add(
18
+ await createStakingPoolInstruction(
19
+ payer.publicKey,
20
+ mintPublicKey,
21
+ hedgeTokensToBeMinted,
22
+ overrideStartTime
23
+ )
24
+ )
25
+ await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions).catch(parseAnchorErrors)
26
+ const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey)
27
+ return poolPublickey
28
+ }
29
+
30
+ export async function createStakingPoolInstruction (
31
+ payerPublicKey: PublicKey,
32
+ mintPublicKey: PublicKey,
33
+ hedgeTokensToBeMinted: number,
34
+ overrideStartTime?: number
35
+ ): Promise<TransactionInstruction> {
36
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
37
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
38
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
39
+ const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey)
40
+
41
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
42
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
43
+
44
+ return program.instruction.createStakingPool(
45
+ poolBump,
46
+ poolSeedPhrase,
47
+ new BN(hedgeTokensToBeMinted),
48
+ new BN(overrideStartTime ?? Date.now() / 1000),
49
+ {
50
+ accounts: {
51
+ signer: payerPublicKey,
52
+ vaultSystemState: vaultSystemStatePublicKey,
53
+ pool: poolPublickey,
54
+ stakedTokenMintInfo: mintPublicKey,
55
+ usdhMint: usdhMintPublickey,
56
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
57
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
58
+ rent: SYSVAR_RENT_PUBKEY,
59
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
60
+ splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
61
+ systemProgram: SystemProgram.programId
62
+ },
63
+ signers: []
64
+ })
65
+ }
@@ -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 { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { ConfirmOptions, Connection, Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { parseAnchorErrors } from '../utils/Errors'
5
+ import { findAssociatedTokenAddress, getPoolPublicKeyForMint, HEDGE_PROGRAM_ID } from '../Constants'
6
+
7
+ import { vaultIdl } from '../idl/idl'
8
+
9
+ export async function depositStakingPool (
10
+ connection: Connection,
11
+ payer: Signer,
12
+ mintPublicKey: PublicKey,
13
+ depositAmount: number,
14
+ overrideStartTime?: number,
15
+ confirmOptions?: ConfirmOptions
16
+ ): Promise<PublicKey> {
17
+ const poolPosition = Keypair.generate()
18
+ const transaction = new Transaction().add(
19
+ await depositStakingPoolInstruction(
20
+ payer.publicKey,
21
+ poolPosition.publicKey,
22
+ mintPublicKey,
23
+ depositAmount,
24
+ overrideStartTime
25
+ )
26
+ )
27
+ await sendAndConfirmTransaction(connection, transaction, [payer, poolPosition], confirmOptions).catch(parseAnchorErrors)
28
+ return poolPosition.publicKey
29
+ }
30
+
31
+ export async function depositStakingPoolInstruction (
32
+ payerPublicKey: PublicKey,
33
+ poolPositionPublicKey: PublicKey,
34
+ stakedTokenMintPublicKey: PublicKey,
35
+ depositAmount: number,
36
+ overrideStartTime?: number
37
+ ): Promise<TransactionInstruction> {
38
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
39
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
40
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
41
+ const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
42
+
43
+ return program.instruction.depositStakingPool(
44
+ new BN(depositAmount),
45
+ new BN(overrideStartTime ?? Date.now() / 1000), // override current time
46
+ {
47
+ accounts: {
48
+ payer: payerPublicKey,
49
+ pool: poolPublickey,
50
+ poolPosition: poolPositionPublicKey,
51
+ stakedTokenMintInfo: stakedTokenMintPublicKey,
52
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
53
+ payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
54
+ rent: SYSVAR_RENT_PUBKEY,
55
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
56
+ splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
57
+ systemProgram: SystemProgram.programId
58
+ },
59
+ signers: []
60
+ })
61
+ }
@@ -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,67 @@
1
+ import { BN, Program } from '@project-serum/anchor'
2
+ import { 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, getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
+
6
+ import { vaultIdl } from '../idl/idl'
7
+
8
+ export async function liquidateVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ chainlinkOverridePrice?: number,
13
+ confirmOptions?: ConfirmOptions
14
+ ): Promise<PublicKey> {
15
+ const history = Keypair.generate()
16
+ const newEra = Keypair.generate()
17
+ const transaction = new Transaction().add(
18
+ await liquidateVaultInstruction(
19
+ payer.publicKey,
20
+ vaultPublicKey,
21
+ history.publicKey,
22
+ newEra.publicKey,
23
+ chainlinkOverridePrice
24
+ )
25
+ )
26
+ await sendAndConfirmTransaction(connection, transaction, [payer, history, newEra], confirmOptions)
27
+ return vaultPublicKey
28
+ }
29
+
30
+ export async function liquidateVaultInstruction (
31
+ payerPublicKey: PublicKey,
32
+ vaultPublickey: PublicKey,
33
+ historyPublicKey: PublicKey,
34
+ newEraPublicKey: PublicKey,
35
+ chainlinkOverridePrice?: number
36
+ ): Promise<TransactionInstruction> {
37
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
38
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
39
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
40
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
41
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
42
+ const [liquidationPoolStatePublicKey] = await getLiquidationPoolStatePublicKey()
43
+ const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey()
44
+
45
+ const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
46
+
47
+ return program.instruction.liquidateVault(
48
+ new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
49
+ {
50
+ accounts: {
51
+ vaultSystemState: vaultSystemStatePublicKey,
52
+ poolEra: poolStateInfo.currentEra,
53
+ vaultAccount: vaultPublickey,
54
+ poolState: liquidationPoolStatePublicKey,
55
+ usdhMint: usdhMintPublickey,
56
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
57
+ history: historyPublicKey,
58
+ payer: payerPublicKey,
59
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
60
+ systemProgram: SystemProgram.programId,
61
+ feePool: hedgeStakingPoolPublicKey,
62
+ poolUsdhAccount: liquidationPoolUsdhAccountPublickey,
63
+ newEra: newEraPublicKey
64
+ },
65
+ signers: []
66
+ })
67
+ }
@@ -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,70 @@
1
+ import { BN, Program } from '@project-serum/anchor'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { ConfirmOptions, Connection, 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, HEDGE_PROGRAM_ID } from '../Constants'
6
+
7
+ import { vaultIdl } from '../idl/idl'
8
+
9
+ export async function withdrawStakingPool (
10
+ connection: Connection,
11
+ payer: Signer,
12
+ poolPositionPublicKey: PublicKey,
13
+ stakedTokenMintPublicKey: PublicKey,
14
+ overrideStartTime?: number,
15
+ confirmOptions?: ConfirmOptions
16
+ ): Promise<PublicKey> {
17
+ const poolPosition = Keypair.generate()
18
+ const transaction = new Transaction().add(
19
+ await withdrawStakingPoolInstruction(
20
+ payer.publicKey,
21
+ poolPositionPublicKey,
22
+ stakedTokenMintPublicKey,
23
+ overrideStartTime
24
+ )
25
+ )
26
+ await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions).catch(parseAnchorErrors)
27
+ return poolPosition.publicKey
28
+ }
29
+
30
+ export async function withdrawStakingPoolInstruction (
31
+ payerPublicKey: PublicKey,
32
+ poolPositionPublicKey: PublicKey,
33
+ stakedTokenMintPublicKey: PublicKey,
34
+ overrideStartTime?: number
35
+ ): Promise<TransactionInstruction> {
36
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
37
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
38
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
39
+ const [hedgeMintPublickey] = await getHedgeMintPublicKey()
40
+ const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
41
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
42
+ const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey)
43
+ const payerAssociatedStakedTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
44
+ const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
45
+ const payerAssociatedUsdhAccount = await findAssociatedTokenAddress(payerPublicKey, usdhMintPublickey)
46
+
47
+ return program.instruction.withdrawStakingPool(
48
+ new BN(overrideStartTime ?? 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
+ poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
57
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
58
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
59
+ payerAssociatedUsdhAccount: payerAssociatedUsdhAccount,
60
+ hedgeMint: hedgeMintPublickey,
61
+ stakedTokenMint: stakedTokenMintPublicKey,
62
+ usdhMint: usdhMintPublickey,
63
+ rent: SYSVAR_RENT_PUBKEY,
64
+ splTokenProgramInfo: TOKEN_PROGRAM_ID,
65
+ splAssociatedTokenProgramInfo: ASSOCIATED_TOKEN_PROGRAM_ID,
66
+ systemProgram: SystemProgram.programId
67
+ },
68
+ signers: []
69
+ })
70
+ }