Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleydon committed Feb 21, 2024
1 parent a575372 commit 5bae28c
Show file tree
Hide file tree
Showing 31 changed files with 642 additions and 701 deletions.
45 changes: 0 additions & 45 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"cSpell.words": [
"biomejs",
"codegen",
"judgements",
"khala",
"phala",
"poolv2",
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ FROM base AS release
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/app/db db
COPY --from=prerelease /usr/src/app/lib lib
COPY --from=prerelease /usr/src/app/initial_state initial_state
COPY --from=prerelease /usr/src/app/schema.graphql .
COPY --from=prerelease /usr/src/app/dump_*.json .
COPY --from=prerelease /usr/src/app/package.json .

# run the app
Expand Down

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"main": "src/main.ts",
"scripts": {
"build": "rm -rf lib && tsc --project tsconfig.build.json",
"build": "rm -rf lib && tsc -p tsconfig.build.json",
"postinstall": "patch-package"
},
"dependencies": {
Expand Down
14 changes: 14 additions & 0 deletions patches/@subsquid+big-decimal+1.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/node_modules/@subsquid/big-decimal/bigdecimal.js b/node_modules/@subsquid/big-decimal/bigdecimal.js
index a9d3887..a641179 100644
--- a/node_modules/@subsquid/big-decimal/bigdecimal.js
+++ b/node_modules/@subsquid/big-decimal/bigdecimal.js
@@ -7,7 +7,8 @@ const Base = Big()


// adjust settings
-Base.DP = 80
+Base.DP = 12
+Base.RM = 0
Base.NE = -19


37 changes: 17 additions & 20 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ type Delegation @entity @index(fields: ["basePool", "account"], unique: true) {
withdrawalStartTime: DateTime
delegationNft: Nft! @unique
withdrawalNft: Nft
snapshots: [DelegationSnapshot!] @derivedFrom(field: "delegation")
}

type Nft @entity {
Expand All @@ -152,10 +151,9 @@ type Worker @entity {
"worker public key"
id: ID!
stakePool: StakePool
session: Session
session: Session @derivedFrom(field: "worker")
confidenceLevel: Int!
initialScore: Int
shares: BigDecimal
}

enum WorkerState {
Expand All @@ -168,9 +166,7 @@ enum WorkerState {
type Session @entity {
"session account address"
id: ID!
isBound: Boolean!
stakePool: StakePool
worker: Worker
worker: Worker @unique
stake: BigDecimal!
state: WorkerState!
v: BigDecimal!
Expand All @@ -179,6 +175,7 @@ type Session @entity {
pInstant: Int!
totalReward: BigDecimal!
coolingDownStartTime: DateTime
shares: BigDecimal!
}

type AccountSnapshot
Expand All @@ -187,7 +184,7 @@ type AccountSnapshot
id: ID!
"block time"
updatedTime: DateTime!
account: Account!
account: String!
delegationValue: BigDecimal!
cumulativeStakePoolOwnerRewards: BigDecimal!
cumulativeVaultOwnerRewards: BigDecimal!
Expand All @@ -199,9 +196,10 @@ type BasePoolSnapshot
id: ID!
"block time"
updatedTime: DateTime!
basePool: BasePool!
basePool: String!
commission: BigDecimal!
apr: BigDecimal!
totalShares: BigDecimal!
totalValue: BigDecimal!
delegatorCount: Int!
sharePrice: BigDecimal!
Expand All @@ -215,7 +213,6 @@ type GlobalStateSnapshot @entity {
id: ID!
"block time"
updatedTime: DateTime! @index

totalValue: BigDecimal!
averageBlockTime: Int!
averageApr: BigDecimal!
Expand All @@ -234,7 +231,7 @@ type DelegationSnapshot
id: ID!
"block time"
updatedTime: DateTime!
delegation: Delegation!
delegation: String!
cost: BigDecimal!
value: BigDecimal!
}
Expand All @@ -245,16 +242,16 @@ type WorkerSnapshot
id: ID!
"block time"
updatedTime: DateTime!
worker: Worker!
stakePoolId: String
sessionId: String
worker: String!
stakePool: String!
session: String!
confidenceLevel: Int!
initialScore: Int
stake: BigDecimal
state: WorkerState
v: BigDecimal
ve: BigDecimal
pInit: Int
pInstant: Int
totalReward: BigDecimal
stake: BigDecimal!
state: WorkerState!
v: BigDecimal!
ve: BigDecimal!
pInit: Int!
pInstant: Int!
totalReward: BigDecimal!
}
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const DUMP_BLOCK = 3000000
export const INITIAL_BLOCK = 3000000
export const BASE_POOL_ACCOUNT =
'42qnPyfw3sbWMGGtTPPc2YFNZRKPGXswRszyQQjGs2FDxdim'
4 changes: 2 additions & 2 deletions src/decodeEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
phalaVault,
rmrkCore,
} from './types/events'
import {encodeAddress, fromBits, toBalance} from './utils/converter'
import {encodeAddress, fromBits, toBalance} from './utils'

const decodeEvent = (
event: Ctx['blocks'][number]['events'][number],
Expand Down Expand Up @@ -362,7 +362,7 @@ const decodeEvent = (
args: {
sessionId: encodeAddress(session),
v: fromBits(vBits),
payout: fromBits(payoutBits).round(12, 0),
payout: fromBits(payoutBits),
},
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/helper/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {BigDecimal} from '@subsquid/big-decimal'
import {Account} from '../model'

export const getAccount = (m: Map<string, Account>, id: string): Account => {
let acc = m.get(id)
if (acc == null) {
acc = new Account({
id,
stakePoolValue: BigDecimal(0),
stakePoolNftCount: 0,
stakePoolAvgAprMultiplier: BigDecimal(0),
vaultValue: BigDecimal(0),
vaultNftCount: 0,
vaultAvgAprMultiplier: BigDecimal(0),
cumulativeStakePoolOwnerRewards: BigDecimal(0),
cumulativeVaultOwnerRewards: BigDecimal(0),
})
m.set(id, acc)
}
return acc
}
68 changes: 45 additions & 23 deletions src/utils/basePool.ts → src/helper/basePool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import {BigDecimal} from '@subsquid/big-decimal'
import {type Account, BasePool, BasePoolKind, StakePool, Vault} from '../model'
import {
type Account,
BasePool,
BasePoolKind,
GlobalState,
StakePool,
Vault,
} from '../model'

export function createPool(
kind: BasePoolKind.StakePool,
Expand Down Expand Up @@ -80,15 +87,12 @@ export function updateSharePrice(basePool: BasePool): void {
if (basePool.totalShares.eq(0)) {
basePool.sharePrice = BigDecimal(1)
basePool.totalValue = BigDecimal(0)
basePool.freeValue = BigDecimal(0)
} else {
basePool.sharePrice = basePool.totalValue
.div(basePool.totalShares)
.round(12, 0)
basePool.sharePrice = basePool.totalValue.div(basePool.totalShares)
}
basePool.withdrawingValue = basePool.withdrawingShares
.times(basePool.sharePrice)
.round(12, 0)
basePool.withdrawingValue = basePool.withdrawingShares.times(
basePool.sharePrice,
)
}

export function updateStakePoolAprMultiplier(
Expand All @@ -100,7 +104,7 @@ export function updateStakePoolAprMultiplier(
: stakePool.idleWorkerShares
.times(BigDecimal(1).minus(basePool.commission))
.div(basePool.totalValue)
.round(6, 0)
.round(6)
}

export const updateVaultAprMultiplier = (
Expand All @@ -114,7 +118,7 @@ export const updateVaultAprMultiplier = (
.times(account.stakePoolValue)
.div(basePool.totalValue)
.times(BigDecimal(1).minus(basePool.commission))
.round(6, 0)
.round(6)
}
}

Expand All @@ -134,11 +138,11 @@ export const updateStakePoolDelegable = (
}

export const getBasePoolAvgAprMultiplier = (
basePools: BasePool[],
basePoolMap: Map<string, BasePool>,
): BigDecimal => {
let weight = BigDecimal(0)
let totalValue = BigDecimal(0)
for (const basePool of basePools) {
for (const basePool of basePoolMap.values()) {
if (basePool.kind === BasePoolKind.StakePool) {
totalValue = totalValue.plus(basePool.totalValue)
weight = weight.plus(basePool.totalValue.times(basePool.aprMultiplier))
Expand All @@ -148,17 +152,35 @@ export const getBasePoolAvgAprMultiplier = (
return BigDecimal(0)
}

return weight.div(totalValue).round(6, 0)
return weight.div(totalValue).round(6)
}

export const updateFreeValue = (
basePool: BasePool,
value: BigDecimal,
): void => {
// free value is wPHA with minBalance
if (value.lt('0.0001')) {
basePool.freeValue = BigDecimal(0)
} else {
basePool.freeValue = value
}
export const getApr = (
globalState: GlobalState,
aprMultiplier: BigDecimal,
): BigDecimal => {
const ONE_YEAR = 365 * 24 * 60 * 60 * 1000
const {averageBlockTime, idleWorkerShares, budgetPerBlock, treasuryRatio} =
globalState
const value = aprMultiplier
.times(budgetPerBlock)
.times(BigDecimal(1).minus(treasuryRatio))
.times(ONE_YEAR)
.div(averageBlockTime)
.div(idleWorkerShares)
.round(6)

return value
}

// export const updateFreeValue = (
// basePool: BasePool,
// value: BigDecimal,
// ): void => {
// // free value is wPHA with minBalance
// if (value.lt('0.0001')) {
// basePool.freeValue = BigDecimal(0)
// } else {
// basePool.freeValue = value
// }
// }
10 changes: 5 additions & 5 deletions src/utils/delegation.ts → src/helper/delegation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {BigDecimal} from '@subsquid/big-decimal'
import {type BasePool, type Delegation} from '../model'
import {sum} from './common'
import {sum} from '../utils'

export const updateDelegationValue = (
delegation: Delegation,
basePool: BasePool,
): void => {
delegation.value = delegation.shares.times(basePool.sharePrice).round(12, 0)
delegation.withdrawingValue = delegation.withdrawingShares
.times(basePool.sharePrice)
.round(12, 0)
delegation.value = delegation.shares.times(basePool.sharePrice)
delegation.withdrawingValue = delegation.withdrawingShares.times(
basePool.sharePrice,
)
}

export const getDelegationAvgAprMultiplier = (
Expand Down
Loading

0 comments on commit 5bae28c

Please sign in to comment.