hedge-web3 0.1.1 → 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/README.md +44 -0
- package/lib/index.js +2102 -0
- package/lib/index.js.map +1 -0
- package/lib/types/src/Constants.d.ts +13 -0
- package/lib/types/src/Constants.d.ts.map +1 -0
- package/lib/types/src/HedgeDecimal.d.ts +15 -0
- package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
- package/lib/types/src/StakingPools.d.ts +4 -0
- package/lib/types/src/StakingPools.d.ts.map +1 -0
- package/lib/types/src/Vaults.d.ts +9 -0
- package/lib/types/src/Vaults.d.ts.map +1 -0
- package/lib/types/src/idl/idl.d.ts +3 -0
- package/lib/types/src/idl/idl.d.ts.map +1 -0
- package/lib/types/src/index.d.ts +20 -0
- package/lib/types/src/index.d.ts.map +1 -0
- package/lib/types/src/instructions/createStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/createVault.d.ts +5 -0
- package/lib/types/src/instructions/createVault.d.ts.map +1 -0
- package/lib/types/src/instructions/depositStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/depositVault.d.ts +5 -0
- package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
- package/lib/types/src/instructions/liquidateVault.d.ts +5 -0
- package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
- package/lib/types/src/instructions/loanVault.d.ts +5 -0
- package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
- package/lib/types/src/instructions/redeemVault.d.ts +5 -0
- package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
- package/lib/types/src/instructions/repayVault.d.ts +5 -0
- package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
- package/lib/types/src/instructions/withdrawStakingPool.d.ts +5 -0
- package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
- package/lib/types/src/instructions/withdrawVault.d.ts +5 -0
- package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
- package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
- package/lib/types/src/state/LiquidationPosition.d.ts +25 -0
- package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
- package/lib/types/src/state/StakingPool.d.ts +16 -0
- package/lib/types/src/state/StakingPool.d.ts.map +1 -0
- package/lib/types/src/state/StakingPoolPosition.d.ts +20 -0
- package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
- package/lib/types/src/state/VaultAccount.d.ts +48 -0
- package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
- package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
- package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
- package/lib/types/src/utils/Errors.d.ts +2 -0
- package/lib/types/src/utils/Errors.d.ts.map +1 -0
- package/lib/types/tsconfig.base.tsbuildinfo +1 -0
- package/package.json +6 -5
- package/rollup.config.js +14 -8
- package/src/Constants.ts +56 -0
- package/src/HedgeDecimal.ts +15 -7
- package/src/StakingPools.ts +3 -0
- package/src/Vaults.ts +8 -0
- package/src/idl/idl.ts +1474 -0
- package/src/index.ts +19 -4
- package/src/instructions/createStakingPool.ts +64 -0
- package/src/instructions/createVault.ts +65 -0
- package/src/instructions/depositStakingPool.ts +60 -0
- package/src/instructions/depositVault.ts +60 -0
- package/src/instructions/liquidateVault.ts +66 -0
- package/src/instructions/loanVault.ts +78 -0
- package/src/instructions/redeemVault.ts +82 -0
- package/src/instructions/repayVault.ts +73 -0
- package/src/instructions/withdrawStakingPool.ts +69 -0
- package/src/instructions/withdrawVault.ts +65 -0
- package/src/state/LiquidationPoolEra.ts +29 -0
- package/src/state/LiquidationPoolState.ts +10 -0
- package/src/state/LiquidationPosition.ts +77 -0
- package/src/state/StakingPool.ts +38 -0
- package/src/state/StakingPoolPosition.ts +39 -0
- package/src/{accounts → state}/VaultAccount.ts +5 -6
- package/src/state/VaultHistoryEvent.ts +61 -0
- package/src/types/buffer-layout/index.d.ts +88 -0
- package/src/utils/Errors.ts +11 -0
- package/tsconfig.base.json +3 -3
- package/tsconfig.d.json +3 -1
- package/tsconfig.json +1 -1
- package/src/accounts/LiquidationPoolEra.ts +0 -16
- package/src/accounts/LiquidationPoolState.ts +0 -22
- package/src/accounts/LiquidationPosition.ts +0 -72
- package/src/accounts/StakingPool.ts +0 -31
- package/src/accounts/StakingPoolPosition.ts +0 -29
- package/src/accounts/VaultHistoryEvent.ts +0 -27
@@ -0,0 +1,65 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
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'
|
5
|
+
|
6
|
+
export async function withdrawVault (
|
7
|
+
program: Program,
|
8
|
+
provider: Provider,
|
9
|
+
payer: Signer,
|
10
|
+
vaultPublicKey: PublicKey,
|
11
|
+
withdrawAmount: number,
|
12
|
+
chainlinkOverridePrice?: number
|
13
|
+
): Promise<PublicKey> {
|
14
|
+
const [usdhMintPublickey] = await getUsdhMintPublicKey()
|
15
|
+
const USDH = new Token(
|
16
|
+
provider.connection,
|
17
|
+
usdhMintPublickey,
|
18
|
+
TOKEN_PROGRAM_ID,
|
19
|
+
payer
|
20
|
+
)
|
21
|
+
|
22
|
+
// Prep the user to get USDH back out at some point
|
23
|
+
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
|
24
|
+
|
25
|
+
const history = Keypair.generate()
|
26
|
+
const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
|
27
|
+
const transaction = new Transaction().add(
|
28
|
+
withdrawVaultInstruction(
|
29
|
+
program,
|
30
|
+
vaultSystemStatePublicKey,
|
31
|
+
payer.publicKey,
|
32
|
+
vaultPublicKey,
|
33
|
+
history.publicKey,
|
34
|
+
withdrawAmount,
|
35
|
+
chainlinkOverridePrice
|
36
|
+
)
|
37
|
+
)
|
38
|
+
await sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts)
|
39
|
+
return vaultPublicKey
|
40
|
+
}
|
41
|
+
|
42
|
+
export function withdrawVaultInstruction (
|
43
|
+
program: Program,
|
44
|
+
vaultSystemStatePublicKey: PublicKey,
|
45
|
+
payerPublicKey: PublicKey,
|
46
|
+
vaultPublickey: PublicKey,
|
47
|
+
historyPublicKey: PublicKey,
|
48
|
+
withdrawAmount: number,
|
49
|
+
chainlinkOverridePrice?: number
|
50
|
+
): TransactionInstruction {
|
51
|
+
return program.instruction.withdrawVault(
|
52
|
+
new BN(withdrawAmount),
|
53
|
+
new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
|
54
|
+
{
|
55
|
+
accounts: {
|
56
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
57
|
+
vaultAccount: vaultPublickey,
|
58
|
+
history: historyPublicKey,
|
59
|
+
vaultOwner: payerPublicKey,
|
60
|
+
chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
|
61
|
+
systemProgram: SystemProgram.programId
|
62
|
+
},
|
63
|
+
signers: []
|
64
|
+
})
|
65
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
import Decimal from 'decimal.js'
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Represents an on-chian pool era. In the event an era is depleted of deposits, a new era is
|
7
|
+
* created to maintain each users contribution to the pool.
|
8
|
+
*/
|
9
|
+
export class LiquidationPoolEra {
|
10
|
+
public totalDeposits: number
|
11
|
+
|
12
|
+
public product: Decimal
|
13
|
+
|
14
|
+
public sum: Decimal
|
15
|
+
|
16
|
+
public hedgeRewardsAccumulator: Decimal
|
17
|
+
|
18
|
+
public hedgeRewardsTimestamp: number
|
19
|
+
|
20
|
+
constructor (public liquidyPoolEra: any) {
|
21
|
+
this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
|
22
|
+
|
23
|
+
this.product = DecimalFromU128(liquidyPoolEra.productBytes)
|
24
|
+
this.sum = DecimalFromU128(liquidyPoolEra.sumBytes)
|
25
|
+
this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
|
26
|
+
|
27
|
+
this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
|
28
|
+
}
|
29
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
|
2
|
+
export class LiquidationPoolState {
|
3
|
+
public lifetimeDeposits: number
|
4
|
+
public hedgeInitRewardsTimestamp: number
|
5
|
+
constructor (public liquidationPoolState: any) {
|
6
|
+
this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber()
|
7
|
+
this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber()
|
8
|
+
// TODO Add the rest that are missing. Do we need them?
|
9
|
+
}
|
10
|
+
}
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
+
import Decimal from 'decimal.js'
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
4
|
+
import { LiquidationPoolEra } from './LiquidationPoolEra'
|
5
|
+
import { LiquidationPoolState } from './LiquidationPoolState'
|
6
|
+
|
7
|
+
export class LiquidationPosition {
|
8
|
+
public publicKey: PublicKey
|
9
|
+
public eraPublicKey: PublicKey
|
10
|
+
public era: LiquidationPoolEra
|
11
|
+
public liquidationPoolState: LiquidationPoolState
|
12
|
+
|
13
|
+
public ownerAccount: PublicKey
|
14
|
+
public deposit: number
|
15
|
+
public closedUsdh: number
|
16
|
+
public closedSol: number
|
17
|
+
public timestampOpened: Date
|
18
|
+
public timestampClosed: Date
|
19
|
+
|
20
|
+
public productSnapshot: Decimal
|
21
|
+
public sumSnapshot: Decimal
|
22
|
+
public hedgeRewardsSnapshot: Decimal
|
23
|
+
public open: boolean
|
24
|
+
|
25
|
+
constructor (poolPositionInfo: any, key: PublicKey, era: LiquidationPoolEra, liquidationPoolState: LiquidationPoolState) {
|
26
|
+
this.publicKey = key
|
27
|
+
this.eraPublicKey = poolPositionInfo.era
|
28
|
+
this.era = era
|
29
|
+
this.liquidationPoolState = liquidationPoolState
|
30
|
+
this.ownerAccount = poolPositionInfo.ownerAccount
|
31
|
+
this.deposit = poolPositionInfo.deposit.toNumber()
|
32
|
+
this.closedUsdh = poolPositionInfo.closedUsdh.toNumber()
|
33
|
+
this.closedSol = poolPositionInfo.closedSol.toNumber()
|
34
|
+
this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
|
35
|
+
this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
|
36
|
+
|
37
|
+
this.productSnapshot = DecimalFromU128(poolPositionInfo.productSnapshotBytes)
|
38
|
+
this.sumSnapshot = DecimalFromU128(poolPositionInfo.sumSnapshotBytes)
|
39
|
+
this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum)
|
40
|
+
this.open = poolPositionInfo.state.open !== undefined
|
41
|
+
}
|
42
|
+
|
43
|
+
public getUsdhAvailable (): Decimal {
|
44
|
+
return (this.era.product.div(this.productSnapshot)).mul(new Decimal(this.deposit))
|
45
|
+
}
|
46
|
+
|
47
|
+
public getSolAvailable (): Decimal {
|
48
|
+
return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal(this.deposit)).floor()
|
49
|
+
}
|
50
|
+
|
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
|
+
|
60
|
+
if (this.era.totalDeposits === 0) {
|
61
|
+
return new Decimal(0)
|
62
|
+
}
|
63
|
+
|
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
|
77
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
import { PublicKey } from '@solana/web3.js'
|
3
|
+
import Decimal from 'decimal.js'
|
4
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
5
|
+
|
6
|
+
export class StakingPool {
|
7
|
+
public publicKey: PublicKey
|
8
|
+
public deposits: number
|
9
|
+
lastTransactionTime: number
|
10
|
+
startTime: number
|
11
|
+
halfLifeInDays: number
|
12
|
+
totalHedgeReward: number
|
13
|
+
|
14
|
+
hedgeRewardAccumulator: Decimal
|
15
|
+
usdhFeeAccumulator: Decimal
|
16
|
+
solFeeAccumulator: Decimal
|
17
|
+
|
18
|
+
constructor (public poolInfo: any, publicKey: PublicKey) {
|
19
|
+
this.publicKey = publicKey
|
20
|
+
this.deposits = poolInfo.deposits.toNumber()
|
21
|
+
// this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
|
22
|
+
// this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
|
23
|
+
this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
|
24
|
+
this.startTime = poolInfo.startTime.toNumber()
|
25
|
+
this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
|
26
|
+
this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber()
|
27
|
+
|
28
|
+
this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
|
29
|
+
this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
|
30
|
+
this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
|
31
|
+
// this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
|
32
|
+
}
|
33
|
+
|
34
|
+
// updateFeeAccumulator () {
|
35
|
+
// this.feeAccumulator = (this.totalFeesNow - this.totalFeesPrevious) / this.deposits
|
36
|
+
// this.totalFeesPrevious = this.totalFeesNow
|
37
|
+
// }
|
38
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
import { PublicKey } from '@solana/web3.js'
|
3
|
+
import Decimal from 'decimal.js'
|
4
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
5
|
+
import { StakingPool } from './StakingPool'
|
6
|
+
|
7
|
+
export class StakingPoolPosition {
|
8
|
+
public publicKey: PublicKey
|
9
|
+
public pool: StakingPool
|
10
|
+
public owner: PublicKey
|
11
|
+
public deposited: number
|
12
|
+
public timestampOpened: number
|
13
|
+
public timestampClosed: number
|
14
|
+
public closedRewardedTokens: number
|
15
|
+
public startHedgeRewardAccumulator: Decimal
|
16
|
+
public startUsdhFeeAccumulator: Decimal
|
17
|
+
public startSolFeeAccumulator: Decimal
|
18
|
+
public open: boolean
|
19
|
+
|
20
|
+
constructor (public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
|
21
|
+
this.publicKey = key
|
22
|
+
this.pool = stakingPool
|
23
|
+
this.owner = poolPositionInfo.owner
|
24
|
+
this.deposited = poolPositionInfo.deposited.toNumber()
|
25
|
+
this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
|
26
|
+
this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
|
27
|
+
this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber()
|
28
|
+
|
29
|
+
this.startHedgeRewardAccumulator = DecimalFromU128(poolPositionInfo.startHedgeRewardAccumulator)
|
30
|
+
this.startUsdhFeeAccumulator = DecimalFromU128(poolPositionInfo.startUsdhFeeAccumulator)
|
31
|
+
this.startSolFeeAccumulator = DecimalFromU128(poolPositionInfo.startSolFeeAccumulator)
|
32
|
+
|
33
|
+
this.open = poolPositionInfo.state.open !== undefined
|
34
|
+
}
|
35
|
+
|
36
|
+
public getCurrentUsdhFeeReward (): Decimal {
|
37
|
+
return this.pool.usdhFeeAccumulator.minus(this.startUsdhFeeAccumulator).times(new Decimal(this.deposited))
|
38
|
+
}
|
39
|
+
}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
-
import
|
2
|
+
import Decimal from 'decimal.js'
|
3
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
3
4
|
|
4
5
|
/**
|
5
6
|
* A class that represents an on-chian vault.
|
@@ -18,7 +19,7 @@ export class VaultAccount {
|
|
18
19
|
deposited: number
|
19
20
|
|
20
21
|
/** The minimum collateral ratio this vault must maintain to not be subject to liquidation. */
|
21
|
-
minCollateralRatio:
|
22
|
+
minCollateralRatio: Decimal
|
22
23
|
|
23
24
|
/** The SOL/USD price at which this vault is subject to liquidation */
|
24
25
|
liquidationPrice: number
|
@@ -26,15 +27,13 @@ export class VaultAccount {
|
|
26
27
|
/** Current State of the vautl ("Open", "Closed", "Liquidated") */
|
27
28
|
vaultStatus: string
|
28
29
|
|
29
|
-
constructor (vault: any
|
30
|
-
, publicKey: PublicKey) {
|
30
|
+
constructor (vault: any, publicKey: PublicKey) {
|
31
31
|
this.publicKey = publicKey
|
32
32
|
this.vaultOwner = vault.vaultOwner
|
33
33
|
this.debt = vault.debt.toNumber()
|
34
34
|
this.deposited = vault.deposited.toNumber()
|
35
|
-
this.minCollateralRatio = vault.minCollateralRatio
|
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
|
|
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
import { PublicKey } from '@solana/web3.js'
|
3
|
+
import Decimal from 'decimal.js'
|
4
|
+
import { DecimalFromU128 } from '../HedgeDecimal'
|
5
|
+
|
6
|
+
export class VaultHistoryEvent {
|
7
|
+
publicKey: PublicKey
|
8
|
+
actorAccount: PublicKey
|
9
|
+
usdSolPrice: Decimal
|
10
|
+
usdhDebtBefore: number
|
11
|
+
usdhDebtAfter: number
|
12
|
+
solBalanceBefore: number
|
13
|
+
solBalanceAfter: number
|
14
|
+
minCollateralRatioBefore: Decimal
|
15
|
+
minCollateralRatioAfter: Decimal
|
16
|
+
vaultStateBefore: PublicKey
|
17
|
+
vaultStateAfter: PublicKey
|
18
|
+
timestamp: PublicKey
|
19
|
+
action: VaultHistoryAction
|
20
|
+
|
21
|
+
constructor (public account: any, publicKey: PublicKey) {
|
22
|
+
this.publicKey = publicKey
|
23
|
+
this.actorAccount = account.actorAccount
|
24
|
+
|
25
|
+
this.usdSolPrice = DecimalFromU128(account.usdSolPrice.toString())
|
26
|
+
|
27
|
+
this.usdhDebtBefore = account.usdhDebtBefore.toNumber()
|
28
|
+
this.usdhDebtAfter = account.usdhDebtAfter.toNumber()
|
29
|
+
this.solBalanceBefore = account.solBalanceBefore.toNumber()
|
30
|
+
this.solBalanceAfter = account.solBalanceAfter.toNumber()
|
31
|
+
this.minCollateralRatioBefore = DecimalFromU128(account.minCollateralRatioBefore)
|
32
|
+
this.minCollateralRatioAfter = DecimalFromU128(account.minCollateralRatioAfter)
|
33
|
+
this.vaultStateBefore = account.vaultStateBefore
|
34
|
+
this.vaultStateAfter = account.vaultStateAfter
|
35
|
+
|
36
|
+
this.timestamp = account.timestamp.toNumber()
|
37
|
+
this.action = account.action
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
export enum VaultStatus {
|
42
|
+
Open,
|
43
|
+
Closed,
|
44
|
+
Liquidated,
|
45
|
+
Distributed,
|
46
|
+
Redeemed,
|
47
|
+
}
|
48
|
+
|
49
|
+
export enum VaultHistoryAction {
|
50
|
+
Create,
|
51
|
+
Close,
|
52
|
+
Liquidate,
|
53
|
+
PartialLiquidate,
|
54
|
+
Distributed,
|
55
|
+
Redeem,
|
56
|
+
TransferOwnership,
|
57
|
+
Deposit,
|
58
|
+
Withdraw,
|
59
|
+
Loan,
|
60
|
+
RepayCredit,
|
61
|
+
}
|
@@ -0,0 +1,88 @@
|
|
1
|
+
|
2
|
+
declare module 'buffer-layout' {
|
3
|
+
// TODO: remove `any`.
|
4
|
+
export class Layout<T = any> {
|
5
|
+
span: number
|
6
|
+
property?: string
|
7
|
+
|
8
|
+
constructor (span: number, property?: string);
|
9
|
+
|
10
|
+
decode (b: Buffer | string, offset?: number): T;
|
11
|
+
encode (src: T, b: Buffer, offset?: number): number;
|
12
|
+
getSpan (b: Buffer, offset?: number): number;
|
13
|
+
replicate (name: string): this;
|
14
|
+
}
|
15
|
+
// TODO: remove any.
|
16
|
+
export class Structure<T = any> extends Layout<T> {
|
17
|
+
span: any
|
18
|
+
}
|
19
|
+
export function greedy (
|
20
|
+
elementSpan?: number,
|
21
|
+
property?: string,
|
22
|
+
): Layout<number>
|
23
|
+
export function offset<T> (
|
24
|
+
layout: Layout<T>,
|
25
|
+
offset?: number,
|
26
|
+
property?: string,
|
27
|
+
): Layout<T>
|
28
|
+
export function u8 (property?: string): Layout<number>
|
29
|
+
export function u16 (property?: string): Layout<number>
|
30
|
+
export function u24 (property?: string): Layout<number>
|
31
|
+
export function u32 (property?: string): Layout<number>
|
32
|
+
export function u40 (property?: string): Layout<number>
|
33
|
+
export function u48 (property?: string): Layout<number>
|
34
|
+
export function nu64 (property?: string): Layout<number>
|
35
|
+
export function u16be (property?: string): Layout<number>
|
36
|
+
export function u24be (property?: string): Layout<number>
|
37
|
+
export function u32be (property?: string): Layout<number>
|
38
|
+
export function u40be (property?: string): Layout<number>
|
39
|
+
export function u48be (property?: string): Layout<number>
|
40
|
+
export function nu64be (property?: string): Layout<number>
|
41
|
+
export function s8 (property?: string): Layout<number>
|
42
|
+
export function s16 (property?: string): Layout<number>
|
43
|
+
export function s24 (property?: string): Layout<number>
|
44
|
+
export function s32 (property?: string): Layout<number>
|
45
|
+
export function s40 (property?: string): Layout<number>
|
46
|
+
export function s48 (property?: string): Layout<number>
|
47
|
+
export function ns64 (property?: string): Layout<number>
|
48
|
+
export function s16be (property?: string): Layout<number>
|
49
|
+
export function s24be (property?: string): Layout<number>
|
50
|
+
export function s32be (property?: string): Layout<number>
|
51
|
+
export function s40be (property?: string): Layout<number>
|
52
|
+
export function s48be (property?: string): Layout<number>
|
53
|
+
export function ns64be (property?: string): Layout<number>
|
54
|
+
export function f32 (property?: string): Layout<number>
|
55
|
+
export function f32be (property?: string): Layout<number>
|
56
|
+
export function f64 (property?: string): Layout<number>
|
57
|
+
export function f64be (property?: string): Layout<number>
|
58
|
+
export function struct<T> (
|
59
|
+
fields: Array<Layout<any>>,
|
60
|
+
property?: string,
|
61
|
+
decodePrefixes?: boolean,
|
62
|
+
): Layout<T>
|
63
|
+
export function bits (
|
64
|
+
word: Layout<number>,
|
65
|
+
msb?: boolean,
|
66
|
+
property?: string,
|
67
|
+
): any
|
68
|
+
export function seq<T> (
|
69
|
+
elementLayout: Layout<T>,
|
70
|
+
count: number | Layout<number>,
|
71
|
+
property?: string,
|
72
|
+
): Layout<T[]>
|
73
|
+
export function union (
|
74
|
+
discr: Layout<any>,
|
75
|
+
defaultLayout?: any,
|
76
|
+
property?: string,
|
77
|
+
): any
|
78
|
+
export function unionLayoutDiscriminator (
|
79
|
+
layout: Layout<any>,
|
80
|
+
property?: string,
|
81
|
+
): any
|
82
|
+
export function blob (
|
83
|
+
length: number | Layout<number>,
|
84
|
+
property?: string,
|
85
|
+
): Layout<Buffer>
|
86
|
+
export function cstr (property?: string): Layout<string>
|
87
|
+
export function utf8 (maxSpan: number, property?: string): Layout<string>
|
88
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { parseIdlErrors, ProgramError } from '@project-serum/anchor'
|
2
|
+
import { vaultIdl } from '../idl/idl'
|
3
|
+
|
4
|
+
export function parseAnchorErrors (error: any): void {
|
5
|
+
const idlErrors = parseIdlErrors(vaultIdl)
|
6
|
+
const parsedError = ProgramError.parse(error, idlErrors)
|
7
|
+
if (parsedError !== null) {
|
8
|
+
throw parsedError
|
9
|
+
}
|
10
|
+
throw error
|
11
|
+
}
|
package/tsconfig.base.json
CHANGED
@@ -11,8 +11,6 @@
|
|
11
11
|
"emitDecoratorMetadata": true,
|
12
12
|
"noImplicitAny": false,
|
13
13
|
"strictNullChecks": true,
|
14
|
-
"esModuleInterop": true,
|
15
|
-
"resolveJsonModule": true,
|
16
14
|
"composite": true,
|
17
15
|
"baseUrl": ".",
|
18
16
|
"typeRoots": [
|
@@ -23,6 +21,8 @@
|
|
23
21
|
"@solana/web3.js": [
|
24
22
|
"./node_modules/@solana/web3.js/lib"
|
25
23
|
]
|
26
|
-
}
|
24
|
+
},
|
25
|
+
"resolveJsonModule": true,
|
26
|
+
"esModuleInterop": true
|
27
27
|
}
|
28
28
|
}
|
package/tsconfig.d.json
CHANGED
package/tsconfig.json
CHANGED
@@ -1,16 +0,0 @@
|
|
1
|
-
// import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
-
// import Decimal from 'decimal.js'
|
3
|
-
|
4
|
-
// import { WAD } from './HedgeConstants'
|
5
|
-
|
6
|
-
export class LiquidationPoolEra {
|
7
|
-
constructor (public liquidyPoolEra: any) {
|
8
|
-
// this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber()
|
9
|
-
|
10
|
-
// this.product = byteArrayToFloat64(liquidyPoolEra.productBytes)
|
11
|
-
// this.sum = byteArrayToFloat64(liquidyPoolEra.sumBytes)
|
12
|
-
// this.hedgeRewardsAccumulator = byteArrayToFloat64(liquidyPoolEra.hedgeRewardsAccumulatorBytes)
|
13
|
-
|
14
|
-
// this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber()
|
15
|
-
}
|
16
|
-
}
|
@@ -1,22 +0,0 @@
|
|
1
|
-
// import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
-
// import Decimal from 'decimal.js'
|
3
|
-
|
4
|
-
// import { WAD } from './HedgeConstants'
|
5
|
-
|
6
|
-
export class LiquidationPoolState {
|
7
|
-
constructor (public liquidationPoolState: any) {
|
8
|
-
// this.lifetimeDeposits = liquidationPoolState.lifetimeDeposits.toNumber()
|
9
|
-
// this.hedgeInitRewardsTimestamp = liquidationPoolState.hedgeInitRewardsTimestamp.toNumber()
|
10
|
-
// // TODO Add the rest that are missing. Do we need them?
|
11
|
-
}
|
12
|
-
|
13
|
-
// getCurrentHedgeRewardsPerDay () {
|
14
|
-
// const dailyRate = hedgeRewardsDecay(
|
15
|
-
// 2000000.0 * LAMPORTS_PER_SOL,
|
16
|
-
// this.hedgeInitRewardsTimestamp * 1000,
|
17
|
-
// Date.now(),
|
18
|
-
// Date.now() + (24 * 60 * 60 * 1000),
|
19
|
-
// 365 * 1000)
|
20
|
-
// return dailyRate / LAMPORTS_PER_SOL
|
21
|
-
// }
|
22
|
-
}
|
@@ -1,72 +0,0 @@
|
|
1
|
-
// import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
-
// import Decimal from 'decimal.js'
|
3
|
-
// import { LiquidationPoolEra, LiquidationPoolState } from '../Accounts';
|
4
|
-
|
5
|
-
import { PublicKey } from '@solana/web3.js'
|
6
|
-
import { LiquidationPoolEra } from './LiquidationPoolEra'
|
7
|
-
import { LiquidationPoolState } from './LiquidationPoolState'
|
8
|
-
|
9
|
-
// import { WAD } from './HedgeConstants'
|
10
|
-
|
11
|
-
export class LiquidationPosition {
|
12
|
-
// publicKey: PublicKey;
|
13
|
-
// eraPublicKey: PublicKey;
|
14
|
-
// era: LiquidationPoolEra;
|
15
|
-
// liquidationPoolState: LiquidationPoolEra;
|
16
|
-
// ownerAccount: PublicKey;
|
17
|
-
// deposit: Decimal
|
18
|
-
// closedUsdh: Decimal
|
19
|
-
// closedSol: Decimal
|
20
|
-
// timestampOpened: Date
|
21
|
-
// timestampClosed: Date
|
22
|
-
|
23
|
-
// productSnapshot: Decimal
|
24
|
-
// sumSnapshot: Decimal
|
25
|
-
// hedgeRewardsSnapshot: Decimal
|
26
|
-
// open: Boolean
|
27
|
-
|
28
|
-
constructor (public poolPositionInfo: any, key: PublicKey, era: LiquidationPoolEra, liquidationPoolState: LiquidationPoolState) {
|
29
|
-
// this.publicKey = key
|
30
|
-
// this.eraPublicKey = poolPositionInfo.era
|
31
|
-
// this.era = era
|
32
|
-
// this.liquidationPoolState = liquidationPoolState
|
33
|
-
// this.ownerAccount = poolPositionInfo.ownerAccount
|
34
|
-
// this.deposit = poolPositionInfo.deposit.toNumber()
|
35
|
-
// this.closedUsdh = poolPositionInfo.closedUsdh.toNumber()
|
36
|
-
// this.closedSol = poolPositionInfo.closedSol.toNumber()
|
37
|
-
// this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
|
38
|
-
// this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
|
39
|
-
|
40
|
-
// this.productSnapshot = new Decimal(poolPositionInfo.productSnapshotBytes.toString())
|
41
|
-
// this.sumSnapshot = byteArrayToFloat64(poolPositionInfo.sumSnapshotBytes)
|
42
|
-
// this.hedgeRewardsSnapshot = byteArrayToFloat64(poolPositionInfo.hedgeRewardsSnapshotAccum)
|
43
|
-
// this.open = !!poolPositionInfo.state.open
|
44
|
-
}
|
45
|
-
|
46
|
-
// getUsdhAvailable () {
|
47
|
-
// return (this.deposit * (this.era.product / this.productSnapshot))
|
48
|
-
// }
|
49
|
-
|
50
|
-
// getSolAvailable () {
|
51
|
-
// return Math.floor(this.deposit * ((this.era.sum - this.sumSnapshot) / this.productSnapshot))
|
52
|
-
// }
|
53
|
-
|
54
|
-
// getHedgeAvailable () {
|
55
|
-
// const LiquidationPoolTotalSupply = 2000000.0 * LAMPORTS_PER_SOL
|
56
|
-
// const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(
|
57
|
-
// LiquidationPoolTotalSupply,
|
58
|
-
// this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000,
|
59
|
-
// this.era.hedgeRewardsTimestamp * 1000,
|
60
|
-
// Date.now(),
|
61
|
-
// 365 * 1000)
|
62
|
-
|
63
|
-
// if (this.era.totalDeposits === 0) {
|
64
|
-
// return 0
|
65
|
-
// }
|
66
|
-
|
67
|
-
// const rewardsPerToken = hedgeRewardsSinceLastUpdate / this.era.totalDeposits * this.era.product
|
68
|
-
// const newAccumulator = this.era.hedgeRewardsAccumulator + rewardsPerToken
|
69
|
-
// const hedgeAvailable = (newAccumulator - this.hedgeRewardsSnapshot) * this.deposit / this.productSnapshot
|
70
|
-
// return hedgeAvailable
|
71
|
-
// }
|
72
|
-
}
|
@@ -1,31 +0,0 @@
|
|
1
|
-
// import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
|
2
|
-
// import Decimal from 'decimal.js'
|
3
|
-
|
4
|
-
import { PublicKey } from '@solana/web3.js';
|
5
|
-
|
6
|
-
// import { WAD } from './HedgeConstants'
|
7
|
-
|
8
|
-
export class StakingPool {
|
9
|
-
constructor (public poolInfo: any, key: PublicKey) {
|
10
|
-
// this.publicKey = key
|
11
|
-
// this.deposits = poolInfo.deposits.toNumber()
|
12
|
-
// // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
|
13
|
-
// // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
|
14
|
-
// this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
|
15
|
-
// this.startTime = poolInfo.startTime.toNumber()
|
16
|
-
// this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
|
17
|
-
// this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber()
|
18
|
-
|
19
|
-
// this.hedgeRewardAccumulator = byteArrayToFloat64(poolInfo.hedgeRewardAccumulator)
|
20
|
-
// this.usdhFeeAccumulator = byteArrayToFloat64(poolInfo.usdhFeeAccumulator)
|
21
|
-
// this.solFeeAccumulator = byteArrayToFloat64(poolInfo.solFeeAccumulator)
|
22
|
-
// this.currentRewardsPerDay = byteArrayToFloat64(poolInfo.currentRewardsPerDay)
|
23
|
-
|
24
|
-
// // "stakedTokenMint": { missing
|
25
|
-
}
|
26
|
-
|
27
|
-
// updateFeeAccumulator () {
|
28
|
-
// this.feeAccumulator = (this.totalFeesNow - this.totalFeesPrevious) / this.deposits
|
29
|
-
// this.totalFeesPrevious = this.totalFeesNow
|
30
|
-
// }
|
31
|
-
}
|