hedge-web3 0.1.27 → 0.1.31

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. package/declarations/Constants.d.ts +1 -1
  2. package/declarations/idl/vault.d.ts +277 -126
  3. package/declarations/index.d.ts +3 -0
  4. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
  5. package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
  6. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
  7. package/declarations/instructions/createStakingPool.d.ts +3 -2
  8. package/declarations/instructions/createVault.d.ts +6 -5
  9. package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
  10. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  11. package/declarations/instructions/depositVault.d.ts +3 -2
  12. package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
  13. package/declarations/instructions/liquidateVault.d.ts +3 -2
  14. package/declarations/instructions/loanVault.d.ts +3 -2
  15. package/declarations/instructions/redeemVault.d.ts +3 -2
  16. package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
  17. package/declarations/instructions/repayVault.d.ts +3 -2
  18. package/declarations/instructions/setHalted.d.ts +3 -2
  19. package/declarations/instructions/setVaultTypeStatus.d.ts +5 -0
  20. package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
  21. package/declarations/instructions/withdrawVault.d.ts +3 -2
  22. package/declarations/state/VaultAccount.d.ts +8 -0
  23. package/declarations/utils/getLinkedListAccounts.d.ts +5 -0
  24. package/lib/Constants.js +1 -1
  25. package/lib/idl/vault.js +277 -126
  26. package/lib/index.js +3 -0
  27. package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
  28. package/lib/instructions/claimStakingPoolPosition.js +19 -19
  29. package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
  30. package/lib/instructions/createStakingPool.js +18 -19
  31. package/lib/instructions/createVault.js +28 -31
  32. package/lib/instructions/depositLiquidationPool.js +17 -18
  33. package/lib/instructions/depositStakingPool.js +16 -18
  34. package/lib/instructions/depositVault.js +31 -26
  35. package/lib/instructions/initHedgeFoundation.js +17 -19
  36. package/lib/instructions/initHedgeFoundationTokens.js +15 -15
  37. package/lib/instructions/liquidateVault.js +36 -32
  38. package/lib/instructions/loanVault.js +27 -22
  39. package/lib/instructions/redeemVault.js +28 -23
  40. package/lib/instructions/refreshOraclePrice.js +17 -17
  41. package/lib/instructions/repayVault.js +27 -22
  42. package/lib/instructions/setHalted.js +8 -9
  43. package/lib/instructions/setVaultTypeStatus.js +37 -0
  44. package/lib/instructions/withdrawStakingPool.js +22 -24
  45. package/lib/instructions/withdrawVault.js +30 -25
  46. package/lib/state/LiquidationPoolEra.js +3 -1
  47. package/lib/state/LiquidationPosition.js +0 -7
  48. package/lib/state/StakingPool.js +3 -4
  49. package/lib/state/VaultAccount.js +51 -1
  50. package/lib/utils/getLinkedListAccounts.js +139 -0
  51. package/package.json +4 -2
  52. package/src/Constants.ts +1 -1
  53. package/src/idl/vault.ts +554 -252
  54. package/src/index.ts +4 -0
  55. package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
  56. package/src/instructions/claimStakingPoolPosition.ts +45 -25
  57. package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
  58. package/src/instructions/createStakingPool.ts +38 -37
  59. package/src/instructions/createVault.ts +81 -125
  60. package/src/instructions/depositLiquidationPool.ts +45 -26
  61. package/src/instructions/depositStakingPool.ts +32 -24
  62. package/src/instructions/depositVault.ts +77 -31
  63. package/src/instructions/initHedgeFoundation.ts +42 -43
  64. package/src/instructions/initHedgeFoundationTokens.ts +38 -39
  65. package/src/instructions/liquidateVault.ts +96 -22
  66. package/src/instructions/loanVault.ts +84 -30
  67. package/src/instructions/redeemVault.ts +91 -32
  68. package/src/instructions/refreshOraclePrice.ts +41 -32
  69. package/src/instructions/repayVault.ts +74 -29
  70. package/src/instructions/setHalted.ts +32 -24
  71. package/src/instructions/setVaultTypeStatus.ts +58 -0
  72. package/src/instructions/withdrawStakingPool.ts +44 -30
  73. package/src/instructions/withdrawVault.ts +87 -33
  74. package/src/state/LiquidationPoolEra.ts +4 -3
  75. package/src/state/LiquidationPosition.ts +0 -27
  76. package/src/state/StakingPool.ts +4 -7
  77. package/src/state/StakingPoolPosition.ts +2 -3
  78. package/src/state/VaultAccount.ts +65 -8
  79. package/src/state/VaultHistoryEvent.ts +1 -2
  80. package/src/utils/getLinkedListAccounts.ts +157 -0
