hedge-web3 0.2.20 → 0.2.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. package/declarations/Constants.d.ts +15 -0
  2. package/declarations/idl/pyth.d.ts +44 -24
  3. package/declarations/idl/switchboard.d.ts +47 -0
  4. package/declarations/idl/vault.d.ts +857 -157
  5. package/declarations/index.d.ts +26 -20
  6. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +2 -2
  7. package/declarations/instructions/createReferralAccount.d.ts +17 -0
  8. package/declarations/instructions/createUserReferralAccount.d.ts +5 -0
  9. package/declarations/instructions/depositLiquidationPool.d.ts +2 -2
  10. package/declarations/instructions/loanVault.d.ts +2 -2
  11. package/declarations/instructions/psmEditAccount.d.ts +12 -0
  12. package/declarations/instructions/psmMintUsh.d.ts +2 -2
  13. package/declarations/instructions/psmRedeemUsh.d.ts +2 -2
  14. package/declarations/instructions/referralClaimFees.d.ts +16 -0
  15. package/declarations/instructions/transferVault.d.ts +3 -17
  16. package/declarations/instructions/updateReferralAccount.d.ts +13 -0
  17. package/declarations/instructions/updateReferralState.d.ts +20 -0
  18. package/declarations/instructions/updateVaultType.d.ts +1 -0
  19. package/declarations/state/VaultAccount.d.ts +2 -1
  20. package/lib/Constants.js +36 -1
  21. package/lib/idl/pyth.js +44 -24
  22. package/lib/idl/switchboard.js +49 -0
  23. package/lib/idl/vault.js +850 -150
  24. package/lib/index.js +26 -20
  25. package/lib/instructions/closeLiquidationPoolPosition.js +16 -3
  26. package/lib/instructions/createReferralAccount.js +80 -0
  27. package/lib/instructions/createUserReferralAccount.js +61 -0
  28. package/lib/instructions/depositLiquidationPool.js +16 -5
  29. package/lib/instructions/loanVault.js +16 -3
  30. package/lib/instructions/psmEditAccount.js +60 -0
  31. package/lib/instructions/psmMintUsh.js +14 -3
  32. package/lib/instructions/psmRedeemUsh.js +14 -3
  33. package/lib/instructions/referralClaimFees.js +84 -0
  34. package/lib/instructions/transferVault.js +10 -24
  35. package/lib/instructions/updateReferralAccount.js +50 -0
  36. package/lib/instructions/updateReferralState.js +57 -0
  37. package/lib/instructions/updateVaultType.js +10 -9
  38. package/package.json +1 -1
  39. package/src/Constants.ts +42 -0
  40. package/src/idl/pyth.ts +88 -48
  41. package/src/idl/switchboard.ts +93 -0
  42. package/src/idl/vault.ts +1762 -362
  43. package/src/index.ts +28 -24
  44. package/src/instructions/closeLiquidationPoolPosition.ts +25 -1
  45. package/src/instructions/createReferralAccount.ts +127 -0
  46. package/src/instructions/createUserReferralAccount.ts +86 -0
  47. package/src/instructions/depositLiquidationPool.ts +24 -4
  48. package/src/instructions/loanVault.ts +20 -1
  49. package/src/instructions/{psmEditAccount.rs → psmEditAccount.ts} +0 -0
  50. package/src/instructions/psmMintUsh.ts +20 -1
  51. package/src/instructions/psmRedeemUsh.ts +20 -1
  52. package/src/instructions/referralClaimFees.ts +159 -0
  53. package/src/instructions/transferVault.ts +21 -58
  54. package/src/instructions/updateReferralAccount.ts +77 -0
  55. package/src/instructions/updateReferralState.ts +93 -0
  56. package/src/instructions/updateVaultType.ts +3 -0
  57. package/src/state/VaultAccount.ts +1 -1
