hedge-web3 0.1.26 → 0.1.29
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 +1056 -768
- 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 +3 -3
- package/lib/idl/vault.js +1054 -766
- 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/Errors.js +2 -2
- package/lib/utils/getLinkedListAccounts.js +131 -0
- package/package.json +3 -1
- package/src/Constants.ts +73 -31
- package/src/idl/vault.ts +2075 -1499
- 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/StakingPool.ts +0 -4
- package/src/state/VaultAccount.ts +86 -10
- package/src/utils/Errors.ts +2 -2
- package/src/utils/getLinkedListAccounts.ts +156 -0
- package/declarations/idl/idl.d.ts +0 -2
- package/lib/idl/idl.js +0 -1475
- package/src/idl/idl.ts +0 -1474
package/src/index.ts
CHANGED
@@ -17,6 +17,7 @@ export * from './instructions/refreshOraclePrice'
|
|
17
17
|
export * from './instructions/initHedgeFoundation'
|
18
18
|
export * from './instructions/initHedgeFoundationTokens'
|
19
19
|
export * from './instructions/setHalted'
|
20
|
+
export * from './instructions/setVaultTypeStatus'
|
20
21
|
|
21
22
|
export * from './HedgeDecimal'
|
22
23
|
export * from './Constants'
|
@@ -28,3 +29,5 @@ export * from './state/StakingPoolPosition'
|
|
28
29
|
export * from './state/LiquidationPoolEra'
|
29
30
|
export * from './state/LiquidationPoolState'
|
30
31
|
export * from './state/LiquidationPosition'
|
32
|
+
|
33
|
+
export * from './utils/getLinkedListAccounts'
|
@@ -36,14 +36,13 @@ export async function createStakingPoolInstruction (
|
|
36
36
|
console.log("createStakingPoolInstruction program ID", program.programId.toString())
|
37
37
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
38
38
|
const ushMintPublickey = await getUshMintPublicKey()
|
39
|
-
const [poolPublickey, poolBump
|
39
|
+
const [poolPublickey, poolBump] = await getPoolPublicKeyForMint(mintPublicKey)
|
40
40
|
|
41
41
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey)
|
42
42
|
const poolAssociatedUshTokenAccount = await findAssociatedTokenAddress(poolPublickey, ushMintPublickey)
|
43
43
|
|
44
44
|
return program.instruction.createStakingPool(
|
45
45
|
poolBump,
|
46
|
-
poolSeedPhrase,
|
47
46
|
new BN(hedgeTokensToBeMinted),
|
48
47
|
new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)),
|
49
48
|
{
|
@@ -1,8 +1,27 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
3
|
+
import {
|
4
|
+
getOrCreateAssociatedTokenAccount,
|
5
|
+
TOKEN_PROGRAM_ID,
|
6
|
+
} from '@solana/spl-token'
|
7
|
+
import {
|
8
|
+
Keypair,
|
9
|
+
PublicKey,
|
10
|
+
sendAndConfirmTransaction,
|
11
|
+
Signer,
|
12
|
+
SystemProgram,
|
13
|
+
Transaction,
|
14
|
+
TransactionInstruction,
|
15
|
+
} from '@solana/web3.js'
|
16
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
17
|
+
import {
|
18
|
+
findAssociatedTokenAddress,
|
19
|
+
getVaultTypeAccountPublicKey,
|
20
|
+
getUshMintPublicKey,
|
21
|
+
getVaultSystemStatePublicKey,
|
22
|
+
getPoolPublicKeyForMint,
|
23
|
+
getHedgeMintPublicKey,
|
24
|
+
} from '../Constants'
|
6
25
|
|
7
26
|
export async function depositVault(
|
8
27
|
program: Program,
|
@@ -15,30 +34,68 @@ export async function depositVault(
|
|
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 vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
21
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
22
|
-
|
23
|
-
|
45
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
46
|
+
vaultAccount.collateralType
|
47
|
+
)
|
48
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
49
|
+
vaultTypeAccountPublicKey
|
50
|
+
)
|
51
|
+
const vaultTypeAssociatedTokenAccount =
|
52
|
+
await getOrCreateAssociatedTokenAccount(
|
53
|
+
provider.connection,
|
54
|
+
payer,
|
55
|
+
vaultTypeAccountInfo.collateralMint,
|
56
|
+
vaultTypeAccountPublicKey,
|
57
|
+
true
|
58
|
+
)
|
24
59
|
|
25
|
-
const payerTokenAccount = await findAssociatedTokenAddress(
|
26
|
-
|
60
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
61
|
+
payer.publicKey,
|
62
|
+
vaultTypeAccountInfo.collateralMint
|
63
|
+
)
|
64
|
+
const vaultAssociatedCollateralAccountPublicKey =
|
65
|
+
await findAssociatedTokenAddress(
|
66
|
+
vaultPublicKey,
|
67
|
+
vaultTypeAccountInfo.collateralMint
|
68
|
+
)
|
27
69
|
|
28
70
|
const history = Keypair.generate()
|
29
71
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
30
72
|
|
31
73
|
const wrappedSolAccount = Keypair.generate()
|
32
74
|
|
33
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
34
|
-
|
35
|
-
hedgeStakingPoolPublicKey,
|
36
|
-
ushMintPublickey
|
75
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
76
|
+
await getHedgeMintPublicKey()
|
37
77
|
)
|
78
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
79
|
+
await findAssociatedTokenAddress(
|
80
|
+
hedgeStakingPoolPublicKey,
|
81
|
+
ushMintPublickey
|
82
|
+
)
|
38
83
|
|
39
84
|
const transaction = new Transaction()
|
40
85
|
const signers = [payer, history]
|
41
86
|
|
87
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
88
|
+
await getLinkedListAccounts(
|
89
|
+
program,
|
90
|
+
provider,
|
91
|
+
vaultTypeAccountPublicKey,
|
92
|
+
vaultPublicKey,
|
93
|
+
depositAmount,
|
94
|
+
0,
|
95
|
+
false,
|
96
|
+
false
|
97
|
+
)
|
98
|
+
|
42
99
|
if (vaultAccount.collateralType === 'SOL') {
|
43
100
|
transaction.add(
|
44
101
|
SystemProgram.createAccount({
|
@@ -46,12 +103,12 @@ export async function depositVault(
|
|
46
103
|
lamports: depositAmount + 2.04e6,
|
47
104
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
48
105
|
programId: TOKEN_PROGRAM_ID,
|
49
|
-
space: 165
|
106
|
+
space: 165,
|
50
107
|
}),
|
51
108
|
TokenInstructions.initializeAccount({
|
52
109
|
account: wrappedSolAccount.publicKey,
|
53
110
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
54
|
-
owner: payer.publicKey
|
111
|
+
owner: payer.publicKey,
|
55
112
|
})
|
56
113
|
)
|
57
114
|
signers.push(wrappedSolAccount)
|
@@ -61,7 +118,9 @@ export async function depositVault(
|
|
61
118
|
program,
|
62
119
|
vaultSystemStatePublicKey,
|
63
120
|
payer.publicKey,
|
64
|
-
vaultAccount.collateralType === 'SOL'
|
121
|
+
vaultAccount.collateralType === 'SOL'
|
122
|
+
? wrappedSolAccount.publicKey
|
123
|
+
: payerTokenAccount,
|
65
124
|
vaultPublicKey,
|
66
125
|
vaultAssociatedCollateralAccountPublicKey,
|
67
126
|
history.publicKey,
|
@@ -71,6 +130,9 @@ export async function depositVault(
|
|
71
130
|
hedgeStakingPoolAssociatedUshTokenAccount,
|
72
131
|
vaultTypeAccountInfo.collateralMint,
|
73
132
|
ushMintPublickey,
|
133
|
+
oldSmallerPublicKey,
|
134
|
+
newSmallerPublicKey,
|
135
|
+
newLargerPublicKey,
|
74
136
|
depositAmount,
|
75
137
|
overrideTime
|
76
138
|
)
|
@@ -80,12 +142,17 @@ export async function depositVault(
|
|
80
142
|
TokenInstructions.closeAccount({
|
81
143
|
source: wrappedSolAccount.publicKey,
|
82
144
|
destination: payer.publicKey,
|
83
|
-
owner: payer.publicKey
|
145
|
+
owner: payer.publicKey,
|
84
146
|
})
|
85
147
|
)
|
86
148
|
}
|
87
149
|
|
88
|
-
await sendAndConfirmTransaction(
|
150
|
+
await sendAndConfirmTransaction(
|
151
|
+
provider.connection,
|
152
|
+
transaction,
|
153
|
+
signers,
|
154
|
+
provider.opts
|
155
|
+
)
|
89
156
|
return vaultPublicKey
|
90
157
|
}
|
91
158
|
|
@@ -103,6 +170,9 @@ export async function depositVaultInstruction(
|
|
103
170
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
104
171
|
collateralMint: PublicKey,
|
105
172
|
ushMintPublickey: PublicKey,
|
173
|
+
oldSmallerPublicKey: PublicKey,
|
174
|
+
newSmallerPublicKey: PublicKey,
|
175
|
+
newLargerPublicKey: PublicKey,
|
106
176
|
depositAmount: number,
|
107
177
|
overrideTime?: number
|
108
178
|
): Promise<TransactionInstruction> {
|
@@ -118,14 +188,19 @@ export async function depositVaultInstruction(
|
|
118
188
|
vault: vaultPublicKey,
|
119
189
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
120
190
|
feePool: hedgeStakingPoolPublicKey,
|
121
|
-
feePoolAssociatedUshTokenAccount:
|
191
|
+
feePoolAssociatedUshTokenAccount:
|
192
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
122
193
|
history: historyPublicKey,
|
123
194
|
vaultOwner: vaultOwner,
|
124
195
|
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
125
196
|
ushMint: ushMintPublickey,
|
197
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
198
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
199
|
+
newLargerVaultInfo: newLargerPublicKey,
|
126
200
|
systemProgram: SystemProgram.programId,
|
127
|
-
tokenProgram: TOKEN_PROGRAM_ID
|
201
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
128
202
|
},
|
129
|
-
signers: []
|
130
|
-
}
|
203
|
+
signers: [],
|
204
|
+
}
|
205
|
+
)
|
131
206
|
}
|
@@ -1,9 +1,31 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
2
|
+
import {
|
3
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
4
|
+
TOKEN_PROGRAM_ID,
|
5
|
+
getOrCreateAssociatedTokenAccount,
|
6
|
+
} from '@solana/spl-token'
|
7
|
+
import {
|
8
|
+
Keypair,
|
9
|
+
PublicKey,
|
10
|
+
sendAndConfirmTransaction,
|
11
|
+
Signer,
|
12
|
+
SystemProgram,
|
13
|
+
SYSVAR_RENT_PUBKEY,
|
14
|
+
Transaction,
|
15
|
+
TransactionInstruction,
|
16
|
+
} from '@solana/web3.js'
|
17
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
18
|
+
import {
|
19
|
+
getHedgeMintPublicKey,
|
20
|
+
getLiquidationPoolStatePublicKey,
|
21
|
+
getLiquidationPoolUshAccountPublicKey,
|
22
|
+
getPoolPublicKeyForMint,
|
23
|
+
getVaultTypeAccountPublicKey,
|
24
|
+
getUshMintPublicKey,
|
25
|
+
getVaultSystemStatePublicKey,
|
26
|
+
} from '../Constants'
|
5
27
|
|
6
|
-
export async function liquidateVault
|
28
|
+
export async function liquidateVault(
|
7
29
|
program: Program,
|
8
30
|
provider: Provider,
|
9
31
|
payer: Signer,
|
@@ -12,22 +34,80 @@ export async function liquidateVault (
|
|
12
34
|
): Promise<PublicKey> {
|
13
35
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
14
36
|
|
15
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
16
|
-
|
37
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
38
|
+
vaultAccount.collateralType
|
39
|
+
)
|
40
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
41
|
+
vaultTypeAccountPublicKey
|
42
|
+
)
|
17
43
|
const collateralMint = vaultTypeAccountInfo.collateralMint
|
18
44
|
|
19
45
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
20
46
|
const ushMintPublickey = await getUshMintPublicKey()
|
21
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
47
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
48
|
+
hedgeMintPublickey
|
49
|
+
)
|
22
50
|
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
23
|
-
const poolStateInfo = await program.account.liquidationPoolState.fetch(
|
51
|
+
const poolStateInfo = await program.account.liquidationPoolState.fetch(
|
52
|
+
liquidationPoolStatePublicKey
|
53
|
+
)
|
54
|
+
|
55
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
56
|
+
provider.connection,
|
57
|
+
payer,
|
58
|
+
collateralMint,
|
59
|
+
payer.publicKey,
|
60
|
+
true
|
61
|
+
)
|
62
|
+
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
63
|
+
provider.connection,
|
64
|
+
payer,
|
65
|
+
collateralMint,
|
66
|
+
hedgeStakingPoolPublicKey,
|
67
|
+
true
|
68
|
+
)
|
69
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
70
|
+
provider.connection,
|
71
|
+
payer,
|
72
|
+
collateralMint,
|
73
|
+
vaultPublicKey,
|
74
|
+
true
|
75
|
+
)
|
76
|
+
const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
77
|
+
provider.connection,
|
78
|
+
payer,
|
79
|
+
collateralMint,
|
80
|
+
liquidationPoolStatePublicKey,
|
81
|
+
true
|
82
|
+
)
|
83
|
+
const vaultTypeAssociatedTokenAccount =
|
84
|
+
await getOrCreateAssociatedTokenAccount(
|
85
|
+
provider.connection,
|
86
|
+
payer,
|
87
|
+
collateralMint,
|
88
|
+
vaultTypeAccountPublicKey,
|
89
|
+
true
|
90
|
+
)
|
91
|
+
const hedgeStakingPoolAssociatedUshTokenAccount =
|
92
|
+
await getOrCreateAssociatedTokenAccount(
|
93
|
+
provider.connection,
|
94
|
+
payer,
|
95
|
+
ushMintPublickey,
|
96
|
+
hedgeStakingPoolPublicKey,
|
97
|
+
true
|
98
|
+
)
|
24
99
|
|
25
|
-
const
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
100
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] =
|
101
|
+
await getLinkedListAccounts(
|
102
|
+
program,
|
103
|
+
provider,
|
104
|
+
vaultTypeAccountPublicKey,
|
105
|
+
vaultPublicKey,
|
106
|
+
0,
|
107
|
+
0,
|
108
|
+
false,
|
109
|
+
true
|
110
|
+
)
|
31
111
|
|
32
112
|
const history = Keypair.generate()
|
33
113
|
const newEra = Keypair.generate()
|
@@ -50,15 +130,23 @@ export async function liquidateVault (
|
|
50
130
|
hedgeStakingPoolAssociatedUshTokenAccount.address,
|
51
131
|
collateralMint,
|
52
132
|
vaultTypeAssociatedTokenAccount.address,
|
133
|
+
oldSmallerPublicKey,
|
134
|
+
newSmallerPublicKey,
|
135
|
+
newLargerPublicKey,
|
53
136
|
vaultAccount.collateralType,
|
54
137
|
overrideTime
|
55
138
|
)
|
56
139
|
)
|
57
|
-
await sendAndConfirmTransaction(
|
140
|
+
await sendAndConfirmTransaction(
|
141
|
+
provider.connection,
|
142
|
+
transaction,
|
143
|
+
[payer, history, newEra],
|
144
|
+
provider.opts
|
145
|
+
)
|
58
146
|
return vaultPublicKey
|
59
147
|
}
|
60
148
|
|
61
|
-
export async function liquidateVaultInstruction
|
149
|
+
export async function liquidateVaultInstruction(
|
62
150
|
program: Program,
|
63
151
|
payerPublicKey: PublicKey,
|
64
152
|
payerAssociatedTokenAccount: PublicKey,
|
@@ -74,12 +162,16 @@ export async function liquidateVaultInstruction (
|
|
74
162
|
hedgeStakingPoolAssociatedUshTokenAccount: PublicKey,
|
75
163
|
collateralMint: PublicKey,
|
76
164
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
165
|
+
oldSmallerPublicKey: PublicKey,
|
166
|
+
newSmallerPublicKey: PublicKey,
|
167
|
+
newLargerPublicKey: PublicKey,
|
77
168
|
collateralType: string,
|
78
169
|
overrideTime?: number
|
79
170
|
): Promise<TransactionInstruction> {
|
80
171
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
81
172
|
const ushMintPublickey = await getUshMintPublicKey()
|
82
|
-
const liquidationPoolUshAccountPublickey =
|
173
|
+
const liquidationPoolUshAccountPublickey =
|
174
|
+
await getLiquidationPoolUshAccountPublicKey()
|
83
175
|
const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
|
84
176
|
|
85
177
|
const payload = {
|
@@ -99,17 +191,22 @@ export async function liquidateVaultInstruction (
|
|
99
191
|
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
100
192
|
feePool: feePool,
|
101
193
|
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
102
|
-
feePoolAssociatedUshTokenAccount:
|
194
|
+
feePoolAssociatedUshTokenAccount:
|
195
|
+
hedgeStakingPoolAssociatedUshTokenAccount,
|
103
196
|
liquidationPoolUshAccount: liquidationPoolUshAccountPublickey,
|
104
197
|
newEra: newEraPublicKey,
|
198
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
199
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
200
|
+
newLargerVaultInfo: newLargerPublicKey,
|
105
201
|
tokenProgram: TOKEN_PROGRAM_ID,
|
106
202
|
systemProgram: SystemProgram.programId,
|
107
203
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
108
|
-
rent: SYSVAR_RENT_PUBKEY
|
204
|
+
rent: SYSVAR_RENT_PUBKEY,
|
109
205
|
},
|
110
|
-
signers: []
|
206
|
+
signers: [],
|
111
207
|
}
|
112
208
|
return program.instruction.liquidateVault(
|
113
209
|
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
114
|
-
payload
|
210
|
+
payload
|
211
|
+
)
|
115
212
|
}
|
@@ -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
|
},
|