diff --git a/chains/centrifuge.yaml b/chains/centrifuge.yaml index b502f1a9..bf2e6b61 100644 --- a/chains/centrifuge.yaml +++ b/chains/centrifuge.yaml @@ -3,4 +3,7 @@ network: endpoint: wss://fullnode.parachain.centrifuge.io genesisHash: '0xb3db41421702df9a7fcac62b53ffeac85f7853cc4e689e0b93aeb3db18c09d82' chaintypes: - file: ./dist/chaintypes.js \ No newline at end of file + file: ./dist/chaintypes.js +dataSources: + - kind: substrate/Runtime + startBlock: 3824709 # block Anemoy was created at diff --git a/src/mappings/handlers/blockHandlers.ts b/src/mappings/handlers/blockHandlers.ts index 7af22d45..3df1c818 100644 --- a/src/mappings/handlers/blockHandlers.ts +++ b/src/mappings/handlers/blockHandlers.ts @@ -34,7 +34,8 @@ async function _handleBlock(block: SubstrateBlock): Promise { 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) diff --git a/src/mappings/handlers/investmentsHandlers.ts b/src/mappings/handlers/investmentsHandlers.ts index c9130e7f..b3242c81 100644 --- a/src/mappings/handlers/investmentsHandlers.ts +++ b/src/mappings/handlers/investmentsHandlers.ts @@ -25,7 +25,7 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent): 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 = { diff --git a/src/mappings/handlers/poolsHandlers.ts b/src/mappings/handlers/poolsHandlers.ts index 7ed8ba05..523c7895 100644 --- a/src/mappings/handlers/poolsHandlers.ts +++ b/src/mappings/handlers/poolsHandlers.ts @@ -136,7 +136,7 @@ async function _handleEpochExecuted(event: SubstrateEvent 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() diff --git a/src/mappings/services/trancheService.test.ts b/src/mappings/services/trancheService.test.ts index 29e33dad..1df98a6f 100644 --- a/src/mappings/services/trancheService.test.ts +++ b/src/mappings/services/trancheService.test.ts @@ -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')) diff --git a/src/mappings/services/trancheService.ts b/src/mappings/services/trancheService.ts index fafaf68f..749c3b14 100644 --- a/src/mappings/services/trancheService.ts +++ b/src/mappings/services/trancheService.ts @@ -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 }