hedge-web3 0.1.25 → 0.1.28

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. package/declarations/Constants.d.ts +3 -3
  2. package/declarations/idl/vault.d.ts +889 -752
  3. package/declarations/index.d.ts +2 -0
  4. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
  5. package/declarations/instructions/createVault.d.ts +1 -1
  6. package/declarations/instructions/depositLiquidationPool.d.ts +1 -1
  7. package/declarations/instructions/depositVault.d.ts +1 -1
  8. package/declarations/instructions/liquidateVault.d.ts +1 -1
  9. package/declarations/instructions/loanVault.d.ts +1 -1
  10. package/declarations/instructions/redeemVault.d.ts +1 -1
  11. package/declarations/instructions/repayVault.d.ts +1 -1
  12. package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
  13. package/declarations/instructions/withdrawVault.d.ts +1 -1
  14. package/declarations/state/LiquidationPosition.d.ts +2 -2
  15. package/declarations/state/StakingPool.d.ts +1 -1
  16. package/declarations/state/StakingPoolPosition.d.ts +2 -2
  17. package/declarations/state/VaultAccount.d.ts +11 -3
  18. package/declarations/state/VaultHistoryEvent.d.ts +2 -2
  19. package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
  20. package/lib/Constants.js +10 -10
  21. package/lib/idl/vault.js +922 -785
  22. package/lib/index.js +2 -0
  23. package/lib/instructions/closeLiquidationPoolPosition.js +9 -9
  24. package/lib/instructions/createStakingPool.js +6 -6
  25. package/lib/instructions/createVault.js +11 -11
  26. package/lib/instructions/depositLiquidationPool.js +9 -9
  27. package/lib/instructions/depositVault.js +20 -13
  28. package/lib/instructions/initHedgeFoundation.js +4 -4
  29. package/lib/instructions/liquidateVault.js +16 -11
  30. package/lib/instructions/loanVault.js +16 -11
  31. package/lib/instructions/redeemVault.js +15 -10
  32. package/lib/instructions/repayVault.js +17 -12
  33. package/lib/instructions/setVaultTypeStatus.js +38 -0
  34. package/lib/instructions/withdrawStakingPool.js +6 -6
  35. package/lib/instructions/withdrawVault.js +18 -13
  36. package/lib/state/LiquidationPosition.js +2 -2
  37. package/lib/state/StakingPool.js +1 -1
  38. package/lib/state/StakingPoolPosition.js +3 -3
  39. package/lib/state/VaultAccount.js +56 -3
  40. package/lib/state/VaultHistoryEvent.js +2 -2
  41. package/lib/utils/Errors.js +2 -2
  42. package/lib/utils/getLinkedListAccounts.js +131 -0
  43. package/package.json +3 -1
  44. package/src/Constants.ts +73 -31
  45. package/src/idl/vault.ts +1848 -1574
  46. package/src/index.ts +3 -0
  47. package/src/instructions/closeLiquidationPoolPosition.ts +10 -10
  48. package/src/instructions/createStakingPool.ts +6 -7
  49. package/src/instructions/createVault.ts +17 -17
  50. package/src/instructions/depositLiquidationPool.ts +10 -10
  51. package/src/instructions/depositVault.ts +104 -29
  52. package/src/instructions/initHedgeFoundation.ts +5 -5
  53. package/src/instructions/initHedgeFoundationTokens.ts +1 -1
  54. package/src/instructions/liquidateVault.ts +124 -27
  55. package/src/instructions/loanVault.ts +97 -25
  56. package/src/instructions/redeemVault.ts +35 -12
  57. package/src/instructions/repayVault.ts +91 -26
  58. package/src/instructions/setHalted.ts +1 -1
  59. package/src/instructions/setVaultTypeStatus.ts +50 -0
  60. package/src/instructions/withdrawStakingPool.ts +7 -7
  61. package/src/instructions/withdrawVault.ts +106 -28
  62. package/src/state/LiquidationPosition.ts +3 -3
  63. package/src/state/StakingPool.ts +2 -6
  64. package/src/state/StakingPoolPosition.ts +4 -4
  65. package/src/state/VaultAccount.ts +89 -13
  66. package/src/state/VaultHistoryEvent.ts +4 -4
  67. package/src/utils/Errors.ts +2 -2
  68. package/src/utils/getLinkedListAccounts.ts +156 -0
  69. package/declarations/idl/idl.d.ts +0 -2
  70. package/lib/idl/idl.js +0 -1475
  71. package/src/idl/idl.ts +0 -1474
