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
package/src/index.ts CHANGED
@@ -1,34 +1,41 @@
1
- export * from './instructions/createStakingPool'
2
- export * from './instructions/depositStakingPool'
3
- export * from './instructions/withdrawStakingPool'
4
- export * from './instructions/claimStakingPoolPosition'
5
- export * from './instructions/depositLiquidationPool'
6
- export * from './instructions/closeLiquidationPoolPosition'
1
+ export * from './Constants'
2
+ export * from './HedgeDecimal'
3
+ export * from './idl/vault'
7
4
  export * from './instructions/claimLiquidationPoolPosition'
5
+ export * from './instructions/claimStakingPoolPosition'
8
6
  export * from './instructions/closeClaimedLiquidationPoolPosition'
7
+ export * from './instructions/closeLiquidationPoolPosition'
8
+ export * from './instructions/createReferralAccount'
9
+ export * from './instructions/createStakingPool'
10
+ export * from './instructions/createUserReferralAccount'
9
11
  export * from './instructions/createVault'
10
- export * from './instructions/psmMintUsh'
11
- export * from './instructions/psmRedeemUsh'
12
+ export * from './instructions/depositLiquidationPool'
13
+ export * from './instructions/depositStakingPool'
12
14
  export * from './instructions/depositVault'
13
- export * from './instructions/withdrawVault'
15
+ export * from './instructions/initHedgeFoundation'
16
+ export * from './instructions/initHedgeFoundationTokens'
17
+ export * from './instructions/liquidateVault'
14
18
  export * from './instructions/loanVault'
15
- export * from './instructions/repayVault'
19
+ export * from './instructions/psmMintUsh'
20
+ export * from './instructions/psmRedeemUsh'
16
21
  export * from './instructions/redeemVault'
17
- export * from './instructions/liquidateVault'
22
+ export * from './instructions/referralClaimFees'
18
23
  export * from './instructions/refreshOraclePrice'
19
- export * from './instructions/initHedgeFoundation'
20
- export * from './instructions/initHedgeFoundationTokens'
24
+ export * from './instructions/repayVault'
21
25
  export * from './instructions/setHalted'
22
- export * from './instructions/updateVaultType'
23
26
  export * from './instructions/transferVault'
24
- export * from './HedgeDecimal'
25
- export * from './Constants'
26
- export * from './state/VaultAccount'
27
- export * from './state/VaultHistoryEvent'
28
- export * from './state/StakingPool'
29
- export * from './state/StakingPoolPosition'
27
+ export * from './instructions/updateReferralAccount'
28
+ export * from './instructions/updateReferralState'
29
+ export * from './instructions/updateVaultType'
30
+ export * from './instructions/withdrawStakingPool'
31
+ export * from './instructions/withdrawVault'
30
32
  export * from './state/LiquidationPoolEra'
31
33
  export * from './state/LiquidationPoolState'
32
34
  export * from './state/LiquidationPosition'
35
+ export * from './state/StakingPool'
36
+ export * from './state/StakingPoolPosition'
37
+ export * from './state/VaultAccount'
38
+ export * from './state/VaultHistoryEvent'
33
39
  export * from './utils/getLinkedListAccounts'
34
- export * from './idl/vault'
40
+
41
+
@@ -16,6 +16,9 @@ import {
16
16
  getLiquidationPoolUshAccountPublicKey,
17
17
  getUshMintPublicKey,
18
18
  getVaultSystemStatePublicKey,
19
+ getReferralAccountPublicKey,
20
+ getUserReferralAccountPublicKey,
21
+ getReferralStatePublicKey
19
22
  } from '../Constants'
20
23
  import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
21
24
  import { Vault } from '../idl/vault'
