hedge-web3 0.1.4 → 0.1.10
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/index.js +394 -242
- package/lib/index.js.map +1 -1
- package/lib/types/src/Constants.d.ts +2 -1
- package/lib/types/src/Constants.d.ts.map +1 -1
- package/lib/types/src/index.d.ts +19 -3
- package/lib/types/src/index.d.ts.map +1 -1
- package/lib/types/src/instructions/createStakingPool.d.ts +4 -3
- package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -1
- package/lib/types/src/instructions/createVault.d.ts +4 -3
- package/lib/types/src/instructions/createVault.d.ts.map +1 -1
- package/lib/types/src/instructions/depositStakingPool.d.ts +4 -3
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -1
- package/lib/types/src/instructions/depositVault.d.ts +4 -3
- package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
- package/lib/types/src/instructions/liquidateVault.d.ts +4 -3
- package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
- package/lib/types/src/instructions/loanVault.d.ts +4 -3
- package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
- package/lib/types/src/instructions/redeemVault.d.ts +4 -3
- package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts +5 -0
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -0
- package/lib/types/src/instructions/repayVault.d.ts +4 -3
- package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
- package/lib/types/src/instructions/withdrawStakingPool.d.ts +4 -3
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -1
- package/lib/types/src/instructions/withdrawVault.d.ts +4 -3
- package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPosition.d.ts +1 -0
- package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
- package/lib/types/src/state/StakingPool.d.ts +1 -2
- package/lib/types/src/state/StakingPool.d.ts.map +1 -1
- package/lib/types/src/state/StakingPoolPosition.d.ts +1 -0
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -1
- package/lib/types/src/state/VaultAccount.d.ts.map +1 -1
- package/lib/types/tsconfig.base.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Constants.ts +6 -7
- package/src/index.ts +21 -3
- package/src/instructions/createStakingPool.ts +9 -10
- package/src/instructions/createVault.ts +10 -11
- package/src/instructions/depositStakingPool.ts +9 -10
- package/src/instructions/depositVault.ts +10 -11
- package/src/instructions/liquidateVault.ts +14 -18
- package/src/instructions/loanVault.ts +14 -18
- package/src/instructions/redeemVault.ts +12 -16
- package/src/instructions/refreshOraclePrice.ts +45 -0
- package/src/instructions/repayVault.ts +10 -11
- package/src/instructions/withdrawStakingPool.ts +9 -10
- package/src/instructions/withdrawVault.ts +17 -21
- package/src/state/LiquidationPoolEra.ts +5 -5
- package/src/state/LiquidationPosition.ts +26 -18
- package/src/state/StakingPool.ts +4 -5
- package/src/state/StakingPoolPosition.ts +4 -5
- package/src/state/VaultAccount.ts +0 -1
- package/src/state/VaultHistoryEvent.ts +1 -1
@@ -1,21 +1,18 @@
|
|
1
|
-
import { BN, Program } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
|
6
|
-
import { vaultIdl } from '../idl/idl'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getSolCollateralPriceAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
7
5
|
|
8
6
|
export async function loanVault (
|
9
|
-
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
10
9
|
payer: Signer,
|
11
10
|
vaultPublicKey: PublicKey,
|
12
|
-
loanAmount: number
|
13
|
-
chainlinkOverridePrice?: number,
|
14
|
-
confirmOptions?: ConfirmOptions
|
11
|
+
loanAmount: number
|
15
12
|
): Promise<PublicKey> {
|
16
13
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
17
14
|
const USDH = new Token(
|
18
|
-
connection,
|
15
|
+
provider.connection,
|
19
16
|
usdhMintPublickey,
|
20
17
|
TOKEN_PROGRAM_ID,
|
21
18
|
payer
|
@@ -27,27 +24,26 @@ export async function loanVault (
|
|
27
24
|
const history = Keypair.generate()
|
28
25
|
const transaction = new Transaction().add(
|
29
26
|
await loanVaultInstruction(
|
27
|
+
program,
|
30
28
|
payer.publicKey,
|
31
29
|
payerUsdhAccount.address,
|
32
30
|
vaultPublicKey,
|
33
31
|
history.publicKey,
|
34
|
-
loanAmount
|
35
|
-
chainlinkOverridePrice
|
32
|
+
loanAmount
|
36
33
|
)
|
37
34
|
)
|
38
|
-
await sendAndConfirmTransaction(connection, transaction, [payer, history],
|
35
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
39
36
|
return vaultPublicKey
|
40
37
|
}
|
41
38
|
|
42
39
|
export async function loanVaultInstruction (
|
40
|
+
program: Program,
|
43
41
|
payerPublicKey: PublicKey,
|
44
42
|
ownerUsdhAccount: PublicKey,
|
45
43
|
vaultPublickey: PublicKey,
|
46
44
|
historyPublicKey: PublicKey,
|
47
|
-
loanAmount: number
|
48
|
-
chainlinkOverridePrice?: number
|
45
|
+
loanAmount: number
|
49
46
|
): Promise<TransactionInstruction> {
|
50
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
51
47
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
52
48
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
53
49
|
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
@@ -56,10 +52,10 @@ export async function loanVaultInstruction (
|
|
56
52
|
hedgeStakingPoolPublicKey,
|
57
53
|
usdhMintPublickey
|
58
54
|
)
|
55
|
+
const solPriceAccount = await getSolCollateralPriceAccountPublicKey()
|
59
56
|
|
60
57
|
return program.instruction.loanVault(
|
61
58
|
new BN(loanAmount),
|
62
|
-
new BN(chainlinkOverridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
63
59
|
{
|
64
60
|
accounts: {
|
65
61
|
vaultSystemState: vaultSystemStatePublicKey,
|
@@ -70,7 +66,7 @@ export async function loanVaultInstruction (
|
|
70
66
|
usdhMint: usdhMintPublickey,
|
71
67
|
vaultOwner: payerPublicKey,
|
72
68
|
ownerUsdhAccount: ownerUsdhAccount,
|
73
|
-
|
69
|
+
priceForCollateral: solPriceAccount,
|
74
70
|
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
75
71
|
systemProgram: SystemProgram.programId
|
76
72
|
},
|
@@ -1,22 +1,19 @@
|
|
1
|
-
import { BN, Program } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
|
6
|
-
import { vaultIdl } from '../idl/idl'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getSolCollateralPriceAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
7
5
|
|
8
6
|
export async function redeemVault (
|
9
|
-
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
10
9
|
payer: Signer,
|
11
10
|
vaultPublicKey: PublicKey,
|
12
11
|
repayAmount: number,
|
13
|
-
|
14
|
-
transactionOverrideTime?: number,
|
15
|
-
confirmOptions?: ConfirmOptions
|
12
|
+
transactionOverrideTime?: number
|
16
13
|
): Promise<PublicKey> {
|
17
14
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
18
15
|
const USDH = new Token(
|
19
|
-
connection,
|
16
|
+
provider.connection,
|
20
17
|
usdhMintPublickey,
|
21
18
|
TOKEN_PROGRAM_ID,
|
22
19
|
payer
|
@@ -28,29 +25,28 @@ export async function redeemVault (
|
|
28
25
|
const history = Keypair.generate()
|
29
26
|
const transaction = new Transaction().add(
|
30
27
|
await redeemVaultInstruction(
|
28
|
+
program,
|
31
29
|
payer.publicKey,
|
32
30
|
payerUsdhAccount.address,
|
33
31
|
vaultPublicKey,
|
34
32
|
history.publicKey,
|
35
33
|
repayAmount,
|
36
|
-
chainlinkOverridePrice,
|
37
34
|
transactionOverrideTime
|
38
35
|
)
|
39
36
|
)
|
40
|
-
await sendAndConfirmTransaction(connection, transaction, [payer, history],
|
37
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
41
38
|
return vaultPublicKey
|
42
39
|
}
|
43
40
|
|
44
41
|
export async function redeemVaultInstruction (
|
42
|
+
program: Program,
|
45
43
|
payerPublicKey: PublicKey,
|
46
44
|
ownerUsdhAccount: PublicKey,
|
47
45
|
vaultPublickey: PublicKey,
|
48
46
|
historyPublicKey: PublicKey,
|
49
47
|
repayAmount: number,
|
50
|
-
chainlinkOverridePrice?: number,
|
51
48
|
transactionOverrideTime?: number
|
52
49
|
): Promise<TransactionInstruction> {
|
53
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
54
50
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
55
51
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
56
52
|
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
@@ -59,10 +55,10 @@ export async function redeemVaultInstruction (
|
|
59
55
|
hedgeStakingPoolPublicKey,
|
60
56
|
usdhMintPublickey
|
61
57
|
)
|
58
|
+
const solPriceAccount = await getSolCollateralPriceAccountPublicKey()
|
62
59
|
|
63
60
|
return program.instruction.redeemVault(
|
64
61
|
new BN(repayAmount),
|
65
|
-
new BN(chainlinkOverridePrice ?? 150 * LAMPORTS_PER_SOL), // override usd/sol price
|
66
62
|
new BN(transactionOverrideTime ?? (Date.now() / 1000)), // override start time
|
67
63
|
{
|
68
64
|
accounts: {
|
@@ -74,7 +70,7 @@ export async function redeemVaultInstruction (
|
|
74
70
|
usdhMint: usdhMintPublickey,
|
75
71
|
payer: payerPublicKey,
|
76
72
|
payerUsdhAccount: ownerUsdhAccount,
|
77
|
-
|
73
|
+
priceForCollateral: solPriceAccount,
|
78
74
|
splTokenProgramInfo: TOKEN_PROGRAM_ID,
|
79
75
|
systemProgram: SystemProgram.programId
|
80
76
|
},
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
3
|
+
import { HEDGE_PROGRAM_PUBLICKEY } from '../Constants'
|
4
|
+
|
5
|
+
export async function refreshOraclePrice (
|
6
|
+
program: Program,
|
7
|
+
provider: Provider,
|
8
|
+
payer: Signer,
|
9
|
+
overridePrice?: number,
|
10
|
+
overrideTime?: number
|
11
|
+
): Promise<string> {
|
12
|
+
const transaction = new Transaction().add(
|
13
|
+
await refreshOraclePriceInstruction(
|
14
|
+
program,
|
15
|
+
overridePrice,
|
16
|
+
overrideTime
|
17
|
+
)
|
18
|
+
)
|
19
|
+
return await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts)
|
20
|
+
}
|
21
|
+
|
22
|
+
export async function refreshOraclePriceInstruction (
|
23
|
+
program: Program,
|
24
|
+
overridePrice?: number,
|
25
|
+
overrideTime?: number
|
26
|
+
): Promise<TransactionInstruction> {
|
27
|
+
const enc = new TextEncoder()
|
28
|
+
const [oracleInfoAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
|
29
|
+
const [collateralPriceAccount] = await PublicKey.findProgramAddress([enc.encode('SOL'), enc.encode('Price')], HEDGE_PROGRAM_PUBLICKEY)
|
30
|
+
|
31
|
+
return program.instruction.refreshOraclePrice(
|
32
|
+
new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
|
33
|
+
new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
|
34
|
+
{
|
35
|
+
accounts: {
|
36
|
+
oracleInfoAccount: oracleInfoAccount,
|
37
|
+
collateralPriceAccount: collateralPriceAccount,
|
38
|
+
oracleChainlink: SystemProgram.programId,
|
39
|
+
oraclePyth: SystemProgram.programId,
|
40
|
+
oracleSwitchboard: SystemProgram.programId,
|
41
|
+
systemProgram: SystemProgram.programId
|
42
|
+
},
|
43
|
+
signers: []
|
44
|
+
})
|
45
|
+
}
|
@@ -1,20 +1,18 @@
|
|
1
|
-
import { BN, Program } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey
|
5
|
-
|
6
|
-
import { vaultIdl } from '../idl/idl'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
7
5
|
|
8
6
|
export async function repayVault (
|
9
|
-
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
10
9
|
payer: Signer,
|
11
10
|
vaultPublicKey: PublicKey,
|
12
|
-
repayAmount: number
|
13
|
-
confirmOptions?: ConfirmOptions
|
11
|
+
repayAmount: number
|
14
12
|
): Promise<PublicKey> {
|
15
13
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
16
14
|
const USDH = new Token(
|
17
|
-
connection,
|
15
|
+
provider.connection,
|
18
16
|
usdhMintPublickey,
|
19
17
|
TOKEN_PROGRAM_ID,
|
20
18
|
payer
|
@@ -26,6 +24,7 @@ export async function repayVault (
|
|
26
24
|
const history = Keypair.generate()
|
27
25
|
const transaction = new Transaction().add(
|
28
26
|
await repayVaultInstruction(
|
27
|
+
program,
|
29
28
|
payer.publicKey,
|
30
29
|
payerUsdhAccount.address,
|
31
30
|
vaultPublicKey,
|
@@ -33,18 +32,18 @@ export async function repayVault (
|
|
33
32
|
repayAmount
|
34
33
|
)
|
35
34
|
)
|
36
|
-
await sendAndConfirmTransaction(connection, transaction, [payer, history],
|
35
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
37
36
|
return vaultPublicKey
|
38
37
|
}
|
39
38
|
|
40
39
|
export async function repayVaultInstruction (
|
40
|
+
program: Program,
|
41
41
|
payerPublicKey: PublicKey,
|
42
42
|
ownerUsdhAccount: PublicKey,
|
43
43
|
vaultPublickey: PublicKey,
|
44
44
|
historyPublicKey: PublicKey,
|
45
45
|
repayAmount: number
|
46
46
|
): Promise<TransactionInstruction> {
|
47
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
48
47
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
49
48
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
50
49
|
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
@@ -1,39 +1,38 @@
|
|
1
|
-
import { BN, Program } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
4
|
import { parseAnchorErrors } from '../utils/Errors'
|
5
|
-
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey
|
6
|
-
|
7
|
-
import { vaultIdl } from '../idl/idl'
|
5
|
+
import { findAssociatedTokenAddress, getHedgeMintPublicKey, getPoolPublicKeyForMint, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
8
6
|
|
9
7
|
export async function withdrawStakingPool (
|
10
|
-
|
8
|
+
program: Program,
|
9
|
+
provider: Provider,
|
11
10
|
payer: Signer,
|
12
11
|
poolPositionPublicKey: PublicKey,
|
13
12
|
stakedTokenMintPublicKey: PublicKey,
|
14
|
-
overrideStartTime?: number
|
15
|
-
confirmOptions?: ConfirmOptions
|
13
|
+
overrideStartTime?: number
|
16
14
|
): Promise<PublicKey> {
|
17
15
|
const poolPosition = Keypair.generate()
|
18
16
|
const transaction = new Transaction().add(
|
19
17
|
await withdrawStakingPoolInstruction(
|
18
|
+
program,
|
20
19
|
payer.publicKey,
|
21
20
|
poolPositionPublicKey,
|
22
21
|
stakedTokenMintPublicKey,
|
23
22
|
overrideStartTime
|
24
23
|
)
|
25
24
|
)
|
26
|
-
await sendAndConfirmTransaction(connection, transaction, [payer],
|
25
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
|
27
26
|
return poolPosition.publicKey
|
28
27
|
}
|
29
28
|
|
30
29
|
export async function withdrawStakingPoolInstruction (
|
30
|
+
program: Program,
|
31
31
|
payerPublicKey: PublicKey,
|
32
32
|
poolPositionPublicKey: PublicKey,
|
33
33
|
stakedTokenMintPublicKey: PublicKey,
|
34
34
|
overrideStartTime?: number
|
35
35
|
): Promise<TransactionInstruction> {
|
36
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
37
36
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
38
37
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
39
38
|
const [hedgeMintPublickey] = await getHedgeMintPublicKey()
|
@@ -1,21 +1,18 @@
|
|
1
|
-
import { BN, Program } from '@project-serum/anchor'
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
2
|
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
|
6
|
-
import { vaultIdl } from '../idl/idl'
|
3
|
+
import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { getSolCollateralPriceAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
|
7
5
|
|
8
6
|
export async function withdrawVault (
|
9
|
-
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
10
9
|
payer: Signer,
|
11
10
|
vaultPublicKey: PublicKey,
|
12
|
-
withdrawAmount: number
|
13
|
-
chainlinkOverridePrice?: number,
|
14
|
-
confirmOptions?: ConfirmOptions
|
11
|
+
withdrawAmount: number
|
15
12
|
): Promise<PublicKey> {
|
16
13
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
17
14
|
const USDH = new Token(
|
18
|
-
connection,
|
15
|
+
provider.connection,
|
19
16
|
usdhMintPublickey,
|
20
17
|
TOKEN_PROGRAM_ID,
|
21
18
|
payer
|
@@ -27,38 +24,37 @@ export async function withdrawVault (
|
|
27
24
|
const history = Keypair.generate()
|
28
25
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
29
26
|
const transaction = new Transaction().add(
|
30
|
-
withdrawVaultInstruction(
|
27
|
+
await withdrawVaultInstruction(
|
28
|
+
program,
|
31
29
|
vaultSystemStatePublicKey,
|
32
30
|
payer.publicKey,
|
33
31
|
vaultPublicKey,
|
34
32
|
history.publicKey,
|
35
|
-
withdrawAmount
|
36
|
-
chainlinkOverridePrice
|
33
|
+
withdrawAmount
|
37
34
|
)
|
38
35
|
)
|
39
|
-
await sendAndConfirmTransaction(connection, transaction, [payer, history],
|
36
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
40
37
|
return vaultPublicKey
|
41
38
|
}
|
42
39
|
|
43
|
-
export function withdrawVaultInstruction (
|
40
|
+
export async function withdrawVaultInstruction (
|
41
|
+
program: Program,
|
44
42
|
vaultSystemStatePublicKey: PublicKey,
|
45
43
|
payerPublicKey: PublicKey,
|
46
44
|
vaultPublickey: PublicKey,
|
47
45
|
historyPublicKey: PublicKey,
|
48
|
-
withdrawAmount: number
|
49
|
-
|
50
|
-
|
51
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
46
|
+
withdrawAmount: number
|
47
|
+
): Promise<TransactionInstruction> {
|
48
|
+
const solPriceAccount = await getSolCollateralPriceAccountPublicKey()
|
52
49
|
return program.instruction.withdrawVault(
|
53
50
|
new BN(withdrawAmount),
|
54
|
-
new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
|
55
51
|
{
|
56
52
|
accounts: {
|
57
53
|
vaultSystemState: vaultSystemStatePublicKey,
|
58
54
|
vaultAccount: vaultPublickey,
|
59
55
|
history: historyPublicKey,
|
60
56
|
vaultOwner: payerPublicKey,
|
61
|
-
|
57
|
+
priceForCollateral: solPriceAccount,
|
62
58
|
systemProgram: SystemProgram.programId
|
63
59
|
},
|
64
60
|
signers: []
|
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
import Decimal from 'decimal.js'
|
3
|
-
import { DecimalFromU128 } from '
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
4
|
|
5
5
|
/**
|
6
6
|
* Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
|
@@ -9,13 +9,13 @@ import { DecimalFromU128 } from '..'
|
|
9
9
|
export class LiquidationPoolEra {
|
10
10
|
public totalDeposits: number
|
11
11
|
|
12
|
-
product: Decimal
|
12
|
+
public product: Decimal
|
13
13
|
|
14
|
-
sum: Decimal
|
14
|
+
public sum: Decimal
|
15
15
|
|
16
|
-
hedgeRewardsAccumulator: Decimal
|
16
|
+
public hedgeRewardsAccumulator: Decimal
|
17
17
|
|
18
|
-
hedgeRewardsTimestamp: number
|
18
|
+
public hedgeRewardsTimestamp: number
|
19
19
|
|
20
20
|
constructor (public liquidyPoolEra: any) {
|
21
21
|
this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { PublicKey } from '@solana/web3.js'
|
1
|
+
import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
2
|
import Decimal from 'decimal.js'
|
3
|
-
import { DecimalFromU128 } from '
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
4
|
import { LiquidationPoolEra } from './LiquidationPoolEra'
|
5
5
|
import { LiquidationPoolState } from './LiquidationPoolState'
|
6
6
|
|
@@ -48,22 +48,30 @@ export class LiquidationPosition {
|
|
48
48
|
return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal(this.deposit)).floor()
|
49
49
|
}
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
51
|
+
public getHedgeAvailable (): Decimal {
|
52
|
+
const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
|
53
|
+
const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
|
54
|
+
LiquidationPoolTotalSupply,
|
55
|
+
this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
|
56
|
+
this.era.hedgeRewardsTimestamp * 1000,
|
57
|
+
Date.now(),
|
58
|
+
365 * 1000)
|
59
59
|
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
if (this.era.totalDeposits === 0) {
|
61
|
+
return new Decimal(0)
|
62
|
+
}
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
const rewardsPerToken = this.era.product.mul(new Decimal(hedgeRewardsSinceLastUpdate / this.era.totalDeposits))
|
65
|
+
const newAccumulator = rewardsPerToken.add(new Decimal(this.era.hedgeRewardsAccumulator))
|
66
|
+
const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal(this.deposit)).div(new Decimal(this.productSnapshot))
|
67
|
+
return hedgeAvailable
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
|
72
|
+
const timeInOffsetStart = timeIn - birthTime
|
73
|
+
const timeOutOffsetStart = timeOut - birthTime
|
74
|
+
const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
|
75
|
+
const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
|
76
|
+
return awardedTokens
|
69
77
|
}
|
package/src/state/StakingPool.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
import { PublicKey } from '@solana/web3.js'
|
3
3
|
import Decimal from 'decimal.js'
|
4
|
-
import { DecimalFromU128 } from '
|
4
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
5
5
|
|
6
6
|
export class StakingPool {
|
7
7
|
public publicKey: PublicKey
|
@@ -14,10 +14,9 @@ export class StakingPool {
|
|
14
14
|
hedgeRewardAccumulator: Decimal
|
15
15
|
usdhFeeAccumulator: Decimal
|
16
16
|
solFeeAccumulator: Decimal
|
17
|
-
currentRewardsPerDay: Decimal
|
18
17
|
|
19
|
-
constructor (public poolInfo: any,
|
20
|
-
this.publicKey =
|
18
|
+
constructor (public poolInfo: any, publicKey: PublicKey) {
|
19
|
+
this.publicKey = publicKey
|
21
20
|
this.deposits = poolInfo.deposits.toNumber()
|
22
21
|
// this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
|
23
22
|
// this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
|
@@ -29,7 +28,7 @@ export class StakingPool {
|
|
29
28
|
this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
|
30
29
|
this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
|
31
30
|
this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
|
32
|
-
this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
|
31
|
+
// this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
|
33
32
|
}
|
34
33
|
|
35
34
|
// updateFeeAccumulator () {
|
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
import { PublicKey } from '@solana/web3.js'
|
3
3
|
import Decimal from 'decimal.js'
|
4
|
-
import { DecimalFromU128 } from '
|
4
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
5
5
|
import { StakingPool } from './StakingPool'
|
6
6
|
|
7
7
|
export class StakingPoolPosition {
|
@@ -33,8 +33,7 @@ export class StakingPoolPosition {
|
|
33
33
|
this.open = poolPositionInfo.state.open !== undefined
|
34
34
|
}
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
// }
|
36
|
+
public getCurrentUsdhFeeReward (): Decimal {
|
37
|
+
return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal(this.deposited))
|
38
|
+
}
|
40
39
|
}
|
@@ -34,7 +34,6 @@ export class VaultAccount {
|
|
34
34
|
this.deposited = vault.deposited.toNumber()
|
35
35
|
this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio)
|
36
36
|
this.liquidationPrice = vault.liquidationPrice.toNumber()
|
37
|
-
this.minCollateralRatio = vault.minCollateralRatio.toNumber()
|
38
37
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0]
|
39
38
|
}
|
40
39
|
|