@@ -1,9 +1,31 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
3
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
2
+ import {
3
+ ASSOCIATED_TOKEN_PROGRAM_ID,
4
+ TOKEN_PROGRAM_ID,
5
+ getOrCreateAssociatedTokenAccount,
6
+ } from '@solana/spl-token'
7
+ import {
8
+ Keypair,
9
+ PublicKey,
10
+ sendAndConfirmTransaction,
11
+ Signer,
12
+ SystemProgram,
13
+ SYSVAR_RENT_PUBKEY,
14
+ Transaction,
15
+ TransactionInstruction,
16
+ } from '@solana/web3.js'
17
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
18
+ import {
19
+ getHedgeMintPublicKey,
20
+ getLiquidationPoolStatePublicKey,
21
+ getLiquidationPoolUshAccountPublicKey,
22
+ getPoolPublicKeyForMint,
23
+ getVaultTypeAccountPublicKey,
24
+ getUshMintPublicKey,
25
+ getVaultSystemStatePublicKey,
26
+ } from '../Constants'
5
27
 
6
- export async function liquidateVault (
28
+ export async function liquidateVault(
7
29
  program: Program,
8
30
  provider: Provider,
9
31
  payer: Signer,
@@ -12,22 +34,80 @@ export async function liquidateVault (
12
34
  ): Promise<PublicKey> {
13
35
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
14
36
 
15
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
16
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
37
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
38
+ vaultAccount.collateralType
39
+ )
40
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(
41
+ vaultTypeAccountPublicKey
42
+ )
17
43
  const collateralMint = vaultTypeAccountInfo.collateralMint
18
44
 
19
45
  const hedgeMintPublickey = await getHedgeMintPublicKey()
20
- const usdhMintPublickey = await getUsdhMintPublicKey()
21
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
46
+ const ushMintPublickey = await getUshMintPublicKey()
47
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
48
+ hedgeMintPublickey
49
+ )
22
50
  const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
23
- const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
51
+ const poolStateInfo = await program.account.liquidationPoolState.fetch(
52
+ liquidationPoolStatePublicKey
53
+ )
54
+
55
+ const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
56
+ provider.connection,
57
+ payer,
58
+ collateralMint,
59
+ payer.publicKey,
60
+ true
61
+ )
62
+ const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
63
+ provider.connection,
64
+ payer,
65
+ collateralMint,
66
+ hedgeStakingPoolPublicKey,
67
+ true
68
+ )
69
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
70
+ provider.connection,
71
+ payer,
72
+ collateralMint,
73
+ vaultPublicKey,
74
+ true
75
+ )
76
+ const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
77
+ provider.connection,
78
+ payer,
79
+ collateralMint,
80
+ liquidationPoolStatePublicKey,
81
+ true
82
+ )
83
+ const vaultTypeAssociatedTokenAccount =
84
+ await getOrCreateAssociatedTokenAccount(
85
+ provider.connection,
86
+ payer,
87
+ collateralMint,
88
+ vaultTypeAccountPublicKey,
89
+ true
90
+ )
91
+ const hedgeStakingPoolAssociatedUshTokenAccount =
92
+ await getOrCreateAssociatedTokenAccount(
93
+ provider.connection,
94
+ payer,
95
+ ushMintPublickey,
96
+ hedgeStakingPoolPublicKey,
97
+ true
98
+ )
24
99
 
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)
100
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
101
+ await getLinkedListAccounts(
102
+ program,
103
+ provider,
104
+ vaultTypeAccountPublicKey,
105
+ vaultPublicKey,
106
+ 0,
107
+ 0,
108
+ false,
109
+ true
110
+ )
31
111
 
32
112
  const history = Keypair.generate()
33
113
  const newEra = Keypair.generate()
