Skip to content

Commit

Permalink
Remove indexStats and mapTimelockStats
Browse files Browse the repository at this point in the history
  • Loading branch information
imkven committed Dec 2, 2022
1 parent d5c83a5 commit 3538564
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions apps/whale-api/src/module.indexer/model/dftx/update.masternode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { P2PKH, P2WPKH } from '@defichain/jellyfish-address'
import { NetworkName } from '@defichain/jellyfish-network'
import { RawBlock } from '../_abstract'
import { Inject, Injectable } from '@nestjs/common'
import { Masternode, MasternodeMapper } from '../../../module.model/masternode'
import { MasternodeStatsMapper, TimelockStats } from '../../../module.model/masternode.stats'
import { MasternodeMapper } from '../../../module.model/masternode'
import { MasternodeStatsMapper } from '../../../module.model/masternode.stats'
import { HexEncoder } from '../../../module.model/_hex.encoder'
import BigNumber from 'bignumber.js'

@Injectable()
export class UpdateMasternodeIndexer extends DfTxIndexer<UpdateMasternode> {
Expand Down Expand Up @@ -88,37 +87,9 @@ export class UpdateMasternodeIndexer extends DfTxIndexer<UpdateMasternode> {
collateral: txn.vout[1].value.toFixed(8),
updateRecords
})

await this.indexStats(block, mn)
}
}

async indexStats (block: RawBlock, data: Masternode): Promise<void> {
const latest = await this.masternodeStatsMapper.getLatest()

await this.masternodeStatsMapper.put({
id: HexEncoder.encodeHeight(block.height),
stats: {
count: (latest?.stats?.count ?? 0) - 1,
tvl: new BigNumber(latest?.stats?.tvl ?? 0).minus(data.collateral).toFixed(8),
locked: this.mapTimelockStats(latest?.stats?.locked ?? [], {
weeks: data.timelock ?? 0,
count: 1,
tvl: data.collateral
})
},
block: { hash: block.hash, height: block.height, medianTime: block.mediantime, time: block.time }
})
}

mapTimelockStats (latest: TimelockStats[], lockStats: TimelockStats): TimelockStats[] {
return latest.map(x => ({
...x,
count: x.weeks === lockStats.weeks ? (x.count - lockStats.count) : x.count,
tvl: x.weeks === lockStats.weeks ? new BigNumber(x.tvl).minus(lockStats.tvl).toFixed(8) : x.tvl
}))
}

async invalidateTransaction (block: RawBlock, transaction: DfTxTransaction<UpdateMasternode>): Promise<void> {
const txn = transaction.txn
const data = transaction.dftx.data
Expand All @@ -141,8 +112,6 @@ export class UpdateMasternodeIndexer extends DfTxIndexer<UpdateMasternode> {
collateral: txn.vout[1].value.toFixed(8),
updateRecords
})

await this.indexStats(block, mn)
}
}

Expand Down

0 comments on commit 3538564

Please sign in to comment.