hedge-web3 0.3.0 → 0.3.2
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 +226 -366
- package/declarations/utils/sendAndConfirmWithDebug.d.ts +2 -2
- package/lib/Constants.js +13 -13
- package/lib/HedgeConstants.js +1 -1
- package/lib/idl/vault.js +226 -366
- package/lib/instructions/createVault.js +15 -29
- package/lib/instructions/depositVault.js +5 -15
- package/lib/instructions/initHedgeFoundation.js +14 -2
- package/lib/instructions/loanVault.js +1 -1
- package/lib/instructions/redeemVault.js +0 -1
- package/lib/instructions/refreshOraclePrice.js +1 -1
- package/lib/instructions/withdrawVault.js +2 -2
- package/lib/state/VaultAccount.js +1 -1
- package/lib/utils/sendAndConfirmWithDebug.js +49 -13
- package/package.json +1 -1
- package/src/HedgeConstants.ts +1 -1
- package/src/idl/vault.ts +451 -731
- package/src/instructions/createVault.ts +24 -35
- package/src/instructions/depositVault.ts +22 -16
- package/src/instructions/initHedgeFoundation.ts +17 -2
- package/src/instructions/loanVault.ts +1 -1
- package/src/instructions/redeemVault.ts +0 -1
- package/src/instructions/refreshOraclePrice.ts +1 -1
- package/src/instructions/withdrawVault.ts +2 -2
- package/src/state/VaultAccount.ts +1 -1
- package/src/utils/sendAndConfirmWithDebug.ts +72 -23
@@ -1,6 +1,10 @@
|
|
1
1
|
import { BN, Program, Provider } from '@coral-xyz/anchor'
|
2
|
-
import {
|
3
|
-
|
2
|
+
import {
|
3
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
4
|
+
getOrCreateAssociatedTokenAccount,
|
5
|
+
NATIVE_MINT,
|
6
|
+
TOKEN_PROGRAM_ID,
|
7
|
+
} from '@solana/spl-token'
|
4
8
|
import {
|
5
9
|
Keypair,
|
6
10
|
PublicKey,
|
@@ -24,6 +28,8 @@ import { v4 as uuidv4 } from 'uuid'
|
|
24
28
|
import { Vault } from '../idl/vault'
|
25
29
|
import { parseAnchorErrors } from '../utils/Errors'
|
26
30
|
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
31
|
+
import { createInitializeAccountInstruction } from '@solana/spl-token'
|
32
|
+
import { createCloseAccountInstruction } from '@solana/spl-token'
|
27
33
|
|
28
34
|
export async function createVault(
|
29
35
|
program: Program<Vault>,
|
@@ -61,7 +67,7 @@ export async function createVault(
|
|
61
67
|
const transaction = new Transaction()
|
62
68
|
const signers = [payer, history]
|
63
69
|
|
64
|
-
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() ===
|
70
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === NATIVE_MINT.toString()
|
65
71
|
|
66
72
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
67
73
|
program.programId,
|
@@ -82,14 +88,11 @@ export async function createVault(
|
|
82
88
|
programId: TOKEN_PROGRAM_ID,
|
83
89
|
space: 165,
|
84
90
|
}),
|
85
|
-
|
86
|
-
account: wrappedSolAccount.publicKey,
|
87
|
-
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
88
|
-
owner: payer.publicKey,
|
89
|
-
})
|
91
|
+
createInitializeAccountInstruction(wrappedSolAccount.publicKey, NATIVE_MINT, payer.publicKey)
|
90
92
|
)
|
91
93
|
signers.push(wrappedSolAccount)
|
92
94
|
}
|
95
|
+
|
93
96
|
transaction.add(
|
94
97
|
await createVaultInstruction(
|
95
98
|
program,
|
@@ -108,17 +111,11 @@ export async function createVault(
|
|
108
111
|
overrideTime
|
109
112
|
)
|
110
113
|
)
|
114
|
+
|
111
115
|
if (isWrappedSol) {
|
112
|
-
transaction.add(
|
113
|
-
TokenInstructions.closeAccount({
|
114
|
-
source: wrappedSolAccount.publicKey,
|
115
|
-
destination: payer.publicKey,
|
116
|
-
owner: payer.publicKey,
|
117
|
-
})
|
118
|
-
)
|
116
|
+
transaction.add(createCloseAccountInstruction(wrappedSolAccount.publicKey, payer.publicKey, payer.publicKey))
|
119
117
|
}
|
120
|
-
|
121
|
-
await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
|
118
|
+
const result = await sendAndConfirmWithDebug(provider.connection, transaction, signers).catch(parseAnchorErrors)
|
122
119
|
return newVaultPublicKey
|
123
120
|
}
|
124
121
|
|
@@ -137,7 +134,8 @@ export async function buildCreateVaultTransaction(
|
|
137
134
|
const { blockhash, lastValidBlockHeight } = await program.provider.connection.getLatestBlockhash()
|
138
135
|
const transaction = new Transaction({
|
139
136
|
feePayer: payerPublicKey,
|
140
|
-
|
137
|
+
blockhash: blockhash,
|
138
|
+
lastValidBlockHeight: lastValidBlockHeight,
|
141
139
|
})
|
142
140
|
const signers = [history]
|
143
141
|
const wrappedSolAccount = Keypair.generate()
|
@@ -152,7 +150,7 @@ export async function buildCreateVaultTransaction(
|
|
152
150
|
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
153
151
|
console.log('Lookup vaultTypeAccountInfo', vaultTypeAccountInfo)
|
154
152
|
|
155
|
-
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() ===
|
153
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === NATIVE_MINT.toString()
|
156
154
|
|
157
155
|
const payerTokenAccount = await findAssociatedTokenAddress(
|
158
156
|
program.programId,
|
@@ -187,11 +185,7 @@ export async function buildCreateVaultTransaction(
|
|
187
185
|
programId: TOKEN_PROGRAM_ID,
|
188
186
|
space: 165,
|
189
187
|
}),
|
190
|
-
|
191
|
-
account: wrappedSolAccount.publicKey,
|
192
|
-
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
193
|
-
owner: payerPublicKey,
|
194
|
-
})
|
188
|
+
createInitializeAccountInstruction(wrappedSolAccount.publicKey, NATIVE_MINT, payerPublicKey)
|
195
189
|
)
|
196
190
|
signers.push(wrappedSolAccount)
|
197
191
|
}
|
@@ -214,14 +208,9 @@ export async function buildCreateVaultTransaction(
|
|
214
208
|
overrideTime
|
215
209
|
)
|
216
210
|
)
|
211
|
+
|
217
212
|
if (isWrappedSol) {
|
218
|
-
transaction.add(
|
219
|
-
TokenInstructions.closeAccount({
|
220
|
-
source: wrappedSolAccount.publicKey,
|
221
|
-
destination: payerPublicKey,
|
222
|
-
owner: payerPublicKey,
|
223
|
-
})
|
224
|
-
)
|
213
|
+
transaction.add(createCloseAccountInstruction(wrappedSolAccount.publicKey, payerPublicKey, payerPublicKey))
|
225
214
|
transaction.partialSign(wrappedSolAccount)
|
226
215
|
}
|
227
216
|
|
@@ -253,20 +242,20 @@ export async function createVaultInstruction(
|
|
253
242
|
.createVault(
|
254
243
|
salt,
|
255
244
|
new BN(depositAmount),
|
256
|
-
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override
|
245
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override time
|
257
246
|
)
|
258
247
|
.accounts({
|
259
248
|
vaultSystemState: vaultSystemStatePublicKey,
|
260
249
|
vaultTypeAccount: vaultTypeAccount,
|
261
250
|
vault: vaultPublicKey,
|
262
251
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
263
|
-
feePool: feePool,
|
264
|
-
feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccount,
|
252
|
+
// feePool: feePool,
|
253
|
+
// feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccount,
|
265
254
|
history: historyPublicKey,
|
266
255
|
payer: payerPublicKey,
|
267
256
|
payerTokenAccount: payerTokenAccountPublicKey,
|
268
257
|
collateralMint: collateralMint,
|
269
|
-
ushMint: ushMintPublickey,
|
258
|
+
// ushMint: ushMintPublickey,
|
270
259
|
systemProgram: SystemProgram.programId,
|
271
260
|
tokenProgram: TOKEN_PROGRAM_ID,
|
272
261
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
@@ -1,7 +1,12 @@
|
|
1
1
|
import { BN, Program, Provider } from '@coral-xyz/anchor'
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
2
|
+
import {
|
3
|
+
NATIVE_MINT,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
getOrCreateAssociatedTokenAccount,
|
6
|
+
createInitializeAccountInstruction,
|
7
|
+
createCloseAccountInstruction
|
8
|
+
} from '@solana/spl-token'
|
9
|
+
|
5
10
|
import {
|
6
11
|
Keypair,
|
7
12
|
PublicKey, Signer,
|
@@ -80,7 +85,7 @@ export async function depositVault(
|
|
80
85
|
false
|
81
86
|
)
|
82
87
|
|
83
|
-
if (vaultTypeAccountInfo.collateralMint.toString() ===
|
88
|
+
if (vaultTypeAccountInfo.collateralMint.toString() === NATIVE_MINT.toString()) {
|
84
89
|
transaction.add(
|
85
90
|
SystemProgram.createAccount({
|
86
91
|
fromPubkey: payer.publicKey,
|
@@ -89,11 +94,11 @@ export async function depositVault(
|
|
89
94
|
programId: TOKEN_PROGRAM_ID,
|
90
95
|
space: 165,
|
91
96
|
}),
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
+
createInitializeAccountInstruction(
|
98
|
+
wrappedSolAccount.publicKey,
|
99
|
+
NATIVE_MINT,
|
100
|
+
payer.publicKey
|
101
|
+
)
|
97
102
|
)
|
98
103
|
signers.push(wrappedSolAccount)
|
99
104
|
}
|
@@ -102,7 +107,7 @@ export async function depositVault(
|
|
102
107
|
program,
|
103
108
|
vaultSystemStatePublicKey,
|
104
109
|
payer.publicKey,
|
105
|
-
vaultTypeAccountInfo.collateralMint.toString() ===
|
110
|
+
vaultTypeAccountInfo.collateralMint.toString() === NATIVE_MINT.toString()
|
106
111
|
? wrappedSolAccount.publicKey
|
107
112
|
: payerTokenAccount,
|
108
113
|
vaultPublicKey,
|
@@ -121,13 +126,14 @@ export async function depositVault(
|
|
121
126
|
overrideTime
|
122
127
|
)
|
123
128
|
)
|
124
|
-
if (vaultTypeAccountInfo.collateralMint.toString() ===
|
129
|
+
if (vaultTypeAccountInfo.collateralMint.toString() === NATIVE_MINT.toString()) {
|
125
130
|
transaction.add(
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
+
createCloseAccountInstruction(
|
132
|
+
wrappedSolAccount.publicKey,
|
133
|
+
payer.publicKey,
|
134
|
+
payer.publicKey,
|
135
|
+
[]
|
136
|
+
)
|
131
137
|
)
|
132
138
|
}
|
133
139
|
|
@@ -21,12 +21,27 @@ import { parseAnchorErrors } from '../utils/Errors'
|
|
21
21
|
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
22
22
|
|
23
23
|
export async function initHedgeFoundation(program: Program<Vault>, provider: Provider, payer: Signer): Promise<PublicKey> {
|
24
|
+
console.log('Initializing Hedge Foundation...')
|
24
25
|
const poolEra = Keypair.generate()
|
25
|
-
|
26
|
+
console.log('Generated poolEra:', poolEra.publicKey.toBase58())
|
27
|
+
|
28
|
+
const transaction = new Transaction()
|
29
|
+
transaction.feePayer = payer.publicKey
|
30
|
+
|
31
|
+
transaction.add(
|
26
32
|
await initHedgeFoundationInstruction(program, poolEra.publicKey, payer.publicKey)
|
27
33
|
)
|
34
|
+
console.log('Created transaction with initHedgeFoundationInstruction')
|
35
|
+
console.log('Payer public key:', payer.publicKey.toBase58())
|
28
36
|
|
29
|
-
|
37
|
+
console.log('Sending transaction...')
|
38
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, poolEra])
|
39
|
+
.catch(err => {
|
40
|
+
console.error('Failed to initialize Hedge Foundation:', err)
|
41
|
+
throw parseAnchorErrors(err)
|
42
|
+
})
|
43
|
+
|
44
|
+
console.log('Successfully initialized Hedge Foundation')
|
30
45
|
return payer.publicKey
|
31
46
|
}
|
32
47
|
|
@@ -90,7 +90,7 @@ export async function loanVault(
|
|
90
90
|
overrideTime
|
91
91
|
)
|
92
92
|
)
|
93
|
-
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history])
|
93
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer, history], 300000)
|
94
94
|
return vaultPublicKey
|
95
95
|
}
|
96
96
|
|
@@ -23,7 +23,7 @@ export async function refreshOraclePrice(
|
|
23
23
|
const transaction = new Transaction().add(
|
24
24
|
await refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime)
|
25
25
|
)
|
26
|
-
return await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
|
26
|
+
return await sendAndConfirmWithDebug(provider.connection, transaction, [payer], 800000)
|
27
27
|
}
|
28
28
|
|
29
29
|
export async function refreshOraclePriceInstruction(
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { BN, Program, Provider } from '@coral-xyz/anchor'
|
2
|
-
import { TokenInstructions } from '@project-serum/serum'
|
3
2
|
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { NATIVE_MINT } from '@solana/spl-token'
|
4
4
|
import {
|
5
5
|
Keypair,
|
6
6
|
PublicKey, Signer,
|
@@ -35,7 +35,7 @@ export async function withdrawVault(
|
|
35
35
|
const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(
|
36
36
|
provider.connection,
|
37
37
|
payer,
|
38
|
-
|
38
|
+
NATIVE_MINT,
|
39
39
|
vaultPublicKey,
|
40
40
|
true
|
41
41
|
)
|
@@ -2,7 +2,7 @@ import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
|
2
2
|
import Decimal from 'decimal.js'
|
3
3
|
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
4
|
|
5
|
-
import * as borsh from '@
|
5
|
+
import * as borsh from '@coral-xyz/borsh'
|
6
6
|
import BN from 'bn.js'
|
7
7
|
import VaultType from './VaultType'
|
8
8
|
|
@@ -1,27 +1,76 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
Connection,
|
3
|
+
Signer,
|
4
|
+
Transaction,
|
5
|
+
TransactionSignature,
|
6
|
+
VersionedTransaction,
|
7
|
+
ComputeBudgetProgram,
|
8
|
+
Keypair,
|
9
|
+
LAMPORTS_PER_SOL
|
10
|
+
} from '@solana/web3.js'
|
2
11
|
|
3
12
|
export default async function sendAndConfirmWithDebug(
|
4
13
|
connection: Connection,
|
5
|
-
transaction: Transaction,
|
6
|
-
signers: Signer[]
|
14
|
+
transaction: Transaction | VersionedTransaction,
|
15
|
+
signers: Signer[],
|
16
|
+
computeUnits?: number
|
7
17
|
): Promise<TransactionSignature | void> {
|
8
|
-
|
9
|
-
|
10
|
-
.
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
throw
|
26
|
-
}
|
27
|
-
|
18
|
+
try {
|
19
|
+
// Get the latest blockhash before sending
|
20
|
+
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
|
21
|
+
|
22
|
+
// Set blockhash AND feePayer BEFORE converting to VersionedTransaction
|
23
|
+
if (transaction instanceof Transaction) {
|
24
|
+
// Add compute unit instruction FIRST if requested
|
25
|
+
if (computeUnits) {
|
26
|
+
const computeIx = ComputeBudgetProgram.setComputeUnitLimit({ units: computeUnits });
|
27
|
+
transaction.instructions.unshift(computeIx);
|
28
|
+
}
|
29
|
+
transaction.recentBlockhash = blockhash;
|
30
|
+
// Ensure feePayer is explicitly set on legacy transaction
|
31
|
+
if (!transaction.feePayer) {
|
32
|
+
transaction.feePayer = signers[0].publicKey;
|
33
|
+
}
|
34
|
+
} else if (computeUnits) {
|
35
|
+
throw new Error('Compute units must be added before creating VersionedTransaction');
|
36
|
+
}
|
37
|
+
|
38
|
+
// Convert legacy Transaction to VersionedTransaction if needed
|
39
|
+
const versionedTx = transaction instanceof Transaction
|
40
|
+
? new VersionedTransaction(transaction.compileMessage())
|
41
|
+
: transaction;
|
42
|
+
|
43
|
+
// For VersionedTransaction, verify blockhash is set
|
44
|
+
if (!(transaction instanceof Transaction) && !versionedTx.message.recentBlockhash) {
|
45
|
+
throw new Error('Versioned transaction requires recent blockhash');
|
46
|
+
}
|
47
|
+
|
48
|
+
// Sign the transaction if signers are provided
|
49
|
+
if (signers.length > 0) {
|
50
|
+
versionedTx.sign(signers);
|
51
|
+
}
|
52
|
+
|
53
|
+
// Send the transaction
|
54
|
+
const signature = await connection.sendTransaction(versionedTx);
|
55
|
+
|
56
|
+
// Confirm using the newer confirmation strategy
|
57
|
+
const confirmationStrategy = {
|
58
|
+
signature,
|
59
|
+
blockhash,
|
60
|
+
lastValidBlockHeight
|
61
|
+
};
|
62
|
+
|
63
|
+
try {
|
64
|
+
await connection.confirmTransaction(confirmationStrategy);
|
65
|
+
return signature;
|
66
|
+
} catch (error) {
|
67
|
+
console.log('There was an error confirming the transaction', error);
|
68
|
+
console.trace();
|
69
|
+
throw error;
|
70
|
+
}
|
71
|
+
} catch (error) {
|
72
|
+
console.log('There was an error sending the transaction', error);
|
73
|
+
console.trace();
|
74
|
+
throw error;
|
75
|
+
}
|
76
|
+
}
|