@@ -47,18 +127,26 @@ export async function liquidateVault (
47
127
  newEra.publicKey,
48
128
  hedgeStakingPoolPublicKey,
49
129
  feePoolAssociatedTokenAccount.address,
50
- hedgeStakingPoolAssociatedUsdhTokenAccount.address,
130
+ hedgeStakingPoolAssociatedUshTokenAccount.address,
51
131
  collateralMint,
52
132
  vaultTypeAssociatedTokenAccount.address,
133
+ oldSmallerPublicKey,
134
+ newSmallerPublicKey,
135
+ newLargerPublicKey,
53
136
  vaultAccount.collateralType,
54
137
  overrideTime
55
138
  )
56
139
  )
57
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts)
140
+ await sendAndConfirmTransaction(
141
+ provider.connection,
142
+ transaction,
143
+ [payer, history, newEra],
144
+ provider.opts
145
+ )
58
146
  return vaultPublicKey
59
147
  }
60
148
 
61
- export async function liquidateVaultInstruction (
149
+ export async function liquidateVaultInstruction(
62
150
  program: Program,
63
151
  payerPublicKey: PublicKey,
64
152
  payerAssociatedTokenAccount: PublicKey,
@@ -71,15 +159,19 @@ export async function liquidateVaultInstruction (
71
159
  newEraPublicKey: PublicKey,
72
160
  feePool: PublicKey,
73
161
  feePoolAssociatedTokenAccount: PublicKey,
74
- hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
162
+ hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
75
163
  collateralMint: PublicKey,
76
164
  vaultTypeAssociatedTokenAccount: PublicKey,
165
+ oldSmallerPublicKey: PublicKey,
166
+ newSmallerPublicKey: PublicKey,
167
+ newLargerPublicKey: PublicKey,
77
168
  collateralType: string,
78
169
  overrideTime?: number
79
170
  ): Promise<TransactionInstruction> {
80
171
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
81
- const usdhMintPublickey = await getUsdhMintPublicKey()
82
- const liquidationPoolUsdhAccountPublickey = await getLiquidationPoolUsdhAccountPublicKey()
172
+ const ushMintPublickey = await getUshMintPublicKey()
173
+ const liquidationPoolUshAccountPublickey =
174
+ await getLiquidationPoolUshAccountPublicKey()
83
175
  const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
84
176
 
85
177
  const payload = {
@@ -93,23 +185,28 @@ export async function liquidateVaultInstruction (
93
185
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
94
186
  poolState: poolState,
95
187
  poolAssociatedTokenAccount: poolAssociatedTokenAccount,
96
- usdhMint: usdhMintPublickey,
188
+ ushMint: ushMintPublickey,
97
189
  history: historyPublicKey,
98
190
  payer: payerPublicKey,
99
191
  payerAssociatedTokenAccount: payerAssociatedTokenAccount,
100
192
  feePool: feePool,
101
193
  feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
102
- feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
103
- liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
194
+ feePoolAssociatedUshTokenAccount:
195
+ hedgeStakingPoolAssociatedUshTokenAccount,
196
+ liquidationPoolUshAccount: liquidationPoolUshAccountPublickey,
104
197
  newEra: newEraPublicKey,
198
+ oldSmallerVaultInfo: oldSmallerPublicKey,
199
+ newSmallerVaultInfo: newSmallerPublicKey,
200
+ newLargerVaultInfo: newLargerPublicKey,
105
201
  tokenProgram: TOKEN_PROGRAM_ID,
106
202
  systemProgram: SystemProgram.programId,
107
203
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
108
- rent: SYSVAR_RENT_PUBKEY
204
+ rent: SYSVAR_RENT_PUBKEY,
109
205
  },
110
- signers: []
206
+ signers: [],
111
207
  }
112
208
  return program.instruction.liquidateVault(
113
209
  new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
114
- payload)
210
+ payload
211
+ )
115
212
  }
@@ -1,9 +1,28 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
2
+ import {
3
+ getOrCreateAssociatedTokenAccount,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token'
6
+ import {
7
+ Keypair,
8
+ PublicKey,
9
+ sendAndConfirmTransaction,
10
+ Signer,
11
+ SystemProgram,
12
+ Transaction,
13
+ TransactionInstruction,
14
+ } from '@solana/web3.js'
15
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
16
+ import {
17
+ findAssociatedTokenAddress,
18
+ getHedgeMintPublicKey,
19
+ getPoolPublicKeyForMint,
20
+ getVaultTypeAccountPublicKey,
21
+ getUshMintPublicKey,
22
+ getVaultSystemStatePublicKey,
23
+ } from '../Constants'
5
24
 
6
- export async function loanVault (
25
+ export async function loanVault(
7
26
  program: Program,
8
27
  provider: Provider,
9
28
  payer: Signer,
@@ -11,55 +30,103 @@ export async function loanVault (
11
30
  loanAmount: number,
12
31
  overrideTime?: number
13
32
  ): Promise<PublicKey> {
14
- const usdhMintPublickey = await getUsdhMintPublicKey()
33
+ const ushMintPublickey = await getUshMintPublicKey()
15
34
 
16
- const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
35
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
36
+ provider.connection,
37
+ payer,
38
+ ushMintPublickey,
39
+ payer.publicKey
40
+ )
17
41
 
18
42
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
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)
43
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
44
+ vaultAccount.collateralType
45
+ )
46
+ const vaultTypeAccount = await program.account.vaultType.fetch(
47
+ vaultTypeAccountPublicKey
48
+ )
49
+ const vaultTypeAssociatedTokenAccount =
50
+ await getOrCreateAssociatedTokenAccount(
51
+ provider.connection,
52
+ payer,
53
+ vaultTypeAccount.collateralMint,
54
+ vaultTypeAccountPublicKey,
55
+ true
56
+ )
57
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
58
+ provider.connection,
59
+ payer,
60
+ vaultTypeAccount.collateralMint,
61
+ vaultPublicKey,
62
+ true
63
+ )
64
+
65
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
66
+ await getLinkedListAccounts(
67
+ program,
68
+ provider,
69
+ vaultTypeAccountPublicKey,
70
+ vaultPublicKey,
71
+ 0,
72
+ loanAmount,
73
+ false,
74
+ false
75
+ )
23
76
 
24
77
  const history = Keypair.generate()
25
78
  const transaction = new Transaction().add(
26
79
  await loanVaultInstruction(
27
80
  program,
28
81
  payer.publicKey,
29
- payerUsdhAccount.address,
82
+ payerUshAccount.address,
30
83
  vaultPublicKey,
31
84
  vaultAssociatedTokenAccount.address,
32
85
  history.publicKey,
33
86
  vaultTypeAccountPublicKey,
34
87
  vaultTypeAssociatedTokenAccount.address,
88
+ oldSmallerPublicKey,
89
+ newSmallerPublicKey,
90
+ newLargerPublicKey,
35
91
  loanAmount,
36
92
  overrideTime
37
93
  )
38
94
  )
39
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
95
+ await sendAndConfirmTransaction(
96
+ provider.connection,
97
+ transaction,
98
+ [payer, history],
99
+ provider.opts
100
+ )
40
101
  return vaultPublicKey
41
102
  }