@@ -0,0 +1,159 @@
1
+ import { BN, Idl, Program, Provider } from '@project-serum/anchor'
2
+ import { TokenInstructions } from '@project-serum/serum'
3
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
4
+ import {
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
+ getVaultSystemStatePublicKey,
16
+ getReferralAccountPublicKey,
17
+ getHedgeMintPublicKey,
18
+ getReferralStatePublicKey,
19
+ getUshMintPublicKey,
20
+ getPoolPublicKeyForMint
21
+ } from '../Constants'
22
+
23
+ import { parseAnchorErrors } from '../utils/Errors'
24
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
25
+ import { Vault } from '../idl/vault'
26
+
27
+
28
+
29
+ /** @type {Function} - Allows a referrer to claims their fees.
30
+ * This checks the user has enough HDG in their wallet or staked and
31
+ * then allows them to claim earned fees. If they meet the PSM referral threshold,
32
+ * they will be eligible for PSM cut.
33
+ * Params:
34
+ * - program: Program<Vault> : The program instance of Hedge Vault program
35
+ * - provider: Provider : Current connection
36
+ * - payer: Signer : who we are creating the referral account for
37
+ * - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
38
+ * - referrer: PublicKey
39
+ */
40
+ export async function referralClaimFees(
41
+ program: Program<Vault>,
42
+ provider: Provider,
43
+ payer: Signer,
44
+ poolPosition: PublicKey
45
+ ): Promise<PublicKey> {
46
+ // setup transaction
47
+ const transaction = new Transaction()
48
+ const signers = [payer]
49
+
50
+ // General variables
51
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
52
+
53
+ // Find referrer account
54
+ const referrer = await payer.publicKey
55
+ const referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
56
+
57
+
58
+ // Derive the user referral account public key
59
+ const referallStatePublicKey = await getReferralStatePublicKey()
60
+
61
+ const hedgeMintPublicKey = await getHedgeMintPublicKey()
62
+
63
+ // Get HDG account of signer
64
+ const payerHdgAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
65
+ provider.connection,
66
+ payer,
67
+ hedgeMintPublicKey,
68
+ payer.publicKey
69
+ )
70
+
71
+ const ushMintPublicKey = await getUshMintPublicKey()
72
+
73
+ // Get USH account of signer
74
+ const payerUshAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
75
+ provider.connection,
76
+ payer,
77
+ ushMintPublicKey,
78
+ payer.publicKey
79
+ )
80
+
81
+ // Get community_associated_hedge_token_account
82
+ const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(
83
+ provider.connection,
84
+ payer,
85
+ hedgeMintPublicKey,
86
+ vaultSystemStatePublicKey,
87
+ true
88
+ )
89
+
90
+ const [feePoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublicKey)
91
+
92
+ const feePoolAssociatedUshTokenAccount = await getOrCreateAssociatedTokenAccount(
93
+ provider.connection,
94
+ payer,
95
+ ushMintPublicKey,
96
+ feePoolPublicKey,
97
+ true
98
+ )
99
+ transaction.add(
100
+ await referralClaimFeesInstruction(
101
+ program,
102
+ payer.publicKey,
103
+ vaultSystemStatePublicKey,
104
+ poolPosition,
105
+ referralAccountPublicKey,
106
+ referallStatePublicKey,
107
+ hedgeMintPublicKey,
108
+ payerHdgAssociatedTokenAccount.address,
109
+ ushMintPublicKey,
110
+ payerUshAssociatedTokenAccount.address,
111
+ communityAssociatedHedgeTokenAccount.address,
112
+ feePoolPublicKey,
113
+ feePoolAssociatedUshTokenAccount.address
114
+ )
115
+ )
116
+
117
+
118
+ await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
119
+ return referralAccountPublicKey
120
+ }
121
+
122
+
123
+ export async function referralClaimFeesInstruction(
124
+ program: Program<Vault>,
125
+ payerPublicKey: PublicKey,
126
+ vaultSystemStatePublicKey: PublicKey,
127
+ poolPositionPublicKey: PublicKey,
128
+ referralAccountPublicKey: PublicKey,
129
+ referralStatePublicKey: PublicKey,
130
+ hedgeMintPublicKey: PublicKey,
131
+ hdgAssociatedTokenAccountPublicKey: PublicKey,
132
+ ushMintPublicKey: PublicKey,
133
+ ushAssociatedTokenAccountPublicKey: PublicKey,
134
+ communityAssociatedHedgeTokenAccountPublicKey: PublicKey,
135
+ feePoolPublicKey: PublicKey,
136
+ feePoolAssociatedUshTokenAccountPublicKey: PublicKey
137
+ ): Promise<TransactionInstruction> {
138
+
139
+ return await program.methods
140
+ .referralClaimFees()
141
+ .accounts({
142
+ signer: payerPublicKey,
143
+ vaultSystemState: vaultSystemStatePublicKey,
144
+ referralState: referralStatePublicKey,
145
+ referralAccount: referralAccountPublicKey,
146
+ poolPosition: poolPositionPublicKey,
147
+ hedgeMint: hedgeMintPublicKey,
148
+ signerHdgAta: hdgAssociatedTokenAccountPublicKey,
149
+ ushMint: ushMintPublicKey,
150
+ signerUshAta: ushAssociatedTokenAccountPublicKey,
151
+ communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccountPublicKey,
152
+ feePool: feePoolPublicKey,
153
+ feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccountPublicKey,
154
+ systemProgram: SystemProgram.programId,
155
+ tokenProgram: TOKEN_PROGRAM_ID,
156
+ rent: SYSVAR_RENT_PUBKEY,
157
+ })
158
+ .instruction()
159
+ }
@@ -9,91 +9,54 @@ import {
9
9
  Transaction,
10
10
  TransactionInstruction,
11
11
  } from '@solana/web3.js'
