hedge-web3 0.2.21 → 0.2.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. package/declarations/Constants.d.ts +25 -12
  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 +905 -155
  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/state/VaultAccount.d.ts +1 -1
  19. package/declarations/utils/getLinkedListAccounts.d.ts +2 -2
  20. package/lib/Constants.js +57 -28
  21. package/lib/idl/pyth.js +44 -24
  22. package/lib/idl/switchboard.js +49 -0
  23. package/lib/idl/vault.js +898 -148
  24. package/lib/index.js +26 -20
  25. package/lib/instructions/claimLiquidationPoolPosition.js +10 -7
  26. package/lib/instructions/claimStakingPoolPosition.js +5 -5
  27. package/lib/instructions/closeClaimedLiquidationPoolPosition.js +1 -1
  28. package/lib/instructions/closeLiquidationPoolPosition.js +25 -12
  29. package/lib/instructions/createReferralAccount.js +80 -0
  30. package/lib/instructions/createStakingPool.js +7 -7
  31. package/lib/instructions/createUserReferralAccount.js +61 -0
  32. package/lib/instructions/createVault.js +15 -15
  33. package/lib/instructions/depositLiquidationPool.js +21 -10
  34. package/lib/instructions/depositStakingPool.js +5 -5
  35. package/lib/instructions/depositVault.js +11 -9
  36. package/lib/instructions/initHedgeFoundation.js +8 -8
  37. package/lib/instructions/initHedgeFoundationTokens.js +5 -5
  38. package/lib/instructions/liquidateVault.js +8 -8
  39. package/lib/instructions/loanVault.js +23 -10
  40. package/lib/instructions/psmEditAccount.js +60 -0
  41. package/lib/instructions/psmMintUsh.js +24 -13
  42. package/lib/instructions/psmRedeemUsh.js +24 -13
  43. package/lib/instructions/redeemVault.js +7 -7
  44. package/lib/instructions/referralClaimFees.js +84 -0
  45. package/lib/instructions/refreshOraclePrice.js +3 -3
  46. package/lib/instructions/repayVault.js +9 -9
  47. package/lib/instructions/setHalted.js +1 -1
  48. package/lib/instructions/transferVault.js +10 -24
  49. package/lib/instructions/updateReferralAccount.js +50 -0
  50. package/lib/instructions/updateReferralState.js +57 -0
  51. package/lib/instructions/updateVaultType.js +1 -1
  52. package/lib/instructions/withdrawStakingPool.js +11 -11
  53. package/lib/instructions/withdrawVault.js +5 -5
  54. package/lib/state/VaultAccount.js +3 -1
  55. package/lib/utils/getLinkedListAccounts.js +2 -3
  56. package/package.json +1 -1
  57. package/src/Constants.ts +64 -63
  58. package/src/idl/pyth.ts +88 -48
  59. package/src/idl/switchboard.ts +93 -0
  60. package/src/idl/vault.ts +1851 -351
  61. package/src/index.ts +28 -24
  62. package/src/instructions/claimLiquidationPoolPosition.ts +31 -29
  63. package/src/instructions/claimStakingPoolPosition.ts +10 -15
  64. package/src/instructions/closeClaimedLiquidationPoolPosition.ts +4 -2
  65. package/src/instructions/closeLiquidationPoolPosition.ts +36 -18
  66. package/src/instructions/createReferralAccount.ts +115 -0
  67. package/src/instructions/createStakingPool.ts +11 -13
  68. package/src/instructions/createUserReferralAccount.ts +75 -0
  69. package/src/instructions/createVault.ts +44 -26
  70. package/src/instructions/depositLiquidationPool.ts +34 -21
  71. package/src/instructions/depositStakingPool.ts +18 -14
  72. package/src/instructions/depositVault.ts +23 -18
  73. package/src/instructions/initHedgeFoundation.ts +16 -14
  74. package/src/instructions/initHedgeFoundationTokens.ts +12 -14
  75. package/src/instructions/liquidateVault.ts +15 -20
  76. package/src/instructions/loanVault.ts +29 -19
  77. package/src/instructions/{psmEditAccount.rs → psmEditAccount.ts} +10 -18
  78. package/src/instructions/psmMintUsh.ts +35 -38
  79. package/src/instructions/psmRedeemUsh.ts +35 -40
  80. package/src/instructions/redeemVault.ts +12 -15
  81. package/src/instructions/referralClaimFees.ts +145 -0
  82. package/src/instructions/refreshOraclePrice.ts +6 -8
  83. package/src/instructions/repayVault.ts +18 -16
  84. package/src/instructions/setHalted.ts +5 -24
  85. package/src/instructions/transferVault.ts +57 -0
  86. package/src/instructions/updateReferralAccount.ts +70 -0
  87. package/src/instructions/updateReferralState.ts +86 -0
  88. package/src/instructions/updateVaultType.ts +9 -23
  89. package/src/instructions/withdrawStakingPool.ts +17 -21
  90. package/src/instructions/withdrawVault.ts +10 -16
  91. package/src/state/VaultAccount.ts +9 -10
  92. package/src/utils/getLinkedListAccounts.ts +4 -7