42
103
 
43
- export async function loanVaultInstruction (
104
+ export async function loanVaultInstruction(
44
105
  program: Program,
45
106
  payerPublicKey: PublicKey,
46
- ownerUsdhAccount: PublicKey,
107
+ ownerUshAccount: PublicKey,
47
108
  vaultPublickey: PublicKey,
48
109
  vaultAssociatedTokenAccount: PublicKey,
49
110
  historyPublicKey: PublicKey,
50
111
  vaultTypeAccount: PublicKey,
51
112
  vaultTypeAssociatedTokenAccount: PublicKey,
113
+ oldSmallerPublicKey: PublicKey,
114
+ newSmallerPublicKey: PublicKey,
115
+ newLargerPublicKey: PublicKey,
52
116
  loanAmount: number,
53
117
  overrideTime?: number
54
118
  ): Promise<TransactionInstruction> {
55
119
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
56
- const usdhMintPublickey = await getUsdhMintPublicKey()
120
+ const ushMintPublickey = await getUshMintPublicKey()
57
121
  const hedgeMintPublickey = await getHedgeMintPublicKey()
58
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
59
- const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
60
- hedgeStakingPoolPublicKey,
61
- usdhMintPublickey
122
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
123
+ hedgeMintPublickey
62
124
  )
125
+ const hedgeStakingPoolAssociatedUshTokenAccount =
126
+ await findAssociatedTokenAddress(
127
+ hedgeStakingPoolPublicKey,
128
+ ushMintPublickey
129
+ )
63
130
 
64
131
  return program.instruction.loanVault(
65
132
  new BN(loanAmount),
@@ -73,13 +140,18 @@ export async function loanVaultInstruction (
73
140
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
74
141
  history: historyPublicKey,
75
142
  feePool: hedgeStakingPoolPublicKey,
76
- feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
77
- usdhMint: usdhMintPublickey,
143
+ feePoolAssociatedUshTokenAccount:
144
+ hedgeStakingPoolAssociatedUshTokenAccount,
145
+ ushMint: ushMintPublickey,
78
146
  vaultOwner: payerPublicKey,
79
- ownerUsdhAccount: ownerUsdhAccount,
147
+ ownerUshAccount: ownerUshAccount,
148
+ oldSmallerVaultInfo: oldSmallerPublicKey,
149
+ newSmallerVaultInfo: newSmallerPublicKey,
150
+ newLargerVaultInfo: newLargerPublicKey,
80
151
  tokenProgram: TOKEN_PROGRAM_ID,
81
- systemProgram: SystemProgram.programId
152
+ systemProgram: SystemProgram.programId,
82
153
  },
83
- signers: []
84
- })
154
+ signers: [],
155
+ }
156
+ )
85
157
  }
