hedge-web3 0.2.22 → 0.2.24

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 (43) hide show
  1. package/declarations/Constants.d.ts +15 -0
  2. package/declarations/idl/vault.d.ts +832 -179
  3. package/declarations/index.d.ts +26 -21
  4. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +2 -2
  5. package/declarations/instructions/createReferralAccount.d.ts +17 -0
  6. package/declarations/instructions/createUserReferralAccount.d.ts +5 -0
  7. package/declarations/instructions/depositLiquidationPool.d.ts +2 -2
  8. package/declarations/instructions/loanVault.d.ts +2 -2
  9. package/declarations/instructions/psmEditAccount.d.ts +12 -0
  10. package/declarations/instructions/psmMintUsh.d.ts +2 -2
  11. package/declarations/instructions/psmRedeemUsh.d.ts +2 -2
  12. package/declarations/instructions/referralClaimFees.d.ts +16 -0
  13. package/declarations/instructions/updateReferralAccount.d.ts +13 -0
  14. package/declarations/instructions/updateReferralState.d.ts +20 -0
  15. package/lib/Constants.js +36 -1
  16. package/lib/idl/vault.js +830 -177
  17. package/lib/index.js +26 -21
  18. package/lib/instructions/closeLiquidationPoolPosition.js +16 -3
  19. package/lib/instructions/createReferralAccount.js +80 -0
  20. package/lib/instructions/createUserReferralAccount.js +61 -0
  21. package/lib/instructions/depositLiquidationPool.js +16 -5
  22. package/lib/instructions/loanVault.js +16 -3
  23. package/lib/instructions/psmEditAccount.js +60 -0
  24. package/lib/instructions/psmMintUsh.js +14 -3
  25. package/lib/instructions/psmRedeemUsh.js +14 -3
  26. package/lib/instructions/referralClaimFees.js +84 -0
  27. package/lib/instructions/updateReferralAccount.js +50 -0
  28. package/lib/instructions/updateReferralState.js +57 -0
  29. package/package.json +1 -1
  30. package/src/Constants.ts +42 -0
  31. package/src/idl/vault.ts +1845 -539
  32. package/src/index.ts +28 -21
  33. package/src/instructions/closeLiquidationPoolPosition.ts +25 -1
  34. package/src/instructions/createReferralAccount.ts +127 -0
  35. package/src/instructions/createUserReferralAccount.ts +86 -0
  36. package/src/instructions/depositLiquidationPool.ts +24 -4
  37. package/src/instructions/loanVault.ts +20 -1
  38. package/src/instructions/{psmEditAccount.rs → psmEditAccount.ts} +0 -0
  39. package/src/instructions/psmMintUsh.ts +20 -1
  40. package/src/instructions/psmRedeemUsh.ts +20 -1
  41. package/src/instructions/referralClaimFees.ts +159 -0
  42. package/src/instructions/updateReferralAccount.ts +77 -0
  43. package/src/instructions/updateReferralState.ts +93 -0
@@ -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
+ }
@@ -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
+ }