@@ -1,10 +1,27 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
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, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getHedgeMintPublicKey,
16
+ getPoolPublicKeyForMint,
17
+ getVaultTypeAccountPublicKey,
18
+ getUshMintPublicKey,
19
+ getVaultSystemStatePublicKey,
20
+ } from '../Constants'
21
+ import { Vault } from 'idl/vault'
5
22
 
6
- export async function loanVault (
7
- program: Program,
23
+ export async function loanVault(
24
+ program: Program<Vault>,
8
25
  provider: Provider,
9
26
  payer: Signer,
10
27
  vaultPublicKey: PublicKey,
@@ -13,13 +30,41 @@ export async function loanVault (
13
30
  ): Promise<PublicKey> {
14
31
  const ushMintPublickey = await getUshMintPublicKey()
15
32
 
16
- const payerUshAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
33
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
34
+ provider.connection,
35
+ payer,
36
+ ushMintPublickey,
37
+ payer.publicKey
38
+ )
17
39
 
18
40
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
19
41
  const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
20
42
  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 vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
44
+ provider.connection,
45
+ payer,
46
+ vaultTypeAccount.collateralMint,
47
+ vaultTypeAccountPublicKey,
48
+ true
49
+ )
50
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
51
+ provider.connection,
52
+ payer,
53
+ vaultTypeAccount.collateralMint,
54
+ vaultPublicKey,
55
+ true
56
+ )
57
+
58
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
59
+ program,
60
+ provider,
61
+ vaultTypeAccountPublicKey,
62
+ vaultPublicKey,
63
+ 0,
64
+ loanAmount,
65
+ false,
66
+ false
67
+ )
23
68
 
24
69
  const history = Keypair.generate()
25
70
  const transaction = new Transaction().add(
@@ -32,16 +77,19 @@ export async function loanVault (
32
77
  history.publicKey,
33
78
  vaultTypeAccountPublicKey,
34
79
  vaultTypeAssociatedTokenAccount.address,
80
+ oldSmallerPublicKey,
81
+ newSmallerPublicKey,
82
+ newLargerPublicKey,
35
83
  loanAmount,
36
84
  overrideTime
37
85
  )
38
86
  )
39
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
87
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
40
88
  return vaultPublicKey
41
89
  }
42
90
 