@@ -2,7 +2,8 @@ import { BN, Program, Provider } from '@project-serum/anchor'
2
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, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
5
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
6
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
7
 
7
8
  export async function redeemVault (
8
9
  program: Program,
@@ -12,10 +13,10 @@ export async function redeemVault (
12
13
  redeemAmount: number,
13
14
  transactionOverrideTime?: number
14
15
  ): Promise<PublicKey> {
15
- const usdhMintPublickey = await getUsdhMintPublicKey()
16
+ const ushMintPublickey = await getUshMintPublicKey()
16
17
 
17
- // Prep the user to get USDH back out at some point
18
- const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
18
+ // Prep the user to get USH back out at some point
19
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
19
20
 
20
21
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
21
22
  const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
@@ -26,18 +27,34 @@ export async function redeemVault (
26
27
 
27
28
  const payerTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
28
29
 
30
+
31
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
32
+ await getLinkedListAccounts(
33
+ program,
34
+ provider,
35
+ vaultTypeAccountPublicKey,
36
+ vaultPublicKey,
37
+ 0,
38
+ 0,
39
+ true,
40
+ false
41
+ )
42
+
29
43
  const history = Keypair.generate()
30
44
  const transaction = new Transaction().add(
31
45
  await redeemVaultInstruction(
32
46
  program,
33
47
  payer.publicKey,
34
- payerUsdhAccount.address,
48
+ payerUshAccount.address,
35
49
  payerTokenAccount.address,
36
50
  vaultPublicKey,
37
51
  vaultAssociatedTokenAccount.address,
38
52
  history.publicKey,
39
53
  vaultTypeAccountPublicKey,
40
54
  vaultTypeAssociatedTokenAccount.address,
55
+ oldSmallerPublicKey,
56
+ newSmallerPublicKey,
57
+ newLargerPublicKey,
41
58
  redeemAmount,
42
59
  transactionOverrideTime
43
60
  )
@@ -49,23 +66,26 @@ export async function redeemVault (
49
66
  export async function redeemVaultInstruction (
50
67
  program: Program,
51
68
  payerPublicKey: PublicKey,
52
- payerUsdhAccount: PublicKey,
69
+ payerUshAccount: PublicKey,
53
70
  destinationTokenAccount: PublicKey,
54
71
  vaultPublickey: PublicKey,
55
72
  vaultAssociatedTokenAccount: PublicKey,
56
73
  historyPublicKey: PublicKey,
57
74
  vaultTypeAccount: PublicKey,
58
75
  vaultTypeAssociatedTokenAccount: PublicKey,
76
+ oldSmallerPublicKey: PublicKey,
77
+ newSmallerPublicKey: PublicKey,
78
+ newLargerPublicKey: PublicKey,
59
79
  redeemAmount: number,
60
80
  transactionOverrideTime?: number
61
81
  ): Promise<TransactionInstruction> {
62
82
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
63
- const usdhMintPublickey = await getUsdhMintPublicKey()
83
+ const ushMintPublickey = await getUshMintPublicKey()
64
84
  const hedgeMintPublickey = await getHedgeMintPublicKey()
65
85
  const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
66
- const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
86
+ const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
67
87
  hedgeStakingPoolPublicKey,
68
- usdhMintPublickey
88
+ ushMintPublickey
69
89
  )
70
90
  return program.instruction.redeemVault(
71
91
  new BN(redeemAmount),
@@ -79,11 +99,14 @@ export async function redeemVaultInstruction (
79
99
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
80
100
  history: historyPublicKey,
81
101
  feePool: hedgeStakingPoolPublicKey,
82
- feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
83
- usdhMint: usdhMintPublickey,
102
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
103
+ ushMint: ushMintPublickey,
84
104
  payer: payerPublicKey,
85
- payerUsdhAccount: payerUsdhAccount,
105
+ payerUshAccount: payerUshAccount,
86
106
  destinationTokenAccount: destinationTokenAccount,
107
+ oldSmallerVaultInfo: oldSmallerPublicKey,
108
+ newSmallerVaultInfo: newSmallerPublicKey,
109
+ newLargerVaultInfo: newLargerPublicKey,
87
110
  tokenProgram: TOKEN_PROGRAM_ID,
88
111
  systemProgram: SystemProgram.programId
89
112
  },
@@ -1,9 +1,28 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
2
+ import {
3
+ getOrCreateAssociatedTokenAccount,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token'
6
+ import {
7
+ Keypair,
8
+ PublicKey,
9
+ sendAndConfirmTransaction,
10
+ Signer,
11
+ SystemProgram,
12
+ Transaction,
13
+ TransactionInstruction,
14
+ } from '@solana/web3.js'
15
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
16
+ import {
17
+ findAssociatedTokenAddress,
18
+ getHedgeMintPublicKey,
19
+ getPoolPublicKeyForMint,
20
+ getVaultTypeAccountPublicKey,
21
+ getUshMintPublicKey,
22
+ getVaultSystemStatePublicKey,
23
+ } from '../Constants'
5
24
 
6
- export async function repayVault (
25
+ export async function repayVault(
7
26
  program: Program,
8
27
  provider: Provider,
9
28
  payer: Signer,
@@ -11,56 +30,97 @@ export async function repayVault (
11
30
  repayAmount: number,
12
31
  overrideTime?: number
13
32
  ): Promise<PublicKey> {
14
- const usdhMintPublickey = await getUsdhMintPublicKey()
33
+ const ushMintPublickey = await getUshMintPublicKey()
15
34
 
16
- // Prep the user to get USDH back out at some point
17
- const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
35
+ // Prep the user to get USH back out at some point
36
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
37
+ provider.connection,
38
+ payer,
39
+ ushMintPublickey,
40
+ payer.publicKey
41
+ )
18
42
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
19
43
 
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)
44
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
45
+ vaultAccount.collateralType
46
+ )
47
+ const vaultTypeAccount = await program.account.vaultType.fetch(
48
+ vaultTypeAccountPublicKey
49
+ )
50
+ const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
51
+ vaultTypeAccountPublicKey,
52
+ vaultTypeAccount.collateralMint
53
+ )
54
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
55
+ vaultPublicKey,
56
+ vaultTypeAccount.collateralMint
57
+ )
58
+
59
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
60
+ await getLinkedListAccounts(
61
+ program,
62
+ provider,
63
+ vaultTypeAccountPublicKey,
64
+ vaultPublicKey,
65
+ 0,
66
+ repayAmount * -1,
67
+ false,
68
+ false
69
+ )
24
70
 
25
71
  const history = Keypair.generate()
26
72
  const transaction = new Transaction().add(
27
73
  await repayVaultInstruction(
28
74
  program,
29
75
  payer.publicKey,
30
- payerUsdhAccount.address,
76
+ payerUshAccount.address,
31
77
  vaultPublicKey,
32
78
  vaultAssociatedTokenAccount,
33
79
  history.publicKey,
34
80
  vaultTypeAccountPublicKey,
35
81
  vaultTypeAssociatedTokenAccount,
82
+ oldSmallerPublicKey,
83
+ newSmallerPublicKey,
84
+ newLargerPublicKey,
36
85
  repayAmount,
37
86
  overrideTime
38
87
  )
39
88
  )
40
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
89
+ await sendAndConfirmTransaction(
90
+ provider.connection,
91
+ transaction,
92
+ [payer, history],
93
+ provider.opts
94
+ )
41
95
  return vaultPublicKey
42
96
  }
43
97
 
44
- export async function repayVaultInstruction (
98
+ export async function repayVaultInstruction(
45
99
  program: Program,
46
100
  payerPublicKey: PublicKey,
47
- ownerUsdhAccount: PublicKey,
101
+ ownerUshAccount: PublicKey,
48
102
  vaultPublickey: PublicKey,
49
103
  vaultAssociatedTokenAccount: PublicKey,
50
104
  historyPublicKey: PublicKey,
51
105
  vaultTypeAccount: PublicKey,
52
106
  vaultTypeAssociatedTokenAccount: PublicKey,
107
+ oldSmallerPublicKey: PublicKey,
108
+ newSmallerPublicKey: PublicKey,
109
+ newLargerPublicKey: PublicKey,
53
110
  repayAmount: number,
54
111
  overrideTime?: number
55
112
  ): Promise<TransactionInstruction> {
56
113
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
57
- const usdhMintPublickey = await getUsdhMintPublicKey()
114
+ const ushMintPublickey = await getUshMintPublicKey()
58
115
  const hedgeMintPublickey = await getHedgeMintPublicKey()
59
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
60
- const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
61
- hedgeStakingPoolPublicKey,
62
- usdhMintPublickey
116
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
117
+ hedgeMintPublickey
63
118
  )
119
+ const hedgeStakingPoolAssociatedUshTokenAccount =
120
+ await findAssociatedTokenAddress(
121
+ hedgeStakingPoolPublicKey,
122
+ ushMintPublickey
123
+ )
64
124
 
65
125
  return program.instruction.repayVault(
66
126
  new BN(repayAmount),
@@ -74,13 +134,18 @@ export async function repayVaultInstruction (
74
134
  vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
75
135
  history: historyPublicKey,
76
136
  feePool: hedgeStakingPoolPublicKey,
77
- feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
78
- usdhMint: usdhMintPublickey,
137
+ feePoolAssociatedUshTokenAccount:
138
+ hedgeStakingPoolAssociatedUshTokenAccount,
139
+ ushMint: ushMintPublickey,
79
140
  vaultOwner: payerPublicKey,
80
- ownerUsdhAccount: ownerUsdhAccount,
141
+ oldSmallerVaultInfo: oldSmallerPublicKey,
142
+ newSmallerVaultInfo: newSmallerPublicKey,
143
+ newLargerVaultInfo: newLargerPublicKey,
144
+ ownerUshAccount: ownerUshAccount,
81
145
  tokenProgram: TOKEN_PROGRAM_ID,
82
- systemProgram: SystemProgram.programId
146
+ systemProgram: SystemProgram.programId,
83
147
  },
84
- signers: []
85
- })
148
+ signers: [],
149
+ }
150
+ )
86
151
  }
@@ -2,7 +2,7 @@ import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { TokenInstructions } from '@project-serum/serum'
3
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, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
5
+ import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
6
6
 
7
7
  import { v4 as uuidv4 } from 'uuid'
8
8
  import { parseAnchorErrors } from '../utils/Errors'