Skip to content

Commit

Permalink
chore: fix token price decimals (#73)
Browse files Browse the repository at this point in the history
* chore: fix token price for prod chains

* chore: set block that upgrade was enacted on
  • Loading branch information
sophialittlejohn authored Oct 31, 2023
1 parent 874552f commit 9a04710
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
5 changes: 4 additions & 1 deletion chains/centrifuge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ network:
endpoint: wss://fullnode.parachain.centrifuge.io
genesisHash: '0xb3db41421702df9a7fcac62b53ffeac85f7853cc4e689e0b93aeb3db18c09d82'
chaintypes:
file: ./dist/chaintypes.js
file: ./dist/chaintypes.js
dataSources:
- kind: substrate/Runtime
startBlock: 3824709 # block Anemoy was created at
3 changes: 2 additions & 1 deletion src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
const trancheTokenPrices = await pool.getTrancheTokenPrices()
for (const tranche of tranches) {
const index = tranche.index
if (trancheTokenPrices !== undefined) await tranche.updatePrice(trancheTokenPrices[index].toBigInt())
if (trancheTokenPrices !== undefined)
await tranche.updatePrice(trancheTokenPrices[index].toBigInt(), block.block.header.number.toNumber())
await tranche.updateSupply()
await tranche.updateDebt(trancheData[tranche.trancheId].data.debt.toBigInt())
await tranche.computeYield('yieldSinceLastPeriod', lastPeriodStart)
Expand Down
8 changes: 4 additions & 4 deletions src/mappings/handlers/investmentsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.updatePriceFromRpc(event.block.block.header.number.toNumber())

const orderData: InvestorTransactionData = {
poolId: poolId.toString(),
Expand Down Expand Up @@ -85,7 +85,7 @@ async function _handleRedeemOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
const account = await AccountService.getOrInit(address.toString())
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

await tranche.updatePriceFromRpc()
await tranche.updatePriceFromRpc(event.block.block.header.number.toNumber())

const orderData: InvestorTransactionData = {
poolId: poolId.toString(),
Expand Down Expand Up @@ -148,7 +148,7 @@ async function _handleInvestOrdersCollected(event: SubstrateEvent<InvestOrdersCo
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.updatePriceFromRpc(event.block.block.header.number.toNumber())
await tranche.save()

const { payoutInvestmentInvest } = investCollection
Expand Down Expand Up @@ -193,7 +193,7 @@ async function _handleRedeemOrdersCollected(event: SubstrateEvent<RedeemOrdersCo
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.updatePriceFromRpc(event.block.block.header.number.toNumber())
await tranche.save()

const { payoutInvestmentRedeem } = redeemCollection
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/handlers/ormlTokensHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function _handleTokenTransfer(event: SubstrateEvent<TokensTransferEvent>):
if (tranche === undefined) throw new Error('Tranche not found!')

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.updatePriceFromRpc(event.block.block.header.number.toNumber())
await tranche.save()

const orderData = {
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/handlers/poolsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
for (const tranche of tranches) {
const epochState = epoch.states.find((epochState) => epochState.trancheId === tranche.trancheId)
await tranche.updateSupply()
await tranche.updatePrice(epochState.tokenPrice)
await tranche.updatePrice(epochState.tokenPrice, event.block.block.header.number.toNumber())
await tranche.updateFulfilledInvestOrders(epochState.sumFulfilledInvestOrders)
await tranche.updateFulfilledRedeemOrders(epochState.sumFulfilledRedeemOrders)
await tranche.save()
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/trancheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ describe('Given a new tranche, when initialised', () => {

describe('Given an existing tranche,', () => {
test('when the rpc price is updated, then the value is fetched and set correctly', async () => {
await tranches[0].updatePriceFromRpc().catch(errorLogger)
await tranches[0].updatePriceFromRpc(4058351).catch(errorLogger)
expect((api.rpc as ExtendedRpc).pools.trancheTokenPrices).toBeCalled()
expect(tranches[0].tokenPrice).toBe(BigInt('2000000000000000000'))
})

test('when a 0 rpc price is delivered, then the value is skipped and logged', async () => {
await tranches[1].updatePriceFromRpc().catch(errorLogger)
await tranches[1].updatePriceFromRpc(4058352).catch(errorLogger)
expect((api.rpc as ExtendedRpc).pools.trancheTokenPrices).toBeCalled()
expect(logger.error).toBeCalled()
expect(tranches[1].tokenPrice).toBe(BigInt('1000000000000000000'))
Expand Down
14 changes: 10 additions & 4 deletions src/mappings/services/trancheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,25 @@ export class TrancheService extends Tranche {
return this
}

public updatePrice(price: bigint) {
logger.info(`Updating price for tranche ${this.id} to :${price}`)
public updatePrice(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 (block < 4058350) {
this.tokenPrice = nToBigInt(bnToBn(price).div(bnToBn(1000000000)))
return this
}
logger.info(`Updating price for tranche ${this.id} to: ${price}`)
this.tokenPrice = price
return this
}

public async updatePriceFromRpc() {
public async updatePriceFromRpc(block: number) {
logger.info(`Querying RPC price for tranche ${this.id}`)
const poolId = this.poolId
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)
this.updatePrice(trancheTokenPrice, block)
return this
}

Expand Down

0 comments on commit 9a04710

Please sign in to comment.