Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hieronx committed Jun 11, 2024
1 parent 5f27952 commit fdd53ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
18 changes: 2 additions & 16 deletions src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
TrancheSnapshot,
} from '../../types/models'
import { AssetPositionService } from '../services/assetPositionService'
import { bnToBn } from '@polkadot/util'

const timekeeper = TimekeeperService.init()

Expand Down Expand Up @@ -66,24 +65,11 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
pool.resetUnrealizedProfit()
for (const loanId in activeLoanData) {
const asset = await AssetService.getById(pool.id, loanId)
const previousUnrealizedProfitByPeriod = bnToBn(asset.unrealizedProfitByPeriod)
await asset.updateActiveAssetData(activeLoanData[loanId])

const unrealizedProfitAtMarketPrice = await AssetPositionService.computeUnrealizedProfitAtPrice(
asset.id,
asset.currentPrice
)
const unrealizedProfitAtNotional = await AssetPositionService.computeUnrealizedProfitAtPrice(
asset.id,
asset.notional
)
logger.info(`byPeriod for ${asset.id} = ${unrealizedProfitAtMarketPrice} - ${previousUnrealizedProfitByPeriod}`)
const unrealizedProfitByPeriod = unrealizedProfitAtMarketPrice - previousUnrealizedProfitByPeriod
logger.info(`byPeriod for ${asset.id} = ${unrealizedProfitByPeriod}`)
await asset.updateUnrealizedProfit(
unrealizedProfitAtMarketPrice,
unrealizedProfitAtNotional,
unrealizedProfitByPeriod
await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.currentPrice),
await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.notional)
)
await asset.save()
await pool.increaseInterestAccrued(asset.interestAccruedByPeriod)
Expand Down
10 changes: 7 additions & 3 deletions src/mappings/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ export class AssetService extends Asset {
)
}

public updateUnrealizedProfit(atMarketPrice: bigint, atNotional: bigint, byPeriod: bigint) {
logger.info(`Updating unrealizedProfit for asset ${this.id}: ${atMarketPrice}, ${atNotional}, ${byPeriod}`)
public updateUnrealizedProfit(atMarketPrice: bigint, atNotional: bigint) {
logger.info(`Updating unrealizedProfit for asset ${this.id}: ${atMarketPrice}, ${atNotional}`)
this.unrealizedProfitAtMarketPrice = atMarketPrice
this.unrealizedProfitAtNotional = atNotional
this.unrealizedProfitByPeriod = byPeriod

logger.info(
`byPeriod: ${atMarketPrice} - ${this.unrealizedProfitByPeriod} = ${atMarketPrice - this.unrealizedProfitByPeriod}`
)
this.unrealizedProfitByPeriod = atMarketPrice - this.unrealizedProfitByPeriod
}
}

Expand Down

0 comments on commit fdd53ab

Please sign in to comment.