package/src/index.ts CHANGED
@@ -1,37 +1,41 @@
1
-
2
- export * from './instructions/createStakingPool'
3
- export * from './instructions/depositStakingPool'
4
- export * from './instructions/withdrawStakingPool'
5
- export * from './instructions/claimStakingPoolPosition'
6
- export * from './instructions/depositLiquidationPool'
7
- export * from './instructions/closeLiquidationPoolPosition'
1
+ export * from './Constants'
2
+ export * from './HedgeDecimal'
3
+ export * from './idl/vault'
8
4
  export * from './instructions/claimLiquidationPoolPosition'
5
+ export * from './instructions/claimStakingPoolPosition'
9
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'
10
11
  export * from './instructions/createVault'
11
- export * from './instructions/psmMintUsh'
12
- export * from './instructions/psmRedeemUsh'
12
+ export * from './instructions/depositLiquidationPool'
13
+ export * from './instructions/depositStakingPool'
13
14
  export * from './instructions/depositVault'
14
- export * from './instructions/withdrawVault'
15
+ export * from './instructions/initHedgeFoundation'
16
+ export * from './instructions/initHedgeFoundationTokens'
17
+ export * from './instructions/liquidateVault'
15
18
  export * from './instructions/loanVault'
16
- export * from './instructions/repayVault'
19
+ export * from './instructions/psmMintUsh'
20
+ export * from './instructions/psmRedeemUsh'
17
21
  export * from './instructions/redeemVault'
18
- export * from './instructions/liquidateVault'
22
+ export * from './instructions/referralClaimFees'
19
23
  export * from './instructions/refreshOraclePrice'
20
- export * from './instructions/initHedgeFoundation'
21
- export * from './instructions/initHedgeFoundationTokens'
24
+ export * from './instructions/repayVault'
22
25
  export * from './instructions/setHalted'
26
+ export * from './instructions/transferVault'
27
+ export * from './instructions/updateReferralAccount'
28
+ export * from './instructions/updateReferralState'
23
29
  export * from './instructions/updateVaultType'
24
-
25
- export * from './HedgeDecimal'
26
- export * from './Constants'
27
-
28
- export * from './state/VaultAccount'
29
- export * from './state/VaultHistoryEvent'
30
- export * from './state/StakingPool'
31
- export * from './state/StakingPoolPosition'
30
+ export * from './instructions/withdrawStakingPool'
31
+ export * from './instructions/withdrawVault'
32
32
  export * from './state/LiquidationPoolEra'
33
33
  export * from './state/LiquidationPoolState'
34
34
  export * from './state/LiquidationPosition'
35
-
35
+ export * from './state/StakingPool'
36
+ export * from './state/StakingPoolPosition'
37
+ export * from './state/VaultAccount'
38
+ export * from './state/VaultHistoryEvent'
36
39
  export * from './utils/getLinkedListAccounts'
37
- export * from './idl/vault'
40
+
41
+
@@ -1,23 +1,19 @@
1
1
  import { Program, Provider } from '@project-serum/anchor'
2
2
  import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import {
4
- PublicKey,
5
- sendAndConfirmTransaction,
6
- Signer,
4
+ PublicKey, Signer,
7
5
  SystemProgram,
8
6
  SYSVAR_RENT_PUBKEY,
9
7
  Transaction,
10
- TransactionInstruction,
8
+ TransactionInstruction
11
9
  } from '@solana/web3.js'
