hedge-web3 0.1.13 → 0.1.16
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 +8 -8
- 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 +6 -1
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +4 -0
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/createStakingPool.d.ts +0 -1
- package/declarations/instructions/createVault.d.ts +4 -0
- package/declarations/instructions/depositLiquidationPool.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/depositStakingPool.d.ts +0 -1
- package/declarations/instructions/depositVault.d.ts +4 -0
- package/declarations/instructions/initHedgeFoundation.d.ts +4 -0
- package/declarations/instructions/initHedgeFoundationTokens.d.ts +4 -0
- package/declarations/instructions/liquidateVault.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/loanVault.d.ts +2 -3
- package/declarations/instructions/redeemVault.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/refreshOraclePrice.d.ts +2 -3
- 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/declarations/instructions/withdrawVault.d.ts +4 -0
- package/{lib/types/src → declarations}/state/LiquidationPoolEra.d.ts +1 -2
- package/{lib/types/src → declarations}/state/LiquidationPoolState.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPosition.d.ts +8 -10
- 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 +0 -1
- 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 +37 -2156
- 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 +92 -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/initHedgeFoundationTokens.js +48 -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 +52 -0
- package/lib/state/VaultHistoryEvent.js +45 -0
- package/lib/utils/Errors.js +14 -0
- package/package.json +14 -8
- package/src/Constants.ts +24 -19
- package/src/HedgeDecimal.ts +3 -3
- package/src/idl/idl.ts +13 -13
- package/src/idl/vault.ts +4565 -0
- package/src/index.ts +6 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +76 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +82 -0
- package/src/instructions/createStakingPool.ts +4 -4
- package/src/instructions/createVault.ts +98 -27
- package/src/instructions/depositLiquidationPool.ts +67 -0
- package/src/instructions/depositStakingPool.ts +5 -3
- package/src/instructions/depositVault.ts +95 -27
- package/src/instructions/initHedgeFoundation.ts +64 -0
- package/src/instructions/initHedgeFoundationTokens.ts +55 -0
- package/src/instructions/liquidateVault.ts +87 -35
- package/src/instructions/loanVault.ts +30 -20
- package/src/instructions/redeemVault.ts +37 -24
- package/src/instructions/refreshOraclePrice.ts +6 -3
- package/src/instructions/repayVault.ts +30 -20
- package/src/instructions/setHalted.ts +48 -0
- package/src/instructions/withdrawStakingPool.ts +6 -6
- package/src/instructions/withdrawVault.ts +50 -17
- package/src/state/LiquidationPoolEra.ts +2 -8
- package/src/state/LiquidationPosition.ts +38 -39
- 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/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/depositStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositVault.d.ts +0 -5
- package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
- package/lib/types/src/instructions/liquidateVault.d.ts +0 -5
- 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 +0 -5
- 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 +0 -5
- 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 -40
package/src/index.ts
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
export * from './instructions/createStakingPool'
|
3
3
|
export * from './instructions/depositStakingPool'
|
4
4
|
export * from './instructions/withdrawStakingPool'
|
5
|
+
export * from './instructions/depositLiquidationPool'
|
6
|
+
export * from './instructions/closeLiquidationPoolPosition'
|
7
|
+
export * from './instructions/claimLiquidationPoolPosition'
|
5
8
|
export * from './instructions/createVault'
|
6
9
|
export * from './instructions/depositVault'
|
7
10
|
export * from './instructions/withdrawVault'
|
@@ -10,6 +13,9 @@ export * from './instructions/repayVault'
|
|
10
13
|
export * from './instructions/redeemVault'
|
11
14
|
export * from './instructions/liquidateVault'
|
12
15
|
export * from './instructions/refreshOraclePrice'
|
16
|
+
export * from './instructions/initHedgeFoundation'
|
17
|
+
export * from './instructions/initHedgeFoundationTokens'
|
18
|
+
export * from './instructions/setHalted'
|
13
19
|
|
14
20
|
export * from './HedgeDecimal'
|
15
21
|
export * from './Constants'
|
@@ -0,0 +1,76 @@
|
|
1
|
+
import { Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getLiquidationPoolStatePublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function claimLiquidationPoolPosition (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
poolPosition: PublicKey,
|
11
|
+
payer: Signer,
|
12
|
+
collateralType: string,
|
13
|
+
overrideStartTime?: number
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
|
16
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
17
|
+
const collateralMint = vaultTypeAccountInfo.collateralMint
|
18
|
+
|
19
|
+
const poolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
20
|
+
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint)
|
21
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
22
|
+
provider.connection,
|
23
|
+
payer,
|
24
|
+
collateralMint,
|
25
|
+
payer.publicKey
|
26
|
+
)
|
27
|
+
|
28
|
+
const transaction = new Transaction().add(
|
29
|
+
await claimLiquidationPoolPositionInstruction(
|
30
|
+
program,
|
31
|
+
poolStatePublicKey,
|
32
|
+
poolAssociatedTokenAccount,
|
33
|
+
vaultTypeAccountPublicKey,
|
34
|
+
collateralMint,
|
35
|
+
poolPosition,
|
36
|
+
payer.publicKey,
|
37
|
+
payerAssociatedTokenAccount.address,
|
38
|
+
overrideStartTime
|
39
|
+
)
|
40
|
+
)
|
41
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
42
|
+
return payerAssociatedTokenAccount.address
|
43
|
+
}
|
44
|
+
|
45
|
+
export async function claimLiquidationPoolPositionInstruction (
|
46
|
+
program: Program,
|
47
|
+
poolState: PublicKey,
|
48
|
+
poolAssociatedTokenAccount: PublicKey,
|
49
|
+
vaultTypeAccount: PublicKey,
|
50
|
+
collateralMint: PublicKey,
|
51
|
+
poolPosition: PublicKey,
|
52
|
+
payer: PublicKey,
|
53
|
+
payerAssociatedTokenAccount: PublicKey,
|
54
|
+
overrideStartTime?: number
|
55
|
+
): Promise<TransactionInstruction> {
|
56
|
+
const vaultSystemState = await getVaultSystemStatePublicKey()
|
57
|
+
|
58
|
+
return program.instruction.claimLiquidationPoolPosition(
|
59
|
+
{
|
60
|
+
accounts: {
|
61
|
+
vaultSystemState: vaultSystemState,
|
62
|
+
poolState: poolState,
|
63
|
+
poolAssociatedTokenAccount: poolAssociatedTokenAccount,
|
64
|
+
vaultTypeAccount: vaultTypeAccount,
|
65
|
+
collateralMint: collateralMint,
|
66
|
+
poolPosition: poolPosition,
|
67
|
+
payer: payer,
|
68
|
+
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
69
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
70
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
71
|
+
systemProgram: SystemProgram.programId,
|
72
|
+
rent: SYSVAR_RENT_PUBKEY
|
73
|
+
},
|
74
|
+
signers: []
|
75
|
+
})
|
76
|
+
}
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function closeLiquidationPoolPosition (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
poolPosition: PublicKey,
|
11
|
+
payer: Signer,
|
12
|
+
overrideStartTime?: number
|
13
|
+
): Promise<PublicKey> {
|
14
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
15
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
16
|
+
|
17
|
+
const poolState = await getLiquidationPoolStatePublicKey()
|
18
|
+
const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
|
19
|
+
const poolEra = liquidationPositionAccount.era
|
20
|
+
|
21
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
22
|
+
const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
23
|
+
const hedgeMint = await getHedgeMintPublicKey()
|
24
|
+
const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, payer.publicKey)
|
25
|
+
const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true)
|
26
|
+
|
27
|
+
const transaction = new Transaction().add(
|
28
|
+
await closeLiquidationPoolPositionInstruction(
|
29
|
+
program,
|
30
|
+
vaultSystemStatePublicKey,
|
31
|
+
poolState,
|
32
|
+
poolEra,
|
33
|
+
poolPosition,
|
34
|
+
poolUsdhAccount,
|
35
|
+
payer.publicKey,
|
36
|
+
payerUsdhAccount.address,
|
37
|
+
hedgeMint,
|
38
|
+
payerAssociatedHedgeAccount.address,
|
39
|
+
communityAssociatedHedgeTokenAccount.address,
|
40
|
+
overrideStartTime
|
41
|
+
)
|
42
|
+
)
|
43
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
44
|
+
return poolPosition
|
45
|
+
}
|
46
|
+
|
47
|
+
export async function closeLiquidationPoolPositionInstruction (
|
48
|
+
program: Program,
|
49
|
+
vaultSystemState: PublicKey,
|
50
|
+
poolState: PublicKey,
|
51
|
+
poolEra: PublicKey,
|
52
|
+
poolPosition: PublicKey,
|
53
|
+
poolUsdhAccount: PublicKey,
|
54
|
+
payerPublicKey: PublicKey,
|
55
|
+
payerUsdhAccount: PublicKey,
|
56
|
+
hedgeMint: PublicKey,
|
57
|
+
payerAssociatedHedgeAccount: PublicKey,
|
58
|
+
communityAssociatedHedgeTokenAccount: PublicKey,
|
59
|
+
overrideStartTime?: number
|
60
|
+
): Promise<TransactionInstruction> {
|
61
|
+
return program.instruction.closeLiquidationPoolPosition(
|
62
|
+
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
63
|
+
{
|
64
|
+
accounts: {
|
65
|
+
vaultSystemState: vaultSystemState,
|
66
|
+
poolState: poolState,
|
67
|
+
poolEra: poolEra,
|
68
|
+
poolPosition: poolPosition,
|
69
|
+
poolUsdhAccount: poolUsdhAccount,
|
70
|
+
payer: payerPublicKey,
|
71
|
+
ownerUsdhAccount: payerUsdhAccount,
|
72
|
+
hedgeMint: hedgeMint,
|
73
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
74
|
+
communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
|
75
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
76
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
77
|
+
systemProgram: SystemProgram.programId,
|
78
|
+
rent: SYSVAR_RENT_PUBKEY
|
79
|
+
},
|
80
|
+
signers: []
|
81
|
+
})
|
82
|
+
}
|
@@ -33,8 +33,8 @@ export async function createStakingPoolInstruction (
|
|
33
33
|
hedgeTokensToBeMinted: number,
|
34
34
|
overrideStartTime?: number
|
35
35
|
): Promise<TransactionInstruction> {
|
36
|
-
const
|
37
|
-
const
|
36
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
37
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
38
38
|
const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey)
|
39
39
|
|
40
40
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
|
@@ -55,8 +55,8 @@ export async function createStakingPoolInstruction (
|
|
55
55
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
56
56
|
poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
|
57
57
|
rent: SYSVAR_RENT_PUBKEY,
|
58
|
-
|
59
|
-
|
58
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
59
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
60
|
systemProgram: SystemProgram.programId
|
61
61
|
},
|
62
62
|
signers: []
|
@@ -1,66 +1,137 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import {
|
2
|
+
import { TokenInstructions } from '@project-serum/serum'
|
3
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
4
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
5
|
+
import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
6
|
+
|
7
|
+
import { v4 as uuidv4 } from 'uuid'
|
8
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
9
|
|
6
10
|
export async function createVault (
|
7
11
|
program: Program,
|
8
12
|
provider: Provider,
|
9
13
|
payer: Signer,
|
14
|
+
collateralType: string,
|
10
15
|
depositAmount: number,
|
11
|
-
|
16
|
+
overrideTime?: number
|
12
17
|
): Promise<PublicKey> {
|
13
|
-
const
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
payer
|
19
|
-
)
|
18
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
19
|
+
|
20
|
+
const salt = uuidv4().substring(0, 8)
|
21
|
+
const newVaultPublicKey = await findVaultAddress(salt)
|
22
|
+
const history = Keypair.generate()
|
20
23
|
|
21
24
|
// Prep the user to get USDH back out at some point
|
22
|
-
await
|
25
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
23
26
|
|
24
|
-
const
|
25
|
-
const
|
26
|
-
|
27
|
-
const
|
27
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(collateralType)
|
28
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
29
|
+
|
30
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
|
31
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(newVaultPublicKey, vaultTypeAccountInfo.collateralMint)
|
32
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
33
|
+
|
34
|
+
const wrappedSolAccount = Keypair.generate()
|
35
|
+
|
36
|
+
const transaction = new Transaction()
|
37
|
+
const signers = [payer, history]
|
38
|
+
|
39
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() === TokenInstructions.WRAPPED_SOL_MINT.toString()
|
40
|
+
|
41
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
42
|
+
const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
43
|
+
hedgeStakingPoolPublicKey,
|
44
|
+
usdhMintPublickey
|
45
|
+
)
|
46
|
+
|
47
|
+
if (isWrappedSol) {
|
48
|
+
transaction.add(
|
49
|
+
SystemProgram.createAccount({
|
50
|
+
fromPubkey: payer.publicKey,
|
51
|
+
lamports: depositAmount + 2.04e6,
|
52
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
53
|
+
programId: TOKEN_PROGRAM_ID,
|
54
|
+
space: 165
|
55
|
+
}),
|
56
|
+
TokenInstructions.initializeAccount({
|
57
|
+
account: wrappedSolAccount.publicKey,
|
58
|
+
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
59
|
+
owner: payer.publicKey
|
60
|
+
})
|
61
|
+
)
|
62
|
+
signers.push(wrappedSolAccount)
|
63
|
+
}
|
64
|
+
transaction.add(
|
28
65
|
await createVaultInstruction(
|
29
66
|
program,
|
67
|
+
salt,
|
30
68
|
vaultSystemStatePublicKey,
|
31
69
|
payer.publicKey,
|
32
|
-
|
70
|
+
isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
|
71
|
+
newVaultPublicKey,
|
72
|
+
vaultAssociatedTokenAccount,
|
73
|
+
hedgeStakingPoolPublicKey,
|
74
|
+
feePoolAssociatedUsdhTokenAccount,
|
75
|
+
vaultTypeAccountPublicKey,
|
76
|
+
vaultTypeAccountInfo.collateralMint,
|
33
77
|
history.publicKey,
|
78
|
+
usdhMintPublickey,
|
34
79
|
depositAmount,
|
35
|
-
|
80
|
+
overrideTime
|
36
81
|
)
|
37
82
|
)
|
38
|
-
|
39
|
-
|
83
|
+
if (isWrappedSol) {
|
84
|
+
transaction.add(
|
85
|
+
TokenInstructions.closeAccount({
|
86
|
+
source: wrappedSolAccount.publicKey,
|
87
|
+
destination: payer.publicKey,
|
88
|
+
owner: payer.publicKey
|
89
|
+
})
|
90
|
+
)
|
91
|
+
}
|
92
|
+
|
93
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers, provider?.opts).catch(parseAnchorErrors)
|
94
|
+
return newVaultPublicKey
|
40
95
|
}
|
41
96
|
|
42
97
|
export async function createVaultInstruction (
|
43
98
|
program: Program,
|
99
|
+
salt: string,
|
44
100
|
vaultSystemStatePublicKey: PublicKey,
|
45
101
|
payerPublicKey: PublicKey,
|
102
|
+
payerTokenAccountPublicKey: PublicKey,
|
46
103
|
vaultPublicKey: PublicKey,
|
104
|
+
vaultAssociatedTokenAccount: PublicKey,
|
105
|
+
feePool: PublicKey,
|
106
|
+
feePoolAssociatedUsdhTokenAccount: PublicKey,
|
107
|
+
vaultTypeAccount: PublicKey,
|
108
|
+
collateralMint: PublicKey,
|
47
109
|
historyPublicKey: PublicKey,
|
110
|
+
usdhMintPublickey: PublicKey,
|
48
111
|
depositAmount: number,
|
49
|
-
|
112
|
+
overrideTime?: number
|
50
113
|
): Promise<TransactionInstruction> {
|
51
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
52
|
-
|
53
114
|
const ix = program.instruction.createVault(
|
115
|
+
salt,
|
54
116
|
new BN(depositAmount),
|
55
|
-
new BN(
|
56
|
-
|
117
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
118
|
+
{
|
57
119
|
accounts: {
|
58
120
|
vaultSystemState: vaultSystemStatePublicKey,
|
59
|
-
|
121
|
+
vaultTypeAccount: vaultTypeAccount,
|
60
122
|
vault: vaultPublicKey,
|
123
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
124
|
+
feePool: feePool,
|
125
|
+
feePoolAssociatedUsdhTokenAccount: feePoolAssociatedUsdhTokenAccount,
|
61
126
|
history: historyPublicKey,
|
62
127
|
payer: payerPublicKey,
|
63
|
-
|
128
|
+
payerTokenAccount: payerTokenAccountPublicKey,
|
129
|
+
collateralMint: collateralMint,
|
130
|
+
usdhMint: usdhMintPublickey,
|
131
|
+
systemProgram: SystemProgram.programId,
|
132
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
133
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
134
|
+
rent: SYSVAR_RENT_PUBKEY
|
64
135
|
},
|
65
136
|
signers: []
|
66
137
|
})
|
@@ -0,0 +1,67 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function depositLiquidationPool (
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
payer: Signer,
|
11
|
+
depositAmount: number,
|
12
|
+
overrideStartTime?: number
|
13
|
+
): Promise<PublicKey> {
|
14
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
15
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
16
|
+
|
17
|
+
const poolPosition = Keypair.generate()
|
18
|
+
const transaction = new Transaction().add(
|
19
|
+
await depositLiquidationPoolInstruction(
|
20
|
+
program,
|
21
|
+
payer.publicKey,
|
22
|
+
payerUsdhAccount.address,
|
23
|
+
poolPosition.publicKey,
|
24
|
+
depositAmount,
|
25
|
+
overrideStartTime
|
26
|
+
)
|
27
|
+
)
|
28
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors)
|
29
|
+
return poolPosition.publicKey
|
30
|
+
}
|
31
|
+
|
32
|
+
export async function depositLiquidationPoolInstruction (
|
33
|
+
program: Program,
|
34
|
+
payerPublicKey: PublicKey,
|
35
|
+
payerUsdhAccount: PublicKey,
|
36
|
+
poolPositionPublicKey: PublicKey,
|
37
|
+
depositAmount: number,
|
38
|
+
overrideStartTime?: number
|
39
|
+
): Promise<TransactionInstruction> {
|
40
|
+
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
41
|
+
const liquidationPoolState = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
42
|
+
|
43
|
+
const poolUSDHAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
44
|
+
const usdhMint = await getUsdhMintPublicKey()
|
45
|
+
|
46
|
+
const vaultSystemState = await getVaultSystemStatePublicKey()
|
47
|
+
|
48
|
+
return program.instruction.depositLiquidationPool(
|
49
|
+
new BN(depositAmount),
|
50
|
+
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
51
|
+
{
|
52
|
+
accounts: {
|
53
|
+
vaultSystemState: vaultSystemState,
|
54
|
+
poolState: liquidationPoolStatePublicKey,
|
55
|
+
poolEra: liquidationPoolState.currentEra,
|
56
|
+
poolPosition: poolPositionPublicKey,
|
57
|
+
poolUsdhAccount: poolUSDHAccount,
|
58
|
+
usdhMint: usdhMint,
|
59
|
+
payer: payerPublicKey,
|
60
|
+
ownerUsdhAccount: payerUsdhAccount,
|
61
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
62
|
+
systemProgram: SystemProgram.programId,
|
63
|
+
rent: SYSVAR_RENT_PUBKEY
|
64
|
+
},
|
65
|
+
signers: []
|
66
|
+
})
|
67
|
+
}
|
@@ -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,14 +46,15 @@ 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,
|
51
53
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
52
54
|
payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
|
53
55
|
rent: SYSVAR_RENT_PUBKEY,
|
54
|
-
|
55
|
-
|
56
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
57
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
56
58
|
systemProgram: SystemProgram.programId
|
57
59
|
},
|
58
60
|
signers: []
|
@@ -1,62 +1,130 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import { TokenInstructions } from '@project-serum/serum'
|
3
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
4
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import {
|
5
|
+
import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
5
6
|
|
6
|
-
export async function depositVault
|
7
|
+
export async function depositVault(
|
7
8
|
program: Program,
|
8
9
|
provider: Provider,
|
9
10
|
payer: Signer,
|
10
11
|
vaultPublicKey: PublicKey,
|
11
|
-
depositAmount: number
|
12
|
+
depositAmount: number,
|
13
|
+
overrideTime?: number
|
12
14
|
): Promise<PublicKey> {
|
13
|
-
const
|
14
|
-
const USDH = new Token(
|
15
|
-
provider.connection,
|
16
|
-
usdhMintPublickey,
|
17
|
-
TOKEN_PROGRAM_ID,
|
18
|
-
payer
|
19
|
-
)
|
15
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
20
16
|
|
21
17
|
// Prep the user to get USDH back out at some point
|
22
|
-
await
|
18
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
19
|
+
|
20
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
21
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
22
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
23
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true)
|
24
|
+
|
25
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
|
26
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccountInfo.collateralMint)
|
23
27
|
|
24
28
|
const history = Keypair.generate()
|
25
|
-
const
|
26
|
-
|
29
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
30
|
+
|
31
|
+
const wrappedSolAccount = Keypair.generate()
|
32
|
+
|
33
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
34
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
35
|
+
hedgeStakingPoolPublicKey,
|
36
|
+
usdhMintPublickey
|
37
|
+
)
|
38
|
+
|
39
|
+
const transaction = new Transaction()
|
40
|
+
const signers = [payer, history]
|
41
|
+
|
42
|
+
if (vaultAccount.collateralType === 'SOL') {
|
43
|
+
transaction.add(
|
44
|
+
SystemProgram.createAccount({
|
45
|
+
fromPubkey: payer.publicKey,
|
46
|
+
lamports: depositAmount + 2.04e6,
|
47
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
48
|
+
programId: TOKEN_PROGRAM_ID,
|
49
|
+
space: 165
|
50
|
+
}),
|
51
|
+
TokenInstructions.initializeAccount({
|
52
|
+
account: wrappedSolAccount.publicKey,
|
53
|
+
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
54
|
+
owner: payer.publicKey
|
55
|
+
})
|
56
|
+
)
|
57
|
+
signers.push(wrappedSolAccount)
|
58
|
+
}
|
59
|
+
transaction.add(
|
27
60
|
await depositVaultInstruction(
|
28
61
|
program,
|
29
62
|
vaultSystemStatePublicKey,
|
30
63
|
payer.publicKey,
|
64
|
+
vaultAccount.collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount,
|
31
65
|
vaultPublicKey,
|
66
|
+
vaultAssociatedCollateralAccountPublicKey,
|
32
67
|
history.publicKey,
|
33
|
-
|
68
|
+
vaultTypeAccountPublicKey,
|
69
|
+
vaultTypeAssociatedTokenAccount.address,
|
70
|
+
hedgeStakingPoolPublicKey,
|
71
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount,
|
72
|
+
vaultTypeAccountInfo.collateralMint,
|
73
|
+
usdhMintPublickey,
|
74
|
+
depositAmount,
|
75
|
+
overrideTime
|
34
76
|
)
|
35
77
|
)
|
36
|
-
|
78
|
+
if (vaultAccount.collateralType === 'SOL') {
|
79
|
+
transaction.add(
|
80
|
+
TokenInstructions.closeAccount({
|
81
|
+
source: wrappedSolAccount.publicKey,
|
82
|
+
destination: payer.publicKey,
|
83
|
+
owner: payer.publicKey
|
84
|
+
})
|
85
|
+
)
|
86
|
+
}
|
87
|
+
|
88
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers, provider.opts)
|
37
89
|
return vaultPublicKey
|
38
90
|
}
|
39
91
|
|
40
|
-
export async function depositVaultInstruction
|
92
|
+
export async function depositVaultInstruction(
|
41
93
|
program: Program,
|
42
94
|
vaultSystemStatePublicKey: PublicKey,
|
43
|
-
|
95
|
+
vaultOwner: PublicKey,
|
96
|
+
vaultOwnerTokenAccount: PublicKey,
|
44
97
|
vaultPublicKey: PublicKey,
|
98
|
+
vaultAssociatedTokenAccount: PublicKey,
|
45
99
|
historyPublicKey: PublicKey,
|
46
|
-
|
100
|
+
vaultTypeAccountPublicKey: PublicKey,
|
101
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
102
|
+
hedgeStakingPoolPublicKey: PublicKey,
|
103
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
|
104
|
+
collateralMint: PublicKey,
|
105
|
+
usdhMintPublickey: PublicKey,
|
106
|
+
depositAmount: number,
|
107
|
+
overrideTime?: number
|
47
108
|
): Promise<TransactionInstruction> {
|
48
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
49
|
-
|
50
109
|
return program.instruction.depositVault(
|
51
|
-
new BN(depositAmount)
|
52
|
-
,
|
110
|
+
new BN(depositAmount),
|
111
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
112
|
+
{
|
53
113
|
accounts: {
|
54
114
|
vaultSystemState: vaultSystemStatePublicKey,
|
55
|
-
|
56
|
-
|
115
|
+
vaultTypeAccount: vaultTypeAccountPublicKey,
|
116
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
117
|
+
collateralTokenMint: collateralMint,
|
118
|
+
vault: vaultPublicKey,
|
119
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
120
|
+
feePool: hedgeStakingPoolPublicKey,
|
121
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
57
122
|
history: historyPublicKey,
|
58
|
-
vaultOwner:
|
59
|
-
|
123
|
+
vaultOwner: vaultOwner,
|
124
|
+
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
125
|
+
usdhMint: usdhMintPublickey,
|
126
|
+
systemProgram: SystemProgram.programId,
|
127
|
+
tokenProgram: TOKEN_PROGRAM_ID
|
60
128
|
},
|
61
129
|
signers: []
|
62
130
|
})
|