hedge-web3 0.1.29 → 0.1.31

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 (77) hide show
  1. package/declarations/idl/vault.d.ts +99 -99
  2. package/declarations/index.d.ts +1 -0
  3. package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
  4. package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
  5. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
  6. package/declarations/instructions/createStakingPool.d.ts +3 -2
  7. package/declarations/instructions/createVault.d.ts +6 -5
  8. package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
  9. package/declarations/instructions/depositStakingPool.d.ts +3 -2
  10. package/declarations/instructions/depositVault.d.ts +3 -2
  11. package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
  12. package/declarations/instructions/liquidateVault.d.ts +3 -2
  13. package/declarations/instructions/loanVault.d.ts +3 -2
  14. package/declarations/instructions/redeemVault.d.ts +3 -2
  15. package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
  16. package/declarations/instructions/repayVault.d.ts +3 -2
  17. package/declarations/instructions/setHalted.d.ts +3 -2
  18. package/declarations/instructions/setVaultTypeStatus.d.ts +3 -2
  19. package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
  20. package/declarations/instructions/withdrawVault.d.ts +3 -2
  21. package/declarations/state/VaultAccount.d.ts +1 -1
  22. package/declarations/utils/getLinkedListAccounts.d.ts +3 -1
  23. package/lib/idl/vault.js +99 -99
  24. package/lib/index.js +1 -0
  25. package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
  26. package/lib/instructions/claimStakingPoolPosition.js +19 -19
  27. package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
  28. package/lib/instructions/createStakingPool.js +17 -18
  29. package/lib/instructions/createVault.js +28 -31
  30. package/lib/instructions/depositLiquidationPool.js +17 -18
  31. package/lib/instructions/depositStakingPool.js +16 -18
  32. package/lib/instructions/depositVault.js +25 -27
  33. package/lib/instructions/initHedgeFoundation.js +17 -19
  34. package/lib/instructions/initHedgeFoundationTokens.js +15 -15
  35. package/lib/instructions/liquidateVault.js +32 -33
  36. package/lib/instructions/loanVault.js +23 -23
  37. package/lib/instructions/redeemVault.js +24 -24
  38. package/lib/instructions/refreshOraclePrice.js +17 -17
  39. package/lib/instructions/repayVault.js +23 -23
  40. package/lib/instructions/setHalted.js +8 -9
  41. package/lib/instructions/setVaultTypeStatus.js +9 -10
  42. package/lib/instructions/withdrawStakingPool.js +22 -24
  43. package/lib/instructions/withdrawVault.js +23 -23
  44. package/lib/state/LiquidationPoolEra.js +3 -1
  45. package/lib/state/LiquidationPosition.js +0 -7
  46. package/lib/state/StakingPool.js +3 -4
  47. package/lib/state/VaultAccount.js +2 -5
  48. package/lib/utils/getLinkedListAccounts.js +24 -16
  49. package/package.json +2 -2
  50. package/src/idl/vault.ts +198 -198
  51. package/src/index.ts +1 -0
  52. package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
  53. package/src/instructions/claimStakingPoolPosition.ts +45 -25
  54. package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
  55. package/src/instructions/createStakingPool.ts +37 -35
  56. package/src/instructions/createVault.ts +81 -125
  57. package/src/instructions/depositLiquidationPool.ts +45 -26
  58. package/src/instructions/depositStakingPool.ts +32 -24
  59. package/src/instructions/depositVault.ts +57 -86
  60. package/src/instructions/initHedgeFoundation.ts +42 -43
  61. package/src/instructions/initHedgeFoundationTokens.ts +38 -39
  62. package/src/instructions/liquidateVault.ts +42 -65
  63. package/src/instructions/loanVault.ts +51 -69
  64. package/src/instructions/redeemVault.ts +83 -47
  65. package/src/instructions/refreshOraclePrice.ts +41 -32
  66. package/src/instructions/repayVault.ts +45 -65
  67. package/src/instructions/setHalted.ts +32 -24
  68. package/src/instructions/setVaultTypeStatus.ts +32 -24
  69. package/src/instructions/withdrawStakingPool.ts +44 -30
  70. package/src/instructions/withdrawVault.ts +58 -82
  71. package/src/state/LiquidationPoolEra.ts +4 -3
  72. package/src/state/LiquidationPosition.ts +0 -27
  73. package/src/state/StakingPool.ts +4 -7
  74. package/src/state/StakingPoolPosition.ts +2 -3
  75. package/src/state/VaultAccount.ts +9 -28
  76. package/src/state/VaultHistoryEvent.ts +1 -2
  77. package/src/utils/getLinkedListAccounts.ts +31 -30
