hedge-web3 0.1.14 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{lib/types/src → declarations}/Constants.d.ts +2 -3
- package/{lib/types/src → declarations}/HedgeDecimal.d.ts +2 -3
- package/{lib/types/src → declarations}/StakingPools.d.ts +0 -1
- package/{lib/types/src → declarations}/Vaults.d.ts +0 -1
- package/{lib/types/src → declarations}/idl/idl.d.ts +0 -1
- package/declarations/idl/vault.d.ts +2283 -0
- package/{lib/types/src → declarations}/index.d.ts +2 -1
- package/{lib/types/src → declarations}/instructions/claimLiquidationPoolPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/closeLiquidationPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/createStakingPool.d.ts +0 -1
- package/declarations/instructions/createVault.d.ts +5 -0
- package/{lib/types/src → declarations}/instructions/depositLiquidationPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/depositVault.d.ts +2 -3
- package/declarations/instructions/initHedgeFoundation.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/liquidateVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/loanVault.d.ts +2 -3
- package/{lib/types/src → declarations}/instructions/redeemVault.d.ts +1 -2
- package/{lib/types/src → declarations}/instructions/refreshOraclePrice.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/repayVault.d.ts +2 -3
- package/declarations/instructions/setHalted.d.ts +4 -0
- package/{lib/types/src → declarations}/instructions/withdrawStakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/instructions/withdrawVault.d.ts +2 -3
- package/{lib/types/src → declarations}/state/LiquidationPoolEra.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPoolState.d.ts +0 -1
- package/{lib/types/src → declarations}/state/LiquidationPosition.d.ts +1 -2
- package/{lib/types/src → declarations}/state/StakingPool.d.ts +0 -1
- package/{lib/types/src → declarations}/state/StakingPoolPosition.d.ts +0 -1
- package/{lib/types/src → declarations}/state/VaultAccount.d.ts +7 -5
- package/{lib/types/src → declarations}/state/VaultHistoryEvent.d.ts +0 -1
- package/{lib/types/src → declarations}/utils/Errors.d.ts +0 -1
- package/lib/Constants.js +86 -0
- package/lib/HedgeDecimal.js +24 -0
- package/lib/StakingPools.js +15 -0
- package/lib/Vaults.js +20 -0
- package/lib/idl/idl.js +1475 -0
- package/lib/idl/vault.js +2285 -0
- package/lib/index.js +36 -2382
- package/lib/instructions/claimLiquidationPoolPosition.js +53 -0
- package/lib/instructions/closeLiquidationPoolPosition.js +60 -0
- package/lib/instructions/createStakingPool.js +52 -0
- package/lib/instructions/createVault.js +152 -0
- package/lib/instructions/depositLiquidationPool.js +55 -0
- package/lib/instructions/depositStakingPool.js +52 -0
- package/lib/instructions/depositVault.js +87 -0
- package/lib/instructions/initHedgeFoundation.js +55 -0
- package/lib/instructions/liquidateVault.js +80 -0
- package/lib/instructions/loanVault.js +61 -0
- package/lib/instructions/redeemVault.js +65 -0
- package/lib/instructions/refreshOraclePrice.js +65 -0
- package/lib/instructions/repayVault.js +62 -0
- package/lib/instructions/setHalted.js +37 -0
- package/lib/instructions/withdrawStakingPool.js +64 -0
- package/lib/instructions/withdrawVault.js +62 -0
- package/lib/state/LiquidationPoolEra.js +19 -0
- package/lib/state/LiquidationPoolState.js +12 -0
- package/lib/state/LiquidationPosition.js +39 -0
- package/lib/state/StakingPool.js +22 -0
- package/lib/state/StakingPoolPosition.js +28 -0
- package/lib/state/VaultAccount.js +55 -0
- package/lib/state/VaultHistoryEvent.js +45 -0
- package/lib/utils/Errors.js +14 -0
- package/package.json +9 -7
- package/src/Constants.ts +5 -4
- package/src/HedgeDecimal.ts +3 -3
- package/src/idl/vault.ts +4565 -0
- package/src/index.ts +2 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +19 -11
- package/src/instructions/closeLiquidationPoolPosition.ts +7 -13
- package/src/instructions/createVault.ts +224 -57
- package/src/instructions/depositLiquidationPool.ts +6 -9
- package/src/instructions/depositStakingPool.ts +3 -1
- package/src/instructions/depositVault.ts +41 -29
- package/src/instructions/initHedgeFoundation.ts +64 -0
- package/src/instructions/liquidateVault.ts +30 -33
- package/src/instructions/loanVault.ts +22 -24
- package/src/instructions/redeemVault.ts +15 -28
- package/src/instructions/refreshOraclePrice.ts +2 -2
- package/src/instructions/repayVault.ts +20 -22
- package/src/instructions/setHalted.ts +48 -0
- package/src/instructions/withdrawVault.ts +36 -26
- package/src/state/LiquidationPosition.ts +2 -2
- package/src/state/VaultAccount.ts +20 -9
- package/tsconfig.json +3 -2
- package/.github/workflows/npm-publish.yml +0 -34
- package/README.md +0 -44
- package/jsconfig.json +0 -12
- package/lib/index.js.map +0 -1
- package/lib/types/src/Constants.d.ts.map +0 -1
- package/lib/types/src/HedgeDecimal.d.ts.map +0 -1
- package/lib/types/src/StakingPools.d.ts.map +0 -1
- package/lib/types/src/Vaults.d.ts.map +0 -1
- package/lib/types/src/idl/idl.d.ts.map +0 -1
- package/lib/types/src/index.d.ts.map +0 -1
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +0 -1
- package/lib/types/src/instructions/createStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/createVault.d.ts +0 -5
- package/lib/types/src/instructions/createVault.d.ts.map +0 -1
- package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/depositVault.d.ts.map +0 -1
- package/lib/types/src/instructions/liquidateVault.d.ts.map +0 -1
- package/lib/types/src/instructions/loanVault.d.ts.map +0 -1
- package/lib/types/src/instructions/redeemVault.d.ts.map +0 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +0 -1
- package/lib/types/src/instructions/repayVault.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +0 -1
- package/lib/types/src/instructions/withdrawVault.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPoolState.d.ts.map +0 -1
- package/lib/types/src/state/LiquidationPosition.d.ts.map +0 -1
- package/lib/types/src/state/StakingPool.d.ts.map +0 -1
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +0 -1
- package/lib/types/src/state/VaultAccount.d.ts.map +0 -1
- package/lib/types/src/state/VaultHistoryEvent.d.ts.map +0 -1
- package/lib/types/src/utils/Errors.d.ts.map +0 -1
- package/lib/types/tsconfig.base.tsbuildinfo +0 -1
- package/rollup.config.js +0 -42
@@ -1,41 +1,41 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { TokenInstructions } from '@project-serum/serum'
|
3
|
-
import {
|
3
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
4
4
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
5
|
-
import { findAssociatedTokenAddress,
|
5
|
+
import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
6
6
|
|
7
|
-
export async function depositVault
|
7
|
+
export async function depositVault(
|
8
8
|
program: Program,
|
9
9
|
provider: Provider,
|
10
10
|
payer: Signer,
|
11
11
|
vaultPublicKey: PublicKey,
|
12
|
-
|
13
|
-
|
12
|
+
depositAmount: number,
|
13
|
+
overrideTime?: number
|
14
14
|
): Promise<PublicKey> {
|
15
15
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
16
|
-
const USDH = new Token(
|
17
|
-
provider.connection,
|
18
|
-
usdhMintPublickey,
|
19
|
-
TOKEN_PROGRAM_ID,
|
20
|
-
payer
|
21
|
-
)
|
22
16
|
|
23
17
|
// Prep the user to get USDH back out at some point
|
24
|
-
await
|
18
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
25
19
|
|
26
20
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
27
|
-
const
|
28
|
-
const
|
29
|
-
const
|
21
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
22
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
23
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true)
|
30
24
|
|
31
|
-
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey,
|
32
|
-
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey,
|
25
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, vaultTypeAccountInfo.collateralMint)
|
26
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccountInfo.collateralMint)
|
33
27
|
|
34
28
|
const history = Keypair.generate()
|
35
29
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
36
30
|
|
37
31
|
const wrappedSolAccount = Keypair.generate()
|
38
32
|
|
33
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
34
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
35
|
+
hedgeStakingPoolPublicKey,
|
36
|
+
usdhMintPublickey
|
37
|
+
)
|
38
|
+
|
39
39
|
const transaction = new Transaction()
|
40
40
|
const signers = [payer, history]
|
41
41
|
|
@@ -65,10 +65,14 @@ export async function depositVault (
|
|
65
65
|
vaultPublicKey,
|
66
66
|
vaultAssociatedCollateralAccountPublicKey,
|
67
67
|
history.publicKey,
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
vaultTypeAccountPublicKey,
|
69
|
+
vaultTypeAssociatedTokenAccount.address,
|
70
|
+
hedgeStakingPoolPublicKey,
|
71
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount,
|
72
|
+
vaultTypeAccountInfo.collateralMint,
|
73
|
+
usdhMintPublickey,
|
74
|
+
depositAmount,
|
75
|
+
overrideTime
|
72
76
|
)
|
73
77
|
)
|
74
78
|
if (vaultAccount.collateralType === 'SOL') {
|
@@ -85,7 +89,7 @@ export async function depositVault (
|
|
85
89
|
return vaultPublicKey
|
86
90
|
}
|
87
91
|
|
88
|
-
export async function depositVaultInstruction
|
92
|
+
export async function depositVaultInstruction(
|
89
93
|
program: Program,
|
90
94
|
vaultSystemStatePublicKey: PublicKey,
|
91
95
|
vaultOwner: PublicKey,
|
@@ -93,24 +97,32 @@ export async function depositVaultInstruction (
|
|
93
97
|
vaultPublicKey: PublicKey,
|
94
98
|
vaultAssociatedTokenAccount: PublicKey,
|
95
99
|
historyPublicKey: PublicKey,
|
96
|
-
|
97
|
-
|
100
|
+
vaultTypeAccountPublicKey: PublicKey,
|
101
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
102
|
+
hedgeStakingPoolPublicKey: PublicKey,
|
103
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
|
98
104
|
collateralMint: PublicKey,
|
99
|
-
|
105
|
+
usdhMintPublickey: PublicKey,
|
106
|
+
depositAmount: number,
|
107
|
+
overrideTime?: number
|
100
108
|
): Promise<TransactionInstruction> {
|
101
109
|
return program.instruction.depositVault(
|
102
|
-
new BN(depositAmount)
|
103
|
-
,
|
110
|
+
new BN(depositAmount),
|
111
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
112
|
+
{
|
104
113
|
accounts: {
|
105
114
|
vaultSystemState: vaultSystemStatePublicKey,
|
106
|
-
|
107
|
-
|
115
|
+
vaultTypeAccount: vaultTypeAccountPublicKey,
|
116
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
108
117
|
collateralTokenMint: collateralMint,
|
109
118
|
vault: vaultPublicKey,
|
110
119
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
120
|
+
feePool: hedgeStakingPoolPublicKey,
|
121
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
111
122
|
history: historyPublicKey,
|
112
123
|
vaultOwner: vaultOwner,
|
113
124
|
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
125
|
+
usdhMint: usdhMintPublickey,
|
114
126
|
systemProgram: SystemProgram.programId,
|
115
127
|
tokenProgram: TOKEN_PROGRAM_ID
|
116
128
|
},
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import { Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
|
+
|
7
|
+
export async function initHedgeFoundation(
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
10
|
+
payer: Signer
|
11
|
+
): Promise<PublicKey> {
|
12
|
+
|
13
|
+
const poolEra = Keypair.generate()
|
14
|
+
const transaction = new Transaction().add(
|
15
|
+
await initHedgeFoundationInstruction(
|
16
|
+
program,
|
17
|
+
poolEra.publicKey,
|
18
|
+
payer.publicKey,
|
19
|
+
)
|
20
|
+
)
|
21
|
+
|
22
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, poolEra], provider.opts).catch(parseAnchorErrors)
|
23
|
+
return payer.publicKey
|
24
|
+
}
|
25
|
+
|
26
|
+
export async function initHedgeFoundationInstruction (
|
27
|
+
program: Program,
|
28
|
+
poolEraPublicKey: PublicKey,
|
29
|
+
payerPublicKey: PublicKey,
|
30
|
+
): Promise<TransactionInstruction> {
|
31
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
32
|
+
const poolStatePublickey = await getLiquidationPoolStatePublicKey()
|
33
|
+
const poolUsdhTokenAccount = await getLiquidationPoolUsdhAccountPublicKey()
|
34
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
35
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
36
|
+
const founderHedgeTokenAccount = await findAssociatedTokenAddress(
|
37
|
+
payerPublicKey,
|
38
|
+
hedgeMintPublickey
|
39
|
+
)
|
40
|
+
const communityHedgeTokenAccount = await findAssociatedTokenAddress(
|
41
|
+
vaultSystemStatePublicKey,
|
42
|
+
hedgeMintPublickey
|
43
|
+
)
|
44
|
+
|
45
|
+
return program.instruction.initHedgeFoundation(
|
46
|
+
{
|
47
|
+
accounts: {
|
48
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
49
|
+
poolState: poolStatePublickey,
|
50
|
+
poolEra: poolEraPublicKey,
|
51
|
+
poolUsdhAccount: poolUsdhTokenAccount,
|
52
|
+
founder: payerPublicKey,
|
53
|
+
usdhMint: usdhMintPublickey,
|
54
|
+
hedgeMint: hedgeMintPublickey,
|
55
|
+
founderAssociatedHedgeTokenAccount: founderHedgeTokenAccount,
|
56
|
+
communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
|
57
|
+
rent: SYSVAR_RENT_PUBKEY,
|
58
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
59
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
60
|
+
systemProgram: SystemProgram.programId
|
61
|
+
},
|
62
|
+
signers: []
|
63
|
+
})
|
64
|
+
}
|
@@ -1,38 +1,33 @@
|
|
1
|
-
import { Program, Provider } from '@project-serum/anchor'
|
2
|
-
import { ASSOCIATED_TOKEN_PROGRAM_ID,
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
3
3
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint,
|
4
|
+
import { getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
5
|
|
6
6
|
export async function liquidateVault (
|
7
7
|
program: Program,
|
8
8
|
provider: Provider,
|
9
9
|
payer: Signer,
|
10
10
|
vaultPublicKey: PublicKey,
|
11
|
-
|
11
|
+
overrideTime?: number
|
12
12
|
): Promise<PublicKey> {
|
13
13
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
14
14
|
|
15
|
-
const
|
16
|
-
const
|
17
|
-
const collateralMint =
|
18
|
-
|
19
|
-
const token = new Token(
|
20
|
-
provider.connection,
|
21
|
-
collateralMint,
|
22
|
-
TOKEN_PROGRAM_ID,
|
23
|
-
payer
|
24
|
-
)
|
15
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
16
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
17
|
+
const collateralMint = vaultTypeAccountInfo.collateralMint
|
25
18
|
|
26
19
|
const hedgeMintPublickey = await getHedgeMintPublicKey()
|
20
|
+
const usdhMintPublickey = await getUsdhMintPublicKey()
|
27
21
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey)
|
28
22
|
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey()
|
29
23
|
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey)
|
30
24
|
|
31
|
-
const payerAssociatedTokenAccount = await
|
32
|
-
const feePoolAssociatedTokenAccount = await
|
33
|
-
const vaultAssociatedTokenAccount = await
|
34
|
-
const poolAssociatedTokenAccount = await
|
35
|
-
const
|
25
|
+
const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, payer.publicKey, true)
|
26
|
+
const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, hedgeStakingPoolPublicKey, true)
|
27
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, vaultPublicKey, true)
|
28
|
+
const poolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, liquidationPoolStatePublicKey, true)
|
29
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, vaultTypeAccountPublicKey, true)
|
30
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, hedgeStakingPoolPublicKey, true)
|
36
31
|
|
37
32
|
const history = Keypair.generate()
|
38
33
|
const newEra = Keypair.generate()
|
@@ -44,18 +39,19 @@ export async function liquidateVault (
|
|
44
39
|
payer.publicKey,
|
45
40
|
payerAssociatedTokenAccount.address,
|
46
41
|
vaultPublicKey,
|
47
|
-
vaultAssociatedTokenAccount,
|
42
|
+
vaultAssociatedTokenAccount.address,
|
48
43
|
liquidationPoolStatePublicKey,
|
49
44
|
poolStateInfo.currentEra,
|
50
|
-
poolAssociatedTokenAccount,
|
45
|
+
poolAssociatedTokenAccount.address,
|
51
46
|
history.publicKey,
|
52
47
|
newEra.publicKey,
|
53
48
|
hedgeStakingPoolPublicKey,
|
54
|
-
feePoolAssociatedTokenAccount,
|
49
|
+
feePoolAssociatedTokenAccount.address,
|
50
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount.address,
|
55
51
|
collateralMint,
|
56
|
-
|
52
|
+
vaultTypeAssociatedTokenAccount.address,
|
57
53
|
vaultAccount.collateralType,
|
58
|
-
|
54
|
+
overrideTime
|
59
55
|
)
|
60
56
|
)
|
61
57
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts)
|
@@ -75,21 +71,22 @@ export async function liquidateVaultInstruction (
|
|
75
71
|
newEraPublicKey: PublicKey,
|
76
72
|
feePool: PublicKey,
|
77
73
|
feePoolAssociatedTokenAccount: PublicKey,
|
74
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
|
78
75
|
collateralMint: PublicKey,
|
79
|
-
|
76
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
80
77
|
collateralType: string,
|
81
|
-
|
78
|
+
overrideTime?: number
|
82
79
|
): Promise<TransactionInstruction> {
|
83
80
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
84
81
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
85
82
|
const liquidationPoolUsdhAccountPublickey = await getLiquidationPoolUsdhAccountPublicKey()
|
86
|
-
const
|
83
|
+
const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
|
87
84
|
|
88
85
|
const payload = {
|
89
86
|
accounts: {
|
90
87
|
vaultSystemState: vaultSystemStatePublicKey,
|
91
|
-
|
92
|
-
|
88
|
+
vaultTypeAccount: vaultTypeAccount,
|
89
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
93
90
|
collateralMint: collateralMint,
|
94
91
|
poolEra: poolEra,
|
95
92
|
vaultAccount: vaultPublickey,
|
@@ -102,6 +99,7 @@ export async function liquidateVaultInstruction (
|
|
102
99
|
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
103
100
|
feePool: feePool,
|
104
101
|
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
102
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
105
103
|
liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
|
106
104
|
newEra: newEraPublicKey,
|
107
105
|
tokenProgram: TOKEN_PROGRAM_ID,
|
@@ -111,8 +109,7 @@ export async function liquidateVaultInstruction (
|
|
111
109
|
},
|
112
110
|
signers: []
|
113
111
|
}
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
return program.instruction.liquidateVault(payload)
|
112
|
+
return program.instruction.liquidateVault(
|
113
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
114
|
+
payload)
|
118
115
|
}
|
@@ -1,30 +1,25 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint,
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
5
|
|
6
6
|
export async function loanVault (
|
7
7
|
program: Program,
|
8
8
|
provider: Provider,
|
9
9
|
payer: Signer,
|
10
10
|
vaultPublicKey: PublicKey,
|
11
|
-
loanAmount: number
|
11
|
+
loanAmount: number,
|
12
|
+
overrideTime?: number
|
12
13
|
): Promise<PublicKey> {
|
13
14
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
14
|
-
const USDH = new Token(
|
15
|
-
provider.connection,
|
16
|
-
usdhMintPublickey,
|
17
|
-
TOKEN_PROGRAM_ID,
|
18
|
-
payer
|
19
|
-
)
|
20
15
|
|
21
|
-
|
22
|
-
|
16
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
17
|
+
|
23
18
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
24
|
-
const
|
25
|
-
const
|
26
|
-
const
|
27
|
-
const vaultAssociatedTokenAccount = await
|
19
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
20
|
+
const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
21
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccount.collateralMint, vaultTypeAccountPublicKey, true)
|
22
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true)
|
28
23
|
|
29
24
|
const history = Keypair.generate()
|
30
25
|
const transaction = new Transaction().add(
|
@@ -33,11 +28,12 @@ export async function loanVault (
|
|
33
28
|
payer.publicKey,
|
34
29
|
payerUsdhAccount.address,
|
35
30
|
vaultPublicKey,
|
36
|
-
vaultAssociatedTokenAccount,
|
31
|
+
vaultAssociatedTokenAccount.address,
|
37
32
|
history.publicKey,
|
38
|
-
|
39
|
-
|
40
|
-
loanAmount
|
33
|
+
vaultTypeAccountPublicKey,
|
34
|
+
vaultTypeAssociatedTokenAccount.address,
|
35
|
+
loanAmount,
|
36
|
+
overrideTime
|
41
37
|
)
|
42
38
|
)
|
43
39
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
@@ -51,9 +47,10 @@ export async function loanVaultInstruction (
|
|
51
47
|
vaultPublickey: PublicKey,
|
52
48
|
vaultAssociatedTokenAccount: PublicKey,
|
53
49
|
historyPublicKey: PublicKey,
|
54
|
-
|
55
|
-
|
56
|
-
loanAmount: number
|
50
|
+
vaultTypeAccount: PublicKey,
|
51
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
52
|
+
loanAmount: number,
|
53
|
+
overrideTime?: number
|
57
54
|
): Promise<TransactionInstruction> {
|
58
55
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
59
56
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
@@ -66,11 +63,12 @@ export async function loanVaultInstruction (
|
|
66
63
|
|
67
64
|
return program.instruction.loanVault(
|
68
65
|
new BN(loanAmount),
|
66
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
69
67
|
{
|
70
68
|
accounts: {
|
71
69
|
vaultSystemState: vaultSystemStatePublicKey,
|
72
|
-
|
73
|
-
|
70
|
+
vaultTypeAccount: vaultTypeAccount,
|
71
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
74
72
|
vaultAccount: vaultPublickey,
|
75
73
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
76
74
|
history: historyPublicKey,
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
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 { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint,
|
5
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function redeemVault (
|
8
8
|
program: Program,
|
@@ -13,31 +13,18 @@ export async function redeemVault (
|
|
13
13
|
transactionOverrideTime?: number
|
14
14
|
): Promise<PublicKey> {
|
15
15
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
16
|
-
const USDH = new Token(
|
17
|
-
provider.connection,
|
18
|
-
usdhMintPublickey,
|
19
|
-
TOKEN_PROGRAM_ID,
|
20
|
-
payer
|
21
|
-
)
|
22
16
|
|
23
17
|
// Prep the user to get USDH back out at some point
|
24
|
-
const payerUsdhAccount = await
|
18
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
25
19
|
|
26
20
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
27
|
-
const
|
28
|
-
const
|
29
|
-
const
|
30
|
-
|
31
|
-
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint)
|
21
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
22
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
23
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccountPublicKey, true)
|
32
24
|
|
33
|
-
const
|
34
|
-
provider.connection,
|
35
|
-
collateralStateAccountInfo.collateralMint,
|
36
|
-
TOKEN_PROGRAM_ID,
|
37
|
-
payer
|
38
|
-
)
|
25
|
+
const vaultAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultPublicKey, true)
|
39
26
|
|
40
|
-
const payerTokenAccount = await
|
27
|
+
const payerTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
|
41
28
|
|
42
29
|
const history = Keypair.generate()
|
43
30
|
const transaction = new Transaction().add(
|
@@ -47,10 +34,10 @@ export async function redeemVault (
|
|
47
34
|
payerUsdhAccount.address,
|
48
35
|
payerTokenAccount.address,
|
49
36
|
vaultPublicKey,
|
50
|
-
vaultAssociatedTokenAccount,
|
37
|
+
vaultAssociatedTokenAccount.address,
|
51
38
|
history.publicKey,
|
52
|
-
|
53
|
-
|
39
|
+
vaultTypeAccountPublicKey,
|
40
|
+
vaultTypeAssociatedTokenAccount.address,
|
54
41
|
redeemAmount,
|
55
42
|
transactionOverrideTime
|
56
43
|
)
|
@@ -67,8 +54,8 @@ export async function redeemVaultInstruction (
|
|
67
54
|
vaultPublickey: PublicKey,
|
68
55
|
vaultAssociatedTokenAccount: PublicKey,
|
69
56
|
historyPublicKey: PublicKey,
|
70
|
-
|
71
|
-
|
57
|
+
vaultTypeAccount: PublicKey,
|
58
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
72
59
|
redeemAmount: number,
|
73
60
|
transactionOverrideTime?: number
|
74
61
|
): Promise<TransactionInstruction> {
|
@@ -86,8 +73,8 @@ export async function redeemVaultInstruction (
|
|
86
73
|
{
|
87
74
|
accounts: {
|
88
75
|
vaultSystemState: vaultSystemStatePublicKey,
|
89
|
-
|
90
|
-
|
76
|
+
vaultTypeAccount: vaultTypeAccount,
|
77
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
91
78
|
vault: vaultPublickey,
|
92
79
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
93
80
|
history: historyPublicKey,
|
@@ -32,7 +32,7 @@ export async function refreshOraclePriceInstruction (
|
|
32
32
|
): Promise<TransactionInstruction> {
|
33
33
|
const enc = new TextEncoder()
|
34
34
|
const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
|
35
|
-
const [
|
35
|
+
const [vaultTypeAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
|
36
36
|
|
37
37
|
return program.instruction.refreshOraclePrice(
|
38
38
|
new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
@@ -40,7 +40,7 @@ export async function refreshOraclePriceInstruction (
|
|
40
40
|
{
|
41
41
|
accounts: {
|
42
42
|
oracleInfoAccount: oracleInfoAccount,
|
43
|
-
|
43
|
+
vaultTypeAccount: vaultTypeAccount,
|
44
44
|
oracleChainlink: chainlinkAccunts[network],
|
45
45
|
oraclePyth: pythAccounts[network],
|
46
46
|
oracleSwitchboard: switchboardAccounts[network],
|
@@ -1,31 +1,26 @@
|
|
1
1
|
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
-
import {
|
2
|
+
import { getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
3
|
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
-
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint,
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
5
5
|
|
6
6
|
export async function repayVault (
|
7
7
|
program: Program,
|
8
8
|
provider: Provider,
|
9
9
|
payer: Signer,
|
10
10
|
vaultPublicKey: PublicKey,
|
11
|
-
repayAmount: number
|
11
|
+
repayAmount: number,
|
12
|
+
overrideTime?: number
|
12
13
|
): Promise<PublicKey> {
|
13
14
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
14
|
-
const USDH = new Token(
|
15
|
-
provider.connection,
|
16
|
-
usdhMintPublickey,
|
17
|
-
TOKEN_PROGRAM_ID,
|
18
|
-
payer
|
19
|
-
)
|
20
15
|
|
21
16
|
// Prep the user to get USDH back out at some point
|
22
|
-
const payerUsdhAccount = await
|
17
|
+
const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
23
18
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
24
19
|
|
25
|
-
const
|
26
|
-
const
|
27
|
-
const
|
28
|
-
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey,
|
20
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
21
|
+
const vaultTypeAccount = await program.account.vaultType.fetch(vaultTypeAccountPublicKey)
|
22
|
+
const vaultTypeAssociatedTokenAccount = await findAssociatedTokenAddress(vaultTypeAccountPublicKey, vaultTypeAccount.collateralMint)
|
23
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, vaultTypeAccount.collateralMint)
|
29
24
|
|
30
25
|
const history = Keypair.generate()
|
31
26
|
const transaction = new Transaction().add(
|
@@ -36,9 +31,10 @@ export async function repayVault (
|
|
36
31
|
vaultPublicKey,
|
37
32
|
vaultAssociatedTokenAccount,
|
38
33
|
history.publicKey,
|
39
|
-
|
40
|
-
|
41
|
-
repayAmount
|
34
|
+
vaultTypeAccountPublicKey,
|
35
|
+
vaultTypeAssociatedTokenAccount,
|
36
|
+
repayAmount,
|
37
|
+
overrideTime
|
42
38
|
)
|
43
39
|
)
|
44
40
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
@@ -52,9 +48,10 @@ export async function repayVaultInstruction (
|
|
52
48
|
vaultPublickey: PublicKey,
|
53
49
|
vaultAssociatedTokenAccount: PublicKey,
|
54
50
|
historyPublicKey: PublicKey,
|
55
|
-
|
56
|
-
|
57
|
-
repayAmount: number
|
51
|
+
vaultTypeAccount: PublicKey,
|
52
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
53
|
+
repayAmount: number,
|
54
|
+
overrideTime?: number
|
58
55
|
): Promise<TransactionInstruction> {
|
59
56
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
60
57
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
@@ -67,11 +64,12 @@ export async function repayVaultInstruction (
|
|
67
64
|
|
68
65
|
return program.instruction.repayVault(
|
69
66
|
new BN(repayAmount),
|
67
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
70
68
|
{
|
71
69
|
accounts: {
|
72
70
|
vaultSystemState: vaultSystemStatePublicKey,
|
73
|
-
|
74
|
-
|
71
|
+
vaultTypeAccount: vaultTypeAccount,
|
72
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
75
73
|
vaultAccount: vaultPublickey,
|
76
74
|
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
77
75
|
history: historyPublicKey,
|
@@ -0,0 +1,48 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { TokenInstructions } from '@project-serum/serum'
|
3
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount } from '@solana/spl-token'
|
4
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
5
|
+
import { findAssociatedTokenAddress, findVaultAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
6
|
+
|
7
|
+
import { v4 as uuidv4 } from 'uuid'
|
8
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
9
|
+
|
10
|
+
export async function setHalted (
|
11
|
+
program: Program,
|
12
|
+
provider: Provider,
|
13
|
+
payer: Signer,
|
14
|
+
halted: boolean
|
15
|
+
): Promise<PublicKey> {
|
16
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
17
|
+
|
18
|
+
|
19
|
+
const transaction = new Transaction().add(
|
20
|
+
await setHaltedInstruction(
|
21
|
+
program,
|
22
|
+
vaultSystemStatePublicKey,
|
23
|
+
payer.publicKey,
|
24
|
+
halted
|
25
|
+
)
|
26
|
+
)
|
27
|
+
|
28
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider?.opts).catch(parseAnchorErrors)
|
29
|
+
return vaultSystemStatePublicKey
|
30
|
+
}
|
31
|
+
|
32
|
+
export async function setHaltedInstruction (
|
33
|
+
program: Program,
|
34
|
+
vaultSystemStatePublicKey: PublicKey,
|
35
|
+
payerPublicKey: PublicKey,
|
36
|
+
halted: boolean
|
37
|
+
): Promise<TransactionInstruction> {
|
38
|
+
const ix = program.instruction.setHalted(
|
39
|
+
halted,
|
40
|
+
{
|
41
|
+
accounts: {
|
42
|
+
payer: payerPublicKey,
|
43
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
44
|
+
},
|
45
|
+
signers: []
|
46
|
+
})
|
47
|
+
return ix
|
48
|
+
}
|