12
- import {
13
- findAssociatedTokenAddress,
14
- findVaultAddress,
15
- getVaultTypeAccountPublicKey,
16
- getUshMintPublicKey,
17
- getVaultSystemStatePublicKey,
18
- getPoolPublicKeyForMint,
19
- getHedgeMintPublicKey,
20
- } from '../Constants'
21
12
 
22
- import { v4 as uuidv4 } from 'uuid'
23
- import { parseAnchorErrors } from '../utils/Errors'
24
13
  import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
25
14
  import { Vault } from '../idl/vault'
26
15
 
27
- export interface VaultTypeConfig {
28
- maxDebtExtended?: BN
29
- minDebtPerVault?: BN
30
- loanInitFee?: BN
31
- emergencyModeThreshold?: BN
32
-
33
- oracleChainlink?: PublicKey
34
- oraclePyth?: PublicKey
35
- oracleSwitchboard?: PublicKey
36
-
37
- priorityPyth?: number
38
- priorityChainlink?: number
39
- prioritySwitchboard?: number
40
-
41
- deprecated?: boolean
42
- }
43
-
44
16
  export async function transferVault(
45
17
  program: Program<Vault>,
46
18
  provider: Provider,
47
19
  payer: Signer,
20
+ vaultPublicKey: PublicKey,
21
+ vaultSystemStatePublicKey: PublicKey,
48
22
  vaultTypeAccount: PublicKey,
49
- oracleInfoAccount: PublicKey,
50
- config: VaultTypeConfig
23
+ newOwner: PublicKey
51
24
  ): Promise<PublicKey> {
52
- const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
25
+ const history = Keypair.generate()
26
+
53
27
  const transaction = new Transaction().add(
54
28
  await transferVaultInstruction(
55
- program,
29
+ vaultPublicKey,
56
30
  vaultSystemStatePublicKey,
57
- payer.publicKey,
58
31
  vaultTypeAccount,
59
- oracleInfoAccount,
60
- config
32
+ history.publicKey,
33
+ payer.publicKey,
34
+ program,
35
+ newOwner
61
36
  )
62
37
  )
63
38
 
64
- await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
65
- return vaultSystemStatePublicKey
39
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
40
+ return vaultPublicKey
66
41
  }
67
42
 
