Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

feat: use rpc prices from trancheTokenPrices() when investor transact… #89

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type PoolSnapshot @entity {

type Tranche @entity {
id: ID! #poolId-trancheId
index: Int!
type: String! @index
pool: Pool! @index

Expand Down
4 changes: 4 additions & 0 deletions src/mappings/handlers/ormlTokensHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ async function _handleTokenTransfer(event: SubstrateEvent<TokensTransferEvent>):
const pool = await PoolService.getById(poolId.toString())
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.save()

const orderData = {
poolId: poolId.toString(),
trancheId: trancheId.toString(),
Expand Down
11 changes: 10 additions & 1 deletion src/mappings/handlers/poolsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function _handlePoolCreated(event: SubstrateEvent<PoolEvent>): Promise<voi
for (const [index, trancheData] of tranches.entries()) {
const trancheId = trancheIds[index]
logger.info(`Creating tranche with id: ${trancheId}`)
const trancheService = await TrancheService.init(trancheId, poolId.toString(), trancheData)
const trancheService = await TrancheService.init(poolId.toString(), trancheId, index, trancheData)
await trancheService.updateSupply()
await trancheService.updateDebt(trancheData.debt.toBigInt())
await trancheService.save()
Expand Down Expand Up @@ -181,6 +181,10 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent<OrderEvent>): Pro
const pool = await PoolService.getById(poolId.toString())
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.save()

const orderData: InvestorTransactionData = {
poolId: poolId.toString(),
trancheId: trancheId.toString(),
Expand Down Expand Up @@ -278,6 +282,11 @@ async function _handleOrdersCollected(event: SubstrateEvent<OrdersCollectedEvent

const pool = await PoolService.getById(poolId.toString())
const tranche = await TrancheService.getById(poolId.toString(), trancheId.toHex())

// Update tranche price
await tranche.updatePriceFromRpc()
await tranche.save()

const { payoutTokenAmount, payoutCurrencyAmount } = outstandingCollections

const orderData = {
Expand Down
11 changes: 5 additions & 6 deletions src/mappings/services/trancheService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { u128, u64 } from '@polkadot/types'
import { bnToBn, hexToU8a, nToBigInt } from '@polkadot/util'
import { bnToBn, nToBigInt } from '@polkadot/util'
import { RAY } from '../../config'
import { errorHandler } from '../../helpers/errorHandler'
import { ExtendedRpc, TrancheDetails } from '../../helpers/types'
Expand All @@ -14,7 +14,7 @@ export class TrancheService {
this.trancheState = trancheState
}

static init = async (trancheId: string, poolId: string, trancheData: TrancheDetails) => {
static init = async (poolId: string, trancheId: string, index: number, trancheData: TrancheDetails) => {
const trancheState = new TrancheState(`${poolId}-${trancheId}`)
trancheState.type = 'ALL'

Expand All @@ -28,6 +28,7 @@ export class TrancheService {
tranche.type = 'ALL'
tranche.poolId = poolId
tranche.trancheId = trancheId
tranche.index = index
tranche.isResidual = trancheData.trancheType.isResidual
tranche.seniority = trancheData.seniority.toNumber()

Expand Down Expand Up @@ -81,10 +82,8 @@ export class TrancheService {
private _updatePricefromRpc = async () => {
logger.info(`Qerying RPC price for tranche ${this.tranche.id}`)
const poolId = new u64(api.registry, this.tranche.poolId)
const trancheId = Array.from(hexToU8a(this.tranche.trancheId, 128))
logger.info(`Compact trancheId: ${trancheId}`)
const tokenPrice = await (api.rpc as ExtendedRpc).pools.trancheTokenPrice(poolId, trancheId)
this.updatePrice(tokenPrice.toBigInt())
const tokenPrices = await (api.rpc as ExtendedRpc).pools.trancheTokenPrices(poolId)
this.updatePrice(tokenPrices[this.tranche.index].toBigInt())
return this
}
public updatePriceFromRpc = errorHandler(this._updatePricefromRpc)
Expand Down