12
- import { parseAnchorErrors } from '../utils/Errors'
13
10
  import {
14
- findAssociatedTokenAddress,
15
- getVaultTypeAccountPublicKey,
16
- getLiquidationPoolStatePublicKey,
17
- getVaultSystemStatePublicKey,
11
+ findAssociatedTokenAddress, getLiquidationPoolStatePublicKey,
12
+ getVaultSystemStatePublicKey, getVaultTypeAccountPublicKey
18
13
  } from '../Constants'
19
- import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
20
14
  import { Vault } from '../idl/vault'
15
+ import { parseAnchorErrors } from '../utils/Errors'
16
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
21
17
 
22
18
  export async function claimLiquidationPoolPosition(
23
19
  program: Program<Vault>,
@@ -27,7 +23,7 @@ export async function claimLiquidationPoolPosition(
27
23
  collateralType: string,
28
24
  overrideStartTime?: number
29
25
  ): Promise<PublicKey> {
30
- const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
26
+ const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(program.programId, collateralType)
31
27
  const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
32
28
  const collateralMintPublicKey = vaultTypeAccountInfo.collateralMint
33
29
 
@@ -62,23 +58,29 @@ export async function claimLiquidationPoolPositionInstruction(
62
58
  payerAssociatedTokenAccount: PublicKey,
63
59
  overrideStartTime?: number
64
60
  ): Promise<TransactionInstruction> {
61
+ const vaultSystemState = await getVaultSystemStatePublicKey(program.programId)
62
+ const poolStatePublicKey = await getLiquidationPoolStatePublicKey(program.programId)
63
+ const poolAssociatedTokenAccount = await findAssociatedTokenAddress(
64
+ program.programId,
65
+ poolStatePublicKey,
66
+ collateralMint
67
+ )
65
68
 
66
- const vaultSystemState = await getVaultSystemStatePublicKey()
67
- const poolStatePublicKey = await getLiquidationPoolStatePublicKey()
68
- const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint)
69
-
70
- return await program.methods.claimLiquidationPoolPosition().accounts({
71
- vaultSystemState: vaultSystemState,
72
- poolState: poolStatePublicKey,
73
- poolAssociatedTokenAccount: poolAssociatedTokenAccount,
74
- vaultTypeAccount: vaultTypeAccount,
75
- collateralMint: collateralMint,
76
- poolPosition: poolPosition,
77
- payer: payer,
78
- payerAssociatedTokenAccount: payerAssociatedTokenAccount,
79
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
80
- tokenProgram: TOKEN_PROGRAM_ID,
81
- systemProgram: SystemProgram.programId,
82
- rent: SYSVAR_RENT_PUBKEY,
83
- }).instruction()
69
+ return await program.methods
70
+ .claimLiquidationPoolPosition()
71
+ .accounts({
72
+ vaultSystemState: vaultSystemState,
73
+ poolState: poolStatePublicKey,
74
+ poolAssociatedTokenAccount: poolAssociatedTokenAccount,
75
+ vaultTypeAccount: vaultTypeAccount,
76
+ collateralMint: collateralMint,
77
+ poolPosition: poolPosition,
78
+ payer: payer,
79
+ payerAssociatedTokenAccount: payerAssociatedTokenAccount,
80
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
81
+ tokenProgram: TOKEN_PROGRAM_ID,
82
+ systemProgram: SystemProgram.programId,
83
+ rent: SYSVAR_RENT_PUBKEY,
84
+ })
85
+ .instruction()
84
86
  }
@@ -1,24 +1,19 @@
1
1
  import { Program, Provider } from '@project-serum/anchor'
2
2
  import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import {
4
- PublicKey,
5
- sendAndConfirmTransaction,
6
- Signer,
4
+ PublicKey, Signer,
7
5
  SystemProgram,
8
6
  SYSVAR_RENT_PUBKEY,
9
7
  Transaction,
10
- TransactionInstruction,
8
+ TransactionInstruction
11
9
  } from '@solana/web3.js'
12
- import { parseAnchorErrors } from '../utils/Errors'
13
10
  import {
14
- getHedgeMintPublicKey,
15
- findAssociatedTokenAddress,
16
- getVaultTypeAccountPublicKey,
17
- getPoolPublicKeyForMint,
18
- getVaultSystemStatePublicKey,
11
+ getHedgeMintPublicKey, getPoolPublicKeyForMint,
12
+ getVaultSystemStatePublicKey, getVaultTypeAccountPublicKey
19
13
  } from '../Constants'
20
- import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
21
14
  import { Vault } from '../idl/vault'
15
+ import { parseAnchorErrors } from '../utils/Errors'
16
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
17
 
23
18
  export async function claimStakingPoolPosition(
24
19
  program: Program<Vault>,
@@ -27,11 +22,11 @@ export async function claimStakingPoolPosition(
27
22
  payer: Signer,
28
23
  collateralType: string
29
24
  ): Promise<PublicKey> {
30
- const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
25
+ const vaultTypeAccount = await getVaultTypeAccountPublicKey(program.programId, collateralType)
31
26
  const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
32
27
  const collateralMint = vaultTypeAccountInfo.collateralMint
33
- const stakedTokenMint = await getHedgeMintPublicKey()
34
- const [feePool] = await getPoolPublicKeyForMint(stakedTokenMint)
28
+ const stakedTokenMint = await getHedgeMintPublicKey(program.programId)
29
+ const [feePool] = await getPoolPublicKeyForMint(program.programId, stakedTokenMint)
35
30
  const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
36
31
  provider.connection,
37
32
  payer,
@@ -74,7 +69,7 @@ export async function claimStakingPoolPositionInstruction(
74
69
  payer: PublicKey,
75
70
  payerAssociatedTokenAccount: PublicKey
76
71
  ): Promise<TransactionInstruction> {
77
- const vaultSystemState = await getVaultSystemStatePublicKey()
72
+ const vaultSystemState = await getVaultSystemStatePublicKey(program.programId)
78
73
 
79
74
  return await program.methods
80
75
  .claimStakingPoolPosition()
@@ -10,7 +10,9 @@ export async function closeClaimedLiquidationPoolPosition(
10
10
  poolPosition: PublicKey,
11
11
  payer: Signer
12
12
  ): Promise<PublicKey> {
13
- const transaction = new Transaction().add(await closeClaimedLiquidationPoolPositionInstruction(program, poolPosition, payer.publicKey))
13
+ const transaction = new Transaction().add(
14
+ await closeClaimedLiquidationPoolPositionInstruction(program, poolPosition, payer.publicKey)
15
+ )
14
16
 
15
17
  await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
16
18
  return poolPosition
@@ -25,7 +27,7 @@ export async function closeClaimedLiquidationPoolPositionInstruction(
25
27
  .closeClaimedLiquidationPoolPosition()
26
28
  .accounts({
27
29
  poolPosition: poolPosition,
28
- payer: payer
30
+ payer: payer,
29
31
  })
30
32
  .instruction()
31
33
  }
@@ -1,44 +1,43 @@
1
1
  import { BN, Program, Provider } from '@project-serum/anchor'
2
2
  import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
3
  import {
4
- PublicKey,
5
- sendAndConfirmTransaction,
6
- Signer,
4
+ PublicKey, Signer,
7
5
  SystemProgram,
8
6
  SYSVAR_RENT_PUBKEY,
9
7
  Transaction,
10
- TransactionInstruction,
8
+ TransactionInstruction
11
9
  } from '@solana/web3.js'
12
- import { parseAnchorErrors } from '../utils/Errors'
13
10
  import {
14
11
  getHedgeMintPublicKey,
15
12
  getLiquidationPoolStatePublicKey,
16
- getLiquidationPoolUshAccountPublicKey,
17
- getUshMintPublicKey,
18
- getVaultSystemStatePublicKey,
13
+ getLiquidationPoolUshAccountPublicKey, getReferralAccountPublicKey, getReferralStatePublicKey, getUserReferralAccountPublicKey, getUshMintPublicKey,
14
+ getVaultSystemStatePublicKey
19
15
  } from '../Constants'
20
- import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
21
16
  import { Vault } from '../idl/vault'
17
+ import { parseAnchorErrors } from '../utils/Errors'
18
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
19
 
23
20
  export async function closeLiquidationPoolPosition(
24
21
  program: Program<Vault>,
25
22
  provider: Provider,
26
23
  poolPosition: PublicKey,
27
24
  payer: Signer,
28
- overrideStartTime?: number
25
+ overrideStartTime?: number,
26
+ referrer?: PublicKey
29
27
  ): Promise<PublicKey> {
30
- const ushMintPublickey = await getUshMintPublicKey()
28
+ const ushMintPublickey = await getUshMintPublicKey(program.programId)
31
29
  const payerUshAccount = await getOrCreateAssociatedTokenAccount(
32
30
  provider.connection,
33
31
  payer,
34
32
  ushMintPublickey,
35
33
  payer.publicKey
36
34
  )
37
- const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
35
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
36
+ const referralStatePublicKey = await getReferralStatePublicKey(program.programId)
38
37
  const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
39
38
  const poolEra = liquidationPositionAccount.era
40
39
 
41
- const hedgeMint = await getHedgeMintPublicKey()
40
+ const hedgeMint = await getHedgeMintPublicKey(program.programId)
42
41
  const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(
43
42
  provider.connection,
44
43
  payer,
@@ -53,6 +52,16 @@ export async function closeLiquidationPoolPosition(
53
52
  true
54
53
  )
55
54
 
55
+ let referralAccountPublicKey = null
56
+ if (typeof referrer === 'undefined') {
57
+ referralAccountPublicKey = await getReferralAccountPublicKey(program.programId, vaultSystemStatePublicKey)
58
+ } else {
59
+ referralAccountPublicKey = await getReferralAccountPublicKey(program.programId, referrer)
60
+ }
61
+
62
+ // Derive the user referral account public key
63
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(program.programId, payer.publicKey)
64
+
56
65
  const transaction = new Transaction().add(
57
66
  await closeLiquidationPoolPositionInstruction(
58
67
  program,
@@ -62,6 +71,9 @@ export async function closeLiquidationPoolPosition(
62
71
  payerUshAccount.address,
63
72
  payerAssociatedHedgeAccount.address,
64
73
  communityAssociatedHedgeTokenAccount.address,
74
+ userReferralAccountPublicKey,
75
+ referralAccountPublicKey,
76
+ referralStatePublicKey,
65
77
  overrideStartTime
66
78
  )
67
79
  )
@@ -77,13 +89,16 @@ export async function closeLiquidationPoolPositionInstruction(
77
89
  payerUshAccount: PublicKey,
78
90
  payerAssociatedHedgeAccount: PublicKey,
79
91
  communityAssociatedHedgeTokenAccount: PublicKey,
92
+ userReferralAccountPublicKey: PublicKey,
93
+ referralAccountPublicKey: PublicKey,
94
+ referralStatePublicKey: PublicKey,
80
95
  overrideStartTime?: number
81
96
  ): Promise<TransactionInstruction> {
82
- const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
83
- const ushMint = await getUshMintPublicKey()
84
- const hedgeMint = await getHedgeMintPublicKey()
85
- const poolUshAccount = await getLiquidationPoolUshAccountPublicKey()
86
- const poolState = await getLiquidationPoolStatePublicKey()
97
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
98
+ const ushMint = await getUshMintPublicKey(program.programId)
99
+ const hedgeMint = await getHedgeMintPublicKey(program.programId)
100
+ const poolUshAccount = await getLiquidationPoolUshAccountPublicKey(program.programId)
101
+ const poolState = await getLiquidationPoolStatePublicKey(program.programId)
87
102
 
88
103
  return await program.methods
89
104
  .closeLiquidationPoolPosition(
@@ -101,6 +116,9 @@ export async function closeLiquidationPoolPositionInstruction(
101
116
  ushMint: ushMint,
102
117
  payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
103
118
  communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
119
+ userReferralAccount: userReferralAccountPublicKey,
120
+ referralAccount: referralAccountPublicKey,
121
+ referralState: referralStatePublicKey,
104
122
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
105
123
  tokenProgram: TOKEN_PROGRAM_ID,
106
124
  systemProgram: SystemProgram.programId,
@@ -0,0 +1,115 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import {
4
+ PublicKey, Signer,
5
+ SystemProgram,
6
+ SYSVAR_RENT_PUBKEY,
7
+ Transaction,
8
+ TransactionInstruction
9
+ } from '@solana/web3.js'
10
+ import {
11
+ getHedgeMintPublicKey, getReferralAccountPublicKey, getReferralStatePublicKey,
12
+ getUserReferralAccountPublicKey, getVaultSystemStatePublicKey
13
+ } from '../Constants'
14
+
15
+ import { Vault } from '../idl/vault'
16
+ import { parseAnchorErrors } from '../utils/Errors'
17
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
18
+
19
+ /** @type {Function} - Creates a new referral account.
20
+ * This checks the user has enough HDG in their wallet or staked and
21
+ * then allows them to create a referral account. If they meet the PSM referral threshold,
22
+ * they will be eligible for PSM cut.
23
+ * Params:
24
+ * - program: Program<Vault> : The program instance of Hedge Vault program
25
+ * - provider: Provider : Current connection
26
+ * - payer: Signer : who we are creating the referral account for
27
+ * - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
28
+ * - referrer: PublicKey
29
+ * - overrideTime?: number
30
+ */
31
+ export async function createReferralAccount(
32
+ program: Program<Vault>,
33
+ provider: Provider,
34
+ payer: Signer,
35
+ poolPosition: PublicKey,
36
+ overrideTime?: number
37
+ ): Promise<PublicKey> {
38
+ // setup transaction
39
+ const transaction = new Transaction()
40
+ const signers = [payer]
41
+ const referrer = payer.publicKey
42
+
43
+ // Setup public keys
44
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
45
+
46
+ // Find referral account
47
+ let referralAccountPublicKey = await getReferralAccountPublicKey(program.programId, referrer)
48
+
49
+ // Get the referral state PDA
50
+ const referallStatePublicKey = await getReferralStatePublicKey(program.programId)
51
+
52
+ // Get HDG mint public key
53
+ const hedgeMintPublicKey = await getHedgeMintPublicKey(program.programId)
54
+
55
+ // Get HDG account of signer
56
+ const payerHdgAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
57
+ provider.connection,
58
+ payer,
59
+ hedgeMintPublicKey,
60
+ payer.publicKey
61
+ )
62
+
63
+ // Derive the user referral account public key
64
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(program.programId, payer.publicKey)
65
+
66
+ transaction.add(
67
+ await createReferralAccountInstruction(
68
+ program,
69
+ payer.publicKey,
70
+ poolPosition,
71
+ referralAccountPublicKey,
72
+ referallStatePublicKey,
73
+ hedgeMintPublicKey,
74
+ payerHdgAssociatedTokenAccount.address,
75
+ userReferralAccountPublicKey,
76
+ overrideTime
77
+ )
78
+ )
79
+
80
+ await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
81
+ return referralAccountPublicKey
82
+ }
83
+
84
+ export async function createReferralAccountInstruction(
85
+ program: Program<Vault>,
86
+ payerPublicKey: PublicKey,
87
+ poolPositionPublicKey: PublicKey,
88
+ referralAccountPublicKey: PublicKey,
89
+ referralStatePublicKey: PublicKey,
90
+ hedgeMintPublicKey: PublicKey,
91
+ hdgAssociatedTokenAccountPublicKey: PublicKey,
92
+ userReferralAccountPublicKey: PublicKey,
93
+ overrideTime?: number
94
+ ): Promise<TransactionInstruction> {
95
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
96
+
97
+ return await program.methods
98
+ .createReferralAccount(
99
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override times
100
+ )
101
+ .accounts({
102
+ signer: payerPublicKey,
103
+ vaultSystemState: vaultSystemStatePublicKey,
104
+ referralState: referralStatePublicKey,
105
+ referralAccount: referralAccountPublicKey,
106
+ userReferralAccount: userReferralAccountPublicKey,
107
+ poolPosition: poolPositionPublicKey,
108
+ hedgeMint: hedgeMintPublicKey,
109
+ hdgAta: hdgAssociatedTokenAccountPublicKey,
110
+ systemProgram: SystemProgram.programId,
111
+ tokenProgram: TOKEN_PROGRAM_ID,
112
+ rent: SYSVAR_RENT_PUBKEY,
113
+ })
114
+ .instruction()
115
+ }
@@ -1,23 +1,21 @@
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
3
  import {
4
- PublicKey,
5
- sendAndConfirmTransaction,
6
- Signer,
4
+ PublicKey, Signer,
7
5
  SystemProgram,
8
6
  SYSVAR_RENT_PUBKEY,
9
7
  Transaction,
10
- TransactionInstruction,
8
+ TransactionInstruction
11
9
  } from '@solana/web3.js'
12
- import { parseAnchorErrors } from '../utils/Errors'
13
10
  import {
14
11
  findAssociatedTokenAddress,
15
12
  getPoolPublicKeyForMint,
16
13
  getUshMintPublicKey,
17
- getVaultSystemStatePublicKey,
14
+ getVaultSystemStatePublicKey
18
15
  } from '../Constants'
19
- import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
20
16
  import { Vault } from '../idl/vault'
17
+ import { parseAnchorErrors } from '../utils/Errors'
18
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
21
19
 
22
20
  export async function createStakingPool(
23
21
  program: Program<Vault>,
@@ -31,7 +29,7 @@ export async function createStakingPool(
31
29
  await createStakingPoolInstruction(program, payer.publicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime)
32
30
  )
33
31
  await sendAndConfirmWithDebug(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
34
- const [poolPublickey] = await getPoolPublicKeyForMint(mintPublicKey)
32
+ const [poolPublickey] = await getPoolPublicKeyForMint(program.programId, mintPublicKey)
35
33
  return poolPublickey
36
34
  }
37
35
 
@@ -42,12 +40,12 @@ export async function createStakingPoolInstruction(
42
40
  hedgeTokensToBeMinted: number,
43
41
  overrideStartTime?: number
44
42
  ): Promise<TransactionInstruction> {
45
- const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
46
- const ushMintPublickey = await getUshMintPublicKey()
47
- const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(mintPublicKey)
43
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
44
+ const ushMintPublickey = await getUshMintPublicKey(program.programId)
45
+ const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(program.programId, mintPublicKey)
48
46
 
49
- const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
50
- const poolAssociatedUshTokenAccount = await findAssociatedTokenAddress(poolPublickey, ushMintPublickey)
47
+ const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(program.programId, poolPublickey, mintPublicKey)
48
+ const poolAssociatedUshTokenAccount = await findAssociatedTokenAddress(program.programId, poolPublickey, ushMintPublickey)
51
49
  return await program.methods
52
50
  .createStakingPool(poolBump, new BN(hedgeTokensToBeMinted), new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)))
53
51
  .accounts({
@@ -0,0 +1,75 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import {
4
+ PublicKey, Signer,
5
+ SystemProgram,
6
+ SYSVAR_RENT_PUBKEY,
7
+ Transaction,
8
+ TransactionInstruction
9
+ } from '@solana/web3.js'
10
+ import { getReferralAccountPublicKey, getUserReferralAccountPublicKey, getVaultSystemStatePublicKey } from '../Constants'
11
+
12
+ import { Vault } from '../idl/vault'
13
+ import { parseAnchorErrors } from '../utils/Errors'
14
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
15
+
16
+ export async function createUserReferralAccount(
17
+ program: Program<Vault>,
18
+ provider: Provider,
19
+ payer: Signer,
20
+ referrer?: PublicKey,
21
+ overrideTime?: number
22
+ ): Promise<PublicKey> {
23
+ // setup transaction
24
+ const transaction = new Transaction()
25
+ const signers = [payer]
26
+
27
+ // Find referrer account
28
+ let referralAccountPublicKey = null
29
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
30
+ if (typeof referrer === 'undefined') {
31
+ referralAccountPublicKey = await getReferralAccountPublicKey(program.programId, vaultSystemStatePublicKey)
32
+ } else {
33
+ referralAccountPublicKey = await getReferralAccountPublicKey(program.programId, referrer)
34
+ }
35
+
36
+ // Derive the user referral account public key
37
+ const userReferralAccountPublicKey = await getUserReferralAccountPublicKey(program.programId, payer.publicKey)
38
+
39
+ transaction.add(
40
+ await createUserReferralAccountInstruction(
41
+ program,
42
+ payer.publicKey,
43
+ referralAccountPublicKey,
44
+ userReferralAccountPublicKey,
45
+ overrideTime
46
+ )
47
+ )
48
+
49
+ await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
50
+ return userReferralAccountPublicKey
51
+ }
52
+
53
+ export async function createUserReferralAccountInstruction(
54
+ program: Program<Vault>,
55
+ payerPublicKey: PublicKey,
56
+ referralAccount: PublicKey,
57
+ userReferralAccount: PublicKey,
58
+ overrideTime?: number
59
+ ): Promise<TransactionInstruction> {
60
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey(program.programId)
61
+
62
+ return await program.methods
63
+ .createUserReferralAccount(
64
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
65
+ )
66
+ .accounts({
67
+ signer: payerPublicKey,
68
+ userReferralAccount: userReferralAccount,
69
+ referralAccount: referralAccount,
70
+ systemProgram: SystemProgram.programId,
71
+ tokenProgram: TOKEN_PROGRAM_ID,
72
+ rent: SYSVAR_RENT_PUBKEY,
73
+ })
74
+ .instruction()
75
+ }