hedge-web3 0.1.14 → 0.1.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/{lib/types/src → declarations}/Constants.d.ts +2 -3
- package/{lib/types/src → declarations}/HedgeDecimal.d.ts +2 -3
- package/{lib/types/src → declarations}/StakingPools.d.ts +0 -1
- package/{lib/types/src → declarations}/Vaults.d.ts +0 -1
- package/{lib/types/src → declarations}/idl/idl.d.ts +0 -1
- package/declarations/idl/vault.d.ts +2283 -0
- package/{lib/types/src → declarations}/index.d.ts +2 -1
- package/{lib/types/src → declarations}/instructions/claimLiquidationPoolPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/closeLiquidationPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/createStakingPool.d.ts +0 -1
- package/declarations/instructions/createVault.d.ts +5 -0
- package/{lib/types/src → declarations}/instructions/depositLiquidationPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositVault.d.ts +2 -3
- package/declarations/instructions/initHedgeFoundation.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/liquidateVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/loanVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/redeemVault.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/refreshOraclePrice.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/repayVault.d.ts +2 -3
- package/declarations/instructions/setHalted.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/withdrawStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/withdrawVault.d.ts +2 -3
- package/{lib/types/src → declarations}/state/LiquidationPoolEra.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPoolState.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/state/StakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/state/StakingPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/state/VaultAccount.d.ts +7 -5
- package/{lib/types/src → declarations}/state/VaultHistoryEvent.d.ts +0 -1
- package/{lib/types/src → declarations}/utils/Errors.d.ts +0 -1
- package/lib/Constants.js +86 -0
- package/lib/HedgeDecimal.js +24 -0
- package/lib/StakingPools.js +15 -0
- package/lib/Vaults.js +20 -0
- package/lib/idl/idl.js +1475 -0
- package/lib/idl/vault.js +2285 -0
- package/lib/index.js +36 -2382
- package/lib/instructions/claimLiquidationPoolPosition.js +53 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +60 -0
- package/lib/instructions/createStakingPool.js +52 -0
- package/lib/instructions/createVault.js +152 -0
- package/lib/instructions/depositLiquidationPool.js +55 -0
- package/lib/instructions/depositStakingPool.js +52 -0
- package/lib/instructions/depositVault.js +87 -0
- package/lib/instructions/initHedgeFoundation.js +55 -0
- package/lib/instructions/liquidateVault.js +80 -0
- package/lib/instructions/loanVault.js +61 -0
- package/lib/instructions/redeemVault.js +65 -0
- package/lib/instructions/refreshOraclePrice.js +65 -0
- package/lib/instructions/repayVault.js +62 -0
- package/lib/instructions/setHalted.js +37 -0
- package/lib/instructions/withdrawStakingPool.js +64 -0
- package/lib/instructions/withdrawVault.js +62 -0
- package/lib/state/LiquidationPoolEra.js +19 -0
- package/lib/state/LiquidationPoolState.js +12 -0
- package/lib/state/LiquidationPosition.js +39 -0
- package/lib/state/StakingPool.js +22 -0
- package/lib/state/StakingPoolPosition.js +28 -0
- package/lib/state/VaultAccount.js +55 -0
- package/lib/state/VaultHistoryEvent.js +45 -0
- package/lib/utils/Errors.js +14 -0
- package/package.json +9 -7
- package/src/Constants.ts +5 -4
- package/src/HedgeDecimal.ts +3 -3
- package/src/idl/vault.ts +4565 -0
- package/src/index.ts +2 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +19 -11
- package/src/instructions/closeLiquidationPoolPosition.ts +7 -13
- package/src/instructions/createVault.ts +224 -57
- package/src/instructions/depositLiquidationPool.ts +6 -9
- package/src/instructions/depositStakingPool.ts +3 -1
- package/src/instructions/depositVault.ts +41 -29
- package/src/instructions/initHedgeFoundation.ts +64 -0
- package/src/instructions/liquidateVault.ts +30 -33
- package/src/instructions/loanVault.ts +22 -24
- package/src/instructions/redeemVault.ts +15 -28
- package/src/instructions/refreshOraclePrice.ts +2 -2
- package/src/instructions/repayVault.ts +20 -22
- package/src/instructions/setHalted.ts +48 -0
- package/src/instructions/withdrawVault.ts +36 -26
- package/src/state/LiquidationPosition.ts +2 -2
- package/src/state/VaultAccount.ts +20 -9
- package/tsconfig.json +3 -2
- package/.github/workflows/npm-publish.yml +0 -34
- package/README.md +0 -44
- package/jsconfig.json +0 -12
- package/lib/index.js.map +0 -1
- package/lib/types/src/Constants.d.ts.map +0 -1
- package/lib/types/src/HedgeDecimal.d.ts.map +0 -1
- package/lib/types/src/StakingPools.d.ts.map +0 -1
- package/lib/types/src/Vaults.d.ts.map +0 -1
- package/lib/types/src/idl/idl.d.ts.map +0 -1
- package/lib/types/src/index.d.ts.map +0 -1
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/createStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/createVault.d.ts +0 -5
- package/lib/types/src/instructions/createVault.d.ts.map +0 -1
- package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
- package/lib/types/src/instructions/liquidateVault.d.ts.map +0 -1
- package/lib/types/src/instructions/loanVault.d.ts.map +0 -1
- package/lib/types/src/instructions/redeemVault.d.ts.map +0 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +0 -1
- package/lib/types/src/instructions/repayVault.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawVault.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolState.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPosition.d.ts.map +0 -1
- package/lib/types/src/state/StakingPool.d.ts.map +0 -1
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +0 -1
- package/lib/types/src/state/VaultAccount.d.ts.map +0 -1
- package/lib/types/src/state/VaultHistoryEvent.d.ts.map +0 -1
- package/lib/types/src/utils/Errors.d.ts.map +0 -1
- package/lib/types/tsconfig.base.tsbuildinfo +0 -1
- package/rollup.config.js +0 -42
package/src/index.ts
CHANGED
@@ -13,6 +13,8 @@ export * from './instructions/repayVault'
|
|
13
13
|
export * from './instructions/redeemVault'
|
14
14
|
export * from './instructions/liquidateVault'
|
15
15
|
export * from './instructions/refreshOraclePrice'
|
16
|
+
export * from './instructions/initHedgeFoundation'
|
17
|
+
export * from './instructions/setHalted'
|
16
18
|
|
17
19
|
export * from './HedgeDecimal'
|
18
20
|
export * from './Constants'
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { Program, Provider } from '@project-serum/anchor'
|
2
|
-
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
4
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import { findAssociatedTokenAddress,
|
5
|
+
import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getLiquidationPoolStatePublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function claimLiquidationPoolPosition (
|
8
8
|
program: Program,
|
@@ -12,48 +12,56 @@ export async function claimLiquidationPoolPosition (
|
|
12
12
|
collateralType: string,
|
13
13
|
overrideStartTime?: number
|
14
14
|
): Promise<PublicKey> {
|
15
|
-
const
|
16
|
-
const
|
17
|
-
const collateralMint =
|
15
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
|
16
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
17
|
+
const collateralMint = vaultTypeAccountInfo.collateralMint
|
18
18
|
|
19
19
|
const poolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
20
20
|
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint)
|
21
|
-
const payerAssociatedTokenAccount = await
|
21
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
22
|
+
provider.connection,
|
23
|
+
payer,
|
24
|
+
collateralMint,
|
25
|
+
payer.publicKey
|
26
|
+
)
|
22
27
|
|
23
28
|
const transaction = new Transaction().add(
|
24
29
|
await claimLiquidationPoolPositionInstruction(
|
25
30
|
program,
|
26
31
|
poolStatePublicKey,
|
27
32
|
poolAssociatedTokenAccount,
|
28
|
-
|
33
|
+
vaultTypeAccountPublicKey,
|
29
34
|
collateralMint,
|
30
35
|
poolPosition,
|
31
36
|
payer.publicKey,
|
32
|
-
payerAssociatedTokenAccount,
|
37
|
+
payerAssociatedTokenAccount.address,
|
33
38
|
overrideStartTime
|
34
39
|
)
|
35
40
|
)
|
36
41
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
37
|
-
return payerAssociatedTokenAccount
|
42
|
+
return payerAssociatedTokenAccount.address
|
38
43
|
}
|
39
44
|
|
40
45
|
export async function claimLiquidationPoolPositionInstruction (
|
41
46
|
program: Program,
|
42
47
|
poolState: PublicKey,
|
43
48
|
poolAssociatedTokenAccount: PublicKey,
|
44
|
-
|
49
|
+
vaultTypeAccount: PublicKey,
|
45
50
|
collateralMint: PublicKey,
|
46
51
|
poolPosition: PublicKey,
|
47
52
|
payer: PublicKey,
|
48
53
|
payerAssociatedTokenAccount: PublicKey,
|
49
54
|
overrideStartTime?: number
|
50
55
|
): Promise<TransactionInstruction> {
|
56
|
+
const vaultSystemState = await getVaultSystemStatePublicKey()
|
57
|
+
|
51
58
|
return program.instruction.claimLiquidationPoolPosition(
|
52
59
|
{
|
53
60
|
accounts: {
|
61
|
+
vaultSystemState: vaultSystemState,
|
54
62
|
poolState: poolState,
|
55
63
|
poolAssociatedTokenAccount: poolAssociatedTokenAccount,
|
56
|
-
|
64
|
+
vaultTypeAccount: vaultTypeAccount,
|
57
65
|
collateralMint: collateralMint,
|
58
66
|
poolPosition: poolPosition,
|
59
67
|
payer: payer,
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import { ASSOCIATED_TOKEN_PROGRAM_ID,
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
4
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
5
|
+
import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function closeLiquidationPoolPosition (
|
8
8
|
program: Program,
|
@@ -12,13 +12,7 @@ export async function closeLiquidationPoolPosition (
|
|
12
12
|
overrideStartTime?: number
|
13
13
|
): Promise<PublicKey> {
|
14
14
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
15
|
-
const
|
16
|
-
provider.connection,
|
17
|
-
usdhMintPublickey,
|
18
|
-
TOKEN_PROGRAM_ID,
|
19
|
-
payer
|
20
|
-
)
|
21
|
-
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
15
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
22
16
|
|
23
17
|
const poolState = await getLiquidationPoolStatePublicKey()
|
24
18
|
const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
|
@@ -27,8 +21,8 @@ export async function closeLiquidationPoolPosition (
|
|
27
21
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
28
22
|
const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
29
23
|
const hedgeMint = await getHedgeMintPublicKey()
|
30
|
-
const payerAssociatedHedgeAccount = await
|
31
|
-
const communityAssociatedHedgeTokenAccount = await
|
24
|
+
const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, payer.publicKey)
|
25
|
+
const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true)
|
32
26
|
|
33
27
|
const transaction = new Transaction().add(
|
34
28
|
await closeLiquidationPoolPositionInstruction(
|
@@ -41,8 +35,8 @@ export async function closeLiquidationPoolPosition (
|
|
41
35
|
payer.publicKey,
|
42
36
|
payerUsdhAccount.address,
|
43
37
|
hedgeMint,
|
44
|
-
payerAssociatedHedgeAccount,
|
45
|
-
communityAssociatedHedgeTokenAccount,
|
38
|
+
payerAssociatedHedgeAccount.address,
|
39
|
+
communityAssociatedHedgeTokenAccount.address,
|
46
40
|
overrideStartTime
|
47
41
|
)
|
48
42
|
)
|
@@ -1,129 +1,296 @@
|
|
1
|
-
import { BN, Program, Provider } from
|
2
|
-
import { TokenInstructions } from
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
1
|
+
import { BN, Idl, Program, Provider } from "@project-serum/anchor";
|
2
|
+
import { TokenInstructions } from "@project-serum/serum";
|
3
|
+
import {
|
4
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
5
|
+
TOKEN_PROGRAM_ID,
|
6
|
+
getOrCreateAssociatedTokenAccount,
|
7
|
+
} from "@solana/spl-token";
|
8
|
+
import {
|
9
|
+
Keypair,
|
10
|
+
PublicKey,
|
11
|
+
sendAndConfirmTransaction,
|
12
|
+
Signer,
|
13
|
+
SystemProgram,
|
14
|
+
SYSVAR_RENT_PUBKEY,
|
15
|
+
Transaction,
|
16
|
+
TransactionInstruction,
|
17
|
+
} from "@solana/web3.js";
|
18
|
+
import {
|
19
|
+
findAssociatedTokenAddress,
|
20
|
+
findVaultAddress,
|
21
|
+
getVaultTypeAccountPublicKey,
|
22
|
+
getUsdhMintPublicKey,
|
23
|
+
getVaultSystemStatePublicKey,
|
24
|
+
getPoolPublicKeyForMint,
|
25
|
+
getHedgeMintPublicKey,
|
26
|
+
} from "../Constants";
|
6
27
|
|
7
|
-
import { v4 as uuidv4 } from
|
8
|
-
import { parseAnchorErrors } from
|
28
|
+
import { v4 as uuidv4 } from "uuid";
|
29
|
+
import { parseAnchorErrors } from "../utils/Errors";
|
9
30
|
|
10
|
-
export async function createVault
|
31
|
+
export async function createVault(
|
11
32
|
program: Program,
|
12
33
|
provider: Provider,
|
13
34
|
payer: Signer,
|
14
35
|
collateralType: string,
|
15
36
|
depositAmount: number,
|
16
|
-
|
37
|
+
overrideTime?: number
|
17
38
|
): Promise<PublicKey> {
|
18
|
-
const usdhMintPublickey = await getUsdhMintPublicKey()
|
19
|
-
const USDH = new Token(
|
20
|
-
provider.connection,
|
21
|
-
usdhMintPublickey,
|
22
|
-
TOKEN_PROGRAM_ID,
|
23
|
-
payer
|
24
|
-
)
|
39
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
25
40
|
|
26
|
-
const salt = uuidv4().substring(0, 8)
|
27
|
-
const newVaultPublicKey = await findVaultAddress(salt)
|
28
|
-
const history = Keypair.generate()
|
41
|
+
const salt = uuidv4().substring(0, 8);
|
42
|
+
const newVaultPublicKey = await findVaultAddress(salt);
|
43
|
+
const history = Keypair.generate();
|
29
44
|
|
30
45
|
// Prep the user to get USDH back out at some point
|
31
|
-
await
|
46
|
+
await getOrCreateAssociatedTokenAccount(
|
47
|
+
provider.connection,
|
48
|
+
payer,
|
49
|
+
usdhMintPublickey,
|
50
|
+
payer.publicKey
|
51
|
+
);
|
52
|
+
|
53
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
54
|
+
collateralType
|
55
|
+
);
|
56
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
57
|
+
vaultTypeAccountPublicKey
|
58
|
+
);
|
32
59
|
|
33
|
-
const
|
34
|
-
|
60
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
61
|
+
payer.publicKey,
|
62
|
+
vaultTypeAccountInfo.collateralMint
|
63
|
+
);
|
64
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
65
|
+
newVaultPublicKey,
|
66
|
+
vaultTypeAccountInfo.collateralMint
|
67
|
+
);
|
35
68
|
|
36
|
-
const
|
37
|
-
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(newVaultPublicKey, collateralStateAccountInfo.collateralMint)
|
69
|
+
const wrappedSolAccount = Keypair.generate();
|
38
70
|
|
39
|
-
const
|
71
|
+
const transaction = new Transaction();
|
72
|
+
const signers = [payer, history];
|
40
73
|
|
41
|
-
const
|
74
|
+
const isWrappedSol =
|
75
|
+
vaultTypeAccountInfo.collateralMint.toString() ===
|
76
|
+
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
42
77
|
|
43
|
-
const
|
44
|
-
|
78
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
79
|
+
await getHedgeMintPublicKey()
|
80
|
+
);
|
81
|
+
const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
82
|
+
hedgeStakingPoolPublicKey,
|
83
|
+
usdhMintPublickey
|
84
|
+
);
|
45
85
|
|
46
|
-
if (
|
86
|
+
if (isWrappedSol) {
|
47
87
|
transaction.add(
|
48
88
|
SystemProgram.createAccount({
|
49
89
|
fromPubkey: payer.publicKey,
|
50
90
|
lamports: depositAmount + 2.04e6,
|
51
91
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
52
92
|
programId: TOKEN_PROGRAM_ID,
|
53
|
-
space: 165
|
93
|
+
space: 165,
|
54
94
|
}),
|
55
95
|
TokenInstructions.initializeAccount({
|
56
96
|
account: wrappedSolAccount.publicKey,
|
57
97
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
58
|
-
owner: payer.publicKey
|
98
|
+
owner: payer.publicKey,
|
59
99
|
})
|
60
|
-
)
|
61
|
-
signers.push(wrappedSolAccount)
|
100
|
+
);
|
101
|
+
signers.push(wrappedSolAccount);
|
62
102
|
}
|
63
103
|
transaction.add(
|
64
104
|
await createVaultInstruction(
|
65
105
|
program,
|
66
106
|
salt,
|
67
|
-
vaultSystemStatePublicKey,
|
68
107
|
payer.publicKey,
|
69
|
-
|
108
|
+
isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
|
70
109
|
newVaultPublicKey,
|
71
|
-
|
72
|
-
|
73
|
-
|
110
|
+
vaultAssociatedTokenAccount,
|
111
|
+
hedgeStakingPoolPublicKey,
|
112
|
+
feePoolAssociatedUsdhTokenAccount,
|
113
|
+
vaultTypeAccountPublicKey,
|
114
|
+
vaultTypeAccountInfo.collateralMint,
|
74
115
|
history.publicKey,
|
116
|
+
usdhMintPublickey,
|
75
117
|
depositAmount,
|
76
|
-
|
118
|
+
overrideTime
|
77
119
|
)
|
78
|
-
)
|
79
|
-
if (
|
120
|
+
);
|
121
|
+
if (isWrappedSol) {
|
80
122
|
transaction.add(
|
81
123
|
TokenInstructions.closeAccount({
|
82
124
|
source: wrappedSolAccount.publicKey,
|
83
125
|
destination: payer.publicKey,
|
84
|
-
owner: payer.publicKey
|
126
|
+
owner: payer.publicKey,
|
127
|
+
})
|
128
|
+
);
|
129
|
+
}
|
130
|
+
|
131
|
+
await sendAndConfirmTransaction(
|
132
|
+
provider.connection,
|
133
|
+
transaction,
|
134
|
+
signers,
|
135
|
+
provider?.opts
|
136
|
+
).catch(parseAnchorErrors);
|
137
|
+
return newVaultPublicKey;
|
138
|
+
}
|
139
|
+
|
140
|
+
export async function buildCreateVaultTransaction(
|
141
|
+
program: Program,
|
142
|
+
collateralType: string,
|
143
|
+
depositAmount: number,
|
144
|
+
overrideTime?: number
|
145
|
+
): Promise<[Transaction, Signer[], PublicKey]> {
|
146
|
+
|
147
|
+
console.log("HEDGE SDK: buildCreateVaultTransaction")
|
148
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
149
|
+
const payerPublicKey = program.provider.wallet.publicKey;
|
150
|
+
|
151
|
+
const salt = uuidv4().substring(0, 8);
|
152
|
+
const newVaultPublicKey = await findVaultAddress(salt);
|
153
|
+
const history = Keypair.generate() as Signer;
|
154
|
+
const { blockhash } = await program.provider.connection.getLatestBlockhash()
|
155
|
+
const transaction = new Transaction({
|
156
|
+
feePayer: payerPublicKey,
|
157
|
+
recentBlockhash: blockhash,
|
158
|
+
})
|
159
|
+
const signers = [history];
|
160
|
+
const wrappedSolAccount = Keypair.generate();
|
161
|
+
|
162
|
+
console.log('Lookup getVaultTypeAccountPublicKey', collateralType)
|
163
|
+
|
164
|
+
// Lookup the vault type info
|
165
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
166
|
+
collateralType
|
167
|
+
);
|
168
|
+
|
169
|
+
console.log('Lookup vaultTypeAccountPublicKey', vaultTypeAccountPublicKey.toString())
|
170
|
+
|
171
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
172
|
+
vaultTypeAccountPublicKey
|
173
|
+
);
|
174
|
+
console.log('Lookup vaultTypeAccountInfo', vaultTypeAccountInfo)
|
175
|
+
|
176
|
+
const isWrappedSol =
|
177
|
+
vaultTypeAccountInfo.collateralMint.toString() ===
|
178
|
+
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
179
|
+
|
180
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
181
|
+
payerPublicKey,
|
182
|
+
vaultTypeAccountInfo.collateralMint
|
183
|
+
);
|
184
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
185
|
+
newVaultPublicKey,
|
186
|
+
vaultTypeAccountInfo.collateralMint
|
187
|
+
);
|
188
|
+
|
189
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
190
|
+
await getHedgeMintPublicKey()
|
191
|
+
);
|
192
|
+
const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
193
|
+
hedgeStakingPoolPublicKey,
|
194
|
+
usdhMintPublickey
|
195
|
+
);
|
196
|
+
|
197
|
+
console.log('about to start building transaction')
|
198
|
+
|
199
|
+
// If wrapped SOL collateral, we convert the SOL to wSOL to use in the instruction
|
200
|
+
if (isWrappedSol) {
|
201
|
+
transaction.add(
|
202
|
+
SystemProgram.createAccount({
|
203
|
+
fromPubkey: payerPublicKey,
|
204
|
+
lamports: depositAmount + 2.04e6,
|
205
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
206
|
+
programId: TOKEN_PROGRAM_ID,
|
207
|
+
space: 165,
|
208
|
+
}),
|
209
|
+
TokenInstructions.initializeAccount({
|
210
|
+
account: wrappedSolAccount.publicKey,
|
211
|
+
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
212
|
+
owner: payerPublicKey,
|
85
213
|
})
|
214
|
+
);
|
215
|
+
signers.push(wrappedSolAccount);
|
216
|
+
}
|
217
|
+
console.log("hedgeStakingPoolPublicKey", hedgeStakingPoolPublicKey.toString())
|
218
|
+
transaction.add(
|
219
|
+
await createVaultInstruction(
|
220
|
+
program,
|
221
|
+
salt,
|
222
|
+
payerPublicKey,
|
223
|
+
isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
|
224
|
+
newVaultPublicKey,
|
225
|
+
vaultAssociatedTokenAccount,
|
226
|
+
hedgeStakingPoolPublicKey,
|
227
|
+
feePoolAssociatedUsdhTokenAccount,
|
228
|
+
vaultTypeAccountPublicKey,
|
229
|
+
vaultTypeAccountInfo.collateralMint,
|
230
|
+
history.publicKey,
|
231
|
+
usdhMintPublickey,
|
232
|
+
depositAmount,
|
233
|
+
overrideTime
|
86
234
|
)
|
235
|
+
);
|
236
|
+
if (isWrappedSol) {
|
237
|
+
transaction.add(
|
238
|
+
TokenInstructions.closeAccount({
|
239
|
+
source: wrappedSolAccount.publicKey,
|
240
|
+
destination: payerPublicKey,
|
241
|
+
owner: payerPublicKey,
|
242
|
+
})
|
243
|
+
);
|
87
244
|
}
|
245
|
+
transaction.partialSign(wrappedSolAccount)
|
246
|
+
transaction.partialSign(history)
|
247
|
+
console.log("transaction", transaction)
|
88
248
|
|
89
|
-
|
90
|
-
return newVaultPublicKey
|
249
|
+
return [transaction, signers, newVaultPublicKey];
|
91
250
|
}
|
92
251
|
|
93
|
-
export async function createVaultInstruction
|
252
|
+
export async function createVaultInstruction(
|
94
253
|
program: Program,
|
95
254
|
salt: string,
|
96
|
-
vaultSystemStatePublicKey: PublicKey,
|
97
255
|
payerPublicKey: PublicKey,
|
98
256
|
payerTokenAccountPublicKey: PublicKey,
|
99
257
|
vaultPublicKey: PublicKey,
|
100
|
-
|
101
|
-
|
258
|
+
vaultAssociatedTokenAccount: PublicKey,
|
259
|
+
feePool: PublicKey,
|
260
|
+
feePoolAssociatedUsdhTokenAccount: PublicKey,
|
261
|
+
vaultTypeAccount: PublicKey,
|
102
262
|
collateralMint: PublicKey,
|
103
263
|
historyPublicKey: PublicKey,
|
264
|
+
usdhMintPublickey: PublicKey,
|
104
265
|
depositAmount: number,
|
105
|
-
|
266
|
+
overrideTime?: number
|
106
267
|
): Promise<TransactionInstruction> {
|
268
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
269
|
+
|
107
270
|
const ix = program.instruction.createVault(
|
108
271
|
salt,
|
109
272
|
new BN(depositAmount),
|
110
|
-
new BN(
|
111
|
-
|
273
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
274
|
+
{
|
112
275
|
accounts: {
|
113
276
|
vaultSystemState: vaultSystemStatePublicKey,
|
114
|
-
|
277
|
+
vaultTypeAccount: vaultTypeAccount,
|
115
278
|
vault: vaultPublicKey,
|
116
|
-
vaultAssociatedTokenAccount:
|
279
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
280
|
+
feePool: feePool,
|
281
|
+
feePoolAssociatedUsdhTokenAccount: feePoolAssociatedUsdhTokenAccount,
|
117
282
|
history: historyPublicKey,
|
118
283
|
payer: payerPublicKey,
|
119
284
|
payerTokenAccount: payerTokenAccountPublicKey,
|
120
285
|
collateralMint: collateralMint,
|
286
|
+
usdhMint: usdhMintPublickey,
|
121
287
|
systemProgram: SystemProgram.programId,
|
122
288
|
tokenProgram: TOKEN_PROGRAM_ID,
|
123
289
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
124
|
-
rent: SYSVAR_RENT_PUBKEY
|
290
|
+
rent: SYSVAR_RENT_PUBKEY,
|
125
291
|
},
|
126
|
-
signers: []
|
127
|
-
}
|
128
|
-
|
292
|
+
signers: [],
|
293
|
+
}
|
294
|
+
);
|
295
|
+
return ix;
|
129
296
|
}
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
4
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import { getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey } from '../Constants'
|
5
|
+
import { getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function depositLiquidationPool (
|
8
8
|
program: Program,
|
@@ -12,13 +12,7 @@ export async function depositLiquidationPool (
|
|
12
12
|
overrideStartTime?: number
|
13
13
|
): Promise<PublicKey> {
|
14
14
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
15
|
-
const
|
16
|
-
provider.connection,
|
17
|
-
usdhMintPublickey,
|
18
|
-
TOKEN_PROGRAM_ID,
|
19
|
-
payer
|
20
|
-
)
|
21
|
-
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
15
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
22
16
|
|
23
17
|
const poolPosition = Keypair.generate()
|
24
18
|
const transaction = new Transaction().add(
|
@@ -49,11 +43,14 @@ export async function depositLiquidationPoolInstruction (
|
|
49
43
|
const poolUSDHAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
50
44
|
const usdhMint = await getUsdhMintPublicKey()
|
51
45
|
|
46
|
+
const vaultSystemState = await getVaultSystemStatePublicKey()
|
47
|
+
|
52
48
|
return program.instruction.depositLiquidationPool(
|
53
49
|
new BN(depositAmount),
|
54
50
|
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
55
51
|
{
|
56
52
|
accounts: {
|
53
|
+
vaultSystemState: vaultSystemState,
|
57
54
|
poolState: liquidationPoolStatePublicKey,
|
58
55
|
poolEra: liquidationPoolState.currentEra,
|
59
56
|
poolPosition: poolPositionPublicKey,
|
@@ -2,7 +2,7 @@ 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 { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
4
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import { findAssociatedTokenAddress, getPoolPublicKeyForMint } from '../Constants'
|
5
|
+
import { findAssociatedTokenAddress, getPoolPublicKeyForMint, getVaultSystemStatePublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function depositStakingPool (
|
8
8
|
program: Program,
|
@@ -38,6 +38,7 @@ export async function depositStakingPoolInstruction (
|
|
38
38
|
const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey)
|
39
39
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey)
|
40
40
|
const payersArbitraryTokenAccount = await findAssociatedTokenAddress(payerPublicKey, stakedTokenMintPublicKey)
|
41
|
+
const vaultSystemState = await getVaultSystemStatePublicKey()
|
41
42
|
|
42
43
|
return program.instruction.depositStakingPool(
|
43
44
|
new BN(depositAmount),
|
@@ -45,6 +46,7 @@ export async function depositStakingPoolInstruction (
|
|
45
46
|
{
|
46
47
|
accounts: {
|
47
48
|
payer: payerPublicKey,
|
49
|
+
vaultSystemState: vaultSystemState,
|
48
50
|
pool: poolPublickey,
|
49
51
|
poolPosition: poolPositionPublicKey,
|
50
52
|
stakedTokenMintInfo: stakedTokenMintPublicKey,
|