hedge-web3 0.1.27 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/declarations/Constants.d.ts +1 -1
- package/declarations/idl/vault.d.ts +277 -126
- package/declarations/index.d.ts +3 -0
- package/declarations/instructions/claimLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/claimStakingPoolPosition.d.ts +3 -2
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +3 -2
- package/declarations/instructions/createStakingPool.d.ts +3 -2
- package/declarations/instructions/createVault.d.ts +6 -5
- package/declarations/instructions/depositLiquidationPool.d.ts +3 -2
- package/declarations/instructions/depositStakingPool.d.ts +3 -2
- package/declarations/instructions/depositVault.d.ts +3 -2
- package/declarations/instructions/initHedgeFoundation.d.ts +3 -2
- package/declarations/instructions/liquidateVault.d.ts +3 -2
- package/declarations/instructions/loanVault.d.ts +3 -2
- package/declarations/instructions/redeemVault.d.ts +3 -2
- package/declarations/instructions/refreshOraclePrice.d.ts +3 -2
- package/declarations/instructions/repayVault.d.ts +3 -2
- package/declarations/instructions/setHalted.d.ts +3 -2
- package/declarations/instructions/setVaultTypeStatus.d.ts +5 -0
- package/declarations/instructions/withdrawStakingPool.d.ts +3 -2
- package/declarations/instructions/withdrawVault.d.ts +3 -2
- package/declarations/state/VaultAccount.d.ts +8 -0
- package/declarations/utils/getLinkedListAccounts.d.ts +5 -0
- package/lib/Constants.js +1 -1
- package/lib/idl/vault.js +277 -126
- package/lib/index.js +3 -0
- package/lib/instructions/claimLiquidationPoolPosition.js +19 -22
- package/lib/instructions/claimStakingPoolPosition.js +19 -19
- package/lib/instructions/closeLiquidationPoolPosition.js +22 -22
- package/lib/instructions/createStakingPool.js +18 -19
- package/lib/instructions/createVault.js +28 -31
- package/lib/instructions/depositLiquidationPool.js +17 -18
- package/lib/instructions/depositStakingPool.js +16 -18
- package/lib/instructions/depositVault.js +31 -26
- package/lib/instructions/initHedgeFoundation.js +17 -19
- package/lib/instructions/initHedgeFoundationTokens.js +15 -15
- package/lib/instructions/liquidateVault.js +36 -32
- package/lib/instructions/loanVault.js +27 -22
- package/lib/instructions/redeemVault.js +28 -23
- package/lib/instructions/refreshOraclePrice.js +17 -17
- package/lib/instructions/repayVault.js +27 -22
- package/lib/instructions/setHalted.js +8 -9
- package/lib/instructions/setVaultTypeStatus.js +37 -0
- package/lib/instructions/withdrawStakingPool.js +22 -24
- package/lib/instructions/withdrawVault.js +30 -25
- package/lib/state/LiquidationPoolEra.js +3 -1
- package/lib/state/LiquidationPosition.js +0 -7
- package/lib/state/StakingPool.js +3 -4
- package/lib/state/VaultAccount.js +51 -1
- package/lib/utils/getLinkedListAccounts.js +139 -0
- package/package.json +4 -2
- package/src/Constants.ts +1 -1
- package/src/idl/vault.ts +554 -252
- package/src/index.ts +4 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +39 -29
- package/src/instructions/claimStakingPoolPosition.ts +45 -25
- package/src/instructions/closeLiquidationPoolPosition.ts +62 -32
- package/src/instructions/createStakingPool.ts +38 -37
- package/src/instructions/createVault.ts +81 -125
- package/src/instructions/depositLiquidationPool.ts +45 -26
- package/src/instructions/depositStakingPool.ts +32 -24
- package/src/instructions/depositVault.ts +77 -31
- package/src/instructions/initHedgeFoundation.ts +42 -43
- package/src/instructions/initHedgeFoundationTokens.ts +38 -39
- package/src/instructions/liquidateVault.ts +96 -22
- package/src/instructions/loanVault.ts +84 -30
- package/src/instructions/redeemVault.ts +91 -32
- package/src/instructions/refreshOraclePrice.ts +41 -32
- package/src/instructions/repayVault.ts +74 -29
- package/src/instructions/setHalted.ts +32 -24
- package/src/instructions/setVaultTypeStatus.ts +58 -0
- package/src/instructions/withdrawStakingPool.ts +44 -30
- package/src/instructions/withdrawVault.ts +87 -33
- package/src/state/LiquidationPoolEra.ts +4 -3
- package/src/state/LiquidationPosition.ts +0 -27
- package/src/state/StakingPool.ts +4 -7
- package/src/state/StakingPoolPosition.ts +2 -3
- package/src/state/VaultAccount.ts +65 -8
- package/src/state/VaultHistoryEvent.ts +1 -2
- package/src/utils/getLinkedListAccounts.ts +157 -0
@@ -1,11 +1,28 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
3
|
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
4
|
-
import {
|
5
|
-
|
4
|
+
import {
|
5
|
+
Keypair,
|
6
|
+
PublicKey,
|
7
|
+
sendAndConfirmTransaction,
|
8
|
+
Signer,
|
9
|
+
SystemProgram,
|
10
|
+
Transaction,
|
11
|
+
TransactionInstruction,
|
12
|
+
} from '@solana/web3.js'
|
13
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
getVaultTypeAccountPublicKey,
|
17
|
+
getUshMintPublicKey,
|
18
|
+
getVaultSystemStatePublicKey,
|
19
|
+
getPoolPublicKeyForMint,
|
20
|
+
getHedgeMintPublicKey,
|
21
|
+
} from '../Constants'
|
22
|
+
import { Vault } from 'idl/vault'
|
6
23
|
|
7
24
|
export async function depositVault(
|
8
|
-
program: Program
|
25
|
+
program: Program<Vault>,
|
9
26
|
provider: Provider,
|
10
27
|
payer: Signer,
|
11
28
|
vaultPublicKey: PublicKey,
|
@@ -20,10 +37,19 @@ export async function depositVault(
|
|
20
37
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
21
38
|
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
22
39
|
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
23
|
-
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
40
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
41
|
+
provider.connection,
|
42
|
+
payer,
|
43
|
+
vaultTypeAccountInfo.collateralMint,
|
44
|
+
vaultTypeAccountPublicKey,
|
45
|
+
true
|
46
|
+
)
|
24
47
|
|
25
48
|
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
|
26
|
-
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(
|
49
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(
|
50
|
+
vaultPublicKey,
|
51
|
+
vaultTypeAccountInfo.collateralMint
|
52
|
+
)
|
27
53
|
|
28
54
|
const history = Keypair.generate()
|
29
55
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
@@ -39,6 +65,17 @@ export async function depositVault(
|
|
39
65
|
const transaction = new Transaction()
|
40
66
|
const signers = [payer, history]
|
41
67
|
|
68
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
69
|
+
program,
|
70
|
+
provider,
|
71
|
+
vaultTypeAccountPublicKey,
|
72
|
+
vaultPublicKey,
|
73
|
+
depositAmount,
|
74
|
+
0,
|
75
|
+
false,
|
76
|
+
false
|
77
|
+
)
|
78
|
+
|
42
79
|
if (vaultAccount.collateralType === 'SOL') {
|
43
80
|
transaction.add(
|
44
81
|
SystemProgram.createAccount({
|
@@ -46,12 +83,12 @@ export async function depositVault(
|
|
46
83
|
lamports: depositAmount + 2.04e6,
|
47
84
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
48
85
|
programId: TOKEN_PROGRAM_ID,
|
49
|
-
space: 165
|
86
|
+
space: 165,
|
50
87
|
}),
|
51
88
|
TokenInstructions.initializeAccount({
|
52
89
|
account: wrappedSolAccount.publicKey,
|
53
90
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
54
|
-
owner: payer.publicKey
|
91
|
+
owner: payer.publicKey,
|
55
92
|
})
|
56
93
|
)
|
57
94
|
signers.push(wrappedSolAccount)
|
@@ -71,6 +108,9 @@ export async function depositVault(
|
|
71
108
|
hedgeStakingPoolAssociatedUshTokenAccount,
|
72
109
|
vaultTypeAccountInfo.collateralMint,
|
73
110
|
ushMintPublickey,
|
111
|
+
oldSmallerPublicKey,
|
112
|
+
newSmallerPublicKey,
|
113
|
+
newLargerPublicKey,
|
74
114
|
depositAmount,
|
75
115
|
overrideTime
|
76
116
|
)
|
@@ -80,17 +120,17 @@ export async function depositVault(
|
|
80
120
|
TokenInstructions.closeAccount({
|
81
121
|
source: wrappedSolAccount.publicKey,
|
82
122
|
destination: payer.publicKey,
|
83
|
-
owner: payer.publicKey
|
123
|
+
owner: payer.publicKey,
|
84
124
|
})
|
85
125
|
)
|
86
126
|
}
|
87
127
|
|
88
|
-
await sendAndConfirmTransaction(provider.connection, transaction, signers
|
128
|
+
await sendAndConfirmTransaction(provider.connection, transaction, signers)
|
89
129
|
return vaultPublicKey
|
90
130
|
}
|
91
131
|
|
92
132
|
export async function depositVaultInstruction(
|
93
|
-
program: Program
|
133
|
+
program: Program<Vault>,
|
94
134
|
vaultSystemStatePublicKey: PublicKey,
|
95
135
|
vaultOwner: PublicKey,
|
96
136
|
vaultOwnerTokenAccount: PublicKey,
|
@@ -103,29 +143,35 @@ export async function depositVaultInstruction(
|
|
103
143
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
104
144
|
collateralMint: PublicKey,
|
105
145
|
ushMintPublickey: PublicKey,
|
146
|
+
oldSmallerPublicKey: PublicKey,
|
147
|
+
newSmallerPublicKey: PublicKey,
|
148
|
+
newLargerPublicKey: PublicKey,
|
106
149
|
depositAmount: number,
|
107
150
|
overrideTime?: number
|
108
151
|
): Promise<TransactionInstruction> {
|
109
|
-
return program.
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
152
|
+
return await program.methods
|
153
|
+
.depositVault(
|
154
|
+
new BN(depositAmount),
|
155
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
156
|
+
)
|
157
|
+
.accounts({
|
158
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
159
|
+
vaultTypeAccount: vaultTypeAccountPublicKey,
|
160
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
161
|
+
collateralTokenMint: collateralMint,
|
162
|
+
vault: vaultPublicKey,
|
163
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
164
|
+
feePool: hedgeStakingPoolPublicKey,
|
165
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
166
|
+
history: historyPublicKey,
|
167
|
+
vaultOwner: vaultOwner,
|
168
|
+
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
169
|
+
ushMint: ushMintPublickey,
|
170
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
171
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
172
|
+
newLargerVaultInfo: newLargerPublicKey,
|
173
|
+
systemProgram: SystemProgram.programId,
|
174
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
130
175
|
})
|
176
|
+
.instruction()
|
131
177
|
}
|
@@ -1,64 +1,63 @@
|
|
1
1
|
import { Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
SYSVAR_RENT_PUBKEY,
|
10
|
+
Transaction,
|
11
|
+
TransactionInstruction,
|
12
|
+
} from '@solana/web3.js'
|
4
13
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
getHedgeMintPublicKey,
|
17
|
+
getLiquidationPoolStatePublicKey,
|
18
|
+
getLiquidationPoolUshAccountPublicKey,
|
19
|
+
getUshMintPublicKey,
|
20
|
+
getVaultSystemStatePublicKey,
|
21
|
+
} from '../Constants'
|
22
|
+
import { Vault } from 'idl/vault'
|
12
23
|
|
24
|
+
export async function initHedgeFoundation(program: Program<Vault>, provider: Provider, payer: Signer): Promise<PublicKey> {
|
13
25
|
const poolEra = Keypair.generate()
|
14
26
|
const transaction = new Transaction().add(
|
15
|
-
await initHedgeFoundationInstruction(
|
16
|
-
program,
|
17
|
-
poolEra.publicKey,
|
18
|
-
payer.publicKey,
|
19
|
-
)
|
27
|
+
await initHedgeFoundationInstruction(program, poolEra.publicKey, payer.publicKey)
|
20
28
|
)
|
21
29
|
|
22
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolEra]
|
30
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolEra]).catch(parseAnchorErrors)
|
23
31
|
return payer.publicKey
|
24
32
|
}
|
25
33
|
|
26
|
-
export async function initHedgeFoundationInstruction
|
27
|
-
program: Program
|
34
|
+
export async function initHedgeFoundationInstruction(
|
35
|
+
program: Program<Vault>,
|
28
36
|
poolEraPublicKey: PublicKey,
|
29
|
-
payerPublicKey: PublicKey
|
37
|
+
payerPublicKey: PublicKey
|
30
38
|
): Promise<TransactionInstruction> {
|
31
39
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
32
40
|
const poolStatePublickey = await getLiquidationPoolStatePublicKey()
|
33
41
|
const poolUshTokenAccount = await getLiquidationPoolUshAccountPublicKey()
|
34
42
|
const ushMintPublickey = await getUshMintPublicKey()
|
35
43
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
36
|
-
const founderHedgeTokenAccount = await findAssociatedTokenAddress(
|
37
|
-
|
38
|
-
hedgeMintPublickey
|
39
|
-
)
|
40
|
-
const communityHedgeTokenAccount = await findAssociatedTokenAddress(
|
41
|
-
vaultSystemStatePublicKey,
|
42
|
-
hedgeMintPublickey
|
43
|
-
)
|
44
|
+
const founderHedgeTokenAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
|
45
|
+
const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
|
44
46
|
|
45
|
-
return program.
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
|
-
systemProgram: SystemProgram.programId
|
61
|
-
},
|
62
|
-
signers: []
|
47
|
+
return await program.methods
|
48
|
+
.initHedgeFoundation()
|
49
|
+
.accounts({
|
50
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
51
|
+
poolState: poolStatePublickey,
|
52
|
+
poolEra: poolEraPublicKey,
|
53
|
+
poolUshAccount: poolUshTokenAccount,
|
54
|
+
founder: payerPublicKey,
|
55
|
+
ushMint: ushMintPublickey,
|
56
|
+
hedgeMint: hedgeMintPublickey,
|
57
|
+
rent: SYSVAR_RENT_PUBKEY,
|
58
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
59
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
|
+
systemProgram: SystemProgram.programId,
|
63
61
|
})
|
62
|
+
.instruction()
|
64
63
|
}
|
@@ -1,55 +1,54 @@
|
|
1
1
|
import { Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
SYSVAR_RENT_PUBKEY,
|
10
|
+
Transaction,
|
11
|
+
TransactionInstruction,
|
12
|
+
} from '@solana/web3.js'
|
4
13
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
getHedgeMintPublicKey,
|
17
|
+
getLiquidationPoolStatePublicKey,
|
18
|
+
getLiquidationPoolUshAccountPublicKey,
|
19
|
+
getUshMintPublicKey,
|
20
|
+
getVaultSystemStatePublicKey,
|
21
|
+
} from '../Constants'
|
12
22
|
|
23
|
+
export async function initHedgeFoundationTokens(program: Program, provider: Provider, payer: Signer): Promise<PublicKey> {
|
13
24
|
const poolEra = Keypair.generate()
|
14
|
-
const transaction = new Transaction().add(
|
15
|
-
await initHedgeFoundationTokensInstruction(
|
16
|
-
program,
|
17
|
-
payer.publicKey,
|
18
|
-
)
|
19
|
-
)
|
25
|
+
const transaction = new Transaction().add(await initHedgeFoundationTokensInstruction(program, payer.publicKey))
|
20
26
|
|
21
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
27
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
22
28
|
return payer.publicKey
|
23
29
|
}
|
24
30
|
|
25
|
-
export async function initHedgeFoundationTokensInstruction
|
31
|
+
export async function initHedgeFoundationTokensInstruction(
|
26
32
|
program: Program,
|
27
|
-
payerPublicKey: PublicKey
|
33
|
+
payerPublicKey: PublicKey
|
28
34
|
): Promise<TransactionInstruction> {
|
29
35
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
30
36
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
31
|
-
const founderHedgeTokenAccount = await findAssociatedTokenAddress(
|
32
|
-
|
33
|
-
hedgeMintPublickey
|
34
|
-
)
|
35
|
-
const communityHedgeTokenAccount = await findAssociatedTokenAddress(
|
36
|
-
vaultSystemStatePublicKey,
|
37
|
-
hedgeMintPublickey
|
38
|
-
)
|
37
|
+
const founderHedgeTokenAccount = await findAssociatedTokenAddress(payerPublicKey, hedgeMintPublickey)
|
38
|
+
const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
|
39
39
|
|
40
|
-
return program.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
},
|
53
|
-
signers: []
|
40
|
+
return await program.methods
|
41
|
+
.initHedgeFoundationTokens()
|
42
|
+
.accounts({
|
43
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
44
|
+
founder: payerPublicKey,
|
45
|
+
hedgeMint: hedgeMintPublickey,
|
46
|
+
founderAssociatedHedgeTokenAccount: founderHedgeTokenAccount,
|
47
|
+
communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
|
48
|
+
rent: SYSVAR_RENT_PUBKEY,
|
49
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
50
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
51
|
+
systemProgram: SystemProgram.programId,
|
54
52
|
})
|
53
|
+
.instruction()
|
55
54
|
}
|
@@ -1,10 +1,29 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
SYSVAR_RENT_PUBKEY,
|
10
|
+
Transaction,
|
11
|
+
TransactionInstruction,
|
12
|
+
} from '@solana/web3.js'
|
13
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
14
|
+
import {
|
15
|
+
getHedgeMintPublicKey,
|
16
|
+
getLiquidationPoolStatePublicKey,
|
17
|
+
getLiquidationPoolUshAccountPublicKey,
|
18
|
+
getPoolPublicKeyForMint,
|
19
|
+
getVaultTypeAccountPublicKey,
|
20
|
+
getUshMintPublicKey,
|
21
|
+
getVaultSystemStatePublicKey,
|
22
|
+
} from '../Constants'
|
23
|
+
import { Vault } from 'idl/vault'
|
5
24
|
|
6
|
-
export async function liquidateVault
|
7
|
-
program: Program
|
25
|
+
export async function liquidateVault(
|
26
|
+
program: Program<Vault>,
|
8
27
|
provider: Provider,
|
9
28
|
payer: Signer,
|
10
29
|
vaultPublicKey: PublicKey,
|
@@ -22,12 +41,59 @@ export async function liquidateVault (
|
|
22
41
|
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
23
42
|
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
24
43
|
|
25
|
-
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
44
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
45
|
+
provider.connection,
|
46
|
+
payer,
|
47
|
+
collateralMint,
|
48
|
+
payer.publicKey,
|
49
|
+
true
|
50
|
+
)
|
51
|
+
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
52
|
+
provider.connection,
|
53
|
+
payer,
|
54
|
+
collateralMint,
|
55
|
+
hedgeStakingPoolPublicKey,
|
56
|
+
true
|
57
|
+
)
|
58
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
59
|
+
provider.connection,
|
60
|
+
payer,
|
61
|
+
collateralMint,
|
62
|
+
vaultPublicKey,
|
63
|
+
true
|
64
|
+
)
|
65
|
+
const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
66
|
+
provider.connection,
|
67
|
+
payer,
|
68
|
+
collateralMint,
|
69
|
+
liquidationPoolStatePublicKey,
|
70
|
+
true
|
71
|
+
)
|
72
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
73
|
+
provider.connection,
|
74
|
+
payer,
|
75
|
+
collateralMint,
|
76
|
+
vaultTypeAccountPublicKey,
|
77
|
+
true
|
78
|
+
)
|
79
|
+
const hedgeStakingPoolAssociatedUshTokenAccount = await getOrCreateAssociatedTokenAccount(
|
80
|
+
provider.connection,
|
81
|
+
payer,
|
82
|
+
ushMintPublickey,
|
83
|
+
hedgeStakingPoolPublicKey,
|
84
|
+
true
|
85
|
+
)
|
86
|
+
|
87
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
88
|
+
program,
|
89
|
+
provider,
|
90
|
+
vaultTypeAccountPublicKey,
|
91
|
+
vaultPublicKey,
|
92
|
+
0,
|
93
|
+
0,
|
94
|
+
false,
|
95
|
+
true
|
96
|
+
)
|
31
97
|
|
32
98
|
const history = Keypair.generate()
|
33
99
|
const newEra = Keypair.generate()
|
@@ -50,16 +116,19 @@ export async function liquidateVault (
|
|
50
116
|
hedgeStakingPoolAssociatedUshTokenAccount.address,
|
51
117
|
collateralMint,
|
52
118
|
vaultTypeAssociatedTokenAccount.address,
|
119
|
+
oldSmallerPublicKey,
|
120
|
+
newSmallerPublicKey,
|
121
|
+
newLargerPublicKey,
|
53
122
|
vaultAccount.collateralType,
|
54
123
|
overrideTime
|
55
124
|
)
|
56
125
|
)
|
57
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra]
|
126
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra])
|
58
127
|
return vaultPublicKey
|
59
128
|
}
|
60
129
|
|
61
|
-
export async function liquidateVaultInstruction
|
62
|
-
program: Program
|
130
|
+
export async function liquidateVaultInstruction(
|
131
|
+
program: Program<Vault>,
|
63
132
|
payerPublicKey: PublicKey,
|
64
133
|
payerAssociatedTokenAccount: PublicKey,
|
65
134
|
vaultPublickey: PublicKey,
|
@@ -74,6 +143,9 @@ export async function liquidateVaultInstruction (
|
|
74
143
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
75
144
|
collateralMint: PublicKey,
|
76
145
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
146
|
+
oldSmallerPublicKey: PublicKey,
|
147
|
+
newSmallerPublicKey: PublicKey,
|
148
|
+
newLargerPublicKey: PublicKey,
|
77
149
|
collateralType: string,
|
78
150
|
overrideTime?: number
|
79
151
|
): Promise<TransactionInstruction> {
|
@@ -82,8 +154,11 @@ export async function liquidateVaultInstruction (
|
|
82
154
|
const liquidationPoolUshAccountPublickey = await getLiquidationPoolUshAccountPublicKey()
|
83
155
|
const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
|
84
156
|
|
85
|
-
|
86
|
-
|
157
|
+
return program.methods
|
158
|
+
.liquidateVault(
|
159
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
160
|
+
)
|
161
|
+
.accounts({
|
87
162
|
vaultSystemState: vaultSystemStatePublicKey,
|
88
163
|
vaultTypeAccount: vaultTypeAccount,
|
89
164
|
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
@@ -102,14 +177,13 @@ export async function liquidateVaultInstruction (
|
|
102
177
|
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
103
178
|
liquidationPoolUshAccount: liquidationPoolUshAccountPublickey,
|
104
179
|
newEra: newEraPublicKey,
|
180
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
181
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
182
|
+
newLargerVaultInfo: newLargerPublicKey,
|
105
183
|
tokenProgram: TOKEN_PROGRAM_ID,
|
106
184
|
systemProgram: SystemProgram.programId,
|
107
185
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
108
|
-
rent: SYSVAR_RENT_PUBKEY
|
109
|
-
}
|
110
|
-
|
111
|
-
}
|
112
|
-
return program.instruction.liquidateVault(
|
113
|
-
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
114
|
-
payload)
|
186
|
+
rent: SYSVAR_RENT_PUBKEY,
|
187
|
+
})
|
188
|
+
.instruction()
|
115
189
|
}
|