43
- export async function loanVaultInstruction (
44
- program: Program,
91
+ export async function loanVaultInstruction(
92
+ program: Program<Vault>,
45
93
  payerPublicKey: PublicKey,
46
94
  ownerUshAccount: PublicKey,
47
95
  vaultPublickey: PublicKey,
@@ -49,6 +97,9 @@ export async function loanVaultInstruction (
49
97
  historyPublicKey: PublicKey,
50
98
  vaultTypeAccount: PublicKey,
51
99
  vaultTypeAssociatedTokenAccount: PublicKey,
100
+ oldSmallerPublicKey: PublicKey,
101
+ newSmallerPublicKey: PublicKey,
102
+ newLargerPublicKey: PublicKey,
52
103
  loanAmount: number,
53
104
  overrideTime?: number
54
105
  ): Promise<TransactionInstruction> {
@@ -61,25 +112,28 @@ export async function loanVaultInstruction (
61
112
  ushMintPublickey
62
113
  )
63
114
 
64
- return program.instruction.loanVault(
65
- new BN(loanAmount),
66
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
67
- {
68
- accounts: {
69
- vaultSystemState: vaultSystemStatePublicKey,
70
- vaultTypeAccount: vaultTypeAccount,
71
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
72
- vaultAccount: vaultPublickey,
73
- vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
74
- history: historyPublicKey,
75
- feePool: hedgeStakingPoolPublicKey,
76
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
77
- ushMint: ushMintPublickey,
78
- vaultOwner: payerPublicKey,
79
- ownerUshAccount: ownerUshAccount,
80
- tokenProgram: TOKEN_PROGRAM_ID,
81
- systemProgram: SystemProgram.programId
82
- },
83
- signers: []
115
+ return program.methods
116
+ .loanVault(
117
+ new BN(loanAmount),
118
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
119
+ )
120
+ .accounts({
121
+ vaultSystemState: vaultSystemStatePublicKey,
122
+ vaultTypeAccount: vaultTypeAccount,
123
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
124
+ vaultAccount: vaultPublickey,
125
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
126
+ history: historyPublicKey,
127
+ feePool: hedgeStakingPoolPublicKey,
128
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
129
+ ushMint: ushMintPublickey,
130
+ vaultOwner: payerPublicKey,
131
+ ownerUshAccount: ownerUshAccount,
132
+ oldSmallerVaultInfo: oldSmallerPublicKey,
133
+ newSmallerVaultInfo: newSmallerPublicKey,
134
+ newLargerVaultInfo: newLargerPublicKey,
135
+ tokenProgram: TOKEN_PROGRAM_ID,
136
+ systemProgram: SystemProgram.programId,
84
137
  })
138
+ .instruction()
85
139
  }
@@ -1,11 +1,28 @@
1
1
  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
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
4
+ import {
5
+ Keypair,
6
+ PublicKey,
7
+ sendAndConfirmTransaction,
8
+ Signer,
9
+ SystemProgram,
10
+ Transaction,
11
+ TransactionInstruction,
12
+ } from '@solana/web3.js'
13
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
14
+ import {
15
+ findAssociatedTokenAddress,
16
+ getHedgeMintPublicKey,
17
+ getPoolPublicKeyForMint,
18
+ getVaultTypeAccountPublicKey,
19
+ getUshMintPublicKey,
20
+ getVaultSystemStatePublicKey,
21
+ } from '../Constants'
22
+ import { Vault } from 'idl/vault'
6
23
 
7
- export async function redeemVault (
8
- program: Program,
24
+ export async function redeemVault(
25
+ program: Program<Vault>,
9
26
  provider: Provider,
10
27
  payer: Signer,
11
28
  vaultPublicKey: PublicKey,
@@ -15,16 +32,49 @@ export async function redeemVault (
15
32
  const ushMintPublickey = await getUshMintPublicKey()
16
33
 
17
34
  // Prep the user to get USH back out at some point
18
- const payerUshAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
35
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
36
+ provider.connection,
37
+ payer,
38
+ ushMintPublickey,
39
+ payer.publicKey
40
+ )
19
41
 
20
42
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
21
43
  const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
22
44
  const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
23
- const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true)
45
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
46
+ provider.connection,
47
+ payer,
48
+ vaultTypeAccountInfo.collateralMint,
49
+ vaultTypeAccountPublicKey,
50
+ true
51
+ )
24
52
 
25
- const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true)
53
+ const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
54
+ provider.connection,
55
+ payer,
56
+ vaultTypeAccountInfo.collateralMint,
57
+ vaultPublicKey,
58
+ true
59
+ )
26
60
 
27
- const payerTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
61
+ const payerTokenAccount = await getOrCreateAssociatedTokenAccount(
62
+ provider.connection,
63
+ payer,
64
+ vaultTypeAccountInfo.collateralMint,
65
+ payer.publicKey
66
+ )
67
+
68
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
69
+ program,
70
+ provider,
71
+ vaultTypeAccountPublicKey,
72
+ vaultPublicKey,
73
+ 0,
74
+ 0,
75
+ true,
76
+ false
77
+ )
28
78
 
29
79
  const history = Keypair.generate()
30
80
  const transaction = new Transaction().add(
@@ -38,16 +88,19 @@ export async function redeemVault (
38
88
  history.publicKey,
39
89
  vaultTypeAccountPublicKey,
40
90
  vaultTypeAssociatedTokenAccount.address,
91
+ oldSmallerPublicKey,
92
+ newSmallerPublicKey,
93
+ newLargerPublicKey,
41
94
  redeemAmount,
42
95
  transactionOverrideTime
43
96
  )
44
97
  )
45
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
98
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
46
99
  return vaultPublicKey
47
100
  }