@@ -1,8 +1,5 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import {
3
- getOrCreateAssociatedTokenAccount,
4
- TOKEN_PROGRAM_ID,
5
- } from '@solana/spl-token'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
6
3
  import {
7
4
  Keypair,
8
5
  PublicKey,
@@ -21,9 +18,10 @@ import {
21
18
  getUshMintPublicKey,
22
19
  getVaultSystemStatePublicKey,
23
20
  } from '../Constants'
21
+ import { Vault } from 'idl/vault'
24
22
 
25
23
  export async function repayVault(
26
- program: Program,
24
+ program: Program<Vault>,
27
25
  provider: Provider,
28
26
  payer: Signer,
29
27
  vaultPublicKey: PublicKey,
@@ -41,33 +39,25 @@ export async function repayVault(
41
39
  )
42
40
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
43
41
 
44
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
45
- vaultAccount.collateralType
46
- )
47
- const vaultTypeAccount = await program.account.vaultType.fetch(
48
- vaultTypeAccountPublicKey
49
- )
42
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
43
+ const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
50
44
  const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
51
45
  vaultTypeAccountPublicKey,
52
46
  vaultTypeAccount.collateralMint
53
47
  )
54
- const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
48
+ const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
49
+
50
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
51
+ program,
52
+ provider,
53
+ vaultTypeAccountPublicKey,
55
54
  vaultPublicKey,
56
- vaultTypeAccount.collateralMint
55
+ 0,
56
+ repayAmount * -1,
57
+ false,
58
+ false
57
59
  )
58
60
 
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
- )
70
-
71
61
  const history = Keypair.generate()
72
62
  const transaction = new Transaction().add(
73
63
  await repayVaultInstruction(
@@ -86,17 +76,12 @@ export async function repayVault(
86
76
  overrideTime
87
77
  )
88
78
  )
89
- await sendAndConfirmTransaction(
90
- provider.connection,
91
- transaction,
92
- [payer, history],
93
- provider.opts
94
- )
79
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
95
80
  return vaultPublicKey
96
81
  }
97
82
 
98
83
  export async function repayVaultInstruction(
99
- program: Program,
84
+ program: Program<Vault>,
100
85
  payerPublicKey: PublicKey,
101
86
  ownerUshAccount: PublicKey,
102
87
  vaultPublickey: PublicKey,
@@ -113,39 +98,34 @@ export async function repayVaultInstruction(
113
98
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
114
99
  const ushMintPublickey = await getUshMintPublicKey()
115
100
  const hedgeMintPublickey = await getHedgeMintPublicKey()
116
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
117
- hedgeMintPublickey
101
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
102
+ const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
103
+ hedgeStakingPoolPublicKey,
104
+ ushMintPublickey
118
105
  )
119
- const hedgeStakingPoolAssociatedUshTokenAccount =
120
- await findAssociatedTokenAddress(
121
- hedgeStakingPoolPublicKey,
122
- ushMintPublickey
123
- )
124
106
 
125
- return program.instruction.repayVault(
126
- new BN(repayAmount),
127
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
128
- {
129
- accounts: {
130
- vaultSystemState: vaultSystemStatePublicKey,
131
- vaultTypeAccount: vaultTypeAccount,
132
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
133
- vaultAccount: vaultPublickey,
134
- vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
135
- history: historyPublicKey,
136
- feePool: hedgeStakingPoolPublicKey,
137
- feePoolAssociatedUshTokenAccount:
138
- hedgeStakingPoolAssociatedUshTokenAccount,
139
- ushMint: ushMintPublickey,
140
- vaultOwner: payerPublicKey,
141
- oldSmallerVaultInfo: oldSmallerPublicKey,
142
- newSmallerVaultInfo: newSmallerPublicKey,
143
- newLargerVaultInfo: newLargerPublicKey,
144
- ownerUshAccount: ownerUshAccount,
145
- tokenProgram: TOKEN_PROGRAM_ID,
146
- systemProgram: SystemProgram.programId,
147
- },
148
- signers: [],
149
- }
150
- )
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,
129
+ })
130
+ .instruction()
151
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
  }
