hedge-web3 0.1.0 → 0.1.4

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.
Files changed (90) hide show
  1. package/.github/workflows/npm-publish.yml +34 -0
  2. package/README.md +44 -0
  3. package/lib/index.js +1976 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/src/Constants.d.ts +13 -0
  6. package/lib/types/src/Constants.d.ts.map +1 -0
  7. package/lib/types/src/HedgeDecimal.d.ts +15 -0
  8. package/lib/types/src/HedgeDecimal.d.ts.map +1 -0
  9. package/lib/types/src/StakingPools.d.ts +4 -0
  10. package/lib/types/src/StakingPools.d.ts.map +1 -0
  11. package/lib/types/src/Vaults.d.ts +9 -0
  12. package/lib/types/src/Vaults.d.ts.map +1 -0
  13. package/lib/types/src/idl/idl.d.ts +3 -0
  14. package/lib/types/src/idl/idl.d.ts.map +1 -0
  15. package/lib/types/src/index.d.ts +5 -0
  16. package/lib/types/src/index.d.ts.map +1 -0
  17. package/lib/types/src/instructions/createStakingPool.d.ts +4 -0
  18. package/lib/types/src/instructions/createStakingPool.d.ts.map +1 -0
  19. package/lib/types/src/instructions/createVault.d.ts +4 -0
  20. package/lib/types/src/instructions/createVault.d.ts.map +1 -0
  21. package/lib/types/src/instructions/depositStakingPool.d.ts +4 -0
  22. package/lib/types/src/instructions/depositStakingPool.d.ts.map +1 -0
  23. package/lib/types/src/instructions/depositVault.d.ts +4 -0
  24. package/lib/types/src/instructions/depositVault.d.ts.map +1 -0
  25. package/lib/types/src/instructions/liquidateVault.d.ts +4 -0
  26. package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -0
  27. package/lib/types/src/instructions/loanVault.d.ts +4 -0
  28. package/lib/types/src/instructions/loanVault.d.ts.map +1 -0
  29. package/lib/types/src/instructions/redeemVault.d.ts +4 -0
  30. package/lib/types/src/instructions/redeemVault.d.ts.map +1 -0
  31. package/lib/types/src/instructions/repayVault.d.ts +4 -0
  32. package/lib/types/src/instructions/repayVault.d.ts.map +1 -0
  33. package/lib/types/src/instructions/withdrawStakingPool.d.ts +4 -0
  34. package/lib/types/src/instructions/withdrawStakingPool.d.ts.map +1 -0
  35. package/lib/types/src/instructions/withdrawVault.d.ts +4 -0
  36. package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -0
  37. package/lib/types/src/state/LiquidationPoolEra.d.ts +15 -0
  38. package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -0
  39. package/lib/types/src/state/LiquidationPoolState.d.ts +7 -0
  40. package/lib/types/src/state/LiquidationPoolState.d.ts.map +1 -0
  41. package/lib/types/src/state/LiquidationPosition.d.ts +24 -0
  42. package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -0
  43. package/lib/types/src/state/StakingPool.d.ts +17 -0
  44. package/lib/types/src/state/StakingPool.d.ts.map +1 -0
  45. package/lib/types/src/state/StakingPoolPosition.d.ts +19 -0
  46. package/lib/types/src/state/StakingPoolPosition.d.ts.map +1 -0
  47. package/lib/types/src/state/VaultAccount.d.ts +48 -0
  48. package/lib/types/src/state/VaultAccount.d.ts.map +1 -0
  49. package/lib/types/src/state/VaultHistoryEvent.d.ts +40 -0
  50. package/lib/types/src/state/VaultHistoryEvent.d.ts.map +1 -0
  51. package/lib/types/src/utils/Errors.d.ts +2 -0
  52. package/lib/types/src/utils/Errors.d.ts.map +1 -0
  53. package/lib/types/tsconfig.base.tsbuildinfo +1 -0
  54. package/package.json +6 -6
  55. package/rollup.config.js +14 -8
  56. package/src/Constants.ts +56 -0
  57. package/src/HedgeDecimal.ts +15 -7
  58. package/src/StakingPools.ts +3 -0
  59. package/src/Vaults.ts +8 -0
  60. package/src/idl/idl.ts +1474 -0
  61. package/src/index.ts +3 -5
  62. package/src/instructions/createStakingPool.ts +65 -0
  63. package/src/instructions/createVault.ts +66 -0
  64. package/src/instructions/depositStakingPool.ts +61 -0
  65. package/src/instructions/depositVault.ts +61 -0
  66. package/src/instructions/liquidateVault.ts +67 -0
  67. package/src/instructions/loanVault.ts +79 -0
  68. package/src/instructions/redeemVault.ts +83 -0
  69. package/src/instructions/repayVault.ts +74 -0
  70. package/src/instructions/withdrawStakingPool.ts +70 -0
  71. package/src/instructions/withdrawVault.ts +66 -0
  72. package/src/state/LiquidationPoolEra.ts +29 -0
  73. package/src/state/LiquidationPoolState.ts +10 -0
  74. package/src/state/LiquidationPosition.ts +69 -0
  75. package/src/state/StakingPool.ts +39 -0
  76. package/src/state/StakingPoolPosition.ts +40 -0
  77. package/src/state/VaultAccount.ts +77 -0
  78. package/src/state/VaultHistoryEvent.ts +61 -0
  79. package/src/types/buffer-layout/index.d.ts +88 -0
  80. package/src/utils/Errors.ts +11 -0
  81. package/tsconfig.base.json +3 -3
  82. package/tsconfig.d.json +3 -1
  83. package/tsconfig.json +1 -1
  84. package/src/accounts/LiquidationPoolEra.ts +0 -16
  85. package/src/accounts/LiquidationPoolState.ts +0 -22
  86. package/src/accounts/LiquidationPosition.ts +0 -72
  87. package/src/accounts/StakingPool.ts +0 -31
  88. package/src/accounts/StakingPoolPosition.ts +0 -29
  89. package/src/accounts/VaultAccount.ts +0 -40
  90. package/src/accounts/VaultHistoryEvent.ts +0 -27
