hedge-web3 0.2.14 → 0.2.17
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.
- package/declarations/idl/vault.d.ts +266 -11
- package/declarations/index.d.ts +2 -0
- package/declarations/instructions/psmMintUsh.d.ts +6 -0
- package/declarations/instructions/psmRedeemUsh.d.ts +6 -0
- package/lib/idl/vault.js +266 -11
- package/lib/index.js +2 -0
- package/lib/instructions/psmMintUsh.js +65 -0
- package/lib/instructions/psmRedeemUsh.js +65 -0
- package/package.json +1 -1
- package/src/idl/vault.ts +532 -22
- package/src/index.ts +2 -0
- package/src/instructions/psmMintUsh.ts +129 -0
- package/src/instructions/psmRedeemUsh.ts +129 -0
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,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
|
+
}
|