48
101
 
49
- export async function redeemVaultInstruction (
50
- program: Program,
102
+ export async function redeemVaultInstruction(
103
+ program: Program<Vault>,
51
104
  payerPublicKey: PublicKey,
52
105
  payerUshAccount: PublicKey,
53
106
  destinationTokenAccount: PublicKey,
@@ -56,6 +109,9 @@ export async function redeemVaultInstruction (
56
109
  historyPublicKey: PublicKey,
57
110
  vaultTypeAccount: PublicKey,
58
111
  vaultTypeAssociatedTokenAccount: PublicKey,
112
+ oldSmallerPublicKey: PublicKey,
113
+ newSmallerPublicKey: PublicKey,
114
+ newLargerPublicKey: PublicKey,
59
115
  redeemAmount: number,
60
116
  transactionOverrideTime?: number
61
117
  ): Promise<TransactionInstruction> {
@@ -67,26 +123,29 @@ export async function redeemVaultInstruction (
67
123
  hedgeStakingPoolPublicKey,
68
124
  ushMintPublickey
69
125
  )
70
- return program.instruction.redeemVault(
71
- new BN(redeemAmount),
72
- new BN(transactionOverrideTime ?? (Date.now() / 1000)), // override start time
73
- {
74
- accounts: {
75
- vaultSystemState: vaultSystemStatePublicKey,
76
- vaultTypeAccount: vaultTypeAccount,
77
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
78
- vault: vaultPublickey,
79
- vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
80
- history: historyPublicKey,
81
- feePool: hedgeStakingPoolPublicKey,
82
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
83
- ushMint: ushMintPublickey,
84
- payer: payerPublicKey,
85
- payerUshAccount: payerUshAccount,
86
- destinationTokenAccount: destinationTokenAccount,
87
- tokenProgram: TOKEN_PROGRAM_ID,
88
- systemProgram: SystemProgram.programId
89
- },
90
- signers: []
126
+ return await program.methods
127
+ .redeemVault(
128
+ new BN(redeemAmount),
129
+ new BN(transactionOverrideTime ?? Date.now() / 1000) // override start time
130
+ )
131
+ .accounts({
132
+ vaultSystemState: vaultSystemStatePublicKey,
133
+ vaultTypeAccount: vaultTypeAccount,
134
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
135
+ vault: vaultPublickey,
136
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
137
+ history: historyPublicKey,
138
+ feePool: hedgeStakingPoolPublicKey,
139
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
140
+ ushMint: ushMintPublickey,
141
+ payer: payerPublicKey,
142
+ payerUshAccount: payerUshAccount,
143
+ destinationTokenAccount: destinationTokenAccount,
144
+ oldSmallerVaultInfo: oldSmallerPublicKey,
145
+ newSmallerVaultInfo: newSmallerPublicKey,
146
+ newLargerVaultInfo: newLargerPublicKey,
147
+ tokenProgram: TOKEN_PROGRAM_ID,
148
+ systemProgram: SystemProgram.programId,
91
149
  })
150
+ .instruction()
92
151
  }
@@ -1,9 +1,18 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
2
+ import {
3
+ LAMPORTS_PER_SOL,
4
+ PublicKey,
5
+ sendAndConfirmTransaction,
6
+ Signer,
7
+ SystemProgram,
8
+ Transaction,
9
+ TransactionInstruction,
10
+ } from '@solana/web3.js'
11
+ import { Vault } from 'idl/vault'
3
12
  import { HEDGE_PROGRAM_PUBLICKEY } from '../Constants'
4
13
 
5
- export async function refreshOraclePrice (
6
- program: Program,
14
+ export async function refreshOraclePrice(
15
+ program: Program<Vault>,
7
16
  provider: Provider,
8
17
  payer: Signer,
9
18
  collateralType: string,
@@ -12,63 +21,63 @@ export async function refreshOraclePrice (
12
21
  overrideTime?: number
13
22
  ): Promise<string> {
14
23
  const transaction = new Transaction().add(
15
- await refreshOraclePriceInstruction(
16
- program,
17
- collateralType,
18
- network,
19
- overridePrice,
20
- overrideTime
21
- )
24
+ await refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime)
22
25
  )
23
- return await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts)
26
+ return await sendAndConfirmTransaction(provider.connection, transaction, [payer])
24
27
  }
25
28
 
26
- export async function refreshOraclePriceInstruction (
27
- program: Program,
29
+ export async function refreshOraclePriceInstruction(
30
+ program: Program<Vault>,
28
31
  collateralType: string,
29
32
  network: Cluster,
30
33
  overridePrice?: number,
31
34
  overrideTime?: number
32
35
  ): Promise<TransactionInstruction> {
33
36
  const enc = new TextEncoder()
34
- const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
35
- const [vaultTypeAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
37
+ const [oracleInfoAccount] = await PublicKey.findProgramAddress(
38
+ [enc.encode(collateralType), enc.encode('Oracle')],
39
+ HEDGE_PROGRAM_PUBLICKEY
40
+ )
41
+ const [vaultTypeAccount] = await PublicKey.findProgramAddress(
42
+ [enc.encode(collateralType), enc.encode('State')],
43
+ HEDGE_PROGRAM_PUBLICKEY
44
+ )
36
45
 
37
- return program.instruction.refreshOraclePrice(
38
- new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
39
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)-1), // override override time
40
- {
41
- accounts: {
42
- oracleInfoAccount: oracleInfoAccount,
43
- vaultTypeAccount: vaultTypeAccount,
44
- oracleChainlink: chainlinkAccunts[network],
45
- oraclePyth: pythAccounts[network],
46
- oracleSwitchboard: switchboardAccounts[network],
47
- systemProgram: SystemProgram.programId
48
- },
49
- signers: []
46
+ return await program.methods
47
+ .refreshOraclePrice(
48
+ new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
49
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000) - 1) // override override time
50
+ )
51
+ .accounts({
52
+ oracleInfoAccount: oracleInfoAccount,
53
+ vaultTypeAccount: vaultTypeAccount,
54
+ oracleChainlink: chainlinkAccunts[network],
55
+ oraclePyth: pythAccounts[network],
56
+ oracleSwitchboard: switchboardAccounts[network],
57
+ systemProgram: SystemProgram.programId,
50
58
  })
59
+ .instruction()
51
60
  }
52
61
 
53
62
  enum Cluster {
54
63
  Testing = 'Testing',
55
64
  Devnet = 'Devnet',
56
- MainnetBeta = 'MainnetBeta'
65
+ MainnetBeta = 'MainnetBeta',
57
66
  }
58
67
 
59
68
  const pythAccounts = {
60
69
  Testing: SystemProgram.programId,
61
70
  Devnet: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
62
- MainnetBeta: new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG')
71
+ MainnetBeta: new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG'),
63
72
  }
64
73
  const chainlinkAccunts = {
65
74
  Testing: SystemProgram.programId,
66
75
  Devnet: new PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
67
- MainnetBeta: SystemProgram.programId // CHAINLINK NOT ON MAINNET YET
76
+ MainnetBeta: SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
68
77
  }
69
78
  const switchboardAccounts = {
70
79
  Testing: SystemProgram.programId,
71
80
  // Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
72
81
  Devnet: new PublicKey('DpoK8Zz69APV9ntjuY9C4LZCxANYMV56M2cbXEdkjxME'),
73
- MainnetBeta: SystemProgram.programId // Switchboard V2 NOT ON MAINNET YET
82
+ MainnetBeta: SystemProgram.programId, // Switchboard V2 NOT ON MAINNET YET
74
83
  }
@@ -1,10 +1,27 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
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, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getHedgeMintPublicKey,
16
+ getPoolPublicKeyForMint,
17
+ getVaultTypeAccountPublicKey,
18
+ getUshMintPublicKey,
19
+ getVaultSystemStatePublicKey,
20
+ } from '../Constants'
21
+ import { Vault } from 'idl/vault'
5
22
 
6
- export async function repayVault (
7
- program: Program,
23
+ export async function repayVault(
24
+ program: Program<Vault>,
8
25
  provider: Provider,
9
26
  payer: Signer,
10
27
  vaultPublicKey: PublicKey,
@@ -14,14 +31,33 @@ export async function repayVault (
14
31
  const ushMintPublickey = await getUshMintPublicKey()
15
32
 
16
33
  // Prep the user to get USH back out at some point
17
- const payerUshAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
34
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
35
+ provider.connection,
36
+ payer,
37
+ ushMintPublickey,
38
+ payer.publicKey
39
+ )
18
40
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
19
41
 
20
42
  const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
21
43
  const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
22
- const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint)
44
+ const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
45
+ vaultTypeAccountPublicKey,
46
+ vaultTypeAccount.collateralMint
47
+ )
23
48
  const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
24
49
 
50
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
51
+ program,
52
+ provider,
53
+ vaultTypeAccountPublicKey,
54
+ vaultPublicKey,
55
+ 0,
56
+ repayAmount * -1,
57
+ false,
58
+ false
59
+ )
60
+
25
61
  const history = Keypair.generate()
26
62
  const transaction = new Transaction().add(
27
63
  await repayVaultInstruction(
@@ -33,16 +69,19 @@ export async function repayVault (
33
69
  history.publicKey,
34
70
  vaultTypeAccountPublicKey,
35
71
  vaultTypeAssociatedTokenAccount,
72
+ oldSmallerPublicKey,
73
+ newSmallerPublicKey,
74
+ newLargerPublicKey,
36
75
  repayAmount,
37
76
  overrideTime
38
77
  )
39
78
  )
40
- await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
79
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
41
80
  return vaultPublicKey
42
81
  }
43
82
 
44
- export async function repayVaultInstruction (
45
- program: Program,
83
+ export async function repayVaultInstruction(
84
+ program: Program<Vault>,
46
85
  payerPublicKey: PublicKey,
47
86
  ownerUshAccount: PublicKey,
48
87
  vaultPublickey: PublicKey,
@@ -50,6 +89,9 @@ export async function repayVaultInstruction (
50
89
  historyPublicKey: PublicKey,
51
90
  vaultTypeAccount: PublicKey,
52
91
  vaultTypeAssociatedTokenAccount: PublicKey,
92
+ oldSmallerPublicKey: PublicKey,
93
+ newSmallerPublicKey: PublicKey,
94
+ newLargerPublicKey: PublicKey,
53
95
  repayAmount: number,
54
96
  overrideTime?: number
55
97
  ): Promise<TransactionInstruction> {
@@ -62,25 +104,28 @@ export async function repayVaultInstruction (
62
104
  ushMintPublickey
63
105
  )
64
106
 
65
- return program.instruction.repayVault(
66
- new BN(repayAmount),
67
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
68
- {
69
- accounts: {
70
- vaultSystemState: vaultSystemStatePublicKey,
71
- vaultTypeAccount: vaultTypeAccount,
72
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
73
- vaultAccount: vaultPublickey,
74
- vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
75
- history: historyPublicKey,
76
- feePool: hedgeStakingPoolPublicKey,
77
- feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
78
- ushMint: ushMintPublickey,
79
- vaultOwner: payerPublicKey,
80
- ownerUshAccount: ownerUshAccount,
81
- tokenProgram: TOKEN_PROGRAM_ID,
82
- systemProgram: SystemProgram.programId
83
- },
84
- signers: []
107
+ return await program.methods
108
+ .repayVault(
109
+ new BN(repayAmount),
110
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
111
+ )
112
+ .accounts({
113
+ vaultSystemState: vaultSystemStatePublicKey,
114
+ vaultTypeAccount: vaultTypeAccount,
115
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
116
+ vaultAccount: vaultPublickey,
117
+ vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
118
+ history: historyPublicKey,
119
+ feePool: hedgeStakingPoolPublicKey,
120
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
121
+ ushMint: ushMintPublickey,
122
+ vaultOwner: payerPublicKey,
123
+ oldSmallerVaultInfo: oldSmallerPublicKey,
124
+ newSmallerVaultInfo: newSmallerPublicKey,
125
+ newLargerVaultInfo: newLargerPublicKey,
126
+ ownerUshAccount: ownerUshAccount,
127
+ tokenProgram: TOKEN_PROGRAM_ID,
128
+ systemProgram: SystemProgram.programId,
85
129
  })
130
+ .instruction()
86
131
  }
@@ -1,48 +1,56 @@
1
1
  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
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
5
- import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
4
+ import {
5
+ Keypair,
6
+ PublicKey,
7
+ sendAndConfirmTransaction,
8
+ Signer,
9
+ SystemProgram,
10
+ SYSVAR_RENT_PUBKEY,
11
+ Transaction,
12
+ TransactionInstruction,
13
+ } from '@solana/web3.js'
14
+ import {
15
+ findAssociatedTokenAddress,
16
+ findVaultAddress,
17
+ getVaultTypeAccountPublicKey,
18
+ getUshMintPublicKey,
19
+ getVaultSystemStatePublicKey,
20
+ getPoolPublicKeyForMint,
21
+ getHedgeMintPublicKey,
22
+ } from '../Constants'
6
23
 
7
- import { v4 as uuidv4 } from 'uuid'
8
24
  import { parseAnchorErrors } from '../utils/Errors'
25
+ import { Vault } from 'idl/vault'
9
26
 
10
- export async function setHalted (
11
- program: Program,
27
+ export async function setHalted(
28
+ program: Program<Vault>,
12
29
  provider: Provider,
13
30
  payer: Signer,
14
31
  halted: boolean
15
32
  ): Promise<PublicKey> {
16
33
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
17
34
 
18
-
19
35
  const transaction = new Transaction().add(
20
- await setHaltedInstruction(
21
- program,
22
- vaultSystemStatePublicKey,
23
- payer.publicKey,
24
- halted
25
- )
36
+ await setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted)
26
37
  )
27
38
 
28
- await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider?.opts).catch(parseAnchorErrors)
39
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
29
40
  return vaultSystemStatePublicKey
30
41
  }
31
42
 
32
- export async function setHaltedInstruction (
33
- program: Program,
43
+ export async function setHaltedInstruction(
44
+ program: Program<Vault>,
34
45
  vaultSystemStatePublicKey: PublicKey,
35
46
  payerPublicKey: PublicKey,
36
47
  halted: boolean
37
48
  ): Promise<TransactionInstruction> {
38
- const ix = program.instruction.setHalted(
39
- halted,
40
- {
41
- accounts: {
42
- payer: payerPublicKey,
43
- vaultSystemState: vaultSystemStatePublicKey,
44
- },
45
- signers: []
49
+ return await program.methods
50
+ .setHalted(halted)
51
+ .accounts({
52
+ payer: payerPublicKey,
53
+ vaultSystemState: vaultSystemStatePublicKey,
46
54
  })
47
- return ix
55
+ .instruction()
48
56
  }