hedge-web3 0.1.14 → 0.1.15
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 +1 -2
- package/{lib/types/src → declarations}/HedgeDecimal.d.ts +0 -1
- 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/{lib/types/src → declarations}/instructions/createVault.d.ts +2 -3
- 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 +0 -1
- 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 +92 -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 +52 -0
- package/lib/state/VaultHistoryEvent.js +45 -0
- package/lib/utils/Errors.js +14 -0
- package/package.json +8 -6
- package/src/Constants.ts +4 -3
- package/src/HedgeDecimal.ts +1 -1
- 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 +39 -29
- 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/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.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,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
|
+
}
|
@@ -1,39 +1,37 @@
|
|
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,
|
5
|
+
import { findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey, getPoolPublicKeyForMint, getHedgeMintPublicKey } from '../Constants'
|
6
6
|
|
7
7
|
export async function withdrawVault (
|
8
8
|
program: Program,
|
9
9
|
provider: Provider,
|
10
10
|
payer: Signer,
|
11
11
|
vaultPublicKey: PublicKey,
|
12
|
-
withdrawAmount: number
|
12
|
+
withdrawAmount: number,
|
13
|
+
overrideTime?: number
|
13
14
|
): Promise<PublicKey> {
|
14
15
|
const usdhMintPublickey = await getUsdhMintPublicKey()
|
15
|
-
const USDH = new Token(
|
16
|
-
provider.connection,
|
17
|
-
usdhMintPublickey,
|
18
|
-
TOKEN_PROGRAM_ID,
|
19
|
-
payer
|
20
|
-
)
|
21
16
|
|
22
17
|
// Prep the user to get USDH back out at some point
|
23
|
-
await
|
18
|
+
await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
|
24
19
|
|
25
20
|
const history = Keypair.generate()
|
26
21
|
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
27
|
-
|
28
22
|
const vaultAccount = await program.account.vault.fetch(vaultPublicKey)
|
29
|
-
const
|
30
|
-
const vaultAssociatedCollateralAccount = await
|
31
|
-
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccount)
|
32
|
-
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccount, collateralStateAccountInfo.collateralMint)
|
23
|
+
const vaultTypeAccount = await getVaultTypeAccountPublicKey(vaultAccount.collateralType)
|
24
|
+
const vaultAssociatedCollateralAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, TokenInstructions.WRAPPED_SOL_MINT, vaultPublicKey, true)
|
33
25
|
|
34
|
-
const
|
26
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
|
27
|
+
const vaultTypeAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, vaultTypeAccount, true)
|
35
28
|
|
36
|
-
const destinationTokenAccount = await
|
29
|
+
const destinationTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, vaultTypeAccountInfo.collateralMint, payer.publicKey)
|
30
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(await getHedgeMintPublicKey())
|
31
|
+
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
32
|
+
hedgeStakingPoolPublicKey,
|
33
|
+
usdhMintPublickey
|
34
|
+
)
|
37
35
|
|
38
36
|
const transaction = new Transaction().add(
|
39
37
|
await withdrawVaultInstruction(
|
@@ -42,11 +40,15 @@ export async function withdrawVault (
|
|
42
40
|
payer.publicKey,
|
43
41
|
destinationTokenAccount.address,
|
44
42
|
vaultPublicKey,
|
45
|
-
vaultAssociatedCollateralAccount,
|
46
|
-
|
47
|
-
|
43
|
+
vaultAssociatedCollateralAccount.address,
|
44
|
+
vaultTypeAccount,
|
45
|
+
vaultTypeAssociatedTokenAccount.address,
|
46
|
+
hedgeStakingPoolPublicKey,
|
47
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount,
|
48
|
+
usdhMintPublickey,
|
48
49
|
history.publicKey,
|
49
|
-
withdrawAmount
|
50
|
+
withdrawAmount,
|
51
|
+
overrideTime
|
50
52
|
)
|
51
53
|
)
|
52
54
|
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
@@ -60,20 +62,28 @@ export async function withdrawVaultInstruction (
|
|
60
62
|
destinationTokenAccount: PublicKey,
|
61
63
|
vaultPublickey: PublicKey,
|
62
64
|
vaultAssociatedCollateralPublicKey: PublicKey,
|
63
|
-
|
64
|
-
|
65
|
+
vaultTypeAccount: PublicKey,
|
66
|
+
vaultTypeAssociatedTokenAccount: PublicKey,
|
67
|
+
hedgeStakingPoolPublicKey: PublicKey,
|
68
|
+
hedgeStakingPoolAssociatedUsdhTokenAccount: PublicKey,
|
69
|
+
usdhMint: PublicKey,
|
65
70
|
historyPublicKey: PublicKey,
|
66
|
-
withdrawAmount: number
|
71
|
+
withdrawAmount: number,
|
72
|
+
overrideTime?: number
|
67
73
|
): Promise<TransactionInstruction> {
|
68
74
|
return program.instruction.withdrawVault(
|
69
75
|
new BN(withdrawAmount),
|
76
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
70
77
|
{
|
71
78
|
accounts: {
|
72
79
|
vaultSystemState: vaultSystemStatePublicKey,
|
73
|
-
|
74
|
-
|
80
|
+
vaultTypeAccount: vaultTypeAccount,
|
81
|
+
vaultTypeAssociatedTokenAccount: vaultTypeAssociatedTokenAccount,
|
75
82
|
vault: vaultPublickey,
|
76
83
|
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
84
|
+
feePool: hedgeStakingPoolPublicKey,
|
85
|
+
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
86
|
+
usdhMint: usdhMint,
|
77
87
|
history: historyPublicKey,
|
78
88
|
vaultOwner: payerPublicKey,
|
79
89
|
destinationTokenAccount: destinationTokenAccount,
|
@@ -43,8 +43,8 @@ export class LiquidationPosition {
|
|
43
43
|
return (era.product.div(this.productSnapshotEntry)).mul(new Decimal(this.deposit))
|
44
44
|
}
|
45
45
|
|
46
|
-
public
|
47
|
-
return era.sum[
|
46
|
+
public getTokensAvailable (era: LiquidationPoolEra, index: number): Decimal {
|
47
|
+
return era.sum[index].minus(this.sumSnapshotsEntry[index]).div(this.productSnapshotEntry).mul(new Decimal(this.deposit)).floor()
|
48
48
|
}
|
49
49
|
|
50
50
|
// public getHedgeAvailable (): Decimal {
|
package/tsconfig.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"compilerOptions": {
|
3
|
-
"target": "
|
3
|
+
"target": "es6",
|
4
4
|
"allowJs": true,
|
5
5
|
"declaration": true,
|
6
6
|
"declarationDir": "declarations",
|
@@ -8,13 +8,14 @@
|
|
8
8
|
"allowSyntheticDefaultImports": true,
|
9
9
|
"strict": true,
|
10
10
|
"forceConsistentCasingInFileNames": true,
|
11
|
-
"module": "
|
11
|
+
"module": "CommonJS",
|
12
12
|
"moduleResolution": "node",
|
13
13
|
"resolveJsonModule": true,
|
14
14
|
"isolatedModules": true,
|
15
15
|
"baseUrl": "src",
|
16
16
|
"noFallthroughCasesInSwitch": true,
|
17
17
|
"noImplicitReturns": true,
|
18
|
+
"outDir": "./lib",
|
18
19
|
},
|
19
20
|
"include": ["src"]
|
20
21
|
}
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
3
|
-
|
4
|
-
name: Node.js Package
|
5
|
-
|
6
|
-
on:
|
7
|
-
release:
|
8
|
-
types: [created]
|
9
|
-
|
10
|
-
jobs:
|
11
|
-
build:
|
12
|
-
runs-on: ubuntu-latest
|
13
|
-
steps:
|
14
|
-
- uses: actions/checkout@v2
|
15
|
-
- uses: actions/setup-node@v2
|
16
|
-
with:
|
17
|
-
node-version: 16
|
18
|
-
- run: npm ci
|
19
|
-
- run: npm run build
|
20
|
-
|
21
|
-
publish-npm:
|
22
|
-
needs: build
|
23
|
-
runs-on: ubuntu-latest
|
24
|
-
steps:
|
25
|
-
- uses: actions/checkout@v2
|
26
|
-
- uses: actions/setup-node@v2
|
27
|
-
with:
|
28
|
-
node-version: 16
|
29
|
-
registry-url: https://registry.npmjs.org/
|
30
|
-
- run: npm ci
|
31
|
-
- run: npm run build
|
32
|
-
- run: npm publish
|
33
|
-
env:
|
34
|
-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
package/README.md
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
# Hedge Web3 API
|
2
|
-
|
3
|
-
Documentation for the Hedge Web3 TypeScript API.
|
4
|
-
|
5
|
-

|
6
|
-
|
7
|
-
## Documentation
|
8
|
-
|
9
|
-
Documentation can be found here: [https://web3-docs.hedge.so/](https://web3-docs.hedge.so/)
|
10
|
-
|
11
|
-
|
12
|
-
## Installation
|
13
|
-
|
14
|
-
TypeDoc runs on Node.js and is available as a NPM package.
|
15
|
-
|
16
|
-
```sh
|
17
|
-
npm install hedge-web3 --save-dev
|
18
|
-
```
|
19
|
-
|
20
|
-
## Usage
|
21
|
-
|
22
|
-
To create a vault, import the package.
|
23
|
-
|
24
|
-
```js
|
25
|
-
const HedgeWeb3 = require('hedge-web3')
|
26
|
-
```
|
27
|
-
|
28
|
-
To create a vault and deposit 3 SOL:
|
29
|
-
|
30
|
-
```js
|
31
|
-
const vaultPublickey = await HedgeWeb3.createVault(connection, ownerPublicKey, LAMPORTS_PER_SOL * 3, LAMPORTS_PER_SOL * 1.2)
|
32
|
-
```
|
33
|
-
|
34
|
-
## Contributing
|
35
|
-
|
36
|
-
This project is maintained by a community of developers. Contributions are welcome and appreciated.
|
37
|
-
You can find Hedge Labs on GitHub; feel free to open an issue or create a pull request:
|
38
|
-
https://github.com/Hedge-Finance/hedge-web3
|
39
|
-
|
40
|
-
## License
|
41
|
-
|
42
|
-
Copyright (c) 2021 [Christopher Coudron](https://hedge.so)<br>
|
43
|
-
Copyright (c) 2021 [Hedge Labs Ltd.](https://hedge.so)<br>
|
44
|
-
Licensed under the Apache License 2.0.
|