hedge-web3 0.1.27 → 0.1.31
Sign up to get free protection for your applications and to get access to all the features.
- 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,10 +1,27 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
12
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
13
|
+
import {
|
14
|
+
findAssociatedTokenAddress,
|
15
|
+
getHedgeMintPublicKey,
|
16
|
+
getPoolPublicKeyForMint,
|
17
|
+
getVaultTypeAccountPublicKey,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
} from '../Constants'
|
21
|
+
import { Vault } from 'idl/vault'
|
5
22
|
|
6
|
-
export async function loanVault
|
7
|
-
program: Program
|
23
|
+
export async function loanVault(
|
24
|
+
program: Program<Vault>,
|
8
25
|
provider: Provider,
|
9
26
|
payer: Signer,
|
10
27
|
vaultPublicKey: PublicKey,
|
@@ -13,13 +30,41 @@ export async function loanVault (
|
|
13
30
|
): Promise<PublicKey> {
|
14
31
|
const ushMintPublickey = await getUshMintPublicKey()
|
15
32
|
|
16
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
33
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
34
|
+
provider.connection,
|
35
|
+
payer,
|
36
|
+
ushMintPublickey,
|
37
|
+
payer.publicKey
|
38
|
+
)
|
17
39
|
|
18
40
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
19
41
|
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
20
42
|
const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
21
|
-
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
22
|
-
|
43
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
44
|
+
provider.connection,
|
45
|
+
payer,
|
46
|
+
vaultTypeAccount.collateralMint,
|
47
|
+
vaultTypeAccountPublicKey,
|
48
|
+
true
|
49
|
+
)
|
50
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
51
|
+
provider.connection,
|
52
|
+
payer,
|
53
|
+
vaultTypeAccount.collateralMint,
|
54
|
+
vaultPublicKey,
|
55
|
+
true
|
56
|
+
)
|
57
|
+
|
58
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
59
|
+
program,
|
60
|
+
provider,
|
61
|
+
vaultTypeAccountPublicKey,
|
62
|
+
vaultPublicKey,
|
63
|
+
0,
|
64
|
+
loanAmount,
|
65
|
+
false,
|
66
|
+
false
|
67
|
+
)
|
23
68
|
|
24
69
|
const history = Keypair.generate()
|
25
70
|
const transaction = new Transaction().add(
|
@@ -32,16 +77,19 @@ export async function loanVault (
|
|
32
77
|
history.publicKey,
|
33
78
|
vaultTypeAccountPublicKey,
|
34
79
|
vaultTypeAssociatedTokenAccount.address,
|
80
|
+
oldSmallerPublicKey,
|
81
|
+
newSmallerPublicKey,
|
82
|
+
newLargerPublicKey,
|
35
83
|
loanAmount,
|
36
84
|
overrideTime
|
37
85
|
)
|
38
86
|
)
|
39
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history]
|
87
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
|
40
88
|
return vaultPublicKey
|
41
89
|
}
|
42
90
|
|
43
|
-
export async function loanVaultInstruction
|
44
|
-
program: Program
|
91
|
+
export async function loanVaultInstruction(
|
92
|
+
program: Program<Vault>,
|
45
93
|
payerPublicKey: PublicKey,
|
46
94
|
ownerUshAccount: PublicKey,
|
47
95
|
vaultPublickey: PublicKey,
|
@@ -49,6 +97,9 @@ export async function loanVaultInstruction (
|
|
49
97
|
historyPublicKey: PublicKey,
|
50
98
|
vaultTypeAccount: PublicKey,
|
51
99
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
100
|
+
oldSmallerPublicKey: PublicKey,
|
101
|
+
newSmallerPublicKey: PublicKey,
|
102
|
+
newLargerPublicKey: PublicKey,
|
52
103
|
loanAmount: number,
|
53
104
|
overrideTime?: number
|
54
105
|
): Promise<TransactionInstruction> {
|
@@ -61,25 +112,28 @@ export async function loanVaultInstruction (
|
|
61
112
|
ushMintPublickey
|
62
113
|
)
|
63
114
|
|
64
|
-
return program.
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
115
|
+
return program.methods
|
116
|
+
.loanVault(
|
117
|
+
new BN(loanAmount),
|
118
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
119
|
+
)
|
120
|
+
.accounts({
|
121
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
122
|
+
vaultTypeAccount: vaultTypeAccount,
|
123
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
124
|
+
vaultAccount: vaultPublickey,
|
125
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
126
|
+
history: historyPublicKey,
|
127
|
+
feePool: hedgeStakingPoolPublicKey,
|
128
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
129
|
+
ushMint: ushMintPublickey,
|
130
|
+
vaultOwner: payerPublicKey,
|
131
|
+
ownerUshAccount: ownerUshAccount,
|
132
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
133
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
134
|
+
newLargerVaultInfo: newLargerPublicKey,
|
135
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
136
|
+
systemProgram: SystemProgram.programId,
|
84
137
|
})
|
138
|
+
.instruction()
|
85
139
|
}
|
@@ -1,11 +1,28 @@
|
|
1
1
|
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
|
-
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
|
+
getHedgeMintPublicKey,
|
17
|
+
getPoolPublicKeyForMint,
|
18
|
+
getVaultTypeAccountPublicKey,
|
19
|
+
getUshMintPublicKey,
|
20
|
+
getVaultSystemStatePublicKey,
|
21
|
+
} from '../Constants'
|
22
|
+
import { Vault } from 'idl/vault'
|
6
23
|
|
7
|
-
export async function redeemVault
|
8
|
-
program: Program
|
24
|
+
export async function redeemVault(
|
25
|
+
program: Program<Vault>,
|
9
26
|
provider: Provider,
|
10
27
|
payer: Signer,
|
11
28
|
vaultPublicKey: PublicKey,
|
@@ -15,16 +32,49 @@ export async function redeemVault (
|
|
15
32
|
const ushMintPublickey = await getUshMintPublicKey()
|
16
33
|
|
17
34
|
// Prep the user to get USH back out at some point
|
18
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
35
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
36
|
+
provider.connection,
|
37
|
+
payer,
|
38
|
+
ushMintPublickey,
|
39
|
+
payer.publicKey
|
40
|
+
)
|
19
41
|
|
20
42
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
21
43
|
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
22
44
|
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
23
|
-
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
45
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
46
|
+
provider.connection,
|
47
|
+
payer,
|
48
|
+
vaultTypeAccountInfo.collateralMint,
|
49
|
+
vaultTypeAccountPublicKey,
|
50
|
+
true
|
51
|
+
)
|
24
52
|
|
25
|
-
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
53
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
|
54
|
+
provider.connection,
|
55
|
+
payer,
|
56
|
+
vaultTypeAccountInfo.collateralMint,
|
57
|
+
vaultPublicKey,
|
58
|
+
true
|
59
|
+
)
|
26
60
|
|
27
|
-
const payerTokenAccount = await getOrCreateAssociatedTokenAccount(
|
61
|
+
const payerTokenAccount = await getOrCreateAssociatedTokenAccount(
|
62
|
+
provider.connection,
|
63
|
+
payer,
|
64
|
+
vaultTypeAccountInfo.collateralMint,
|
65
|
+
payer.publicKey
|
66
|
+
)
|
67
|
+
|
68
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
69
|
+
program,
|
70
|
+
provider,
|
71
|
+
vaultTypeAccountPublicKey,
|
72
|
+
vaultPublicKey,
|
73
|
+
0,
|
74
|
+
0,
|
75
|
+
true,
|
76
|
+
false
|
77
|
+
)
|
28
78
|
|
29
79
|
const history = Keypair.generate()
|
30
80
|
const transaction = new Transaction().add(
|
@@ -38,16 +88,19 @@ export async function redeemVault (
|
|
38
88
|
history.publicKey,
|
39
89
|
vaultTypeAccountPublicKey,
|
40
90
|
vaultTypeAssociatedTokenAccount.address,
|
91
|
+
oldSmallerPublicKey,
|
92
|
+
newSmallerPublicKey,
|
93
|
+
newLargerPublicKey,
|
41
94
|
redeemAmount,
|
42
95
|
transactionOverrideTime
|
43
96
|
)
|
44
97
|
)
|
45
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history]
|
98
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
|
46
99
|
return vaultPublicKey
|
47
100
|
}
|
48
101
|
|
49
|
-
export async function redeemVaultInstruction
|
50
|
-
program: Program
|
102
|
+
export async function redeemVaultInstruction(
|
103
|
+
program: Program<Vault>,
|
51
104
|
payerPublicKey: PublicKey,
|
52
105
|
payerUshAccount: PublicKey,
|
53
106
|
destinationTokenAccount: PublicKey,
|
@@ -56,6 +109,9 @@ export async function redeemVaultInstruction (
|
|
56
109
|
historyPublicKey: PublicKey,
|
57
110
|
vaultTypeAccount: PublicKey,
|
58
111
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
112
|
+
oldSmallerPublicKey: PublicKey,
|
113
|
+
newSmallerPublicKey: PublicKey,
|
114
|
+
newLargerPublicKey: PublicKey,
|
59
115
|
redeemAmount: number,
|
60
116
|
transactionOverrideTime?: number
|
61
117
|
): Promise<TransactionInstruction> {
|
@@ -67,26 +123,29 @@ export async function redeemVaultInstruction (
|
|
67
123
|
hedgeStakingPoolPublicKey,
|
68
124
|
ushMintPublickey
|
69
125
|
)
|
70
|
-
return program.
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
126
|
+
return await program.methods
|
127
|
+
.redeemVault(
|
128
|
+
new BN(redeemAmount),
|
129
|
+
new BN(transactionOverrideTime ?? Date.now() / 1000) // override start time
|
130
|
+
)
|
131
|
+
.accounts({
|
132
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
133
|
+
vaultTypeAccount: vaultTypeAccount,
|
134
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
135
|
+
vault: vaultPublickey,
|
136
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
137
|
+
history: historyPublicKey,
|
138
|
+
feePool: hedgeStakingPoolPublicKey,
|
139
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
140
|
+
ushMint: ushMintPublickey,
|
141
|
+
payer: payerPublicKey,
|
142
|
+
payerUshAccount: payerUshAccount,
|
143
|
+
destinationTokenAccount: destinationTokenAccount,
|
144
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
145
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
146
|
+
newLargerVaultInfo: newLargerPublicKey,
|
147
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
148
|
+
systemProgram: SystemProgram.programId,
|
91
149
|
})
|
150
|
+
.instruction()
|
92
151
|
}
|
@@ -1,9 +1,18 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
LAMPORTS_PER_SOL,
|
4
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
Transaction,
|
9
|
+
TransactionInstruction,
|
10
|
+
} from '@solana/web3.js'
|
11
|
+
import { Vault } from 'idl/vault'
|
3
12
|
import { HEDGE_PROGRAM_PUBLICKEY } from '../Constants'
|
4
13
|
|
5
|
-
export async function refreshOraclePrice
|
6
|
-
program: Program
|
14
|
+
export async function refreshOraclePrice(
|
15
|
+
program: Program<Vault>,
|
7
16
|
provider: Provider,
|
8
17
|
payer: Signer,
|
9
18
|
collateralType: string,
|
@@ -12,63 +21,63 @@ export async function refreshOraclePrice (
|
|
12
21
|
overrideTime?: number
|
13
22
|
): Promise<string> {
|
14
23
|
const transaction = new Transaction().add(
|
15
|
-
await refreshOraclePriceInstruction(
|
16
|
-
program,
|
17
|
-
collateralType,
|
18
|
-
network,
|
19
|
-
overridePrice,
|
20
|
-
overrideTime
|
21
|
-
)
|
24
|
+
await refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime)
|
22
25
|
)
|
23
|
-
return await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
26
|
+
return await sendAndConfirmTransaction(provider.connection, transaction, [payer])
|
24
27
|
}
|
25
28
|
|
26
|
-
export async function refreshOraclePriceInstruction
|
27
|
-
program: Program
|
29
|
+
export async function refreshOraclePriceInstruction(
|
30
|
+
program: Program<Vault>,
|
28
31
|
collateralType: string,
|
29
32
|
network: Cluster,
|
30
33
|
overridePrice?: number,
|
31
34
|
overrideTime?: number
|
32
35
|
): Promise<TransactionInstruction> {
|
33
36
|
const enc = new TextEncoder()
|
34
|
-
const [oracleInfoAccount] = await PublicKey.findProgramAddress(
|
35
|
-
|
37
|
+
const [oracleInfoAccount] = await PublicKey.findProgramAddress(
|
38
|
+
[enc.encode(collateralType), enc.encode('Oracle')],
|
39
|
+
HEDGE_PROGRAM_PUBLICKEY
|
40
|
+
)
|
41
|
+
const [vaultTypeAccount] = await PublicKey.findProgramAddress(
|
42
|
+
[enc.encode(collateralType), enc.encode('State')],
|
43
|
+
HEDGE_PROGRAM_PUBLICKEY
|
44
|
+
)
|
36
45
|
|
37
|
-
return program.
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
signers: []
|
46
|
+
return await program.methods
|
47
|
+
.refreshOraclePrice(
|
48
|
+
new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
49
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000) - 1) // override override time
|
50
|
+
)
|
51
|
+
.accounts({
|
52
|
+
oracleInfoAccount: oracleInfoAccount,
|
53
|
+
vaultTypeAccount: vaultTypeAccount,
|
54
|
+
oracleChainlink: chainlinkAccunts[network],
|
55
|
+
oraclePyth: pythAccounts[network],
|
56
|
+
oracleSwitchboard: switchboardAccounts[network],
|
57
|
+
systemProgram: SystemProgram.programId,
|
50
58
|
})
|
59
|
+
.instruction()
|
51
60
|
}
|
52
61
|
|
53
62
|
enum Cluster {
|
54
63
|
Testing = 'Testing',
|
55
64
|
Devnet = 'Devnet',
|
56
|
-
MainnetBeta = 'MainnetBeta'
|
65
|
+
MainnetBeta = 'MainnetBeta',
|
57
66
|
}
|
58
67
|
|
59
68
|
const pythAccounts = {
|
60
69
|
Testing: SystemProgram.programId,
|
61
70
|
Devnet: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
62
|
-
MainnetBeta: new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG')
|
71
|
+
MainnetBeta: new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG'),
|
63
72
|
}
|
64
73
|
const chainlinkAccunts = {
|
65
74
|
Testing: SystemProgram.programId,
|
66
75
|
Devnet: new PublicKey('FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'),
|
67
|
-
MainnetBeta: SystemProgram.programId // CHAINLINK NOT ON MAINNET YET
|
76
|
+
MainnetBeta: SystemProgram.programId, // CHAINLINK NOT ON MAINNET YET
|
68
77
|
}
|
69
78
|
const switchboardAccounts = {
|
70
79
|
Testing: SystemProgram.programId,
|
71
80
|
// Devnet: new PublicKey('GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR'),
|
72
81
|
Devnet: new PublicKey('DpoK8Zz69APV9ntjuY9C4LZCxANYMV56M2cbXEdkjxME'),
|
73
|
-
MainnetBeta: SystemProgram.programId // Switchboard V2 NOT ON MAINNET YET
|
82
|
+
MainnetBeta: SystemProgram.programId, // Switchboard V2 NOT ON MAINNET YET
|
74
83
|
}
|
@@ -1,10 +1,27 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
|
3
|
+
import {
|
4
|
+
Keypair,
|
5
|
+
PublicKey,
|
6
|
+
sendAndConfirmTransaction,
|
7
|
+
Signer,
|
8
|
+
SystemProgram,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
12
|
+
import { getLinkedListAccounts } from '../utils/getLinkedListAccounts'
|
13
|
+
import {
|
14
|
+
findAssociatedTokenAddress,
|
15
|
+
getHedgeMintPublicKey,
|
16
|
+
getPoolPublicKeyForMint,
|
17
|
+
getVaultTypeAccountPublicKey,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
} from '../Constants'
|
21
|
+
import { Vault } from 'idl/vault'
|
5
22
|
|
6
|
-
export async function repayVault
|
7
|
-
program: Program
|
23
|
+
export async function repayVault(
|
24
|
+
program: Program<Vault>,
|
8
25
|
provider: Provider,
|
9
26
|
payer: Signer,
|
10
27
|
vaultPublicKey: PublicKey,
|
@@ -14,14 +31,33 @@ export async function repayVault (
|
|
14
31
|
const ushMintPublickey = await getUshMintPublicKey()
|
15
32
|
|
16
33
|
// Prep the user to get USH back out at some point
|
17
|
-
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
34
|
+
const payerUshAccount = await getOrCreateAssociatedTokenAccount(
|
35
|
+
provider.connection,
|
36
|
+
payer,
|
37
|
+
ushMintPublickey,
|
38
|
+
payer.publicKey
|
39
|
+
)
|
18
40
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
19
41
|
|
20
42
|
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
21
43
|
const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
22
|
-
const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
|
44
|
+
const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(
|
45
|
+
vaultTypeAccountPublicKey,
|
46
|
+
vaultTypeAccount.collateralMint
|
47
|
+
)
|
23
48
|
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
|
24
49
|
|
50
|
+
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = await getLinkedListAccounts(
|
51
|
+
program,
|
52
|
+
provider,
|
53
|
+
vaultTypeAccountPublicKey,
|
54
|
+
vaultPublicKey,
|
55
|
+
0,
|
56
|
+
repayAmount * -1,
|
57
|
+
false,
|
58
|
+
false
|
59
|
+
)
|
60
|
+
|
25
61
|
const history = Keypair.generate()
|
26
62
|
const transaction = new Transaction().add(
|
27
63
|
await repayVaultInstruction(
|
@@ -33,16 +69,19 @@ export async function repayVault (
|
|
33
69
|
history.publicKey,
|
34
70
|
vaultTypeAccountPublicKey,
|
35
71
|
vaultTypeAssociatedTokenAccount,
|
72
|
+
oldSmallerPublicKey,
|
73
|
+
newSmallerPublicKey,
|
74
|
+
newLargerPublicKey,
|
36
75
|
repayAmount,
|
37
76
|
overrideTime
|
38
77
|
)
|
39
78
|
)
|
40
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history]
|
79
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history])
|
41
80
|
return vaultPublicKey
|
42
81
|
}
|
43
82
|
|
44
|
-
export async function repayVaultInstruction
|
45
|
-
program: Program
|
83
|
+
export async function repayVaultInstruction(
|
84
|
+
program: Program<Vault>,
|
46
85
|
payerPublicKey: PublicKey,
|
47
86
|
ownerUshAccount: PublicKey,
|
48
87
|
vaultPublickey: PublicKey,
|
@@ -50,6 +89,9 @@ export async function repayVaultInstruction (
|
|
50
89
|
historyPublicKey: PublicKey,
|
51
90
|
vaultTypeAccount: PublicKey,
|
52
91
|
vaultTypeAssociatedTokenAccount: PublicKey,
|
92
|
+
oldSmallerPublicKey: PublicKey,
|
93
|
+
newSmallerPublicKey: PublicKey,
|
94
|
+
newLargerPublicKey: PublicKey,
|
53
95
|
repayAmount: number,
|
54
96
|
overrideTime?: number
|
55
97
|
): Promise<TransactionInstruction> {
|
@@ -62,25 +104,28 @@ export async function repayVaultInstruction (
|
|
62
104
|
ushMintPublickey
|
63
105
|
)
|
64
106
|
|
65
|
-
return program.
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
107
|
+
return await program.methods
|
108
|
+
.repayVault(
|
109
|
+
new BN(repayAmount),
|
110
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)) // override override time
|
111
|
+
)
|
112
|
+
.accounts({
|
113
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
114
|
+
vaultTypeAccount: vaultTypeAccount,
|
115
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
116
|
+
vaultAccount: vaultPublickey,
|
117
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
118
|
+
history: historyPublicKey,
|
119
|
+
feePool: hedgeStakingPoolPublicKey,
|
120
|
+
feePoolAssociatedUshTokenAccount: hedgeStakingPoolAssociatedUshTokenAccount,
|
121
|
+
ushMint: ushMintPublickey,
|
122
|
+
vaultOwner: payerPublicKey,
|
123
|
+
oldSmallerVaultInfo: oldSmallerPublicKey,
|
124
|
+
newSmallerVaultInfo: newSmallerPublicKey,
|
125
|
+
newLargerVaultInfo: newLargerPublicKey,
|
126
|
+
ownerUshAccount: ownerUshAccount,
|
127
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
128
|
+
systemProgram: SystemProgram.programId,
|
85
129
|
})
|
130
|
+
.instruction()
|
86
131
|
}
|
@@ -1,48 +1,56 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
3
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
4
|
-
import {
|
5
|
-
|
4
|
+
import {
|
5
|
+
Keypair,
|
6
|
+
PublicKey,
|
7
|
+
sendAndConfirmTransaction,
|
8
|
+
Signer,
|
9
|
+
SystemProgram,
|
10
|
+
SYSVAR_RENT_PUBKEY,
|
11
|
+
Transaction,
|
12
|
+
TransactionInstruction,
|
13
|
+
} from '@solana/web3.js'
|
14
|
+
import {
|
15
|
+
findAssociatedTokenAddress,
|
16
|
+
findVaultAddress,
|
17
|
+
getVaultTypeAccountPublicKey,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
getPoolPublicKeyForMint,
|
21
|
+
getHedgeMintPublicKey,
|
22
|
+
} from '../Constants'
|
6
23
|
|
7
|
-
import { v4 as uuidv4 } from 'uuid'
|
8
24
|
import { parseAnchorErrors } from '../utils/Errors'
|
25
|
+
import { Vault } from 'idl/vault'
|
9
26
|
|
10
|
-
export async function setHalted
|
11
|
-
program: Program
|
27
|
+
export async function setHalted(
|
28
|
+
program: Program<Vault>,
|
12
29
|
provider: Provider,
|
13
30
|
payer: Signer,
|
14
31
|
halted: boolean
|
15
32
|
): Promise<PublicKey> {
|
16
33
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
17
34
|
|
18
|
-
|
19
35
|
const transaction = new Transaction().add(
|
20
|
-
await setHaltedInstruction(
|
21
|
-
program,
|
22
|
-
vaultSystemStatePublicKey,
|
23
|
-
payer.publicKey,
|
24
|
-
halted
|
25
|
-
)
|
36
|
+
await setHaltedInstruction(program, vaultSystemStatePublicKey, payer.publicKey, halted)
|
26
37
|
)
|
27
38
|
|
28
|
-
await sendAndConfirmTransaction(provider.connection, transaction, [payer]
|
39
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer]).catch(parseAnchorErrors)
|
29
40
|
return vaultSystemStatePublicKey
|
30
41
|
}
|
31
42
|
|
32
|
-
export async function setHaltedInstruction
|
33
|
-
program: Program
|
43
|
+
export async function setHaltedInstruction(
|
44
|
+
program: Program<Vault>,
|
34
45
|
vaultSystemStatePublicKey: PublicKey,
|
35
46
|
payerPublicKey: PublicKey,
|
36
47
|
halted: boolean
|
37
48
|
): Promise<TransactionInstruction> {
|
38
|
-
|
39
|
-
halted
|
40
|
-
{
|
41
|
-
|
42
|
-
|
43
|
-
vaultSystemState: vaultSystemStatePublicKey,
|
44
|
-
},
|
45
|
-
signers: []
|
49
|
+
return await program.methods
|
50
|
+
.setHalted(halted)
|
51
|
+
.accounts({
|
52
|
+
payer: payerPublicKey,
|
53
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
46
54
|
})
|
47
|
-
|
55
|
+
.instruction()
|
48
56
|
}
|