hedge-web3 0.1.0

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/jsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es2020",
5
+ "jsx": "preserve",
6
+ "strictFunctionTypes": true
7
+ },
8
+ "exclude": [
9
+ "node_modules",
10
+ "**/node_modules/*"
11
+ ]
12
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "hedge-web3",
3
+ "version": "0.1.0",
4
+ "description": "Hedge Javascript API",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "npm run clean; rollup -c",
8
+ "clean": "rimraf ./lib",
9
+ "dev": "rollup -c",
10
+ "deploy-docs": "npm run docs; gh-pages -d doc",
11
+ "docs": "set -ex; typedoc --treatWarningsAsErrors",
12
+ "test": "echo \"Error: no test specified\" && exit 1"
13
+ },
14
+ "author": "Chris Coudron <coudron@gmail.com>",
15
+ "license": "ISC",
16
+ "devDependencies": {
17
+ "@rollup/plugin-typescript": "^8.3.0",
18
+ "rollup": "^2.62.0",
19
+ "ts-standard": "^11.0.0",
20
+ "typedoc": "^0.22.10"
21
+ },
22
+ "dependencies": {
23
+ "@project-serum/anchor": "^0.19.1-beta.1",
24
+ "@solana/web3.js": "^1.31.0",
25
+ "decimal.js": "^10.3.1"
26
+ }
27
+ }
@@ -0,0 +1,34 @@
1
+ import typescript from "@rollup/plugin-typescript";
2
+
3
+ export default {
4
+ input: 'src/index.ts',
5
+ output: {
6
+ dir: 'lib',
7
+ format: 'cjs',
8
+ sourcemap: true
9
+ },
10
+ plugins: [
11
+ // commonjs(),
12
+ // nodeResolve({
13
+ // browser: true,
14
+ // extensions: [".js", ".ts"],
15
+ // dedupe: ["bn.js", "buffer"],
16
+ // preferBuiltins: false,
17
+ // }),
18
+ typescript({
19
+ tsconfig: "./tsconfig.base.json",
20
+ moduleResolution: "node",
21
+ outDir: "lib/types",
22
+ target: "es2019",
23
+ outputToFilesystem: false,
24
+ }),
25
+ // replace({
26
+ // preventAssignment: true,
27
+ // values: {
28
+ // "process.env.NODE_ENV": JSON.stringify(env),
29
+ // "process.env.BROWSER": JSON.stringify(true),
30
+ // },
31
+ // }),
32
+ // terser(),
33
+ ],
34
+ };
@@ -0,0 +1,10 @@
1
+ import Decimal from 'decimal.js'
2
+
3
+ export class HedgeDecimal {
4
+ public value: Decimal
5
+
6
+ constructor (val: Number) {
7
+ this.value = new Decimal(val.toString())
8
+ this.value = this.value.div(new Decimal('1e+24'))
9
+ }
10
+ }
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,22 @@
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
+ }
@@ -0,0 +1,72 @@
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
+ }
@@ -0,0 +1,31 @@
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
+ }
@@ -0,0 +1,29 @@
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
+ import { StakingPool } from './StakingPool'
6
+
7
+ // import { WAD } from './HedgeConstants'
8
+
9
+ export class StakingPoolPosition {
10
+ constructor (public poolPositionInfo: any, key: PublicKey, stakingPool: StakingPool) {
11
+ // this.open = !!poolPositionInfo.state.open
12
+ // this.publicKey = key
13
+ // this.pool = stakingPool
14
+ // this.owner = poolPositionInfo.owner
15
+ // this.deposited = poolPositionInfo.deposited.toNumber()
16
+ // this.timestampOpened = poolPositionInfo.timestampOpened.toNumber()
17
+ // this.timestampClosed = poolPositionInfo.timestampClosed.toNumber()
18
+ // this.closedRewardedTokens = poolPositionInfo.closedRewardedTokens.toNumber()
19
+
20
+ // this.startHedgeRewardAccumulator = byteArrayToFloat64(poolPositionInfo.startHedgeRewardAccumulator)
21
+ // this.startUsdhFeeAccumulator = byteArrayToFloat64(poolPositionInfo.startUsdhFeeAccumulator)
22
+ // this.startSolFeeAccumulator = byteArrayToFloat64(poolPositionInfo.startSolFeeAccumulator)
23
+ }
24
+
25
+ // getCurrentUsdhFeeReward () {
26
+ // const feesAccured = (this.pool.usdhFeeAccumulator - this.startUsdhFeeAccumulator) * this.deposited
27
+ // return feesAccured
28
+ // }
29
+ }
@@ -0,0 +1,40 @@
1
+ import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'
2
+ import { HedgeDecimal } from '../HedgeDecimal'
3
+
4
+ export class VaultAccount {
5
+ publicKey: PublicKey
6
+ vaultOwner: PublicKey
7
+ debt: number
8
+ deposited: number
9
+ minCollateralRatio: HedgeDecimal
10
+ liquidationPrice: number
11
+ vaultStatus: string
12
+
13
+ constructor (vault: any
14
+ , publicKey: PublicKey) {
15
+ this.publicKey = publicKey
16
+ this.vaultOwner = vault.vaultOwner
17
+ this.debt = vault.debt.toNumber()
18
+ this.deposited = vault.deposited.toNumber()
19
+ this.minCollateralRatio = vault.minCollateralRatio.toNumber()
20
+ this.liquidationPrice = vault.liquidationPrice.toNumber()
21
+ this.minCollateralRatio = vault.minCollateralRatio.toNumber()
22
+ this.vaultStatus = Object.keys(vault.vaultStatus)[0]
23
+ }
24
+
25
+ public isOwnedBy (publicKey: PublicKey): boolean {
26
+ return publicKey.toString() === this.vaultOwner.toString()
27
+ }
28
+
29
+ public inSol (): number {
30
+ return this.deposited / LAMPORTS_PER_SOL
31
+ }
32
+
33
+ public inUsd (): number {
34
+ return this.debt / LAMPORTS_PER_SOL
35
+ }
36
+
37
+ public toDisplayString (): string {
38
+ return `${this.publicKey.toString().substring(0, 6)}...${this.publicKey.toString().substring(this.publicKey.toString().length - 6)}`
39
+ }
40
+ }
@@ -0,0 +1,27 @@
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 VaultHistoryEvent {
9
+ constructor (public account: any, publicKey: PublicKey) {
10
+ // this.publicKey = publicKey
11
+ // this.actorAccount = account.actorAccount
12
+
13
+ // this.usdSolPrice = (new Decimal(account.usdSolPrice.toString())).dividedBy(WAD)
14
+
15
+ // this.usdhDebtBefore = account.usdhDebtBefore.toNumber()
16
+ // this.usdhDebtAfter = account.usdhDebtAfter.toNumber()
17
+ // this.solBalanceBefore = account.solBalanceBefore.toNumber()
18
+ // this.solBalanceAfter = account.solBalanceAfter.toNumber()
19
+ // this.minCollateralRatioBefore = account.minCollateralRatioBefore.toNumber()
20
+ // this.minCollateralRatioAfter = account.minCollateralRatioAfter.toNumber()
21
+ // this.vaultStateBefore = Object.keys(account.vaultStateBefore)[0]
22
+ // this.vaultStateAfter = Object.keys(account.vaultStateAfter)[0]
23
+
24
+ // this.timestamp = account.timestamp.toNumber()
25
+ // this.action = Object.keys(account.action)[0]
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+
2
+ export function foo (input: String): void {
3
+ console.log('Input', input)
4
+ }
5
+
6
+ export * from './accounts/VaultAccount'
7
+ export * from './HedgeDecimal'
@@ -0,0 +1,28 @@
1
+ {
2
+ "include": [
3
+ "./src/**/*"
4
+ ],
5
+ "compilerOptions": {
6
+ "sourceMap": true,
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "allowSyntheticDefaultImports": true,
10
+ "experimentalDecorators": true,
11
+ "emitDecoratorMetadata": true,
12
+ "noImplicitAny": false,
13
+ "strictNullChecks": true,
14
+ "esModuleInterop": true,
15
+ "resolveJsonModule": true,
16
+ "composite": true,
17
+ "baseUrl": ".",
18
+ "typeRoots": [
19
+ "types/",
20
+ "node_modules/@types"
21
+ ],
22
+ "paths": {
23
+ "@solana/web3.js": [
24
+ "./node_modules/@solana/web3.js/lib"
25
+ ]
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "emitDeclarationOnly": true,
5
+ "stripInternal": true
6
+ },
7
+ "include": [
8
+ "./src/**/*"
9
+ ],
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "allowJs": true,
5
+ "declaration": true,
6
+ "declarationDir": "declarations",
7
+ "esModuleInterop": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "strict": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "module": "esnext",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "baseUrl": "src",
16
+ "noFallthroughCasesInSwitch": true,
17
+ "noImplicitReturns": true
18
+ },
19
+ "include": ["src"]
20
+ }
package/typedoc.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "entryPoints": ["src/index.ts"],
3
+ "excludeInternal": true,
4
+ "excludePrivate": true,
5
+ "out": "doc"
6
+ }