68
43
  export async function transferVaultInstruction(
69
- program: Program<Vault>,
44
+ vaultPublickey: PublicKey,
70
45
  vaultSystemStatePublicKey: PublicKey,
71
- payerPublicKey: PublicKey,
72
46
  vaultTypeAccount: PublicKey,
73
- oracleInfoAccount: PublicKey,
74
- vaultTypeConfig: VaultTypeConfig
47
+ historyPublicKey: PublicKey,
48
+ vaultOwner: PublicKey,
49
+ program: Program<Vault>,
50
+ newOwner: PublicKey
75
51
  ): Promise<TransactionInstruction> {
76
- const config = {
77
- maxDebtExtended: vaultTypeConfig.maxDebtExtended ?? null,
78
- minDebtPerVault: vaultTypeConfig.minDebtPerVault ?? null,
79
- loanInitFee: vaultTypeConfig.loanInitFee ?? null,
80
- emergencyModeThreshold: vaultTypeConfig.emergencyModeThreshold ?? null,
81
- oracleChainlink: vaultTypeConfig.oracleChainlink ?? null,
82
- oraclePyth: vaultTypeConfig.oraclePyth ?? null,
83
- oracleSwitchboard: vaultTypeConfig.oracleSwitchboard ?? null,
84
- deprecated: vaultTypeConfig.deprecated ?? null,
85
- priorityPyth: vaultTypeConfig.priorityPyth ?? null,
86
- priorityChainlink: vaultTypeConfig.priorityChainlink ?? null,
87
- prioritySwitchboard: vaultTypeConfig.prioritySwitchboard ?? null,
88
- }
89
-
90
52
  return await program.methods
91
- .updateVaultType(config)
53
+ .transferVault(newOwner)
92
54
  .accounts({
93
- payer: payerPublicKey,
55
+ vault: vaultPublickey,
94
56
  vaultSystemState: vaultSystemStatePublicKey,
95
57
  vaultType: vaultTypeAccount,
96
- oracleInfoAccount: oracleInfoAccount,
58
+ history: historyPublicKey,
59
+ vaultOwner: vaultOwner,
97
60
  })
98
61
  .instruction()
99
62
  }
