hedge-web3 0.1.4 → 0.1.6
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 +356 -230
- package/lib/index.js.map +1 -1
- package/lib/types/src/index.d.ts +18 -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/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/index.ts +20 -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 +9 -10
- package/src/instructions/loanVault.ts +10 -11
- package/src/instructions/redeemVault.ts +10 -11
- package/src/instructions/repayVault.ts +10 -11
- package/src/instructions/withdrawStakingPool.ts +9 -10
- package/src/instructions/withdrawVault.ts +10 -11
- 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,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,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 { CHAINLINK_SOL_USD_PUBLICKEY, getUsdhMintPublicKey, getVaultSystemStatePublicKey
|
5
|
-
|
6
|
-
import { vaultIdl } from '../idl/idl'
|
3
|
+
import { Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
|
4
|
+
import { CHAINLINK_SOL_USD_PUBLICKEY, 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
11
|
withdrawAmount: number,
|
13
|
-
chainlinkOverridePrice?: number
|
14
|
-
confirmOptions?: ConfirmOptions
|
12
|
+
chainlinkOverridePrice?: number
|
15
13
|
): Promise<PublicKey> {
|
16
14
|
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
17
15
|
const USDH = new Token(
|
18
|
-
connection,
|
16
|
+
provider.connection,
|
19
17
|
usdhMintPublickey,
|
20
18
|
TOKEN_PROGRAM_ID,
|
21
19
|
payer
|
@@ -28,6 +26,7 @@ export async function withdrawVault (
|
|
28
26
|
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
29
27
|
const transaction = new Transaction().add(
|
30
28
|
withdrawVaultInstruction(
|
29
|
+
program,
|
31
30
|
vaultSystemStatePublicKey,
|
32
31
|
payer.publicKey,
|
33
32
|
vaultPublicKey,
|
@@ -36,11 +35,12 @@ export async function withdrawVault (
|
|
36
35
|
chainlinkOverridePrice
|
37
36
|
)
|
38
37
|
)
|
39
|
-
await sendAndConfirmTransaction(connection, transaction, [payer, history],
|
38
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
40
39
|
return vaultPublicKey
|
41
40
|
}
|
42
41
|
|
43
42
|
export function withdrawVaultInstruction (
|
43
|
+
program: Program,
|
44
44
|
vaultSystemStatePublicKey: PublicKey,
|
45
45
|
payerPublicKey: PublicKey,
|
46
46
|
vaultPublickey: PublicKey,
|
@@ -48,7 +48,6 @@ export function withdrawVaultInstruction (
|
|
48
48
|
withdrawAmount: number,
|
49
49
|
chainlinkOverridePrice?: number
|
50
50
|
): TransactionInstruction {
|
51
|
-
const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
|
52
51
|
return program.instruction.withdrawVault(
|
53
52
|
new BN(withdrawAmount),
|
54
53
|
new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
|
@@ -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
|
|