hedge-web3 0.1.14 → 0.1.15

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 (117) hide show
  1. package/{lib/types/src → declarations}/Constants.d.ts +1 -2
  2. package/{lib/types/src → declarations}/HedgeDecimal.d.ts +0 -1
  3. package/{lib/types/src → declarations}/StakingPools.d.ts +0 -1
  4. package/{lib/types/src → declarations}/Vaults.d.ts +0 -1
  5. package/{lib/types/src → declarations}/idl/idl.d.ts +0 -1
  6. package/declarations/idl/vault.d.ts +2283 -0
  7. package/{lib/types/src → declarations}/index.d.ts +2 -1
  8. package/{lib/types/src → declarations}/instructions/claimLiquidationPoolPosition.d.ts +1 -2
  9. package/{lib/types/src → declarations}/instructions/closeLiquidationPoolPosition.d.ts +0 -1
  10. package/{lib/types/src → declarations}/instructions/createStakingPool.d.ts +0 -1
  11. package/{lib/types/src → declarations}/instructions/createVault.d.ts +2 -3
  12. package/{lib/types/src → declarations}/instructions/depositLiquidationPool.d.ts +0 -1
  13. package/{lib/types/src → declarations}/instructions/depositStakingPool.d.ts +0 -1
  14. package/{lib/types/src → declarations}/instructions/depositVault.d.ts +2 -3
  15. package/declarations/instructions/initHedgeFoundation.d.ts +4 -0
  16. package/{lib/types/src → declarations}/instructions/liquidateVault.d.ts +2 -3
  17. package/{lib/types/src → declarations}/instructions/loanVault.d.ts +2 -3
  18. package/{lib/types/src → declarations}/instructions/redeemVault.d.ts +1 -2
  19. package/{lib/types/src → declarations}/instructions/refreshOraclePrice.d.ts +0 -1
  20. package/{lib/types/src → declarations}/instructions/repayVault.d.ts +2 -3
  21. package/declarations/instructions/setHalted.d.ts +4 -0
  22. package/{lib/types/src → declarations}/instructions/withdrawStakingPool.d.ts +0 -1
  23. package/{lib/types/src → declarations}/instructions/withdrawVault.d.ts +2 -3
  24. package/{lib/types/src → declarations}/state/LiquidationPoolEra.d.ts +0 -1
  25. package/{lib/types/src → declarations}/state/LiquidationPoolState.d.ts +0 -1
  26. package/{lib/types/src → declarations}/state/LiquidationPosition.d.ts +1 -2
  27. package/{lib/types/src → declarations}/state/StakingPool.d.ts +0 -1
  28. package/{lib/types/src → declarations}/state/StakingPoolPosition.d.ts +0 -1
  29. package/{lib/types/src → declarations}/state/VaultAccount.d.ts +0 -1
  30. package/{lib/types/src → declarations}/state/VaultHistoryEvent.d.ts +0 -1
  31. package/{lib/types/src → declarations}/utils/Errors.d.ts +0 -1
  32. package/lib/Constants.js +86 -0
  33. package/lib/HedgeDecimal.js +24 -0
  34. package/lib/StakingPools.js +15 -0
  35. package/lib/Vaults.js +20 -0
  36. package/lib/idl/idl.js +1475 -0
  37. package/lib/idl/vault.js +2285 -0
  38. package/lib/index.js +36 -2382
  39. package/lib/instructions/claimLiquidationPoolPosition.js +53 -0
  40. package/lib/instructions/closeLiquidationPoolPosition.js +60 -0
  41. package/lib/instructions/createStakingPool.js +52 -0
  42. package/lib/instructions/createVault.js +92 -0
  43. package/lib/instructions/depositLiquidationPool.js +55 -0
  44. package/lib/instructions/depositStakingPool.js +52 -0
  45. package/lib/instructions/depositVault.js +87 -0
  46. package/lib/instructions/initHedgeFoundation.js +55 -0
  47. package/lib/instructions/liquidateVault.js +80 -0
  48. package/lib/instructions/loanVault.js +61 -0
  49. package/lib/instructions/redeemVault.js +65 -0
  50. package/lib/instructions/refreshOraclePrice.js +65 -0
  51. package/lib/instructions/repayVault.js +62 -0
  52. package/lib/instructions/setHalted.js +37 -0
  53. package/lib/instructions/withdrawStakingPool.js +64 -0
  54. package/lib/instructions/withdrawVault.js +62 -0
  55. package/lib/state/LiquidationPoolEra.js +19 -0
  56. package/lib/state/LiquidationPoolState.js +12 -0
  57. package/lib/state/LiquidationPosition.js +39 -0
  58. package/lib/state/StakingPool.js +22 -0
  59. package/lib/state/StakingPoolPosition.js +28 -0
  60. package/lib/state/VaultAccount.js +52 -0
  61. package/lib/state/VaultHistoryEvent.js +45 -0
  62. package/lib/utils/Errors.js +14 -0
  63. package/package.json +8 -6
  64. package/src/Constants.ts +4 -3
  65. package/src/HedgeDecimal.ts +1 -1
  66. package/src/idl/vault.ts +4565 -0
  67. package/src/index.ts +2 -0
  68. package/src/instructions/claimLiquidationPoolPosition.ts +19 -11
  69. package/src/instructions/closeLiquidationPoolPosition.ts +7 -13
  70. package/src/instructions/createVault.ts +39 -29
  71. package/src/instructions/depositLiquidationPool.ts +6 -9
  72. package/src/instructions/depositStakingPool.ts +3 -1
  73. package/src/instructions/depositVault.ts +41 -29
  74. package/src/instructions/initHedgeFoundation.ts +64 -0
  75. package/src/instructions/liquidateVault.ts +30 -33
  76. package/src/instructions/loanVault.ts +22 -24
  77. package/src/instructions/redeemVault.ts +15 -28
  78. package/src/instructions/refreshOraclePrice.ts +2 -2
  79. package/src/instructions/repayVault.ts +20 -22
  80. package/src/instructions/setHalted.ts +48 -0
  81. package/src/instructions/withdrawVault.ts +36 -26
  82. package/src/state/LiquidationPosition.ts +2 -2
  83. package/tsconfig.json +3 -2
  84. package/.github/workflows/npm-publish.yml +0 -34
  85. package/README.md +0 -44
  86. package/jsconfig.json +0 -12
  87. package/lib/index.js.map +0 -1
  88. package/lib/types/src/Constants.d.ts.map +0 -1
  89. package/lib/types/src/HedgeDecimal.d.ts.map +0 -1
  90. package/lib/types/src/StakingPools.d.ts.map +0 -1
  91. package/lib/types/src/Vaults.d.ts.map +0 -1
  92. package/lib/types/src/idl/idl.d.ts.map +0 -1
  93. package/lib/types/src/index.d.ts.map +0 -1
  94. package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +0 -1
  95. package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +0 -1
  96. package/lib/types/src/instructions/createStakingPool.d.ts.map +0 -1
  97. package/lib/types/src/instructions/createVault.d.ts.map +0 -1
  98. package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +0 -1
  99. package/lib/types/src/instructions/depositStakingPool.d.ts.map +0 -1
  100. package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
  101. package/lib/types/src/instructions/liquidateVault.d.ts.map +0 -1
  102. package/lib/types/src/instructions/loanVault.d.ts.map +0 -1
  103. package/lib/types/src/instructions/redeemVault.d.ts.map +0 -1
  104. package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +0 -1
  105. package/lib/types/src/instructions/repayVault.d.ts.map +0 -1
  106. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +0 -1
  107. package/lib/types/src/instructions/withdrawVault.d.ts.map +0 -1
  108. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +0 -1
  109. package/lib/types/src/state/LiquidationPoolState.d.ts.map +0 -1
  110. package/lib/types/src/state/LiquidationPosition.d.ts.map +0 -1
  111. package/lib/types/src/state/StakingPool.d.ts.map +0 -1
  112. package/lib/types/src/state/StakingPoolPosition.d.ts.map +0 -1
  113. package/lib/types/src/state/VaultAccount.d.ts.map +0 -1
  114. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +0 -1
  115. package/lib/types/src/utils/Errors.d.ts.map +0 -1
  116. package/lib/types/tsconfig.base.tsbuildinfo +0 -1
  117. package/rollup.config.js +0 -42