@@ -0,0 +1,77 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
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
+ getVaultSystemStatePublicKey,
14
+ getReferralAccountPublicKey,
15
+ } from '../Constants'
16
+
17
+ import { parseAnchorErrors } from '../utils/Errors'
18
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
19
+ import { Vault } from '../idl/vault'
20
+
21
+ export interface ReferralAccountConfig {
22
+ setupCut?: BN
23
+ referredUserDiscount?: BN
24
+ stabilityCut?: BN
25
+ psmCut?: BN
26
+
27
+ hdgThreshold?: BN
28
+ }
29
+
30
+ export async function updateReferralAccount(
31
+ program: Program<Vault>,
32
+ provider: Provider,
33
+ payer: Signer,
34
+ referrer: PublicKey,
35
+ config: ReferralAccountConfig
36
+ ): Promise<PublicKey> {
37
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
38
+ const referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
39
+ const transaction = new Transaction().add(
40
+ await updateReferralAccountInstruction(
41
+ program,
42
+ vaultSystemStatePublicKey,
43
+ referralAccountPublicKey,
44
+ payer.publicKey,
45
+ config
46
+ )
47
+ )
48
+
49
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
50
+ return referralAccountPublicKey
51
+ }
52
+
53
+ export async function updateReferralAccountInstruction(
54
+ program: Program<Vault>,
55
+ vaultSystemStatePublicKey: PublicKey,
56
+ referralAccountPublicKey: PublicKey,
57
+ payerPublicKey: PublicKey,
58
+ referralAccountConfig: ReferralAccountConfig
59
+ ): Promise<TransactionInstruction> {
60
+ const config = {
61
+ setupCut: referralAccountConfig.setupCut ?? null,
62
+ stabilityCut: referralAccountConfig.stabilityCut ?? null,
63
+ referredUserDiscount: referralAccountConfig.referredUserDiscount ?? null,
64
+ psmCut: referralAccountConfig.psmCut ?? null,
65
+ hdgThreshold: referralAccountConfig.hdgThreshold ?? null,
66
+ }
67
+
68
+ return await program.methods
69
+ .updateReferralAccount(config)
70
+ .accounts({
71
+ signer: payerPublicKey,
72
+ vaultSystemState: vaultSystemStatePublicKey,
73
+ referralAccount: referralAccountPublicKey,
74
+ systemProgram: SystemProgram.programId,
75
+ })
76
+ .instruction()
77
+ }
@@ -0,0 +1,93 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
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
+ getVaultSystemStatePublicKey,
14
+ getReferralStatePublicKey,
15
+ } from '../Constants'
16
+
17
+ import { parseAnchorErrors } from '../utils/Errors'
18
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
19
+ import { Vault } from '../idl/vault'
20
+
21
+ export interface ReferralStateConfig {
22
+ setupCut?: BN
23
+ referredUserDiscount?: BN
24
+ stabilityCut?: BN
25
+ psmCut?: BN
26
+
27
+ maxSetupCut?: BN
28
+ maxReferredDiscount?: BN
29
+ maxStabilityCut?: BN
30
+ maxPsmCut?: BN
31
+
32
+ hdgThreshold?: BN
33
+ hdgPsmThreshold?: BN
34
+
35
+ referralThreshold?: BN
36
+
37
+ referralEnabled?: boolean
38
+ }
39
+
40
+ export async function updateReferralState(
41
+ program: Program<Vault>,
42
+ provider: Provider,
43
+ payer: Signer,
44
+ config: ReferralStateConfig
45
+ ): Promise<PublicKey> {
46
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
47
+ const referralStatePublicKey = await getReferralStatePublicKey()
48
+ const transaction = new Transaction().add(
49
+ await updateReferralStateInstruction(
50
+ program,
51
+ vaultSystemStatePublicKey,
52
+ referralStatePublicKey,
53
+ payer.publicKey,
54
+ config
55
+ )
56
+ )
57
+
58
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
59
+ return referralStatePublicKey
60
+ }
61
+
62
+ export async function updateReferralStateInstruction(
63
+ program: Program<Vault>,
64
+ vaultSystemStatePublicKey: PublicKey,
65
+ referralStatePublicKey: PublicKey,
66
+ payerPublicKey: PublicKey,
67
+ referralStateConfig: ReferralStateConfig
68
+ ): Promise<TransactionInstruction> {
69
+ const config = {
70
+ setupCut: referralStateConfig.setupCut ?? null,
71
+ stabilityCut: referralStateConfig.stabilityCut ?? null,
72
+ referredUserDiscount: referralStateConfig.referredUserDiscount ?? null,
73
+ psmCut: referralStateConfig.psmCut ?? null,
74
+ maxSetupCut: referralStateConfig.maxSetupCut ?? null,
75
+ maxReferredDiscount: referralStateConfig.maxReferredDiscount ?? null,
76
+ maxStabilityCut: referralStateConfig.maxStabilityCut ?? null,
77
+ maxPsmCut: referralStateConfig.maxPsmCut ?? null,
78
+ hdgThreshold: referralStateConfig.hdgThreshold ?? null,
79
+ hdgPsmThreshold: referralStateConfig.hdgPsmThreshold ?? null,
80
+ referralPromoThreshold: referralStateConfig.referralThreshold ?? null,
81
+ referralEnabled: referralStateConfig.referralEnabled ?? null,
82
+ }
83
+
84
+ return await program.methods
85
+ .updateReferralState(config)
86
+ .accounts({
87
+ signer: payerPublicKey,
88
+ vaultSystemState: vaultSystemStatePublicKey,
89
+ referralState: referralStatePublicKey,
90
+ systemProgram: SystemProgram.programId,
91
+ })
92
+ .instruction()
93
+ }
@@ -28,6 +28,8 @@ export interface VaultTypeConfig {
28
28
  maxDebtExtended?: BN
29
29
  minDebtPerVault?: BN
30
30
  loanInitFee?: BN
31
+ interestRatePerSecond?: BN
32
+
31
33
  emergencyModeThreshold?: BN
32
34
 
33
35
  oracleChainlink?: PublicKey
@@ -77,6 +79,7 @@ export async function updateVaultTypeStatusInstruction(
77
79
  maxDebtExtended: vaultTypeConfig.maxDebtExtended ?? null,
78
80
  minDebtPerVault: vaultTypeConfig.minDebtPerVault ?? null,
79
81
  loanInitFee: vaultTypeConfig.loanInitFee ?? null,
82
+ interestRatePerSecond: vaultTypeConfig.interestRatePerSecond ?? null,
80
83
  emergencyModeThreshold: vaultTypeConfig.emergencyModeThreshold ?? null,
81
84
  oracleChainlink: vaultTypeConfig.oracleChainlink ?? null,
82
85
  oraclePyth: vaultTypeConfig.oraclePyth ?? null,
@@ -16,7 +16,7 @@ export class VaultAccount {
16
16
  /** The public key of the vault owner. */
17
17
  vaultOwner: PublicKey
18
18
 
19
- /** The public key of the vault owner. */ // dont think this is correct? thoughts?
19
+ /** The salt used to derive the PDA for the vault. */
20
20
  pdaSalt: string
21
21
 
22
22
  /** The deposited collateral of the vault (in SOL Lamports). */