hedge-web3 0.1.14 → 0.1.17

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 (119) hide show
  1. package/{lib/types/src → declarations}/Constants.d.ts +2 -3
  2. package/{lib/types/src → declarations}/HedgeDecimal.d.ts +2 -3
  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/declarations/instructions/createVault.d.ts +5 -0
  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 +7 -5
  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 +152 -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 +55 -0
  61. package/lib/state/VaultHistoryEvent.js +45 -0
  62. package/lib/utils/Errors.js +14 -0
  63. package/package.json +9 -7
  64. package/src/Constants.ts +5 -4
  65. package/src/HedgeDecimal.ts +3 -3
  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 +224 -57
  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/src/state/VaultAccount.ts +20 -9
  84. package/tsconfig.json +3 -2
  85. package/.github/workflows/npm-publish.yml +0 -34
  86. package/README.md +0 -44
  87. package/jsconfig.json +0 -12
  88. package/lib/index.js.map +0 -1
  89. package/lib/types/src/Constants.d.ts.map +0 -1
  90. package/lib/types/src/HedgeDecimal.d.ts.map +0 -1
  91. package/lib/types/src/StakingPools.d.ts.map +0 -1
  92. package/lib/types/src/Vaults.d.ts.map +0 -1
  93. package/lib/types/src/idl/idl.d.ts.map +0 -1
  94. package/lib/types/src/index.d.ts.map +0 -1
  95. package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +0 -1
  96. package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +0 -1
  97. package/lib/types/src/instructions/createStakingPool.d.ts.map +0 -1
  98. package/lib/types/src/instructions/createVault.d.ts +0 -5
  99. package/lib/types/src/instructions/createVault.d.ts.map +0 -1
  100. package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +0 -1
  101. package/lib/types/src/instructions/depositStakingPool.d.ts.map +0 -1
  102. package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
  103. package/lib/types/src/instructions/liquidateVault.d.ts.map +0 -1
  104. package/lib/types/src/instructions/loanVault.d.ts.map +0 -1
  105. package/lib/types/src/instructions/redeemVault.d.ts.map +0 -1
  106. package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +0 -1
  107. package/lib/types/src/instructions/repayVault.d.ts.map +0 -1
  108. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +0 -1
  109. package/lib/types/src/instructions/withdrawVault.d.ts.map +0 -1
  110. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +0 -1
  111. package/lib/types/src/state/LiquidationPoolState.d.ts.map +0 -1
  112. package/lib/types/src/state/LiquidationPosition.d.ts.map +0 -1
  113. package/lib/types/src/state/StakingPool.d.ts.map +0 -1
  114. package/lib/types/src/state/StakingPoolPosition.d.ts.map +0 -1
  115. package/lib/types/src/state/VaultAccount.d.ts.map +0 -1
  116. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +0 -1
  117. package/lib/types/src/utils/Errors.d.ts.map +0 -1
  118. package/lib/types/tsconfig.base.tsbuildinfo +0 -1
  119. package/rollup.config.js +0 -42
@@ -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
  }
@@ -1,30 +1,25 @@
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, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
4
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
5
 
6
6
  export async function loanVault (
7
7
  program: Program,
8
8
  provider: Provider,
9
9
  payer: Signer,
10
10
  vaultPublicKey: PublicKey,
11
- loanAmount: number
11
+ loanAmount: number,
12
+ overrideTime?: number
12
13
  ): Promise<PublicKey> {
13
14
  const usdhMintPublickey = await getUsdhMintPublicKey()
14
- const USDH = new Token(
15
- provider.connection,
16
- usdhMintPublickey,
17
- TOKEN_PROGRAM_ID,
18
- payer
19
- )
20
15
 
21
- // Prep the user to get USDH back out at some point
22
- const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
16
+ const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
17
+
23
18
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
24
- const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
25
- const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
26
- const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint)
27
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint)
19
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
20
+ const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
21
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccount.collateralMint, vaultTypeAccountPublicKey, true)
22
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true)
28
23
 
29
24
  const history = Keypair.generate()
30
25
  const transaction = new Transaction().add(
@@ -33,11 +28,12 @@ export async function loanVault (
33
28
  payer.publicKey,
34
29
  payerUsdhAccount.address,
35
30
  vaultPublicKey,
36
- vaultAssociatedTokenAccount,
31
+ vaultAssociatedTokenAccount.address,
37
32
  history.publicKey,
38
- collateralStateAccountPublicKey,
39
- collateralAssociatedTokenAccount,
40
- loanAmount
33
+ vaultTypeAccountPublicKey,
34
+ vaultTypeAssociatedTokenAccount.address,
35
+ loanAmount,
36
+ overrideTime
41
37
  )
42
38
  )