@@ -0,0 +1,66 @@
1
+ import { BN, Program } from '@project-serum/anchor'
2
+ import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { ConfirmOptions, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { CHAINLINK_SOL_USD_PUBLICKEY, getUsdhMintPublicKey, getVaultSystemStatePublicKey, HEDGE_PROGRAM_ID } from '../Constants'
5
+
6
+ import { vaultIdl } from '../idl/idl'
7
+
8
+ export async function withdrawVault (
9
+ connection: Connection,
10
+ payer: Signer,
11
+ vaultPublicKey: PublicKey,
12
+ withdrawAmount: number,
13
+ chainlinkOverridePrice?: number,
14
+ confirmOptions?: ConfirmOptions
15
+ ): Promise<PublicKey> {
16
+ const [usdhMintPublickey] = await getUsdhMintPublicKey()
17
+ const USDH = new Token(
18
+ connection,
19
+ usdhMintPublickey,
20
+ TOKEN_PROGRAM_ID,
21
+ payer
22
+ )
23
+
24
+ // Prep the user to get USDH back out at some point
25
+ await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey)
26
+
27
+ const history = Keypair.generate()
28
+ const [vaultSystemStatePublicKey] = await getVaultSystemStatePublicKey()
29
+ const transaction = new Transaction().add(
30
+ withdrawVaultInstruction(
31
+ vaultSystemStatePublicKey,
32
+ payer.publicKey,
33
+ vaultPublicKey,
34
+ history.publicKey,
35
+ withdrawAmount,
36
+ chainlinkOverridePrice
37
+ )
38
+ )
39
+ await sendAndConfirmTransaction(connection, transaction, [payer, history], confirmOptions)
40
+ return vaultPublicKey
41
+ }
42
+
43
+ export function withdrawVaultInstruction (
44
+ vaultSystemStatePublicKey: PublicKey,
45
+ payerPublicKey: PublicKey,
46
+ vaultPublickey: PublicKey,
47
+ historyPublicKey: PublicKey,
48
+ withdrawAmount: number,
49
+ chainlinkOverridePrice?: number
50
+ ): TransactionInstruction {
51
+ const program = new Program(vaultIdl, HEDGE_PROGRAM_ID)
52
+ return program.instruction.withdrawVault(
53
+ new BN(withdrawAmount),
54
+ new BN(chainlinkOverridePrice ?? 200 * LAMPORTS_PER_SOL),
55
+ {
56
+ accounts: {
57
+ vaultSystemState: vaultSystemStatePublicKey,
58
+ vaultAccount: vaultPublickey,
59
+ history: historyPublicKey,
60
+ vaultOwner: payerPublicKey,
61
+ chainlinkFeedAccount: CHAINLINK_SOL_USD_PUBLICKEY,
62
+ systemProgram: SystemProgram.programId
63
+ },
64
+ signers: []
65
+ })
66
+ }
@@ -0,0 +1,29 @@
1
+
2
+ import Decimal from 'decimal.js'
3
+ import { DecimalFromU128 } from '..'
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
+ product: Decimal
13
+
14
+ sum: Decimal
15
+
16
+ hedgeRewardsAccumulator: Decimal
17
+
18
+ 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,69 @@
1
+ import { PublicKey } from '@solana/web3.js'
2
+ import Decimal from 'decimal.js'
3
+ import { DecimalFromU128 } from '..'
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
+ // getHedgeAvailable () {
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 0
62
+ // }
63
+
64
+ // const rewardsPerToken = hedgeRewardsSinceLastUpdate / this.era.totalDeposits * this.era.product
65
+ // const newAccumulator = this.era.hedgeRewardsAccumulator + rewardsPerToken
66
+ // const hedgeAvailable = (newAccumulator - this.hedgeRewardsSnapshot) * this.deposit / this.productSnapshot
67
+ // return hedgeAvailable
68
+ // }
69
+ }
@@ -0,0 +1,39 @@
1
+
2
+ import { PublicKey } from '@solana/web3.js'
3
+ import Decimal from 'decimal.js'
4
+ import { DecimalFromU128 } from '..'
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
+ currentRewardsPerDay: Decimal
18
+
19
+ constructor (public poolInfo: any, key: PublicKey) {
20
+ this.publicKey = key
21
+ this.deposits = poolInfo.deposits.toNumber()
22
+ // this.totalFeesNow = poolInfo.totalFeesNow.toNumber()
23
+ // this.totalFeesPrevious = poolInfo.totalFeesPrevious.toNumber()
24
+ this.lastTransactionTime = poolInfo.lastTransactionTime.toNumber()
25
+ this.startTime = poolInfo.startTime.toNumber()
26
+ this.halfLifeInDays = poolInfo.halfLifeInDays.toNumber()
27
+ this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber()
28
+
29
+ this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
30
+ this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
31
+ this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
32
+ this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
33
+ }
34
+
35
+ // updateFeeAccumulator () {
36
+ // this.feeAccumulator = (this.totalFeesNow - this.totalFeesPrevious) / this.deposits
37
+ // this.totalFeesPrevious = this.totalFeesNow
38
+ // }
39
+ }
@@ -0,0 +1,40 @@
1
+
2
+ import { PublicKey } from '@solana/web3.js'
3
+ import Decimal from 'decimal.js'
4
+ import { DecimalFromU128 } from '..'
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
+ // getCurrentUsdhFeeReward () {
37
+ // const feesAccured = (this.pool.usdhFeeAccumulator - this.startUsdhFeeAccumulator) * this.deposited
38
+ // return feesAccured
39
+ // }
40
+ }
@@ -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
+
5
+ /**
6
+ * A class that represents an on-chian vault.
7
+ */
8
+ export class VaultAccount {
9
+ /** The public key of this vault. */
10
+ publicKey: PublicKey
11
+
12
+ /** The public key of the vault owner. */
13
+ vaultOwner: PublicKey
14
+
15
+ /** The outstanding debt of the vault (in USDH Lamports). */
16
+ debt: number
17
+
18
+ /** The deposited collateral of the vault (in SOL Lamports). */
19
+ deposited: number
20
+
21
+ /** The minimum collateral ratio this vault must maintain to not be subject to liquidation. */
22
+ minCollateralRatio: Decimal
23
+
24
+ /** The SOL/USD price at which this vault is subject to liquidation */
25
+ liquidationPrice: number
26
+
27
+ /** Current State of the vautl ("Open", "Closed", "Liquidated") */
28
+ vaultStatus: string
29
+
30
+ constructor (vault: any, publicKey: PublicKey) {
31
+ this.publicKey = publicKey
32
+ this.vaultOwner = vault.vaultOwner
33
+ this.debt = vault.debt.toNumber()
34
+ this.deposited = vault.deposited.toNumber()
35
+ this.minCollateralRatio = DecimalFromU128(vault.minCollateralRatio)
36
+ this.liquidationPrice = vault.liquidationPrice.toNumber()
37
+ this.minCollateralRatio = vault.minCollateralRatio.toNumber()
38
+ this.vaultStatus = Object.keys(vault.vaultStatus)[0]
39
+ }
40
+
41
+ /**
42
+ * Check if some `PublicKey` is the owner
43
+ *
44
+ * @param publicKey the publicKey to check against the vault owner
45
+ * @returns true if publicKey matches the owner publicKey
46
+ */
47
+ public isOwnedBy (publicKey: PublicKey): boolean {
48
+ return publicKey.toString() === this.vaultOwner.toString()
49
+ }
50
+
51
+ /**
52
+ * Get the collateral value in SOL
53
+ *
54
+ * @returns collateral value in SOL
55
+ */
56
+ public inSol (): number {
57
+ return this.deposited / LAMPORTS_PER_SOL
58
+ }
59
+
60
+ /**
61
+ * Get the debt value in USDH
62
+ *
63
+ * @returns debt value in USDH
64
+ */
65
+ public inUsd (): number {
66
+ return this.debt / LAMPORTS_PER_SOL
67
+ }
68
+
69
+ /**
70
+ * Pretty print the vault publickey for easy display
71
+ *
72
+ * @returns example: `1b6ca...azy71s`
73
+ */
74
+ public toDisplayString (): string {
75
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`
76
+ }
77
+ }
@@ -0,0 +1,61 @@
1
+
2
+ import { PublicKey } from '@solana/web3.js'
3
+ import Decimal from 'decimal.js'
4
+ import { DecimalFromU128 } from '..'
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
+ }
@@ -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
@@ -2,7 +2,9 @@
2
2
  "extends": "./tsconfig.json",
3
3
  "compilerOptions": {
4
4
  "emitDeclarationOnly": true,
5
- "stripInternal": true
5
+ "stripInternal": true,
6
+ "resolveJsonModule": true,
7
+ "esModuleInterop": true,
6
8
  },
7
9
  "include": [
8
10
  "./src/**/*"
package/tsconfig.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "isolatedModules": true,
15
15
  "baseUrl": "src",
16
16
  "noFallthroughCasesInSwitch": true,
17
- "noImplicitReturns": true
17
+ "noImplicitReturns": true,
18
18
  },
19
19
  "include": ["src"]
20
20
  }
@@ -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
- }