@@ -1,12 +1,30 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
3
- import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
2
+ import {
3
+ Keypair,
4
+ PublicKey,
5
+ sendAndConfirmTransaction,
6
+ Signer,
7
+ SystemProgram,
8
+ SYSVAR_RENT_PUBKEY,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import {
13
+ findAssociatedTokenAddress,
14
+ findVaultAddress,
15
+ getVaultTypeAccountPublicKey,
16
+ getUshMintPublicKey,
17
+ getVaultSystemStatePublicKey,
18
+ getPoolPublicKeyForMint,
19
+ getHedgeMintPublicKey,
20
+ } from '../Constants'
4
21
 
5
22
  import { v4 as uuidv4 } from 'uuid'
6
23
  import { parseAnchorErrors } from '../utils/Errors'
24
+ import { Vault } from 'idl/vault'
7
25
 
8
- export async function setVaultTypeStatus (
9
- program: Program,
26
+ export async function setVaultTypeStatus(
27
+ program: Program<Vault>,
10
28
  provider: Provider,
11
29
  payer: Signer,
12
30
  vaultTypeAccount: PublicKey,
@@ -14,37 +32,27 @@ export async function setVaultTypeStatus (
14
32
  ): Promise<PublicKey> {
15
33
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
16
34
 
17
-
18
35
  const transaction = new Transaction().add(
19
- await setVaultTypeStatusInstruction(
20
- program,
21
- vaultSystemStatePublicKey,
22
- payer.publicKey,
23
- vaultTypeAccount,
24
- deprecated,
25
- )
36
+ await setVaultTypeStatusInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, deprecated)
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
43
  export async function setVaultTypeStatusInstruction(
33
- program: Program,
44
+ program: Program<Vault>,
34
45
  vaultSystemStatePublicKey: PublicKey,
35
46
  payerPublicKey: PublicKey,
36
47
  vaultTypeAccount: PublicKey,
37
48
  deprecated: boolean
38
49
  ): Promise<TransactionInstruction> {
39
- const ix = program.instruction.setVaultTypeStatus(
40
- deprecated,
41
- {
42
- accounts: {
43
- payer: payerPublicKey,
44
- vaultSystemState: vaultSystemStatePublicKey,
45
- vaultType: vaultTypeAccount,
46
- },
47
- signers: []
50
+ return await program.methods
51
+ .setVaultTypeStatus(deprecated)
52
+ .accounts({
53
+ payer: payerPublicKey,
54
+ vaultSystemState: vaultSystemStatePublicKey,
55
+ vaultType: vaultTypeAccount,
48
56
  })
49
- return ix
57
+ .instruction()
50
58
  }
@@ -1,11 +1,27 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
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'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ SYSVAR_RENT_PUBKEY,
10
+ Transaction,
11
+ TransactionInstruction,
12
+ } from '@solana/web3.js'
4
13
  import { parseAnchorErrors } from '../utils/Errors'
5
- import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
14
+ import {
15
+ findAssociatedTokenAddress,
16
+ getHedgeMintPublicKey,
17
+ getPoolPublicKeyForMint,
18
+ getUshMintPublicKey,
19
+ getVaultSystemStatePublicKey,
20
+ } from '../Constants'
21
+ import { Vault } from 'idl/vault'
6
22
 
7
- export async function withdrawStakingPool (
8
- program: Program,
23
+ export async function withdrawStakingPool(
24
+ program: Program<Vault>,
9
25
  provider: Provider,
10
26
  payer: Signer,
11
27
  poolPositionPublicKey: PublicKey,
@@ -22,12 +38,12 @@ export async function withdrawStakingPool (
22
38
  overrideStartTime
23
39
  )
24
40
  )
25
- await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
41
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
26
42
  return poolPosition.publicKey
27
43
  }
28
44
 
29
- export async function withdrawStakingPoolInstruction (
30
- program: Program,
45
+ export async function withdrawStakingPoolInstruction(
46
+ program: Program<Vault>,
31
47
  payerPublicKey: PublicKey,
32
48
  poolPositionPublicKey: PublicKey,
33
49
  stakedTokenMintPublicKey: PublicKey,
@@ -44,28 +60,26 @@ export async function withdrawStakingPoolInstruction (
44
60
  const payerAssociatedUshAccount = await findAssociatedTokenAddress(payerPublicKey, ushMintPublickey)
45
61
  const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
46
62
 
47
- return program.instruction.withdrawStakingPool(
48
- new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
49
- {
50
- accounts: {
51
- payer: payerPublicKey,
52
- vaultSystemState: vaultSystemStatePublicKey,
53
- pool: poolPublickey,
54
- poolPosition: poolPositionPublicKey,
55
- poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
56
- poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
57
- payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
58
- payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
59
- payerAssociatedUshAccount: payerAssociatedUshAccount,
60
- communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
61
- hedgeMint: hedgeMintPublickey,
62
- stakedTokenMint: stakedTokenMintPublicKey,
63
- ushMint: ushMintPublickey,
64
- rent: SYSVAR_RENT_PUBKEY,
65
- tokenProgram: TOKEN_PROGRAM_ID,
66
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
67
- systemProgram: SystemProgram.programId
68
- },
69
- signers: []
63
+ return await program.methods
64
+ .withdrawStakingPool(
65
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
66
+ )
67
+ .accounts({
68
+ payer: payerPublicKey,
69
+ vaultSystemState: vaultSystemStatePublicKey,
70
+ pool: poolPublickey,
71
+ poolPosition: poolPositionPublicKey,
72
+ poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
73
+ poolAssociatedUshTokenAccount: poolAssociatedUshTokenAccount,
74
+ payerAssociatedStakedTokenAccount: payerAssociatedStakedTokenAccount,
75
+ payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
76
+ payerAssociatedUshAccount: payerAssociatedUshAccount,
77
+ communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
78
+ hedgeMint: hedgeMintPublickey,
79
+ stakedTokenMint: stakedTokenMintPublicKey,
80
+ ushMint: ushMintPublickey,
81
+ tokenProgram: TOKEN_PROGRAM_ID,
82
+ systemProgram: SystemProgram.programId,
70
83
  })
84
+ .instruction()
71
85
  }
@@ -1,8 +1,5 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
- import {
3
- getOrCreateAssociatedTokenAccount,
4
- TOKEN_PROGRAM_ID,
5
- } from '@solana/spl-token'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
6
3
  import { TokenInstructions } from '@project-serum/serum'
7
4
  import {
8
5
  Keypair,
@@ -22,9 +19,10 @@ import {
22
19
  getHedgeMintPublicKey,
23
20
  } from '../Constants'
24
21
  import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
22
+ import { Vault } from 'idl/vault'
25
23
 
26
24
  export async function withdrawVault(
27
- program: Program,
25
+ program: Program<Vault>,
28
26
  provider: Provider,
29
27
  payer: Signer,
30
28
  vaultPublicKey: PublicKey,
@@ -34,39 +32,28 @@ export async function withdrawVault(
34
32
  const ushMintPublickey = await getUshMintPublicKey()
35
33
 
36
34
  // Prep the user to get USH back out at some point
37
- await getOrCreateAssociatedTokenAccount(
38
- provider.connection,
39
- payer,
40
- ushMintPublickey,
41
- payer.publicKey
42
- )
35
+ await getOrCreateAssociatedTokenAccount(provider.connection, payer, ushMintPublickey, payer.publicKey)
43
36
 
44
37
  const history = Keypair.generate()
45
38
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
46
39
  const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
47
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
48
- vaultAccount.collateralType
40
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
41
+ const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
42
+ provider.connection,
43
+ payer,
44
+ TokenInstructions.WRAPPED_SOL_MINT,
45
+ vaultPublicKey,
46
+ true
49
47
  )
50
- const vaultAssociatedCollateralAccount =
51
- await getOrCreateAssociatedTokenAccount(
52
- provider.connection,
53
- payer,
54
- TokenInstructions.WRAPPED_SOL_MINT,
55
- vaultPublicKey,
56
- true
57
- )
58
48
 
59
- const vaultTypeAccountInfo = await program.account.vaultType.fetch(
60
- vaultTypeAccountPublicKey
49
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
50
+ const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
51
+ provider.connection,
52
+ payer,
53
+ vaultTypeAccountInfo.collateralMint,
54
+ vaultTypeAccountPublicKey,
55
+ true
61
56
  )
62
- const vaultTypeAssociatedTokenAccount =
63
- await getOrCreateAssociatedTokenAccount(
64
- provider.connection,
65
- payer,
66
- vaultTypeAccountInfo.collateralMint,
67
- vaultTypeAccountPublicKey,
68
- true
69
- )
70
57
 
71
58
  const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
72
59
  provider.connection,
@@ -74,26 +61,22 @@ export async function withdrawVault(
74
61
  vaultTypeAccountInfo.collateralMint,
75
62
  payer.publicKey
76
63
  )
77
- const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
78
- await getHedgeMintPublicKey()
64
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
65
+ const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
66
+ hedgeStakingPoolPublicKey,
67
+ ushMintPublickey
79
68
  )
80
- const hedgeStakingPoolAssociatedUshTokenAccount =
81
- await findAssociatedTokenAddress(
82
- hedgeStakingPoolPublicKey,
83
- ushMintPublickey
84
- )
85
69
 
86
- const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
87
- await getLinkedListAccounts(
88
- program,
89
- provider,
90
- vaultTypeAccountPublicKey,
91
- vaultPublicKey,
92
- withdrawAmount * -1,
93
- 0,
94
- false,
95
- false
96
- )
70
+ const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
71
+ program,
72
+ provider,
73
+ vaultTypeAccountPublicKey,
74
+ vaultPublicKey,
75
+ withdrawAmount * -1,
76
+ 0,
77
+ false,
78
+ false
79
+ )
97
80
 
98
81
  const transaction = new Transaction().add(
99
82
  await withdrawVaultInstruction(
@@ -116,17 +99,12 @@ export async function withdrawVault(
116
99
  overrideTime
117
100
  )
118
101
  )
119
- await sendAndConfirmTransaction(
120
- provider.connection,
121
- transaction,
122
- [payer, history],
123
- provider.opts
124
- )
102
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
125
103
  return vaultPublicKey
126
104
  }
127
105
 
128
106
  export async function withdrawVaultInstruction(
129
- program: Program,
107
+ program: Program<Vault>,
130
108
  vaultSystemStatePublicKey: PublicKey,
131
109
  payerPublicKey: PublicKey,
132
110
  destinationTokenAccount: PublicKey,
@@ -144,30 +122,28 @@ export async function withdrawVaultInstruction(
144
122
  withdrawAmount: number,
145
123
  overrideTime?: number
146
124
  ): Promise<TransactionInstruction> {
147
- return program.instruction.withdrawVault(
148
- new BN(withdrawAmount),
149
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
150
- {
151
- accounts: {
152
- vaultSystemState: vaultSystemStatePublicKey,
153
- vaultTypeAccount: vaultTypeAccount,
154
- vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
155
- vault: vaultPublickey,
156
- vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
157
- feePool: hedgeStakingPoolPublicKey,
158
- feePoolAssociatedUshTokenAccount:
159
- hedgeStakingPoolAssociatedUshTokenAccount,
160
- ushMint: ushMint,
161
- history: historyPublicKey,
162
- vaultOwner: payerPublicKey,
163
- destinationTokenAccount: destinationTokenAccount,
164
- oldSmallerVaultInfo: oldSmallerPublicKey,
165
- newSmallerVaultInfo: newSmallerPublicKey,
166
- newLargerVaultInfo: newLargerPublicKey,
167
- tokenProgram: TOKEN_PROGRAM_ID,
168
- systemProgram: SystemProgram.programId,
169
- },
170
- signers: [],
171
- }
172
- )
125
+ return await program.methods
126
+ .withdrawVault(
127
+ new BN(withdrawAmount),
128
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
129
+ )
130
+ .accounts({
131
+ vaultSystemState: vaultSystemStatePublicKey,
132
+ vaultTypeAccount: vaultTypeAccount,
133
+ vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
134
+ vault: vaultPublickey,
135
+ vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
136
+ feePool: hedgeStakingPoolPublicKey,
137
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
138
+ ushMint: ushMint,
139
+ history: historyPublicKey,
140
+ vaultOwner: payerPublicKey,
141
+ destinationTokenAccount: destinationTokenAccount,
142
+ oldSmallerVaultInfo: oldSmallerPublicKey,
143
+ newSmallerVaultInfo: newSmallerPublicKey,
144
+ newLargerVaultInfo: newLargerPublicKey,
145
+ tokenProgram: TOKEN_PROGRAM_ID,
146
+ systemProgram: SystemProgram.programId,
147
+ })
148
+ .instruction()
173
149
  }
@@ -1,4 +1,3 @@
1
-
2
1
  import Decimal from 'decimal.js'
3
2
  import { DecimalFromU128 } from '../HedgeDecimal'
4
3
 
@@ -13,10 +12,12 @@ export class LiquidationPoolEra {
13
12
  public hedgeRewardsAccumulator: Decimal
14
13
  public hedgeRewardsTimestamp: number
15
14
 
16
- constructor (public liquidyPoolEra: any) {
15
+ constructor(public liquidyPoolEra: any) {
17
16
  this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
18
17
  this.product = DecimalFromU128(liquidyPoolEra.productBytes)
19
- this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => { return DecimalFromU128(sumBytes) })
18
+ this.sum = liquidyPoolEra.sumBytes.map((sumBytes: number) => {
19
+ return DecimalFromU128(sumBytes)
20
+ })
20
21
  this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
21
22
  this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
22
23
  }
@@ -47,31 +47,4 @@ export class LiquidationPosition {
47
47
  public getTokensAvailable (era: LiquidationPoolEra, index: number): Decimal {
48
48
  return era.sum[index].minus(this.sumSnapshotsEntry[index]).div(this.productSnapshotEntry).mul(new Decimal(this.deposit)).floor()
49
49
  }
50
-
51
- // public getHedgeAvailable (): Decimal {
52
- // const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
53
- // const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
54
- // LiquidationPoolTotalSupply,
55
- // this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
56
- // this.era.hedgeRewardsTimestamp * 1000,
57
- // Date.now(),
58
- // 365 * 1000)
59
-
60
- // if (this.era.totalDeposits === 0) {
61
- // return new Decimal(0)
62
- // }
63
-
64
- // const rewardsPerToken = this.era.product.mul(new Decimal(hedgeRewardsSinceLastUpdate / this.era.totalDeposits))
65
- // const newAccumulator = rewardsPerToken.add(new Decimal(this.era.hedgeRewardsAccumulator))
66
- // const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal(this.deposit)).div(new Decimal(this.productSnapshot))
67
- // return hedgeAvailable
68
- // }
69
50
  }
70
-
71
- // function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
72
- // const timeInOffsetStart = timeIn - birthTime
73
- // const timeOutOffsetStart = timeOut - birthTime
74
- // const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
75
- // const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
76
- // return awardedTokens
77
- // }