package/src/index.ts CHANGED
@@ -13,6 +13,8 @@ export * from './instructions/repayVault'
13
13
  export * from './instructions/redeemVault'
14
14
  export * from './instructions/liquidateVault'
15
15
  export * from './instructions/refreshOraclePrice'
16
+ export * from './instructions/initHedgeFoundation'
17
+ export * from './instructions/setHalted'
16
18
 
17
19
  export * from './HedgeDecimal'
18
20
  export * from './Constants'
@@ -1,8 +1,8 @@
1
1
  import { Program, Provider } from '@project-serum/anchor'
2
- import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
4
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { findAssociatedTokenAddress, getCollateralStateAccountPublicKey, getLiquidationPoolStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getLiquidationPoolStatePublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
6
 
7
7
  export async function claimLiquidationPoolPosition (
8
8
  program: Program,
@@ -12,48 +12,56 @@ export async function claimLiquidationPoolPosition (
12
12
  collateralType: string,
13
13
  overrideStartTime?: number
14
14
  ): Promise<PublicKey> {
15
- const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(collateralType)
16
- const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
17
- const collateralMint = collateralStateAccountInfo.collateralMint
15
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
16
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
17
+ const collateralMint = vaultTypeAccountInfo.collateralMint
18
18
 
19
19
  const poolStatePublicKey = await getLiquidationPoolStatePublicKey()
20
20
  const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint)
21
- const payerAssociatedTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralMint)
21
+ const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
22
+ provider.connection,
23
+ payer,
24
+ collateralMint,
25
+ payer.publicKey
26
+ )
22
27
 