43
39
  await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
@@ -51,9 +47,10 @@ export async function loanVaultInstruction (
51
47
  vaultPublickey: PublicKey,
52
48
  vaultAssociatedTokenAccount: PublicKey,
53
49
  historyPublicKey: PublicKey,
54
- collateralStateAccount: PublicKey,
55
- collateralAssociatedTokenAccount: PublicKey,
56
- loanAmount: number
50
+ vaultTypeAccount: PublicKey,
51
+ vaultTypeAssociatedTokenAccount: PublicKey,
52
+ loanAmount: number,
53
+ overrideTime?: number
57
54
  ): Promise<TransactionInstruction> {
58
55
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
59
56
  const usdhMintPublickey = await getUsdhMintPublicKey()
@@ -66,11 +63,12 @@ export async function loanVaultInstruction (
66
63
 
67
64
  return program.instruction.loanVault(
68
65
  new BN(loanAmount),
66
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
69
67
  {
70
68
  accounts: {
71
69
  vaultSystemState: vaultSystemStatePublicKey,
72
- collateralStateAccount: collateralStateAccount,
73
- collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
70
+ vaultTypeAccount: vaultTypeAccount,
71
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
74
72
  vaultAccount: vaultPublickey,
75
73
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
76
74
  history: historyPublicKey,
@@ -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 { TokenInstructions } from '@project-serum/serum'
4
4
  import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
6
 
7
7
  export async function redeemVault (
8
8
  program: Program,
@@ -13,31 +13,18 @@ export async function redeemVault (
13
13
  transactionOverrideTime?: 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
- const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
18
+ const payerUsdhAccount = 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)
30
-
31
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, 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)
32
24
 
33
- const token = new Token(
34
- provider.connection,
35
- collateralStateAccountInfo.collateralMint,
36
- TOKEN_PROGRAM_ID,
37
- payer
38
- )
25
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true)
39
26
 
40
- const payerTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey)
27
+ const payerTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
41
28
 
42
29
  const history = Keypair.generate()
43
30
  const transaction = new Transaction().add(
@@ -47,10 +34,10 @@ export async function redeemVault (
47
34
  payerUsdhAccount.address,
48
35
  payerTokenAccount.address,
49
36
  vaultPublicKey,
50
- vaultAssociatedTokenAccount,
37
+ vaultAssociatedTokenAccount.address,
51
38
  history.publicKey,
52
- collateralStateAccountPublicKey,
53
- collateralAssociatedTokenAccount,
39
+ vaultTypeAccountPublicKey,
40
+ vaultTypeAssociatedTokenAccount.address,
54
41
  redeemAmount,
55
42
  transactionOverrideTime
56
43
  )
@@ -67,8 +54,8 @@ export async function redeemVaultInstruction (
67
54
  vaultPublickey: PublicKey,
68
55
  vaultAssociatedTokenAccount: PublicKey,
69
56
  historyPublicKey: PublicKey,
70
- collateralStateAccount: PublicKey,
71
- collateralAssociatedTokenAccount: PublicKey,
57
+ vaultTypeAccount: PublicKey,
58
+ vaultTypeAssociatedTokenAccount: PublicKey,
72
59
  redeemAmount: number,
73
60
  transactionOverrideTime?: number
74
61
  ): Promise<TransactionInstruction> {
@@ -86,8 +73,8 @@ export async function redeemVaultInstruction (
86
73
  {
87
74
  accounts: {
88
75
  vaultSystemState: vaultSystemStatePublicKey,
89
- collateralStateAccount: collateralStateAccount,
90
- collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
76
+ vaultTypeAccount: vaultTypeAccount,
77
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
91
78
  vault: vaultPublickey,
92
79
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
93
80
  history: historyPublicKey,
@@ -32,7 +32,7 @@ export async function refreshOraclePriceInstruction (
32
32
  ): Promise<TransactionInstruction> {
33
33
  const enc = new TextEncoder()
34
34
  const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
35
- const [collateralStateAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
35
+ const [vaultTypeAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
36
36
 
37
37
  return program.instruction.refreshOraclePrice(
38
38
  new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
@@ -40,7 +40,7 @@ export async function refreshOraclePriceInstruction (
40
40
  {
41
41
  accounts: {
42
42
  oracleInfoAccount: oracleInfoAccount,
43
- collateralStateAccount: collateralStateAccount,
43
+ vaultTypeAccount: vaultTypeAccount,
44
44
  oracleChainlink: chainlinkAccunts[network],
45
45
  oraclePyth: pythAccounts[network],
46
46
  oracleSwitchboard: switchboardAccounts[network],
@@ -1,31 +1,26 @@
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, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
4
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
5
 
6
6
  export async function repayVault (
7
7
  program: Program,
8
8
  provider: Provider,
9
9
  payer: Signer,
10
10
  vaultPublicKey: PublicKey,
11
- repayAmount: number
11
+ repayAmount: number,
12
+ overrideTime?: number
12
13
  ): Promise<PublicKey> {
13
14
  const usdhMintPublickey = await getUsdhMintPublicKey()
14
- const USDH = new Token(
15
- provider.connection,
16
- usdhMintPublickey,
17
- TOKEN_PROGRAM_ID,
18
- payer
19
- )
20
15
 
21
16
  // Prep the user to get USDH back out at some point
22
- const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
17
+ const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
23
18
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
24
19
 
25
- const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
26
- const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
27
- const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint)
28
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint)
20
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
21
+ const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
22
+ const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint)
23
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
29
24
 
30
25
  const history = Keypair.generate()
31
26
  const transaction = new Transaction().add(
@@ -36,9 +31,10 @@ export async function repayVault (
36
31
  vaultPublicKey,
37
32
  vaultAssociatedTokenAccount,
38
33
  history.publicKey,
39
- collateralStateAccountPublicKey,
40
- collateralAssociatedTokenAccount,
41
- repayAmount
34
+ vaultTypeAccountPublicKey,
35
+ vaultTypeAssociatedTokenAccount,
36
+ repayAmount,
37
+ overrideTime
42
38
  )
43
39
  )
44
40
  await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
@@ -52,9 +48,10 @@ export async function repayVaultInstruction (
52
48
  vaultPublickey: PublicKey,
53
49
  vaultAssociatedTokenAccount: PublicKey,
54
50
  historyPublicKey: PublicKey,
55
- collateralStateAccount: PublicKey,
56
- collateralAssociatedTokenAccount: PublicKey,
57
- repayAmount: number
51
+ vaultTypeAccount: PublicKey,
52
+ vaultTypeAssociatedTokenAccount: PublicKey,
53
+ repayAmount: number,
54
+ overrideTime?: number
58
55
  ): Promise<TransactionInstruction> {
59
56
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
60
57
  const usdhMintPublickey = await getUsdhMintPublicKey()
@@ -67,11 +64,12 @@ export async function repayVaultInstruction (
67
64
 
68
65
  return program.instruction.repayVault(
69
66
  new BN(repayAmount),
67
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
70
68
  {
71
69
  accounts: {
72
70
  vaultSystemState: vaultSystemStatePublicKey,
73
- collateralStateAccount: collateralStateAccount,
74
- collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
71
+ vaultTypeAccount: vaultTypeAccount,
72
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
75
73
  vaultAccount: vaultPublickey,
76
74
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
77
75
  history: historyPublicKey,
@@ -0,0 +1,48 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { TokenInstructions } from '@project-serum/serum'
3
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
4
+ import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
5
+ import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
6
+
7
+ import { v4 as uuidv4 } from 'uuid'
8
+ import { parseAnchorErrors } from '../utils/Errors'
9
+
10
+ export async function setHalted (
11
+ program: Program,
12
+ provider: Provider,
13
+ payer: Signer,
14
+ halted: boolean
15
+ ): Promise<PublicKey> {
16
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
17
+
18
+
19
+ const transaction = new Transaction().add(
20
+ await setHaltedInstruction(
21
+ program,
22
+ vaultSystemStatePublicKey,
23
+ payer.publicKey,
24
+ halted
25
+ )
26
+ )
27
+
28
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider?.opts).catch(parseAnchorErrors)
29
+ return vaultSystemStatePublicKey
30
+ }
31
+
32
+ export async function setHaltedInstruction (
33
+ program: Program,
34
+ vaultSystemStatePublicKey: PublicKey,
35
+ payerPublicKey: PublicKey,
36
+ halted: boolean
37
+ ): Promise<TransactionInstruction> {
38
+ const ix = program.instruction.setHalted(
39
+ halted,
40
+ {
41
+ accounts: {
42
+ payer: payerPublicKey,
43
+ vaultSystemState: vaultSystemStatePublicKey,
44
+ },
45
+ signers: []
46
+ })
47
+ return ix
48
+ }