hedge-web3 0.1.13 → 0.1.14
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/index.js +373 -147
- package/lib/index.js.map +1 -1
- package/lib/types/src/Constants.d.ts +7 -6
- package/lib/types/src/Constants.d.ts.map +1 -1
- package/lib/types/src/index.d.ts +3 -0
- package/lib/types/src/index.d.ts.map +1 -1
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts +5 -0
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +1 -0
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts +5 -0
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +1 -0
- package/lib/types/src/instructions/createVault.d.ts +2 -2
- package/lib/types/src/instructions/createVault.d.ts.map +1 -1
- package/lib/types/src/instructions/depositLiquidationPool.d.ts +5 -0
- package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +1 -0
- package/lib/types/src/instructions/depositVault.d.ts +1 -1
- package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
- package/lib/types/src/instructions/liquidateVault.d.ts +2 -2
- package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
- package/lib/types/src/instructions/loanVault.d.ts +1 -1
- package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
- package/lib/types/src/instructions/redeemVault.d.ts +2 -2
- package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts +2 -2
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -1
- package/lib/types/src/instructions/repayVault.d.ts +1 -1
- package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
- package/lib/types/src/instructions/withdrawVault.d.ts +1 -1
- package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts +1 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPosition.d.ts +8 -9
- package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
- package/lib/types/tsconfig.base.tsbuildinfo +1 -1
- package/package.json +6 -2
- package/rollup.config.js +3 -1
- package/src/Constants.ts +21 -17
- package/src/idl/idl.ts +13 -13
- package/src/index.ts +3 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +68 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +88 -0
- package/src/instructions/createStakingPool.ts +4 -4
- package/src/instructions/createVault.ts +75 -14
- package/src/instructions/depositLiquidationPool.ts +70 -0
- package/src/instructions/depositStakingPool.ts +2 -2
- package/src/instructions/depositVault.ts +68 -12
- package/src/instructions/liquidateVault.ts +90 -35
- package/src/instructions/loanVault.ts +19 -7
- package/src/instructions/redeemVault.ts +41 -15
- package/src/instructions/refreshOraclePrice.ts +5 -2
- package/src/instructions/repayVault.ts +20 -8
- package/src/instructions/withdrawStakingPool.ts +6 -6
- package/src/instructions/withdrawVault.ts +28 -5
- package/src/state/LiquidationPoolEra.ts +2 -8
- package/src/state/LiquidationPosition.ts +38 -39
@@ -1,16 +1,21 @@
|
|
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, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
4
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
5
|
+
import { findAssociatedTokenAddress, findVaultAddress, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } 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
|
collateralRatio: number
|
12
17
|
): Promise<PublicKey> {
|
13
|
-
const
|
18
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
14
19
|
const USDH = new Token(
|
15
20
|
provider.connection,
|
16
21
|
usdhMintPublickey,
|
@@ -18,39 +23,89 @@ export async function createVault (
|
|
18
23
|
payer
|
19
24
|
)
|
20
25
|
|
26
|
+
const salt = uuidv4().substring(0, 8)
|
27
|
+
const newVaultPublicKey = await findVaultAddress(salt)
|
28
|
+
const history = Keypair.generate()
|
29
|
+
|
21
30
|
// Prep the user to get USDH back out at some point
|
22
31
|
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
32
|
|
24
|
-
const
|
25
|
-
const
|
26
|
-
|
27
|
-
const
|
33
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(collateralType)
|
34
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
|
35
|
+
|
36
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint)
|
37
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(newVaultPublicKey, collateralStateAccountInfo.collateralMint)
|
38
|
+
|
39
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
40
|
+
|
41
|
+
const wrappedSolAccount = Keypair.generate()
|
42
|
+
|
43
|
+
const transaction = new Transaction()
|
44
|
+
const signers = [payer, history]
|
45
|
+
|
46
|
+
if (collateralType === 'SOL') {
|
47
|
+
transaction.add(
|
48
|
+
SystemProgram.createAccount({
|
49
|
+
fromPubkey: payer.publicKey,
|
50
|
+
lamports: depositAmount + 2.04e6,
|
51
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
52
|
+
programId: TOKEN_PROGRAM_ID,
|
53
|
+
space: 165
|
54
|
+
}),
|
55
|
+
TokenInstructions.initializeAccount({
|
56
|
+
account: wrappedSolAccount.publicKey,
|
57
|
+
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
58
|
+
owner: payer.publicKey
|
59
|
+
})
|
60
|
+
)
|
61
|
+
signers.push(wrappedSolAccount)
|
62
|
+
}
|
63
|
+
transaction.add(
|
28
64
|
await createVaultInstruction(
|
29
65
|
program,
|
66
|
+
salt,
|
30
67
|
vaultSystemStatePublicKey,
|
31
68
|
payer.publicKey,
|
32
|
-
|
69
|
+
collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount,
|
70
|
+
newVaultPublicKey,
|
71
|
+
vaultAssociatedCollateralAccountPublicKey,
|
72
|
+
collateralStateAccountPublicKey,
|
73
|
+
collateralStateAccountInfo.collateralMint,
|
33
74
|
history.publicKey,
|
34
75
|
depositAmount,
|
35
76
|
collateralRatio
|
36
77
|
)
|
37
78
|
)
|
38
|
-
|
39
|
-
|
79
|
+
if (collateralType === 'SOL') {
|
80
|
+
transaction.add(
|
81
|
+
TokenInstructions.closeAccount({
|
82
|
+
source: wrappedSolAccount.publicKey,
|
83
|
+
destination: payer.publicKey,
|
84
|
+
owner: payer.publicKey
|
85
|
+
})
|
86
|
+
)
|
87
|
+
}
|
88
|
+
|
89
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers, provider?.opts).catch(parseAnchorErrors)
|
90
|
+
return newVaultPublicKey
|
40
91
|
}
|
41
92
|
|
42
93
|
export async function createVaultInstruction (
|
43
94
|
program: Program,
|
95
|
+
salt: string,
|
44
96
|
vaultSystemStatePublicKey: PublicKey,
|
45
97
|
payerPublicKey: PublicKey,
|
98
|
+
payerTokenAccountPublicKey: PublicKey,
|
46
99
|
vaultPublicKey: PublicKey,
|
100
|
+
vaultAssociatedCollateralPublicKey: PublicKey,
|
101
|
+
collateralStateAccount: PublicKey,
|
102
|
+
collateralMint: PublicKey,
|
47
103
|
historyPublicKey: PublicKey,
|
48
104
|
depositAmount: number,
|
49
105
|
collateralRatio: number
|
50
106
|
): Promise<TransactionInstruction> {
|
51
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
52
|
-
|
53
107
|
const ix = program.instruction.createVault(
|
108
|
+
salt,
|
54
109
|
new BN(depositAmount),
|
55
110
|
new BN(collateralRatio)
|
56
111
|
, {
|
@@ -58,9 +113,15 @@ export async function createVaultInstruction (
|
|
58
113
|
vaultSystemState: vaultSystemStatePublicKey,
|
59
114
|
collateralStateAccount: collateralStateAccount,
|
60
115
|
vault: vaultPublicKey,
|
116
|
+
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
61
117
|
history: historyPublicKey,
|
62
118
|
payer: payerPublicKey,
|
63
|
-
|
119
|
+
payerTokenAccount: payerTokenAccountPublicKey,
|
120
|
+
collateralMint: collateralMint,
|
121
|
+
systemProgram: SystemProgram.programId,
|
122
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
123
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
124
|
+
rent: SYSVAR_RENT_PUBKEY
|
64
125
|
},
|
65
126
|
signers: []
|
66
127
|
})
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, 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 } 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 USDH = new Token(
|
16
|
+
provider.connection,
|
17
|
+
usdhMintPublickey,
|
18
|
+
TOKEN_PROGRAM_ID,
|
19
|
+
payer
|
20
|
+
)
|
21
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
22
|
+
|
23
|
+
const poolPosition = Keypair.generate()
|
24
|
+
const transaction = new Transaction().add(
|
25
|
+
await depositLiquidationPoolInstruction(
|
26
|
+
program,
|
27
|
+
payer.publicKey,
|
28
|
+
payerUsdhAccount.address,
|
29
|
+
poolPosition.publicKey,
|
30
|
+
depositAmount,
|
31
|
+
overrideStartTime
|
32
|
+
)
|
33
|
+
)
|
34
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors)
|
35
|
+
return poolPosition.publicKey
|
36
|
+
}
|
37
|
+
|
38
|
+
export async function depositLiquidationPoolInstruction (
|
39
|
+
program: Program,
|
40
|
+
payerPublicKey: PublicKey,
|
41
|
+
payerUsdhAccount: PublicKey,
|
42
|
+
poolPositionPublicKey: PublicKey,
|
43
|
+
depositAmount: number,
|
44
|
+
overrideStartTime?: number
|
45
|
+
): Promise<TransactionInstruction> {
|
46
|
+
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
47
|
+
const liquidationPoolState = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
48
|
+
|
49
|
+
const poolUSDHAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
50
|
+
const usdhMint = await getUsdhMintPublicKey()
|
51
|
+
|
52
|
+
return program.instruction.depositLiquidationPool(
|
53
|
+
new BN(depositAmount),
|
54
|
+
new BN(overrideStartTime ?? Date.now() / 1000), // override current time
|
55
|
+
{
|
56
|
+
accounts: {
|
57
|
+
poolState: liquidationPoolStatePublicKey,
|
58
|
+
poolEra: liquidationPoolState.currentEra,
|
59
|
+
poolPosition: poolPositionPublicKey,
|
60
|
+
poolUsdhAccount: poolUSDHAccount,
|
61
|
+
usdhMint: usdhMint,
|
62
|
+
payer: payerPublicKey,
|
63
|
+
ownerUsdhAccount: payerUsdhAccount,
|
64
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
65
|
+
systemProgram: SystemProgram.programId,
|
66
|
+
rent: SYSVAR_RENT_PUBKEY
|
67
|
+
},
|
68
|
+
signers: []
|
69
|
+
})
|
70
|
+
}
|
@@ -51,8 +51,8 @@ export async function depositStakingPoolInstruction (
|
|
51
51
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
52
52
|
payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
|
53
53
|
rent: SYSVAR_RENT_PUBKEY,
|
54
|
-
|
55
|
-
|
54
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
55
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
56
56
|
systemProgram: SystemProgram.programId
|
57
57
|
},
|
58
58
|
signers: []
|
@@ -1,16 +1,18 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { TokenInstructions } from '@project-serum/serum'
|
2
3
|
import { Token, 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, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
6
|
|
6
7
|
export async function depositVault (
|
7
8
|
program: Program,
|
8
9
|
provider: Provider,
|
9
10
|
payer: Signer,
|
10
11
|
vaultPublicKey: PublicKey,
|
12
|
+
// collateralType: string,
|
11
13
|
depositAmount: number
|
12
14
|
): Promise<PublicKey> {
|
13
|
-
const
|
15
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
14
16
|
const USDH = new Token(
|
15
17
|
provider.connection,
|
16
18
|
usdhMintPublickey,
|
@@ -21,42 +23,96 @@ export async function depositVault (
|
|
21
23
|
// Prep the user to get USDH back out at some point
|
22
24
|
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
25
|
|
26
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
27
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
|
28
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
|
29
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccountInfo.collateralMint)
|
30
|
+
|
31
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint)
|
32
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint)
|
33
|
+
|
24
34
|
const history = Keypair.generate()
|
25
|
-
const
|
26
|
-
|
35
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
36
|
+
|
37
|
+
const wrappedSolAccount = Keypair.generate()
|
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,
|
68
|
+
collateralStateAccountPublicKey,
|
69
|
+
collateralAssociatedTokenAccount,
|
70
|
+
collateralStateAccountInfo.collateralMint,
|
33
71
|
depositAmount
|
34
72
|
)
|
35
73
|
)
|
36
|
-
|
74
|
+
if (vaultAccount.collateralType === 'SOL') {
|
75
|
+
transaction.add(
|
76
|
+
TokenInstructions.closeAccount({
|
77
|
+
source: wrappedSolAccount.publicKey,
|
78
|
+
destination: payer.publicKey,
|
79
|
+
owner: payer.publicKey
|
80
|
+
})
|
81
|
+
)
|
82
|
+
}
|
83
|
+
|
84
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers, provider.opts)
|
37
85
|
return vaultPublicKey
|
38
86
|
}
|
39
87
|
|
40
88
|
export async function depositVaultInstruction (
|
41
89
|
program: Program,
|
42
90
|
vaultSystemStatePublicKey: PublicKey,
|
43
|
-
|
91
|
+
vaultOwner: PublicKey,
|
92
|
+
vaultOwnerTokenAccount: PublicKey,
|
44
93
|
vaultPublicKey: PublicKey,
|
94
|
+
vaultAssociatedTokenAccount: PublicKey,
|
45
95
|
historyPublicKey: PublicKey,
|
96
|
+
collateralStateAccountPublicKey: PublicKey,
|
97
|
+
collateralAssociatedTokenAccount: PublicKey,
|
98
|
+
collateralMint: PublicKey,
|
46
99
|
depositAmount: number
|
47
100
|
): Promise<TransactionInstruction> {
|
48
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
49
|
-
|
50
101
|
return program.instruction.depositVault(
|
51
102
|
new BN(depositAmount)
|
52
103
|
, {
|
53
104
|
accounts: {
|
54
105
|
vaultSystemState: vaultSystemStatePublicKey,
|
55
|
-
collateralStateAccount:
|
56
|
-
|
106
|
+
collateralStateAccount: collateralStateAccountPublicKey,
|
107
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
108
|
+
collateralTokenMint: collateralMint,
|
109
|
+
vault: vaultPublicKey,
|
110
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
57
111
|
history: historyPublicKey,
|
58
|
-
vaultOwner:
|
59
|
-
|
112
|
+
vaultOwner: vaultOwner,
|
113
|
+
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
114
|
+
systemProgram: SystemProgram.programId,
|
115
|
+
tokenProgram: TOKEN_PROGRAM_ID
|
60
116
|
},
|
61
117
|
signers: []
|
62
118
|
})
|
@@ -1,23 +1,61 @@
|
|
1
1
|
import { Program, Provider } from '@project-serum/anchor'
|
2
|
-
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint,
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, 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 { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, findAssociatedTokenAddress } from '../Constants'
|
5
5
|
|
6
6
|
export async function liquidateVault (
|
7
7
|
program: Program,
|
8
8
|
provider: Provider,
|
9
9
|
payer: Signer,
|
10
|
-
vaultPublicKey: PublicKey
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
preload: boolean
|
11
12
|
): Promise<PublicKey> {
|
13
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
14
|
+
|
15
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
|
16
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
|
17
|
+
const collateralMint = collateralStateAccountInfo.collateralMint
|
18
|
+
|
19
|
+
const token = new Token(
|
20
|
+
provider.connection,
|
21
|
+
collateralMint,
|
22
|
+
TOKEN_PROGRAM_ID,
|
23
|
+
payer
|
24
|
+
)
|
25
|
+
|
26
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
27
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
28
|
+
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
29
|
+
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
30
|
+
|
31
|
+
const payerAssociatedTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
32
|
+
const feePoolAssociatedTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, collateralMint)
|
33
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralMint)
|
34
|
+
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(liquidationPoolStatePublicKey, collateralMint)
|
35
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralMint)
|
36
|
+
|
12
37
|
const history = Keypair.generate()
|
13
38
|
const newEra = Keypair.generate()
|
14
|
-
const transaction = new Transaction()
|
39
|
+
const transaction = new Transaction()
|
40
|
+
|
41
|
+
transaction.add(
|
15
42
|
await liquidateVaultInstruction(
|
16
43
|
program,
|
17
44
|
payer.publicKey,
|
45
|
+
payerAssociatedTokenAccount.address,
|
18
46
|
vaultPublicKey,
|
47
|
+
vaultAssociatedTokenAccount,
|
48
|
+
liquidationPoolStatePublicKey,
|
49
|
+
poolStateInfo.currentEra,
|
50
|
+
poolAssociatedTokenAccount,
|
19
51
|
history.publicKey,
|
20
|
-
newEra.publicKey
|
52
|
+
newEra.publicKey,
|
53
|
+
hedgeStakingPoolPublicKey,
|
54
|
+
feePoolAssociatedTokenAccount,
|
55
|
+
collateralMint,
|
56
|
+
collateralAssociatedTokenAccount,
|
57
|
+
vaultAccount.collateralType,
|
58
|
+
preload
|
21
59
|
)
|
22
60
|
)
|
23
61
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts)
|
@@ -27,37 +65,54 @@ export async function liquidateVault (
|
|
27
65
|
export async function liquidateVaultInstruction (
|
28
66
|
program: Program,
|
29
67
|
payerPublicKey: PublicKey,
|
68
|
+
payerAssociatedTokenAccount: PublicKey,
|
30
69
|
vaultPublickey: PublicKey,
|
70
|
+
vaultAssociatedTokenAccount: PublicKey,
|
71
|
+
poolState: PublicKey,
|
72
|
+
poolEra: PublicKey,
|
73
|
+
poolAssociatedTokenAccount: PublicKey,
|
31
74
|
historyPublicKey: PublicKey,
|
32
|
-
newEraPublicKey: PublicKey
|
75
|
+
newEraPublicKey: PublicKey,
|
76
|
+
feePool: PublicKey,
|
77
|
+
feePoolAssociatedTokenAccount: PublicKey,
|
78
|
+
collateralMint: PublicKey,
|
79
|
+
collateralAssociatedTokenAccount: PublicKey,
|
80
|
+
collateralType: string,
|
81
|
+
preload: boolean
|
33
82
|
): Promise<TransactionInstruction> {
|
34
|
-
const
|
35
|
-
const
|
36
|
-
const
|
37
|
-
const
|
38
|
-
const [liquidationPoolStatePublicKey] = await getLiquidationPoolStatePublicKey()
|
39
|
-
const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey()
|
40
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
41
|
-
|
42
|
-
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
83
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
84
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
85
|
+
const liquidationPoolUsdhAccountPublickey = await getLiquidationPoolUsdhAccountPublicKey()
|
86
|
+
const collateralStateAccount = await getCollateralStateAccountPublicKey(collateralType)
|
43
87
|
|
44
|
-
|
45
|
-
{
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
88
|
+
const payload = {
|
89
|
+
accounts: {
|
90
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
91
|
+
collateralStateAccount: collateralStateAccount,
|
92
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
93
|
+
collateralMint: collateralMint,
|
94
|
+
poolEra: poolEra,
|
95
|
+
vaultAccount: vaultPublickey,
|
96
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
97
|
+
poolState: poolState,
|
98
|
+
poolAssociatedTokenAccount: poolAssociatedTokenAccount,
|
99
|
+
usdhMint: usdhMintPublickey,
|
100
|
+
history: historyPublicKey,
|
101
|
+
payer: payerPublicKey,
|
102
|
+
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
103
|
+
feePool: feePool,
|
104
|
+
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
105
|
+
liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
|
106
|
+
newEra: newEraPublicKey,
|
107
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
108
|
+
systemProgram: SystemProgram.programId,
|
109
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
110
|
+
rent: SYSVAR_RENT_PUBKEY
|
111
|
+
},
|
112
|
+
signers: []
|
113
|
+
}
|
114
|
+
if (preload) {
|
115
|
+
return program.instruction.liquidateVaultPrepAccounts(payload)
|
116
|
+
}
|
117
|
+
return program.instruction.liquidateVault(payload)
|
63
118
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint,
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getCollateralStateAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
5
|
|
6
6
|
export async function loanVault (
|
7
7
|
program: Program,
|
@@ -10,7 +10,7 @@ export async function loanVault (
|
|
10
10
|
vaultPublicKey: PublicKey,
|
11
11
|
loanAmount: number
|
12
12
|
): Promise<PublicKey> {
|
13
|
-
const
|
13
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
14
14
|
const USDH = new Token(
|
15
15
|
provider.connection,
|
16
16
|
usdhMintPublickey,
|
@@ -20,6 +20,11 @@ export async function loanVault (
|
|
20
20
|
|
21
21
|
// Prep the user to get USDH back out at some point
|
22
22
|
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
23
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
24
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType)
|
25
|
+
const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey)
|
26
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint)
|
27
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint)
|
23
28
|
|
24
29
|
const history = Keypair.generate()
|
25
30
|
const transaction = new Transaction().add(
|
@@ -28,7 +33,10 @@ export async function loanVault (
|
|
28
33
|
payer.publicKey,
|
29
34
|
payerUsdhAccount.address,
|
30
35
|
vaultPublicKey,
|
36
|
+
vaultAssociatedTokenAccount,
|
31
37
|
history.publicKey,
|
38
|
+
collateralStateAccountPublicKey,
|
39
|
+
collateralAssociatedTokenAccount,
|
32
40
|
loanAmount
|
33
41
|
)
|
34
42
|
)
|
@@ -41,18 +49,20 @@ export async function loanVaultInstruction (
|
|
41
49
|
payerPublicKey: PublicKey,
|
42
50
|
ownerUsdhAccount: PublicKey,
|
43
51
|
vaultPublickey: PublicKey,
|
52
|
+
vaultAssociatedTokenAccount: PublicKey,
|
44
53
|
historyPublicKey: PublicKey,
|
54
|
+
collateralStateAccount: PublicKey,
|
55
|
+
collateralAssociatedTokenAccount: PublicKey,
|
45
56
|
loanAmount: number
|
46
57
|
): Promise<TransactionInstruction> {
|
47
|
-
const
|
48
|
-
const
|
49
|
-
const
|
58
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
59
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
60
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
50
61
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
51
62
|
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
52
63
|
hedgeStakingPoolPublicKey,
|
53
64
|
usdhMintPublickey
|
54
65
|
)
|
55
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey()
|
56
66
|
|
57
67
|
return program.instruction.loanVault(
|
58
68
|
new BN(loanAmount),
|
@@ -60,14 +70,16 @@ export async function loanVaultInstruction (
|
|
60
70
|
accounts: {
|
61
71
|
vaultSystemState: vaultSystemStatePublicKey,
|
62
72
|
collateralStateAccount: collateralStateAccount,
|
73
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
63
74
|
vaultAccount: vaultPublickey,
|
75
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
64
76
|
history: historyPublicKey,
|
65
77
|
feePool: hedgeStakingPoolPublicKey,
|
66
78
|
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
67
79
|
usdhMint: usdhMintPublickey,
|
68
80
|
vaultOwner: payerPublicKey,
|
69
81
|
ownerUsdhAccount: ownerUsdhAccount,
|
70
|
-
|
82
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
71
83
|
systemProgram: SystemProgram.programId
|
72
84
|
},
|
73
85
|
signers: []
|