hedge-web3 0.1.27 → 0.1.28
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/index.d.ts +2 -0
- package/declarations/instructions/depositVault.d.ts +1 -1
- package/declarations/instructions/liquidateVault.d.ts +1 -1
- package/declarations/instructions/loanVault.d.ts +1 -1
- package/declarations/instructions/redeemVault.d.ts +1 -1
- package/declarations/instructions/repayVault.d.ts +1 -1
- package/declarations/instructions/setVaultTypeStatus.d.ts +4 -0
- package/declarations/instructions/withdrawVault.d.ts +1 -1
- package/declarations/state/VaultAccount.d.ts +8 -0
- package/declarations/utils/getLinkedListAccounts.d.ts +3 -0
- package/lib/Constants.js +1 -1
- package/lib/index.js +2 -0
- package/lib/instructions/createStakingPool.js +2 -2
- package/lib/instructions/depositVault.js +14 -7
- package/lib/instructions/liquidateVault.js +9 -4
- package/lib/instructions/loanVault.js +9 -4
- package/lib/instructions/redeemVault.js +7 -2
- package/lib/instructions/repayVault.js +9 -4
- package/lib/instructions/setVaultTypeStatus.js +38 -0
- package/lib/instructions/withdrawVault.js +12 -7
- package/lib/state/VaultAccount.js +54 -1
- package/lib/utils/getLinkedListAccounts.js +131 -0
- package/package.json +3 -1
- package/src/Constants.ts +1 -1
- package/src/index.ts +3 -0
- package/src/instructions/createStakingPool.ts +1 -2
- package/src/instructions/depositVault.ts +97 -22
- package/src/instructions/liquidateVault.ts +118 -21
- package/src/instructions/loanVault.ts +91 -19
- package/src/instructions/redeemVault.ts +23 -0
- package/src/instructions/repayVault.ts +84 -19
- package/src/instructions/setVaultTypeStatus.ts +50 -0
- package/src/instructions/withdrawVault.ts +99 -21
- package/src/state/VaultAccount.ts +86 -10
- package/src/utils/getLinkedListAccounts.ts +156 -0
@@ -1,9 +1,28 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
2
|
+
import {
|
3
|
+
getOrCreateAssociatedTokenAccount,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
} from '@solana/spl-token'
|
6
|
+
import {
|
7
|
+
Keypair,
|
8
|
+
PublicKey,
|
9
|
+
sendAndConfirmTransaction,
|
10
|
+
Signer,
|
11
|
+
SystemProgram,
|
12
|
+
Transaction,
|
13
|
+
TransactionInstruction,
|
14
|
+
} from '@solana/web3.js'
|
15
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
16
|
+
import {
|
17
|
+
findAssociatedTokenAddress,
|
18
|
+
getHedgeMintPublicKey,
|
19
|
+
getPoolPublicKeyForMint,
|
20
|
+
getVaultTypeAccountPublicKey,
|
21
|
+
getUshMintPublicKey,
|
22
|
+
getVaultSystemStatePublicKey,
|
23
|
+
} from '../Constants'
|
5
24
|
|
6
|
-
export async function loanVault
|
25
|
+
export async function loanVault(
|
7
26
|
program: Program,
|
8
27
|
provider: Provider,
|
9
28
|
payer: Signer,
|
@@ -13,13 +32,47 @@ export async function loanVault (
|
|
13
32
|
): Promise<PublicKey> {
|
14
33
|
const ushMintPublickey = await getUshMintPublicKey()
|
15
34
|
|
16
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
35
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
36
|
+
provider.connection,
|
37
|
+
payer,
|
38
|
+
ushMintPublickey,
|
39
|
+
payer.publicKey
|
40
|
+
)
|
17
41
|
|
18
42
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
19
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
20
|
-
|
21
|
-
|
22
|
-
const
|
43
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
44
|
+
vaultAccount.collateralType
|
45
|
+
)
|
46
|
+
const vaultTypeAccount = await program.account.vaultType.fetch(
|
47
|
+
vaultTypeAccountPublicKey
|
48
|
+
)
|
49
|
+
const vaultTypeAssociatedTokenAccount =
|
50
|
+
await getOrCreateAssociatedTokenAccount(
|
51
|
+
provider.connection,
|
52
|
+
payer,
|
53
|
+
vaultTypeAccount.collateralMint,
|
54
|
+
vaultTypeAccountPublicKey,
|
55
|
+
true
|
56
|
+
)
|
57
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
58
|
+
provider.connection,
|
59
|
+
payer,
|
60
|
+
vaultTypeAccount.collateralMint,
|
61
|
+
vaultPublicKey,
|
62
|
+
true
|
63
|
+
)
|
64
|
+
|
65
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
66
|
+
await getLinkedListAccounts(
|
67
|
+
program,
|
68
|
+
provider,
|
69
|
+
vaultTypeAccountPublicKey,
|
70
|
+
vaultPublicKey,
|
71
|
+
0,
|
72
|
+
loanAmount,
|
73
|
+
false,
|
74
|
+
false
|
75
|
+
)
|
23
76
|
|
24
77
|
const history = Keypair.generate()
|
25
78
|
const transaction = new Transaction().add(
|
@@ -32,15 +85,23 @@ export async function loanVault (
|
|
32
85
|
history.publicKey,
|
33
86
|
vaultTypeAccountPublicKey,
|
34
87
|
vaultTypeAssociatedTokenAccount.address,
|
88
|
+
oldSmallerPublicKey,
|
89
|
+
newSmallerPublicKey,
|
90
|
+
newLargerPublicKey,
|
35
91
|
loanAmount,
|
36
92
|
overrideTime
|
37
93
|
)
|
38
94
|
)
|
39
|
-
await sendAndConfirmTransaction(
|
95
|
+
await sendAndConfirmTransaction(
|
96
|
+
provider.connection,
|
97
|
+
transaction,
|
98
|
+
[payer, history],
|
99
|
+
provider.opts
|
100
|
+
)
|
40
101
|
return vaultPublicKey
|
41
102
|
}
|
42
103
|
|
43
|
-
export async function loanVaultInstruction
|
104
|
+
export async function loanVaultInstruction(
|
44
105
|
program: Program,
|
45
106
|
payerPublicKey: PublicKey,
|
46
107
|
ownerUshAccount: PublicKey,
|
@@ -49,17 +110,23 @@ export async function loanVaultInstruction (
|
|
49
110
|
historyPublicKey: PublicKey,
|
50
111
|
vaultTypeAccount: PublicKey,
|
51
112
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
113
|
+
oldSmallerPublicKey: PublicKey,
|
114
|
+
newSmallerPublicKey: PublicKey,
|
115
|
+
newLargerPublicKey: PublicKey,
|
52
116
|
loanAmount: number,
|
53
117
|
overrideTime?: number
|
54
118
|
): Promise<TransactionInstruction> {
|
55
119
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
56
120
|
const ushMintPublickey = await getUshMintPublicKey()
|
57
121
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
58
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
59
|
-
|
60
|
-
hedgeStakingPoolPublicKey,
|
61
|
-
ushMintPublickey
|
122
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
123
|
+
hedgeMintPublickey
|
62
124
|
)
|
125
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
126
|
+
await findAssociatedTokenAddress(
|
127
|
+
hedgeStakingPoolPublicKey,
|
128
|
+
ushMintPublickey
|
129
|
+
)
|
63
130
|
|
64
131
|
return program.instruction.loanVault(
|
65
132
|
new BN(loanAmount),
|
@@ -73,13 +140,18 @@ export async function loanVaultInstruction (
|
|
73
140
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
74
141
|
history: historyPublicKey,
|
75
142
|
feePool: hedgeStakingPoolPublicKey,
|
76
|
-
feePoolAssociatedUshTokenAccount:
|
143
|
+
feePoolAssociatedUshTokenAccount:
|
144
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
77
145
|
ushMint: ushMintPublickey,
|
78
146
|
vaultOwner: payerPublicKey,
|
79
147
|
ownerUshAccount: ownerUshAccount,
|
148
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
149
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
150
|
+
newLargerVaultInfo: newLargerPublicKey,
|
80
151
|
tokenProgram: TOKEN_PROGRAM_ID,
|
81
|
-
systemProgram: SystemProgram.programId
|
152
|
+
systemProgram: SystemProgram.programId,
|
82
153
|
},
|
83
|
-
signers: []
|
84
|
-
}
|
154
|
+
signers: [],
|
155
|
+
}
|
156
|
+
)
|
85
157
|
}
|
@@ -2,6 +2,7 @@ import { BN, Program, Provider } from '@project-serum/anchor'
|
|
2
2
|
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
// import { TokenInstructions } from '@project-serum/serum'
|
4
4
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
5
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
5
6
|
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
7
|
|
7
8
|
export async function redeemVault (
|
@@ -26,6 +27,19 @@ export async function redeemVault (
|
|
26
27
|
|
27
28
|
const payerTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
|
28
29
|
|
30
|
+
|
31
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
32
|
+
await getLinkedListAccounts(
|
33
|
+
program,
|
34
|
+
provider,
|
35
|
+
vaultTypeAccountPublicKey,
|
36
|
+
vaultPublicKey,
|
37
|
+
0,
|
38
|
+
0,
|
39
|
+
true,
|
40
|
+
false
|
41
|
+
)
|
42
|
+
|
29
43
|
const history = Keypair.generate()
|
30
44
|
const transaction = new Transaction().add(
|
31
45
|
await redeemVaultInstruction(
|
@@ -38,6 +52,9 @@ export async function redeemVault (
|
|
38
52
|
history.publicKey,
|
39
53
|
vaultTypeAccountPublicKey,
|
40
54
|
vaultTypeAssociatedTokenAccount.address,
|
55
|
+
oldSmallerPublicKey,
|
56
|
+
newSmallerPublicKey,
|
57
|
+
newLargerPublicKey,
|
41
58
|
redeemAmount,
|
42
59
|
transactionOverrideTime
|
43
60
|
)
|
@@ -56,6 +73,9 @@ export async function redeemVaultInstruction (
|
|
56
73
|
historyPublicKey: PublicKey,
|
57
74
|
vaultTypeAccount: PublicKey,
|
58
75
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
76
|
+
oldSmallerPublicKey: PublicKey,
|
77
|
+
newSmallerPublicKey: PublicKey,
|
78
|
+
newLargerPublicKey: PublicKey,
|
59
79
|
redeemAmount: number,
|
60
80
|
transactionOverrideTime?: number
|
61
81
|
): Promise<TransactionInstruction> {
|
@@ -84,6 +104,9 @@ export async function redeemVaultInstruction (
|
|
84
104
|
payer: payerPublicKey,
|
85
105
|
payerUshAccount: payerUshAccount,
|
86
106
|
destinationTokenAccount: destinationTokenAccount,
|
107
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
108
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
109
|
+
newLargerVaultInfo: newLargerPublicKey,
|
87
110
|
tokenProgram: TOKEN_PROGRAM_ID,
|
88
111
|
systemProgram: SystemProgram.programId
|
89
112
|
},
|
@@ -1,9 +1,28 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
2
|
+
import {
|
3
|
+
getOrCreateAssociatedTokenAccount,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
} from '@solana/spl-token'
|
6
|
+
import {
|
7
|
+
Keypair,
|
8
|
+
PublicKey,
|
9
|
+
sendAndConfirmTransaction,
|
10
|
+
Signer,
|
11
|
+
SystemProgram,
|
12
|
+
Transaction,
|
13
|
+
TransactionInstruction,
|
14
|
+
} from '@solana/web3.js'
|
15
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
16
|
+
import {
|
17
|
+
findAssociatedTokenAddress,
|
18
|
+
getHedgeMintPublicKey,
|
19
|
+
getPoolPublicKeyForMint,
|
20
|
+
getVaultTypeAccountPublicKey,
|
21
|
+
getUshMintPublicKey,
|
22
|
+
getVaultSystemStatePublicKey,
|
23
|
+
} from '../Constants'
|
5
24
|
|
6
|
-
export async function repayVault
|
25
|
+
export async function repayVault(
|
7
26
|
program: Program,
|
8
27
|
provider: Provider,
|
9
28
|
payer: Signer,
|
@@ -14,13 +33,40 @@ export async function repayVault (
|
|
14
33
|
const ushMintPublickey = await getUshMintPublicKey()
|
15
34
|
|
16
35
|
// Prep the user to get USH back out at some point
|
17
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
36
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
37
|
+
provider.connection,
|
38
|
+
payer,
|
39
|
+
ushMintPublickey,
|
40
|
+
payer.publicKey
|
41
|
+
)
|
18
42
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
19
43
|
|
20
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
21
|
-
|
22
|
-
|
23
|
-
const
|
44
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
45
|
+
vaultAccount.collateralType
|
46
|
+
)
|
47
|
+
const vaultTypeAccount = await program.account.vaultType.fetch(
|
48
|
+
vaultTypeAccountPublicKey
|
49
|
+
)
|
50
|
+
const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
|
51
|
+
vaultTypeAccountPublicKey,
|
52
|
+
vaultTypeAccount.collateralMint
|
53
|
+
)
|
54
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
55
|
+
vaultPublicKey,
|
56
|
+
vaultTypeAccount.collateralMint
|
57
|
+
)
|
58
|
+
|
59
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
60
|
+
await getLinkedListAccounts(
|
61
|
+
program,
|
62
|
+
provider,
|
63
|
+
vaultTypeAccountPublicKey,
|
64
|
+
vaultPublicKey,
|
65
|
+
0,
|
66
|
+
repayAmount * -1,
|
67
|
+
false,
|
68
|
+
false
|
69
|
+
)
|
24
70
|
|
25
71
|
const history = Keypair.generate()
|
26
72
|
const transaction = new Transaction().add(
|
@@ -33,15 +79,23 @@ export async function repayVault (
|
|
33
79
|
history.publicKey,
|
34
80
|
vaultTypeAccountPublicKey,
|
35
81
|
vaultTypeAssociatedTokenAccount,
|
82
|
+
oldSmallerPublicKey,
|
83
|
+
newSmallerPublicKey,
|
84
|
+
newLargerPublicKey,
|
36
85
|
repayAmount,
|
37
86
|
overrideTime
|
38
87
|
)
|
39
88
|
)
|
40
|
-
await sendAndConfirmTransaction(
|
89
|
+
await sendAndConfirmTransaction(
|
90
|
+
provider.connection,
|
91
|
+
transaction,
|
92
|
+
[payer, history],
|
93
|
+
provider.opts
|
94
|
+
)
|
41
95
|
return vaultPublicKey
|
42
96
|
}
|
43
97
|
|
44
|
-
export async function repayVaultInstruction
|
98
|
+
export async function repayVaultInstruction(
|
45
99
|
program: Program,
|
46
100
|
payerPublicKey: PublicKey,
|
47
101
|
ownerUshAccount: PublicKey,
|
@@ -50,17 +104,23 @@ export async function repayVaultInstruction (
|
|
50
104
|
historyPublicKey: PublicKey,
|
51
105
|
vaultTypeAccount: PublicKey,
|
52
106
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
107
|
+
oldSmallerPublicKey: PublicKey,
|
108
|
+
newSmallerPublicKey: PublicKey,
|
109
|
+
newLargerPublicKey: PublicKey,
|
53
110
|
repayAmount: number,
|
54
111
|
overrideTime?: number
|
55
112
|
): Promise<TransactionInstruction> {
|
56
113
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
57
114
|
const ushMintPublickey = await getUshMintPublicKey()
|
58
115
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
59
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
60
|
-
|
61
|
-
hedgeStakingPoolPublicKey,
|
62
|
-
ushMintPublickey
|
116
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
117
|
+
hedgeMintPublickey
|
63
118
|
)
|
119
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
120
|
+
await findAssociatedTokenAddress(
|
121
|
+
hedgeStakingPoolPublicKey,
|
122
|
+
ushMintPublickey
|
123
|
+
)
|
64
124
|
|
65
125
|
return program.instruction.repayVault(
|
66
126
|
new BN(repayAmount),
|
@@ -74,13 +134,18 @@ export async function repayVaultInstruction (
|
|
74
134
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
75
135
|
history: historyPublicKey,
|
76
136
|
feePool: hedgeStakingPoolPublicKey,
|
77
|
-
feePoolAssociatedUshTokenAccount:
|
137
|
+
feePoolAssociatedUshTokenAccount:
|
138
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
78
139
|
ushMint: ushMintPublickey,
|
79
140
|
vaultOwner: payerPublicKey,
|
141
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
142
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
143
|
+
newLargerVaultInfo: newLargerPublicKey,
|
80
144
|
ownerUshAccount: ownerUshAccount,
|
81
145
|
tokenProgram: TOKEN_PROGRAM_ID,
|
82
|
-
systemProgram: SystemProgram.programId
|
146
|
+
systemProgram: SystemProgram.programId,
|
83
147
|
},
|
84
|
-
signers: []
|
85
|
-
}
|
148
|
+
signers: [],
|
149
|
+
}
|
150
|
+
)
|
86
151
|
}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
3
|
+
import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUshMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
4
|
+
|
5
|
+
import { v4 as uuidv4 } from 'uuid'
|
6
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
7
|
+
|
8
|
+
export async function setVaultTypeStatus (
|
9
|
+
program: Program,
|
10
|
+
provider: Provider,
|
11
|
+
payer: Signer,
|
12
|
+
vaultTypeAccount: PublicKey,
|
13
|
+
deprecated: boolean
|
14
|
+
): Promise<PublicKey> {
|
15
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
16
|
+
|
17
|
+
|
18
|
+
const transaction = new Transaction().add(
|
19
|
+
await setVaultTypeStatusInstruction(
|
20
|
+
program,
|
21
|
+
vaultSystemStatePublicKey,
|
22
|
+
payer.publicKey,
|
23
|
+
vaultTypeAccount,
|
24
|
+
deprecated,
|
25
|
+
)
|
26
|
+
)
|
27
|
+
|
28
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider?.opts).catch(parseAnchorErrors)
|
29
|
+
return vaultSystemStatePublicKey
|
30
|
+
}
|
31
|
+
|
32
|
+
export async function setVaultTypeStatusInstruction(
|
33
|
+
program: Program,
|
34
|
+
vaultSystemStatePublicKey: PublicKey,
|
35
|
+
payerPublicKey: PublicKey,
|
36
|
+
vaultTypeAccount: PublicKey,
|
37
|
+
deprecated: boolean
|
38
|
+
): Promise<TransactionInstruction> {
|
39
|
+
const ix = program.instruction.setVaultTypeStatus(
|
40
|
+
deprecated,
|
41
|
+
{
|
42
|
+
accounts: {
|
43
|
+
payer: payerPublicKey,
|
44
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
45
|
+
vaultType: vaultTypeAccount,
|
46
|
+
},
|
47
|
+
signers: []
|
48
|
+
})
|
49
|
+
return ix
|
50
|
+
}
|
@@ -1,10 +1,29 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
getOrCreateAssociatedTokenAccount,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
} from '@solana/spl-token'
|
3
6
|
import { TokenInstructions } from '@project-serum/serum'
|
4
|
-
import {
|
5
|
-
|
7
|
+
import {
|
8
|
+
Keypair,
|
9
|
+
PublicKey,
|
10
|
+
sendAndConfirmTransaction,
|
11
|
+
Signer,
|
12
|
+
SystemProgram,
|
13
|
+
Transaction,
|
14
|
+
TransactionInstruction,
|
15
|
+
} from '@solana/web3.js'
|
16
|
+
import {
|
17
|
+
findAssociatedTokenAddress,
|
18
|
+
getVaultTypeAccountPublicKey,
|
19
|
+
getUshMintPublicKey,
|
20
|
+
getVaultSystemStatePublicKey,
|
21
|
+
getPoolPublicKeyForMint,
|
22
|
+
getHedgeMintPublicKey,
|
23
|
+
} from '../Constants'
|
24
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
6
25
|
|
7
|
-
export async function withdrawVault
|
26
|
+
export async function withdrawVault(
|
8
27
|
program: Program,
|
9
28
|
provider: Provider,
|
10
29
|
payer: Signer,
|
@@ -15,23 +34,66 @@ export async function withdrawVault (
|
|
15
34
|
const ushMintPublickey = await getUshMintPublicKey()
|
16
35
|
|
17
36
|
// Prep the user to get USH back out at some point
|
18
|
-
await getOrCreateAssociatedTokenAccount(
|
37
|
+
await getOrCreateAssociatedTokenAccount(
|
38
|
+
provider.connection,
|
39
|
+
payer,
|
40
|
+
ushMintPublickey,
|
41
|
+
payer.publicKey
|
42
|
+
)
|
19
43
|
|
20
44
|
const history = Keypair.generate()
|
21
45
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
22
46
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
23
|
-
const
|
24
|
-
|
47
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
48
|
+
vaultAccount.collateralType
|
49
|
+
)
|
50
|
+
const vaultAssociatedCollateralAccount =
|
51
|
+
await getOrCreateAssociatedTokenAccount(
|
52
|
+
provider.connection,
|
53
|
+
payer,
|
54
|
+
TokenInstructions.WRAPPED_SOL_MINT,
|
55
|
+
vaultPublicKey,
|
56
|
+
true
|
57
|
+
)
|
25
58
|
|
26
|
-
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
27
|
-
|
59
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
60
|
+
vaultTypeAccountPublicKey
|
61
|
+
)
|
62
|
+
const vaultTypeAssociatedTokenAccount =
|
63
|
+
await getOrCreateAssociatedTokenAccount(
|
64
|
+
provider.connection,
|
65
|
+
payer,
|
66
|
+
vaultTypeAccountInfo.collateralMint,
|
67
|
+
vaultTypeAccountPublicKey,
|
68
|
+
true
|
69
|
+
)
|
28
70
|
|
29
|
-
const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
71
|
+
const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(
|
72
|
+
provider.connection,
|
73
|
+
payer,
|
74
|
+
vaultTypeAccountInfo.collateralMint,
|
75
|
+
payer.publicKey
|
34
76
|
)
|
77
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
78
|
+
await getHedgeMintPublicKey()
|
79
|
+
)
|
80
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
81
|
+
await findAssociatedTokenAddress(
|
82
|
+
hedgeStakingPoolPublicKey,
|
83
|
+
ushMintPublickey
|
84
|
+
)
|
85
|
+
|
86
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
87
|
+
await getLinkedListAccounts(
|
88
|
+
program,
|
89
|
+
provider,
|
90
|
+
vaultTypeAccountPublicKey,
|
91
|
+
vaultPublicKey,
|
92
|
+
withdrawAmount * -1,
|
93
|
+
0,
|
94
|
+
false,
|
95
|
+
false
|
96
|
+
)
|
35
97
|
|
36
98
|
const transaction = new Transaction().add(
|
37
99
|
await withdrawVaultInstruction(
|
@@ -41,21 +103,29 @@ export async function withdrawVault (
|
|
41
103
|
destinationTokenAccount.address,
|
42
104
|
vaultPublicKey,
|
43
105
|
vaultAssociatedCollateralAccount.address,
|
44
|
-
|
106
|
+
vaultTypeAccountPublicKey,
|
45
107
|
vaultTypeAssociatedTokenAccount.address,
|
46
108
|
hedgeStakingPoolPublicKey,
|
47
109
|
hedgeStakingPoolAssociatedUshTokenAccount,
|
48
110
|
ushMintPublickey,
|
49
111
|
history.publicKey,
|
112
|
+
oldSmallerPublicKey,
|
113
|
+
newSmallerPublicKey,
|
114
|
+
newLargerPublicKey,
|
50
115
|
withdrawAmount,
|
51
116
|
overrideTime
|
52
117
|
)
|
53
118
|
)
|
54
|
-
await sendAndConfirmTransaction(
|
119
|
+
await sendAndConfirmTransaction(
|
120
|
+
provider.connection,
|
121
|
+
transaction,
|
122
|
+
[payer, history],
|
123
|
+
provider.opts
|
124
|
+
)
|
55
125
|
return vaultPublicKey
|
56
126
|
}
|
57
127
|
|
58
|
-
export async function withdrawVaultInstruction
|
128
|
+
export async function withdrawVaultInstruction(
|
59
129
|
program: Program,
|
60
130
|
vaultSystemStatePublicKey: PublicKey,
|
61
131
|
payerPublicKey: PublicKey,
|
@@ -68,6 +138,9 @@ export async function withdrawVaultInstruction (
|
|
68
138
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
69
139
|
ushMint: PublicKey,
|
70
140
|
historyPublicKey: PublicKey,
|
141
|
+
oldSmallerPublicKey: PublicKey,
|
142
|
+
newSmallerPublicKey: PublicKey,
|
143
|
+
newLargerPublicKey: PublicKey,
|
71
144
|
withdrawAmount: number,
|
72
145
|
overrideTime?: number
|
73
146
|
): Promise<TransactionInstruction> {
|
@@ -82,14 +155,19 @@ export async function withdrawVaultInstruction (
|
|
82
155
|
vault: vaultPublickey,
|
83
156
|
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
84
157
|
feePool: hedgeStakingPoolPublicKey,
|
85
|
-
feePoolAssociatedUshTokenAccount:
|
158
|
+
feePoolAssociatedUshTokenAccount:
|
159
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
86
160
|
ushMint: ushMint,
|
87
161
|
history: historyPublicKey,
|
88
162
|
vaultOwner: payerPublicKey,
|
89
163
|
destinationTokenAccount: destinationTokenAccount,
|
164
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
165
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
166
|
+
newLargerVaultInfo: newLargerPublicKey,
|
90
167
|
tokenProgram: TOKEN_PROGRAM_ID,
|
91
|
-
systemProgram: SystemProgram.programId
|
168
|
+
systemProgram: SystemProgram.programId,
|
92
169
|
},
|
93
|
-
signers: []
|
94
|
-
}
|
170
|
+
signers: [],
|
171
|
+
}
|
172
|
+
)
|
95
173
|
}
|