@@ -25,7 +28,8 @@ export async function closeLiquidationPoolPosition(
25
28
  provider: Provider,
26
29
  poolPosition: PublicKey,
27
30
  payer: Signer,
28
- overrideStartTime?: number
31
+ overrideStartTime?: number,
32
+ referrer?: PublicKey
29
33
  ): Promise<PublicKey> {
30
34
  const ushMintPublickey = await getUshMintPublicKey()
31
35
  const payerUshAccount = await getOrCreateAssociatedTokenAccount(
@@ -35,6 +39,7 @@ export async function closeLiquidationPoolPosition(
35
39
  payer.publicKey
36
40
  )
37
41
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
42
+ const referralStatePublicKey = await getReferralStatePublicKey()
38
43
  const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
39
44
  const poolEra = liquidationPositionAccount.era
40
45
 
@@ -53,6 +58,16 @@ export async function closeLiquidationPoolPosition(
53
58
  true
54
59
  )
55
60
 
61
+ let referralAccountPublicKey = null
62
+ if (typeof referrer === 'undefined') {
63
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
64
+ } else {
65
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
66
+ }
67
+
68
+ // Derive the user referral account public key
69
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(payer.publicKey)
70
+
56
71
  const transaction = new Transaction().add(
57
72
  await closeLiquidationPoolPositionInstruction(
58
73
  program,
@@ -62,6 +77,9 @@ export async function closeLiquidationPoolPosition(
62
77
  payerUshAccount.address,
63
78
  payerAssociatedHedgeAccount.address,
64
79
  communityAssociatedHedgeTokenAccount.address,
80
+ userReferralAccountPublicKey,
81
+ referralAccountPublicKey,
82
+ referralStatePublicKey,
65
83
  overrideStartTime
66
84
  )
67
85
  )
@@ -77,6 +95,9 @@ export async function closeLiquidationPoolPositionInstruction(
77
95
  payerUshAccount: PublicKey,
78
96
  payerAssociatedHedgeAccount: PublicKey,
79
97
  communityAssociatedHedgeTokenAccount: PublicKey,
98
+ userReferralAccountPublicKey: PublicKey,
99
+ referralAccountPublicKey: PublicKey,
100
+ referralStatePublicKey: PublicKey,
80
101
  overrideStartTime?: number
81
102
  ): Promise<TransactionInstruction> {
82
103
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -101,6 +122,9 @@ export async function closeLiquidationPoolPositionInstruction(
101
122
  ushMint: ushMint,
102
123
  payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
103
124
  communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
125
+ userReferralAccount: userReferralAccountPublicKey,
126
+ referralAccount: referralAccountPublicKey,
127
+ referralState: referralStatePublicKey,
104
128
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
105
129
  tokenProgram: TOKEN_PROGRAM_ID,
106
130
  systemProgram: SystemProgram.programId,
@@ -0,0 +1,127 @@
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
+ getUserReferralAccountPublicKey
20
+ } from '../Constants'
21
+
22
+ import { parseAnchorErrors } from '../utils/Errors'
23
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
24
+ import { Vault } from '../idl/vault'
25
+
26
+
27
+
28
+ /** @type {Function} - Creates a new referral account.
29
+ * This checks the user has enough HDG in their wallet or staked and
30
+ * then allows them to create a referral account. If they meet the PSM referral threshold,
31
+ * they will be eligible for PSM cut.
32
+ * Params:
33
+ * - program: Program<Vault> : The program instance of Hedge Vault program
34
+ * - provider: Provider : Current connection
35
+ * - payer: Signer : who we are creating the referral account for
36
+ * - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
37
+ * - referrer: PublicKey
38
+ * - overrideTime?: number
39
+ */
40
+ export async function createReferralAccount(
41
+ program: Program<Vault>,
42
+ provider: Provider,
43
+ payer: Signer,
44
+ poolPosition: PublicKey,
45
+ overrideTime?: number
46
+ ): Promise<PublicKey> {
47
+ // setup transaction
48
+ const transaction = new Transaction()
49
+ const signers = [payer]
50
+ const referrer = payer.publicKey
51
+
52
+ // Setup public keys
53
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
54
+
55
+ // Find referral account
56
+ let referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
57
+
58
+
59
+ // Get the referral state PDA
60
+ const referallStatePublicKey = await getReferralStatePublicKey()
61
+
62
+ // Get HDG mint public key
63
+ const hedgeMintPublicKey = await getHedgeMintPublicKey()
64
+
65
+ // Get HDG account of signer
66
+ const payerHdgAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
67
+ provider.connection,
68
+ payer,
69
+ hedgeMintPublicKey,
70
+ payer.publicKey
71
+ )
72
+
73
+ // Derive the user referral account public key
74
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(payer.publicKey)
75
+
76
+ transaction.add(
77
+ await createReferralAccountInstruction(
78
+ program,
79
+ payer.publicKey,
80
+ poolPosition,
81
+ referralAccountPublicKey,
82
+ referallStatePublicKey,
83
+ hedgeMintPublicKey,
84
+ payerHdgAssociatedTokenAccount.address,
85
+ userReferralAccountPublicKey,
86
+ overrideTime
87
+ )
88
+ )
89
+
90
+
91
+ await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
92
+ return referralAccountPublicKey
93
+ }
94
+
95
+
96
+ export async function createReferralAccountInstruction(
97
+ program: Program<Vault>,
98
+ payerPublicKey: PublicKey,
99
+ poolPositionPublicKey: PublicKey,
100
+ referralAccountPublicKey: PublicKey,
101
+ referralStatePublicKey: PublicKey,
102
+ hedgeMintPublicKey: PublicKey,
103
+ hdgAssociatedTokenAccountPublicKey: PublicKey,
104
+ userReferralAccountPublicKey: PublicKey,
105
+ overrideTime?: number
106
+ ): Promise<TransactionInstruction> {
107
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
108
+
109
+ return await program.methods
110
+ .createReferralAccount(
111
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override times
112
+ )
113
+ .accounts({
114
+ signer: payerPublicKey,
115
+ vaultSystemState: vaultSystemStatePublicKey,
116
+ referralState: referralStatePublicKey,
117
+ referralAccount: referralAccountPublicKey,
118
+ userReferralAccount: userReferralAccountPublicKey,
119
+ poolPosition: poolPositionPublicKey,
120
+ hedgeMint: hedgeMintPublicKey,
121
+ hdgAta: hdgAssociatedTokenAccountPublicKey,
122
+ systemProgram: SystemProgram.programId,
123
+ tokenProgram: TOKEN_PROGRAM_ID,
124
+ rent: SYSVAR_RENT_PUBKEY,
125
+ })
126
+ .instruction()
127
+ }
@@ -0,0 +1,86 @@
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
+ getUserReferralAccountPublicKey
18
+ } from '../Constants'
19
+
20
+ import { v4 as uuidv4 } from 'uuid'
21
+ import { parseAnchorErrors } from '../utils/Errors'
22
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
23
+ import { Vault } from '../idl/vault'
24
+
25
+ export async function createUserReferralAccount(
26
+ program: Program<Vault>,
27
+ provider: Provider,
28
+ payer: Signer,
29
+ referrer?: PublicKey,
30
+ overrideTime?: number
31
+ ): Promise<PublicKey> {
32
+ // setup transaction
33
+ const transaction = new Transaction()
34
+ const signers = [payer]
35
+
36
+ // Find referrer account
37
+ let referralAccountPublicKey = null
38
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
39
+ if (typeof referrer === 'undefined') {
40
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
41
+ } else {
42
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
43
+ }
44
+
45
+ // Derive the user referral account public key
46
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(payer.publicKey)
47
+
48
+ transaction.add(
49
+ await createUserReferralAccountInstruction(
50
+ program,
51
+ payer.publicKey,
52
+ referralAccountPublicKey,
53
+ userReferralAccountPublicKey,
54
+ overrideTime
55
+ )
56
+ )
57
+
58
+
59
+ await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
60
+ return userReferralAccountPublicKey
61
+ }
62
+
63
+
64
+ export async function createUserReferralAccountInstruction(
65
+ program: Program<Vault>,
66
+ payerPublicKey: PublicKey,
67
+ referralAccount: PublicKey,
68
+ userReferralAccount: PublicKey,
69
+ overrideTime?: number
70
+ ): Promise<TransactionInstruction> {
71
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
72
+
73
+ return await program.methods
74
+ .createUserReferralAccount(
75
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
76
+ )
77
+ .accounts({
78
+ signer: payerPublicKey,
79
+ userReferralAccount: userReferralAccount,
80
+ referralAccount: referralAccount,
81
+ systemProgram: SystemProgram.programId,
82
+ tokenProgram: TOKEN_PROGRAM_ID,
83
+ rent: SYSVAR_RENT_PUBKEY,
84
+ })
85
+ .instruction()
86
+ }
@@ -14,6 +14,8 @@ import { parseAnchorErrors } from '../utils/Errors'
14
14
  import {
15
15
  getLiquidationPoolStatePublicKey,
16
16
  getLiquidationPoolUshAccountPublicKey,
17
+ getUserReferralAccountPublicKey,
18
+ getReferralAccountPublicKey,
17
19
  getUshMintPublicKey,
18
20
  getVaultSystemStatePublicKey,
19
21
  } from '../Constants'
@@ -25,7 +27,8 @@ export async function depositLiquidationPool(
25
27
  provider: Provider,
26
28
  payer: Signer,
27
29
  depositAmount: BN,
28
- overrideStartTime?: number
30
+ overrideStartTime?: number,
31
+ referrer?: PublicKey
29
32
  ): Promise<PublicKey> {
30
33
  const ushMintPublickey = await getUshMintPublicKey()
31
34
  const payerUshAccount = await getOrCreateAssociatedTokenAccount(
@@ -35,14 +38,28 @@ export async function depositLiquidationPool(
35
38
  payer.publicKey
36
39
  )
37
40
 
41
+ let referralAccountPublicKey = null
42
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
43
+ if (typeof referrer === 'undefined') {
44
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
45
+ } else {
46
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
47
+ }
48
+
49
+ // Derive the user referral account public key
50
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(payer.publicKey)
51
+
38
52
  const poolPosition = Keypair.generate()
39
53
  const transaction = new Transaction().add(
40
54
  await depositLiquidationPoolInstruction(
41
55
  program,
42
56
  payer.publicKey,
57
+ vaultSystemStatePublicKey,
43
58
  payerUshAccount.address,
44
59
  poolPosition.publicKey,
45
60
  new BN(depositAmount),
61
+ userReferralAccountPublicKey,
62
+ referralAccountPublicKey,
46
63
  overrideStartTime
47
64
  )
48
65
  )
@@ -53,9 +70,12 @@ export async function depositLiquidationPool(
53
70
  export async function depositLiquidationPoolInstruction(
54
71
  program: Program<Vault>,
55
72
  payerPublicKey: PublicKey,
73
+ vaultSystemStatePublicKey: PublicKey,
56
74
  payerUshAccount: PublicKey,
57
75
  poolPositionPublicKey: PublicKey,
58
76
  depositAmount: BN,
77
+ userReferralAccountPublicKey: PublicKey,
78
+ referralAccountPublicKey: PublicKey,
59
79
  overrideStartTime?: number
60
80
  ): Promise<TransactionInstruction> {
61
81
  const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
@@ -64,15 +84,13 @@ export async function depositLiquidationPoolInstruction(
64
84
  const poolUSHAccount = await getLiquidationPoolUshAccountPublicKey()
65
85
  const ushMint = await getUshMintPublicKey()
66
86
 
67
- const vaultSystemState = await getVaultSystemStatePublicKey()
68
-
69
87
  return await program.methods
70
88
  .depositLiquidationPool(
71
89
  depositAmount,
72
90
  new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)) // override current time
73
91
  )
74
92
  .accounts({
75
- vaultSystemState: vaultSystemState,
93
+ vaultSystemState: vaultSystemStatePublicKey,
76
94
  poolState: liquidationPoolStatePublicKey,
77
95
  poolUshAccount: poolUSHAccount,
78
96
  poolEra: liquidationPoolState.currentEra,
@@ -80,6 +98,8 @@ export async function depositLiquidationPoolInstruction(
80
98
  ushMint: ushMint,
81
99
  payer: payerPublicKey,
82
100
  ownerUshAccount: payerUshAccount,
101
+ userReferralAccount: userReferralAccountPublicKey,
102
+ referralAccount: referralAccountPublicKey,
83
103
  tokenProgram: TOKEN_PROGRAM_ID,
84
104
  systemProgram: SystemProgram.programId,
85
105
  })
@@ -18,6 +18,9 @@ import {
18
18
  getUshMintPublicKey,
19
19
  getVaultSystemStatePublicKey,
20
20
  HEDGE_PROGRAM_PUBLICKEY,
21
+ getReferralStatePublicKey,
22
+ getReferralAccountPublicKey,
23
+ getUserReferralAccountPublicKey,
21
24
  } from '../Constants'
22
25
  import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
23
26
  import { Vault } from '../idl/vault'
@@ -30,7 +33,8 @@ export async function loanVault(
30
33
  payer: Signer,
31
34
  vaultPublicKey: PublicKey,
32
35
  loanAmount: number,
33
- overrideTime?: number
36
+ overrideTime?: number,
37
+ referrer?: PublicKey,
34
38
  ): Promise<PublicKey> {
35
39
  const ushMintPublickey = await getUshMintPublicKey()
36
40
 
@@ -69,6 +73,14 @@ export async function loanVault(
69
73
  false
70
74
  )
71
75
 
76
+ let referralAccountPublicKey = null
77
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
78
+ if (typeof referrer === 'undefined') {
79
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
80
+ } else {
81
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
82
+ }
83
+
72
84
  const history = Keypair.generate()
73
85
  const transaction = new Transaction().add(
74
86
  await loanVaultInstruction(
@@ -84,6 +96,7 @@ export async function loanVault(
84
96
  newSmallerPublicKey,
85
97
  newLargerPublicKey,
86
98
  new BN(loanAmount),
99
+ referralAccountPublicKey,
87
100
  overrideTime
88
101
  )
89
102
  )
@@ -104,6 +117,7 @@ export async function loanVaultInstruction(
104
117
  newSmallerPublicKey: PublicKey,
105
118
  newLargerPublicKey: PublicKey,
106
119
  loanAmount: BN,
120
+ referralAccountPublicKey: PublicKey,
107
121
  overrideTime?: number
108
122
  ): Promise<TransactionInstruction> {
109
123
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -114,6 +128,8 @@ export async function loanVaultInstruction(
114
128
  hedgeStakingPoolPublicKey,
115
129
  ushMintPublickey
116
130
  )
131
+ const referralStatePublicKey = await getReferralStatePublicKey()
132
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(payerPublicKey)
117
133
 
118
134
  return await program.methods
119
135
  .loanVault(
@@ -135,6 +151,9 @@ export async function loanVaultInstruction(
135
151
  oldSmallerVaultInfo: oldSmallerPublicKey,
136
152
  newSmallerVaultInfo: newSmallerPublicKey,
137
153
  newLargerVaultInfo: newLargerPublicKey,
154
+ referralState: referralStatePublicKey,
155
+ referralAccount: referralAccountPublicKey,
156
+ userReferralAccount: userReferralAccountPublicKey,
138
157
  tokenProgram: TOKEN_PROGRAM_ID,
139
158
  systemProgram: SystemProgram.programId,
140
159
  })
@@ -14,6 +14,8 @@ import {
14
14
  findAssociatedTokenAddress,
15
15
  getHedgeMintPublicKey,
16
16
  getPoolPublicKeyForMint,
17
+ getReferralAccountPublicKey,
18
+ getReferralStatePublicKey,
17
19
  getUshMintPublicKey,
18
20
  getVaultSystemStatePublicKey,
19
21
  HEDGE_PROGRAM_PUBLICKEY,
@@ -29,7 +31,8 @@ export async function psmMintUsh(
29
31
  payer: Signer,
30
32
  collateralMint: PublicKey,
31
33
  collateralAmount: number,
32
- overrideTime?: number
34
+ overrideTime?: number,
35
+ referrer?: PublicKey
33
36
  ): Promise<PublicKey> {
34
37
  const ushMintPublickey = await getUshMintPublicKey()
35
38
 
@@ -62,6 +65,16 @@ export async function psmMintUsh(
62
65
  true
63
66
  )
64
67
 
68
+ let referralAccountPublicKey
69
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
70
+ if (!referrer) {
71
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
72
+ } else {
73
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
74
+ }
75
+
76
+ const referralStatePublicKey = await getReferralStatePublicKey()
77
+
65
78
  const transaction = new Transaction().add(
66
79
  await psmMintUshInstruction(
67
80
  program,
@@ -69,6 +82,8 @@ export async function psmMintUsh(
69
82
  psmAccount,
70
83
  collateralMint,
71
84
  new BN(collateralAmount),
85
+ referralStatePublicKey,
86
+ referralAccountPublicKey,
72
87
  overrideTime
73
88
  )
74
89
  )
@@ -82,6 +97,8 @@ export async function psmMintUshInstruction(
82
97
  psmAccount: PublicKey,
83
98
  collateralMint: PublicKey,
84
99
  collateralAmount: BN,
100
+ referralStatePublicKey: PublicKey,
101
+ referralAccountPublicKey: PublicKey,
85
102
  overrideTime?: number
86
103
  ): Promise<TransactionInstruction> {
87
104
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -122,6 +139,8 @@ export async function psmMintUshInstruction(
122
139
  ownerCollateralAccount: ownerCollateralAccount,
123
140
  collateralMint: collateralMint,
124
141
  ushMint: ushMintPublickey,
142
+ referralState: referralStatePublicKey,
143
+ referralAccount: referralAccountPublicKey,
125
144
  systemProgram: SystemProgram.programId,
126
145
  tokenProgram: TOKEN_PROGRAM_ID,
127
146
  })
@@ -17,6 +17,8 @@ import {
17
17
  getUshMintPublicKey,
18
18
  getVaultSystemStatePublicKey,
19
19
  HEDGE_PROGRAM_PUBLICKEY,
20
+ getReferralAccountPublicKey,
21
+ getReferralStatePublicKey
20
22
  } from '../Constants'
21
23
  import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
24
  import { Vault } from '../idl/vault'
@@ -29,7 +31,8 @@ export async function psmRedeemUsh(
29
31
  payer: Signer,
30
32
  collateralMint: PublicKey,
31
33
  ushAmount: number,
32
- overrideTime?: number
34
+ overrideTime?: number,
35
+ referrer?: PublicKey
33
36
  ): Promise<PublicKey> {
34
37
  const ushMintPublickey = await getUshMintPublicKey()
35
38
 
@@ -62,6 +65,16 @@ export async function psmRedeemUsh(
62
65
  true
63
66
  )
64
67
 
68
+ let referralAccountPublicKey
69
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
70
+ if (!referrer) {
71
+ referralAccountPublicKey = await getReferralAccountPublicKey(vaultSystemStatePublicKey)
72
+ } else {
73
+ referralAccountPublicKey = await getReferralAccountPublicKey(referrer)
74
+ }
75
+
76
+ const referralStatePublicKey = await getReferralStatePublicKey()
77
+
65
78
  const transaction = new Transaction().add(
66
79
  await psmRedeemUshInstruction(
67
80
  program,
@@ -69,6 +82,8 @@ export async function psmRedeemUsh(
69
82
  psmAccount,
70
83
  collateralMint,
71
84
  new BN(ushAmount),
85
+ referralStatePublicKey,
86
+ referralAccountPublicKey,
72
87
  overrideTime
73
88
  )
74
89
  )
@@ -82,6 +97,8 @@ export async function psmRedeemUshInstruction(
82
97
  psmAccount: PublicKey,
83
98
  collateralMint: PublicKey,
84
99
  ushAmount: BN,
100
+ referralStatePublicKey: PublicKey,
101
+ referralAccountPublicKey: PublicKey,
85
102
  overrideTime?: number
86
103
  ): Promise<TransactionInstruction> {
87
104
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
@@ -122,6 +139,8 @@ export async function psmRedeemUshInstruction(
122
139
  ownerCollateralAccount: ownerCollateralAccount,
123
140
  collateralMint: collateralMint,
124
141
  ushMint: ushMintPublickey,
142
+ referralState: referralStatePublicKey,
143
+ referralAccount: referralAccountPublicKey,
125
144
  systemProgram: SystemProgram.programId,
126
145
  tokenProgram: TOKEN_PROGRAM_ID,
127
146
  })