23
28
  const transaction = new Transaction().add(
24
29
  await claimLiquidationPoolPositionInstruction(
25
30
  program,
26
31
  poolStatePublicKey,
27
32
  poolAssociatedTokenAccount,
28
- collateralStateAccountPublicKey,
33
+ vaultTypeAccountPublicKey,
29
34
  collateralMint,
30
35
  poolPosition,
31
36
  payer.publicKey,
32
- payerAssociatedTokenAccount,
37
+ payerAssociatedTokenAccount.address,
33
38
  overrideStartTime
34
39
  )
35
40
  )
36
41
  await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
37
- return payerAssociatedTokenAccount
42
+ return payerAssociatedTokenAccount.address
38
43
  }
39
44
 
40
45
  export async function claimLiquidationPoolPositionInstruction (
41
46
  program: Program,
42
47
  poolState: PublicKey,
43
48
  poolAssociatedTokenAccount: PublicKey,
44
- collateralStateAccount: PublicKey,
49
+ vaultTypeAccount: PublicKey,
45
50
  collateralMint: PublicKey,
46
51
  poolPosition: PublicKey,
47
52
  payer: PublicKey,
48
53
  payerAssociatedTokenAccount: PublicKey,
49
54
  overrideStartTime?: number
50
55
  ): Promise<TransactionInstruction> {
56
+ const vaultSystemState = await getVaultSystemStatePublicKey()
57
+
51
58
  return program.instruction.claimLiquidationPoolPosition(
52
59
  {
53
60
  accounts: {
61
+ vaultSystemState: vaultSystemState,
54
62
  poolState: poolState,
55
63
  poolAssociatedTokenAccount: poolAssociatedTokenAccount,
56
- collateralStateAccount: collateralStateAccount,
64
+ vaultTypeAccount: vaultTypeAccount,
57
65
  collateralMint: collateralMint,
58
66
  poolPosition: poolPosition,
59
67
  payer: payer,
@@ -1,8 +1,8 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
4
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
6
 
7
7
  export async function closeLiquidationPoolPosition (
8
8
  program: Program,
@@ -12,13 +12,7 @@ export async function closeLiquidationPoolPosition (
12
12
  overrideStartTime?: number
13
13
  ): Promise<PublicKey> {
14
14
  const usdhMintPublickey = await getUsdhMintPublicKey()
15
- const USDH = new Token(
16
- provider.connection,
17
- usdhMintPublickey,
18
- TOKEN_PROGRAM_ID,
19
- payer
20
- )
21
- const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
15
+ const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
22
16
 
23
17
  const poolState = await getLiquidationPoolStatePublicKey()
24
18
  const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
@@ -27,8 +21,8 @@ export async function closeLiquidationPoolPosition (
27
21
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
28
22
  const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
29
23
  const hedgeMint = await getHedgeMintPublicKey()
30
- const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payer.publicKey, hedgeMint)
31
- const communityAssociatedHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMint)
24
+ const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, payer.publicKey)
25
+ const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true)
32
26
 
33
27
  const transaction = new Transaction().add(
34
28
  await closeLiquidationPoolPositionInstruction(
@@ -41,8 +35,8 @@ export async function closeLiquidationPoolPosition (
41
35
  payer.publicKey,
42
36
  payerUsdhAccount.address,
43
37
  hedgeMint,
44
- payerAssociatedHedgeAccount,
45
- communityAssociatedHedgeTokenAccount,
38
+ payerAssociatedHedgeAccount.address,
39
+ communityAssociatedHedgeTokenAccount.address,
46
40
  overrideStartTime
47
41
  )
48
42
  )
@@ -1,8 +1,8 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { TokenInstructions } from '@project-serum/serum'
3
- import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
4
4
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, findVaultAddress, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
6
6
 
7
7
  import { v4 as uuidv4 } from 'uuid'
8
8
  import { parseAnchorErrors } from '../utils/Errors'
@@ -13,29 +13,22 @@ export async function createVault (
13
13
  payer: Signer,
14
14
  collateralType: string,
15
15
  depositAmount: number,
16
- collateralRatio: number
16
+ overrideTime?: number
17
17
  ): Promise<PublicKey> {
18
18
  const usdhMintPublickey = await getUsdhMintPublicKey()
19
- const USDH = new Token(
20
- provider.connection,
21
- usdhMintPublickey,
22
- TOKEN_PROGRAM_ID,
23
- payer
24
- )
25
19
 
26
20
  const salt = uuidv4().substring(0, 8)
27
21
  const newVaultPublicKey = await findVaultAddress(salt)
28
22
  const history = Keypair.generate()
29
23
 
30
24
  // Prep the user to get USDH back out at some point
31
- await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
32
-
33
- const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(collateralType)
34
- const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
25
+ await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
35
26
 
36
- const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint)
37
- const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(newVaultPublicKey, collateralStateAccountInfo.collateralMint)
27
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
28
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
38
29
 
30
+ const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
31
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(newVaultPublicKey, vaultTypeAccountInfo.collateralMint)
39
32
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
40
33
 
41
34
  const wrappedSolAccount = Keypair.generate()
@@ -43,7 +36,15 @@ export async function createVault (
43
36
  const transaction = new Transaction()
44
37
  const signers = [payer, history]
45
38
 
46
- if (collateralType === 'SOL') {
39
+ const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === TokenInstructions.WRAPPED_SOL_MINT.toString()
40
+
41
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
42
+ const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
43
+ hedgeStakingPoolPublicKey,
44
+ usdhMintPublickey
45
+ )
46
+
47
+ if (isWrappedSol) {
47
48
  transaction.add(
48
49
  SystemProgram.createAccount({
49
50
  fromPubkey: payer.publicKey,
@@ -66,17 +67,20 @@ export async function createVault (
66
67
  salt,
67
68
  vaultSystemStatePublicKey,
68
69
  payer.publicKey,
69
- collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount,
70
+ isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
70
71
  newVaultPublicKey,
71
- vaultAssociatedCollateralAccountPublicKey,
72
- collateralStateAccountPublicKey,
73
- collateralStateAccountInfo.collateralMint,
72
+ vaultAssociatedTokenAccount,
73
+ hedgeStakingPoolPublicKey,
74
+ feePoolAssociatedUsdhTokenAccount,
75
+ vaultTypeAccountPublicKey,
76
+ vaultTypeAccountInfo.collateralMint,
74
77
  history.publicKey,
78
+ usdhMintPublickey,
75
79
  depositAmount,
76
- collateralRatio
80
+ overrideTime
77
81
  )
78
82
  )
79
- if (collateralType === 'SOL') {
83
+ if (isWrappedSol) {
80
84
  transaction.add(
81
85
  TokenInstructions.closeAccount({
82
86
  source: wrappedSolAccount.publicKey,
@@ -97,27 +101,33 @@ export async function createVaultInstruction (
97
101
  payerPublicKey: PublicKey,
98
102
  payerTokenAccountPublicKey: PublicKey,
99
103
  vaultPublicKey: PublicKey,
100
- vaultAssociatedCollateralPublicKey: PublicKey,
101
- collateralStateAccount: PublicKey,
104
+ vaultAssociatedTokenAccount: PublicKey,
105
+ feePool: PublicKey,
106
+ feePoolAssociatedUsdhTokenAccount: PublicKey,
107
+ vaultTypeAccount: PublicKey,
102
108
  collateralMint: PublicKey,
103
109
  historyPublicKey: PublicKey,
110
+ usdhMintPublickey: PublicKey,
104
111
  depositAmount: number,
105
- collateralRatio: number
112
+ overrideTime?: number
106
113
  ): Promise<TransactionInstruction> {
107
114
  const ix = program.instruction.createVault(
108
115
  salt,
109
116
  new BN(depositAmount),
110
- new BN(collateralRatio)
111
- , {
117
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
118
+ {
112
119
  accounts: {
113
120
  vaultSystemState: vaultSystemStatePublicKey,
114
- collateralStateAccount: collateralStateAccount,
121
+ vaultTypeAccount: vaultTypeAccount,
115
122
  vault: vaultPublicKey,
116
- vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
123
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
124
+ feePool: feePool,
125
+ feePoolAssociatedUsdhTokenAccount: feePoolAssociatedUsdhTokenAccount,
117
126
  history: historyPublicKey,
118
127
  payer: payerPublicKey,
119
128
  payerTokenAccount: payerTokenAccountPublicKey,
120
129
  collateralMint: collateralMint,
130
+ usdhMint: usdhMintPublickey,
121
131
  systemProgram: SystemProgram.programId,
122
132
  tokenProgram: TOKEN_PROGRAM_ID,
123
133
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -1,8 +1,8 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
4
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey } from '../Constants'
5
+ import { getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
6
 
7
7
  export async function depositLiquidationPool (
8
8
  program: Program,
@@ -12,13 +12,7 @@ export async function depositLiquidationPool (
12
12
  overrideStartTime?: number
13
13
  ): Promise<PublicKey> {
14
14
  const usdhMintPublickey = await getUsdhMintPublicKey()
15
- const USDH = new Token(
16
- provider.connection,
17
- usdhMintPublickey,
18
- TOKEN_PROGRAM_ID,
19
- payer
20
- )
21
- const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
15
+ const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
22
16
 
23
17
  const poolPosition = Keypair.generate()
24
18
  const transaction = new Transaction().add(
@@ -49,11 +43,14 @@ export async function depositLiquidationPoolInstruction (
49
43
  const poolUSDHAccount = await getLiquidationPoolUsdhAccountPublicKey()
50
44
  const usdhMint = await getUsdhMintPublicKey()
51
45
 
46
+ const vaultSystemState = await getVaultSystemStatePublicKey()
47
+
52
48
  return program.instruction.depositLiquidationPool(
53
49
  new BN(depositAmount),
54
50
  new BN(overrideStartTime ?? Date.now() / 1000), // override current time
55
51
  {
56
52
  accounts: {
53
+ vaultSystemState: vaultSystemState,
57
54
  poolState: liquidationPoolStatePublicKey,
58
55
  poolEra: liquidationPoolState.currentEra,
59
56
  poolPosition: poolPositionPublicKey,
@@ -2,7 +2,7 @@ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
4
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { findAssociatedTokenAddress, getPoolPublicKeyForMint } from '../Constants'
5
+ import { findAssociatedTokenAddress, getPoolPublicKeyForMint, getVaultSystemStatePublicKey } from '../Constants'
6
6
 
7
7
  export async function depositStakingPool (
8
8
  program: Program,
@@ -38,6 +38,7 @@ export async function depositStakingPoolInstruction (
38
38
  const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
39
39
  const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
40
40
  const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
41
+ const vaultSystemState = await getVaultSystemStatePublicKey()
41
42
 
42
43
  return program.instruction.depositStakingPool(
43
44
  new BN(depositAmount),
@@ -45,6 +46,7 @@ export async function depositStakingPoolInstruction (
45
46
  {
46
47
  accounts: {
47
48
  payer: payerPublicKey,
49
+ vaultSystemState: vaultSystemState,
48
50
  pool: poolPublickey,
49
51
  poolPosition: poolPositionPublicKey,
50
52
  stakedTokenMintInfo: stakedTokenMintPublicKey,
@@ -1,41 +1,41 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { TokenInstructions } from '@project-serum/serum'
3
- import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
4
4
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
6
6
 
7
- export async function depositVault (
7
+ export async function depositVault(
8
8
  program: Program,
9
9
  provider: Provider,
10
10
  payer: Signer,
11
11
  vaultPublicKey: PublicKey,
12
- // collateralType: string,
13
- depositAmount: number
12
+ depositAmount: number,
13
+ overrideTime?: number
14
14
  ): Promise<PublicKey> {
15
15
  const usdhMintPublickey = await getUsdhMintPublicKey()
16
- const USDH = new Token(
17
- provider.connection,
18
- usdhMintPublickey,
19
- TOKEN_PROGRAM_ID,
20
- payer
21
- )
22
16
 
23
17
  // Prep the user to get USDH back out at some point
24
- await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
18
+ await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
25
19
 
26
20
  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)
21
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
22
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
23
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true)
30
24
 
31
- const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint)
32
- const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint)
25
+ const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
26
+ const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccountInfo.collateralMint)
33
27
 
34
28
  const history = Keypair.generate()
35
29
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
36
30
 
37
31
  const wrappedSolAccount = Keypair.generate()
38
32
 
33
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
34
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
35
+ hedgeStakingPoolPublicKey,
36
+ usdhMintPublickey
37
+ )
38
+
39
39
  const transaction = new Transaction()
40
40
  const signers = [payer, history]
41
41
 
@@ -65,10 +65,14 @@ export async function depositVault (
65
65
  vaultPublicKey,
66
66
  vaultAssociatedCollateralAccountPublicKey,
67
67
  history.publicKey,
68
- collateralStateAccountPublicKey,
69
- collateralAssociatedTokenAccount,
70
- collateralStateAccountInfo.collateralMint,
71
- depositAmount
68
+ vaultTypeAccountPublicKey,
69
+ vaultTypeAssociatedTokenAccount.address,
70
+ hedgeStakingPoolPublicKey,
71
+ hedgeStakingPoolAssociatedUsdhTokenAccount,
72
+ vaultTypeAccountInfo.collateralMint,
73
+ usdhMintPublickey,
74
+ depositAmount,
75
+ overrideTime
72
76
  )
73
77
  )
74
78
  if (vaultAccount.collateralType === 'SOL') {
@@ -85,7 +89,7 @@ export async function depositVault (
85
89
  return vaultPublicKey
86
90
  }
87
91
 
88
- export async function depositVaultInstruction (
92
+ export async function depositVaultInstruction(
89
93
  program: Program,
90
94
  vaultSystemStatePublicKey: PublicKey,
91
95
  vaultOwner: PublicKey,
@@ -93,24 +97,32 @@ export async function depositVaultInstruction (
93
97
  vaultPublicKey: PublicKey,
94
98
  vaultAssociatedTokenAccount: PublicKey,
95
99
  historyPublicKey: PublicKey,
96
- collateralStateAccountPublicKey: PublicKey,
97
- collateralAssociatedTokenAccount: PublicKey,
100
+ vaultTypeAccountPublicKey: PublicKey,
101
+ vaultTypeAssociatedTokenAccount: PublicKey,
102
+ hedgeStakingPoolPublicKey: PublicKey,
103
+ hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
98
104
  collateralMint: PublicKey,
99
- depositAmount: number
105
+ usdhMintPublickey: PublicKey,
106
+ depositAmount: number,
107
+ overrideTime?: number
100
108
  ): Promise<TransactionInstruction> {
101
109
  return program.instruction.depositVault(
102
- new BN(depositAmount)
103
- , {
110
+ new BN(depositAmount),
111
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
112
+ {
104
113
  accounts: {
105
114
  vaultSystemState: vaultSystemStatePublicKey,
106
- collateralStateAccount: collateralStateAccountPublicKey,
107
- collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
115
+ vaultTypeAccount: vaultTypeAccountPublicKey,
116
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
108
117
  collateralTokenMint: collateralMint,
109
118
  vault: vaultPublicKey,
110
119
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
120
+ feePool: hedgeStakingPoolPublicKey,
121
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
111
122
  history: historyPublicKey,
112
123
  vaultOwner: vaultOwner,
113
124
  vaultOwnerTokenAccount: vaultOwnerTokenAccount,
125
+ usdhMint: usdhMintPublickey,
114
126
  systemProgram: SystemProgram.programId,
115
127
  tokenProgram: TOKEN_PROGRAM_ID
116
128
  },
@@ -0,0 +1,64 @@
1
+ import { 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, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
+
7
+ export async function initHedgeFoundation(
8
+ program: Program,
9
+ provider: Provider,
10
+ payer: Signer
11
+ ): Promise<PublicKey> {
12
+
13
+ const poolEra = Keypair.generate()
14
+ const transaction = new Transaction().add(
15
+ await initHedgeFoundationInstruction(
16
+ program,
17
+ poolEra.publicKey,
18
+ payer.publicKey,
19
+ )
20
+ )
21
+
22
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolEra], provider.opts).catch(parseAnchorErrors)
23
+ return payer.publicKey
24
+ }
25
+
26
+ export async function initHedgeFoundationInstruction (
27
+ program: Program,
28
+ poolEraPublicKey: PublicKey,
29
+ payerPublicKey: PublicKey,
30
+ ): Promise<TransactionInstruction> {
31
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
32
+ const poolStatePublickey = await getLiquidationPoolStatePublicKey()
33
+ const poolUsdhTokenAccount = await getLiquidationPoolUsdhAccountPublicKey()
34
+ const usdhMintPublickey = await getUsdhMintPublicKey()
35
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
36
+ const founderHedgeTokenAccount = await findAssociatedTokenAddress(
37
+ payerPublicKey,
38
+ hedgeMintPublickey
39
+ )
40
+ const communityHedgeTokenAccount = await findAssociatedTokenAddress(
41
+ vaultSystemStatePublicKey,
42
+ hedgeMintPublickey
43
+ )
44
+
45
+ return program.instruction.initHedgeFoundation(
46
+ {
47
+ accounts: {
48
+ vaultSystemState: vaultSystemStatePublicKey,
49
+ poolState: poolStatePublickey,
50
+ poolEra: poolEraPublicKey,
51
+ poolUsdhAccount: poolUsdhTokenAccount,
52
+ founder: payerPublicKey,
53
+ usdhMint: usdhMintPublickey,
54
+ hedgeMint: hedgeMintPublickey,
55
+ founderAssociatedHedgeTokenAccount: founderHedgeTokenAccount,
56
+ communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
57
+ rent: SYSVAR_RENT_PUBKEY,
58
+ tokenProgram: TOKEN_PROGRAM_ID,
59
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
60
+ systemProgram: SystemProgram.programId
61
+ },
62
+ signers: []
63
+ })
64
+ }
@@ -1,38 +1,33 @@
1
- import { Program, Provider } from '@project-serum/anchor'
2
- import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
3
3
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, findAssociatedTokenAddress } from '../Constants'
4
+ import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
5
 
6
6
  export async function liquidateVault (
7
7
  program: Program,
8
8
  provider: Provider,
9
9
  payer: Signer,
10
10
  vaultPublicKey: PublicKey,
11
- preload: boolean
11
+ overrideTime?: number
12
12
  ): Promise<PublicKey> {
13
13
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
14
14
 
15
- const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
16
- const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
17
- const collateralMint = collateralStateAccountInfo.collateralMint
18
-
19
- const token = new Token(
20
- provider.connection,
21
- collateralMint,
22
- TOKEN_PROGRAM_ID,
23
- payer
24
- )
15
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
16
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
17
+ const collateralMint = vaultTypeAccountInfo.collateralMint
25
18
 
26
19
  const hedgeMintPublickey = await getHedgeMintPublicKey()
20
+ const usdhMintPublickey = await getUsdhMintPublicKey()
27
21
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
28
22
  const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
29
23
  const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
30
24
 
31
- const payerAssociatedTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey)
32
- const feePoolAssociatedTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, collateralMint)
33
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralMint)
34
- const poolAssociatedTokenAccount = await findAssociatedTokenAddress(liquidationPoolStatePublicKey, collateralMint)
35
- const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralMint)
25
+ const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, payer.publicKey, true)
26
+ const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, hedgeStakingPoolPublicKey, true)
27
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, vaultPublicKey, true)
28
+ const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, liquidationPoolStatePublicKey, true)
29
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, vaultTypeAccountPublicKey, true)
30
+ const hedgeStakingPoolAssociatedUsdhTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, hedgeStakingPoolPublicKey, true)
36
31
 
37
32
  const history = Keypair.generate()
38
33
  const newEra = Keypair.generate()
@@ -44,18 +39,19 @@ export async function liquidateVault (
44
39
  payer.publicKey,
45
40
  payerAssociatedTokenAccount.address,
46
41
  vaultPublicKey,
47
- vaultAssociatedTokenAccount,
42
+ vaultAssociatedTokenAccount.address,
48
43
  liquidationPoolStatePublicKey,
49
44
  poolStateInfo.currentEra,
50
- poolAssociatedTokenAccount,
45
+ poolAssociatedTokenAccount.address,
51
46
  history.publicKey,
52
47
  newEra.publicKey,
53
48
  hedgeStakingPoolPublicKey,
54
- feePoolAssociatedTokenAccount,
49
+ feePoolAssociatedTokenAccount.address,
50
+ hedgeStakingPoolAssociatedUsdhTokenAccount.address,
55
51
  collateralMint,
56
- collateralAssociatedTokenAccount,
52
+ vaultTypeAssociatedTokenAccount.address,
57
53
  vaultAccount.collateralType,
58
- preload
54
+ overrideTime
59
55
  )
60
56
  )
61
57
  await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts)
@@ -75,21 +71,22 @@ export async function liquidateVaultInstruction (
75
71
  newEraPublicKey: PublicKey,
76
72
  feePool: PublicKey,
77
73
  feePoolAssociatedTokenAccount: PublicKey,
74
+ hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
78
75
  collateralMint: PublicKey,
79
- collateralAssociatedTokenAccount: PublicKey,
76
+ vaultTypeAssociatedTokenAccount: PublicKey,
80
77
  collateralType: string,
81
- preload: boolean
78
+ overrideTime?: number
82
79
  ): Promise<TransactionInstruction> {
83
80
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
84
81
  const usdhMintPublickey = await getUsdhMintPublicKey()
85
82
  const liquidationPoolUsdhAccountPublickey = await getLiquidationPoolUsdhAccountPublicKey()
86
- const collateralStateAccount = await getCollateralStateAccountPublicKey(collateralType)
83
+ const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
87
84
 
88
85
  const payload = {
89
86
  accounts: {
90
87
  vaultSystemState: vaultSystemStatePublicKey,
91
- collateralStateAccount: collateralStateAccount,
92
- collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
88
+ vaultTypeAccount: vaultTypeAccount,
89
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
93
90
  collateralMint: collateralMint,
94
91
  poolEra: poolEra,
95
92
  vaultAccount: vaultPublickey,
@@ -102,6 +99,7 @@ export async function liquidateVaultInstruction (
102
99
  payerAssociatedTokenAccount: payerAssociatedTokenAccount,
103
100
  feePool: feePool,
104
101
  feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
102
+ feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
105
103
  liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
106
104
  newEra: newEraPublicKey,
107
105
  tokenProgram: TOKEN_PROGRAM_ID,
@@ -111,8 +109,7 @@ export async function liquidateVaultInstruction (
111
109
  },
112
110
  signers: []
113
111
  }
114
- if (preload) {
115
- return program.instruction.liquidateVaultPrepAccounts(payload)
116
- }
117
- return program.instruction.liquidateVault(payload)
112
+ return program.instruction.liquidateVault(
113
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
114
+ payload)
118
115
  }