Skip to content

Commit

Permalink
fix: update price to account for fees (#184)
Browse files Browse the repository at this point in the history
* fix: update price to account for fees

* chore: add logging

* fix: pool id

* fix: include spec version

* fix: multiply by WAD

* chore: add more logs

* fix: multiply first

* refactor: readability

* chore: cleanup

---------

Co-authored-by: Filippo Fontana <[email protected]>
  • Loading branch information
hieronx and filo87 committed Jun 3, 2024
1 parent f25864c commit c1e3fc8
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/mappings/services/trancheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { u128 } from '@polkadot/types'
import { bnToBn, nToBigInt } from '@polkadot/util'
import { paginatedGetter } from '../../helpers/paginatedGetter'
import { WAD } from '../../config'
import { ExtendedRpc, TrancheDetails } from '../../helpers/types'
import { ExtendedCall, ExtendedRpc, TrancheDetails } from '../../helpers/types'
import { Tranche, TrancheSnapshot } from '../../types'

const MAINNET_CHAINID = '0xb3db41421702df9a7fcac62b53ffeac85f7853cc4e689e0b93aeb3db18c09d82'
Expand Down Expand Up @@ -53,15 +53,15 @@ export class TrancheService extends Tranche {
}

static async getByPoolId(poolId: string): Promise<TrancheService[]> {
const tranches = (await paginatedGetter<Tranche>(this, [['poolId', '=', poolId]]))
const tranches = await paginatedGetter<Tranche>(this, [['poolId', '=', poolId]])
return tranches as TrancheService[]
}

static async getActivesByPoolId(poolId: string): Promise<TrancheService[]> {
const tranches = (await paginatedGetter<Tranche>(this, [
const tranches = await paginatedGetter<Tranche>(this, [
['poolId', '=', poolId],
['isActive', '=', true],
]))
])
return tranches as TrancheService[]
}

Expand All @@ -73,16 +73,39 @@ export class TrancheService extends Tranche {
return this
}

public updatePrice(price: bigint, block?: number) {
public async updatePrice(price: bigint, block?: number) {
const specVersion = api.runtimeVersion.specVersion.toNumber()
if (MAINNET_CHAINID === chainId && !!block) {
if (block < 4058350) return this.updatePriceFixDecimalError(price, block)
if (specVersion >= 1025 && specVersion < 1029) return await this.updatePriceFixForFees(price)
}
logger.info(`Updating price for tranche ${this.id} to: ${this.tokenPrice}`)
this.tokenPrice = price
return this
}

private updatePriceFixDecimalError(price: bigint, block: number) {
// https://centrifuge.subscan.io/extrinsic/4058350-0?event=4058350-0
// fix decimal error in old blocks, the fix was enacted at block #4058350
if (MAINNET_CHAINID === chainId && !!block && block < 4058350) {
this.tokenPrice = nToBigInt(bnToBn(price).div(bnToBn(1000000000)))
logger.info(`Updating price for tranche ${this.id} to: ${this.tokenPrice} (WITH CORRECTION FACTOR)`)
} else {
logger.info(
`Updating price for tranche ${this.id} to: ${this.tokenPrice} (WITH CORRECTION FACTOR) at block ${block}`
)
this.tokenPrice = nToBigInt(bnToBn(price).div(bnToBn(1000000000)))
return this
}

private async updatePriceFixForFees(price: bigint) {
// fix token price not accounting for fees
const apiCall = api.call as ExtendedCall
const navResponse = await apiCall.poolsApi.nav(this.poolId)
if (navResponse.isEmpty) {
logger.warn(`No NAV response! Saving inaccurate price: ${price} `)
this.tokenPrice = price
logger.info(`Updating price for tranche ${this.id} to: ${this.tokenPrice}`)
return this
}
const accruedFees = bnToBn(navResponse.unwrap().navFees.toBigInt())
this.tokenPrice = nToBigInt(bnToBn(price).sub(accruedFees.mul(WAD).div(bnToBn(this.tokenSupply))))
logger.info(`Updating price for tranche ${this.id} to: ${this.tokenPrice} (ACCOUNTING FOR ACCRUED FEES)`)
return this
}

Expand All @@ -92,7 +115,7 @@ export class TrancheService extends Tranche {
const tokenPrices = await (api.rpc as ExtendedRpc).pools.trancheTokenPrices(poolId)
const trancheTokenPrice = tokenPrices[this.index].toBigInt()
if (trancheTokenPrice <= BigInt(0)) throw new Error(`Zero or negative price returned for tranche: ${this.id}`)
this.updatePrice(trancheTokenPrice, block)
await this.updatePrice(trancheTokenPrice, block)
return this
}

Expand Down

0 comments on commit c1e3fc8

Please sign in to comment.