hedge-web3 0.2.15 → 0.2.18

Sign up to get free protection for your applications and to get access to all the features.
package/src/index.ts CHANGED
@@ -8,6 +8,8 @@ export * from './instructions/closeLiquidationPoolPosition'
8
8
  export * from './instructions/claimLiquidationPoolPosition'
9
9
  export * from './instructions/closeClaimedLiquidationPoolPosition'
10
10
  export * from './instructions/createVault'
11
+ export * from './instructions/psmMintUsh'
12
+ export * from './instructions/psmRedeemUsh'
11
13
  export * from './instructions/depositVault'
12
14
  export * from './instructions/withdrawVault'
13
15
  export * from './instructions/loanVault'
@@ -0,0 +1,124 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getHedgeMintPublicKey,
16
+ getPoolPublicKeyForMint,
17
+ getUshMintPublicKey,
18
+ getVaultSystemStatePublicKey,
19
+ HEDGE_PROGRAM_PUBLICKEY,
20
+ } from '../Constants'
21
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
+ import { Vault } from '../idl/vault'
23
+ import { parseAnchorErrors } from '../utils/Errors'
24
+ import { VaultAccount } from '../state/VaultAccount'
25
+
26
+ export enum psmStatus {
27
+ PsmEnabled,
28
+ PsmDisabled,
29
+ MintOnly,
30
+ RedeemOnly
31
+ }
32
+
33
+ export async function psmEditAccount(
34
+ program: Program<Vault>,
35
+ provider: Provider,
36
+ payer: Signer,
37
+ collateralMint: PublicKey,
38
+ mintFee: number,
39
+ redeemFee: number,
40
+ debtLimit: number,
41
+ minSwap: number,
42
+ overrideTime?: number
43
+ ): Promise<PublicKey> {
44
+ const ushMintPublickey = await getUshMintPublicKey()
45
+
46
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
47
+ provider.connection,
48
+ payer,
49
+ ushMintPublickey,
50
+ payer.publicKey
51
+ )
52
+
53
+ const payerCollateralAccount = await getOrCreateAssociatedTokenAccount(
54
+ provider.connection,
55
+ payer,
56
+ collateralMint,
57
+ payer.publicKey
58
+ )
59
+
60
+
61
+ const enc = new TextEncoder()
62
+ const [psmAccount] = await PublicKey.findProgramAddress(
63
+ [enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')],
64
+ HEDGE_PROGRAM_PUBLICKEY
65
+ )
66
+
67
+ const psmAccountAta = await getOrCreateAssociatedTokenAccount(
68
+ provider.connection,
69
+ payer,
70
+ collateralMint,
71
+ psmAccount,
72
+ true
73
+ )
74
+
75
+ const transaction = new Transaction().add(
76
+ await psmEditAccountInstruction(
77
+ program,
78
+ payer.publicKey,
79
+ psmAccount,
80
+ collateralMint,
81
+ new BN(mintFee),
82
+ new BN(redeemFee),
83
+ new BN(debtLimit),
84
+ new BN(minSwap),
85
+ overrideTime
86
+ )
87
+ )
88
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
89
+ return payer.publicKey
90
+ }
91
+
92
+ export async function psmEditAccountInstruction(
93
+ program: Program<Vault>,
94
+ payerPublicKey: PublicKey,
95
+ psmAccount: PublicKey,
96
+ collateralMint: PublicKey,
97
+ mintFee: BN,
98
+ redeemFee: BN,
99
+ debtLimit: BN,
100
+ minSwap: BN,
101
+ overrideTime?: number
102
+ ): Promise<TransactionInstruction> {
103
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
104
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
105
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
106
+
107
+
108
+ return await program.methods
109
+ .psmEditAccount(
110
+ mintFee,
111
+ redeemFee,
112
+ debtLimit,
113
+ minSwap,
114
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
115
+ )
116
+ .accounts({
117
+ payer: payerPublicKey,
118
+ vaultSystemState: vaultSystemStatePublicKey,
119
+ psmAccount: psmAccount,
120
+ collateralMint: collateralMint,
121
+ systemProgram: SystemProgram.programId,
122
+ })
123
+ .instruction()
124
+ }
@@ -0,0 +1,129 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getHedgeMintPublicKey,
16
+ getPoolPublicKeyForMint,
17
+ getUshMintPublicKey,
18
+ getVaultSystemStatePublicKey,
19
+ HEDGE_PROGRAM_PUBLICKEY,
20
+ } from '../Constants'
21
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
+ import { Vault } from '../idl/vault'
23
+ import { parseAnchorErrors } from '../utils/Errors'
24
+ import { VaultAccount } from '../state/VaultAccount'
25
+
26
+ export async function psmMintUsh(
27
+ program: Program<Vault>,
28
+ provider: Provider,
29
+ payer: Signer,
30
+ collateralMint: PublicKey,
31
+ collateralAmount: number,
32
+ overrideTime?: number
33
+ ): Promise<PublicKey> {
34
+ const ushMintPublickey = await getUshMintPublicKey()
35
+
36
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
37
+ provider.connection,
38
+ payer,
39
+ ushMintPublickey,
40
+ payer.publicKey
41
+ )
42
+
43
+ const payerCollateralAccount = await getOrCreateAssociatedTokenAccount(
44
+ provider.connection,
45
+ payer,
46
+ collateralMint,
47
+ payer.publicKey
48
+ )
49
+
50
+
51
+ const enc = new TextEncoder()
52
+ const [psmAccount] = await PublicKey.findProgramAddress(
53
+ [enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')],
54
+ HEDGE_PROGRAM_PUBLICKEY
55
+ )
56
+
57
+ const psmAccountAta = await getOrCreateAssociatedTokenAccount(
58
+ provider.connection,
59
+ payer,
60
+ collateralMint,
61
+ psmAccount,
62
+ true
63
+ )
64
+
65
+ const transaction = new Transaction().add(
66
+ await psmMintUshInstruction(
67
+ program,
68
+ payer.publicKey,
69
+ psmAccount,
70
+ collateralMint,
71
+ new BN(collateralAmount),
72
+ overrideTime
73
+ )
74
+ )
75
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
76
+ return payer.publicKey
77
+ }
78
+
79
+ export async function psmMintUshInstruction(
80
+ program: Program<Vault>,
81
+ payerPublicKey: PublicKey,
82
+ psmAccount: PublicKey,
83
+ collateralMint: PublicKey,
84
+ collateralAmount: BN,
85
+ overrideTime?: number
86
+ ): Promise<TransactionInstruction> {
87
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
88
+ const ushMintPublickey = await getUshMintPublicKey()
89
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
90
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
91
+ const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
92
+ hedgeStakingPoolPublicKey,
93
+ ushMintPublickey
94
+ )
95
+ const psmAccountAta = await findAssociatedTokenAddress(
96
+ psmAccount,
97
+ collateralMint
98
+ )
99
+ const ownerCollateralAccount = await findAssociatedTokenAddress(
100
+ payerPublicKey,
101
+ collateralMint
102
+ )
103
+
104
+ const ownerUshAccount = await findAssociatedTokenAddress(
105
+ payerPublicKey,
106
+ ushMintPublickey
107
+ )
108
+
109
+ return await program.methods
110
+ .psmMintUsh(
111
+ collateralAmount,
112
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
113
+ )
114
+ .accounts({
115
+ payer: payerPublicKey,
116
+ vaultSystemState: vaultSystemStatePublicKey,
117
+ feePool: hedgeStakingPoolPublicKey,
118
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
119
+ psmAccount: psmAccount,
120
+ psmAccountAta: psmAccountAta,
121
+ ownerUshAccount: ownerUshAccount,
122
+ ownerCollateralAccount: ownerCollateralAccount,
123
+ collateralMint: collateralMint,
124
+ ushMint: ushMintPublickey,
125
+ systemProgram: SystemProgram.programId,
126
+ tokenProgram: TOKEN_PROGRAM_ID,
127
+ })
128
+ .instruction()
129
+ }
@@ -0,0 +1,129 @@
1
+ import { BN, Program, Provider } from '@project-serum/anchor'
2
+ import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import {
4
+ Keypair,
5
+ PublicKey,
6
+ sendAndConfirmTransaction,
7
+ Signer,
8
+ SystemProgram,
9
+ Transaction,
10
+ TransactionInstruction,
11
+ } from '@solana/web3.js'
12
+ import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
13
+ import {
14
+ findAssociatedTokenAddress,
15
+ getHedgeMintPublicKey,
16
+ getPoolPublicKeyForMint,
17
+ getUshMintPublicKey,
18
+ getVaultSystemStatePublicKey,
19
+ HEDGE_PROGRAM_PUBLICKEY,
20
+ } from '../Constants'
21
+ import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
22
+ import { Vault } from '../idl/vault'
23
+ import { parseAnchorErrors } from '../utils/Errors'
24
+ import { VaultAccount } from '../state/VaultAccount'
25
+
26
+ export async function psmRedeemUsh(
27
+ program: Program<Vault>,
28
+ provider: Provider,
29
+ payer: Signer,
30
+ collateralMint: PublicKey,
31
+ ushAmount: number,
32
+ overrideTime?: number
33
+ ): Promise<PublicKey> {
34
+ const ushMintPublickey = await getUshMintPublicKey()
35
+
36
+ const payerUshAccount = await getOrCreateAssociatedTokenAccount(
37
+ provider.connection,
38
+ payer,
39
+ ushMintPublickey,
40
+ payer.publicKey
41
+ )
42
+
43
+ const payerCollateralAccount = await getOrCreateAssociatedTokenAccount(
44
+ provider.connection,
45
+ payer,
46
+ collateralMint,
47
+ payer.publicKey
48
+ )
49
+
50
+
51
+ const enc = new TextEncoder()
52
+ const [psmAccount] = await PublicKey.findProgramAddress(
53
+ [enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')],
54
+ HEDGE_PROGRAM_PUBLICKEY
55
+ )
56
+
57
+ const psmAccountAta = await getOrCreateAssociatedTokenAccount(
58
+ provider.connection,
59
+ payer,
60
+ collateralMint,
61
+ psmAccount,
62
+ true
63
+ )
64
+
65
+ const transaction = new Transaction().add(
66
+ await psmRedeemUshInstruction(
67
+ program,
68
+ payer.publicKey,
69
+ psmAccount,
70
+ collateralMint,
71
+ new BN(ushAmount),
72
+ overrideTime
73
+ )
74
+ )
75
+ await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
76
+ return payer.publicKey
77
+ }
78
+
79
+ export async function psmRedeemUshInstruction(
80
+ program: Program<Vault>,
81
+ payerPublicKey: PublicKey,
82
+ psmAccount: PublicKey,
83
+ collateralMint: PublicKey,
84
+ ushAmount: BN,
85
+ overrideTime?: number
86
+ ): Promise<TransactionInstruction> {
87
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
88
+ const ushMintPublickey = await getUshMintPublicKey()
89
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
90
+ const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
91
+ const hedgeStakingPoolAssociatedUshTokenAccount = await findAssociatedTokenAddress(
92
+ hedgeStakingPoolPublicKey,
93
+ ushMintPublickey
94
+ )
95
+ const psmAccountAta = await findAssociatedTokenAddress(
96
+ psmAccount,
97
+ collateralMint
98
+ )
99
+ const ownerCollateralAccount = await findAssociatedTokenAddress(
100
+ payerPublicKey,
101
+ collateralMint
102
+ )
103
+
104
+ const ownerUshAccount = await findAssociatedTokenAddress(
105
+ payerPublicKey,
106
+ ushMintPublickey
107
+ )
108
+
109
+ return await program.methods
110
+ .psmRedeemUsh(
111
+ ushAmount,
112
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
113
+ )
114
+ .accounts({
115
+ payer: payerPublicKey,
116
+ vaultSystemState: vaultSystemStatePublicKey,
117
+ feePool: hedgeStakingPoolPublicKey,
118
+ feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
119
+ psmAccount: psmAccount,
120
+ psmAccountAta: psmAccountAta,
121
+ ownerUshAccount: ownerUshAccount,
122
+ ownerCollateralAccount: ownerCollateralAccount,
123
+ collateralMint: collateralMint,
124
+ ushMint: ushMintPublickey,
125
+ systemProgram: SystemProgram.programId,
126
+ tokenProgram: TOKEN_PROGRAM_ID,
127
+ })
128
+ .instruction()
129
+ }