Skip to content

Commit

Permalink
feat: add initialised at timestamp to tranche balances (#264)
Browse files Browse the repository at this point in the history
* feat: add initialised at timestamp to tranche balances

* chore: cosmetics

* fix: timestamps init

---------

Co-authored-by: Filippo Fontana <[email protected]>
  • Loading branch information
hieronx and filo87 authored Nov 13, 2024
1 parent 99480da commit e9faa04
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ type TrancheBalance @entity {
pool: Pool! @index
tranche: Tranche! @index

initialisedAt: Date!

pendingInvestCurrency: BigInt!
claimableTrancheTokens: BigInt!

Expand Down
10 changes: 6 additions & 4 deletions src/mappings/handlers/evmHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import { InvestorTransactionData, InvestorTransactionService } from '../services
import { CurrencyService } from '../services/currencyService'
import { BlockchainService } from '../services/blockchainService'
import { CurrencyBalanceService } from '../services/currencyBalanceService'
import type { Provider } from '@ethersproject/providers'
import { TrancheBalanceService } from '../services/trancheBalanceService'
import { escrows } from '../../config'
import { InvestorPositionService } from '../services/investorPositionService'
import { getPeriodStart } from '../../helpers/timekeeperService'

const _ethApi = api as Provider
//const networkPromise = typeof ethApi.getNetwork === 'function' ? ethApi.getNetwork() : null

export const handleEvmDeployTranche = errorHandler(_handleEvmDeployTranche)
Expand Down Expand Up @@ -122,7 +119,12 @@ async function _handleEvmTransfer(event: TransferLog): Promise<void> {
const investLpCollect = InvestorTransactionService.collectLpInvestOrder({ ...orderData, address: toAccount!.id })
await investLpCollect.save()

const trancheBalance = await TrancheBalanceService.getOrInit(toAccount!.id, orderData.poolId, orderData.trancheId)
const trancheBalance = await TrancheBalanceService.getOrInit(
toAccount!.id,
orderData.poolId,
orderData.trancheId,
timestamp
)
await trancheBalance.investCollect(orderData.amount)
await trancheBalance.save()
}
Expand Down
28 changes: 24 additions & 4 deletions src/mappings/handlers/investmentsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
await epoch.saveWithStates()

// Update trancheBalance
const trancheBalance = await TrancheBalanceService.getOrInit(orderData.address, orderData.poolId, orderData.trancheId)
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId,
timestamp
)
await trancheBalance.investOrder(orderData.amount)
await trancheBalance.save()
}
Expand Down Expand Up @@ -143,7 +148,12 @@ async function _handleRedeemOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
await epoch.saveWithStates()

// Update trancheBalance
const trancheBalance = await TrancheBalanceService.getOrInit(orderData.address, orderData.poolId, orderData.trancheId)
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId,
timestamp
)
await trancheBalance.redeemOrder(orderData.amount)
await trancheBalance.save()
}
Expand Down Expand Up @@ -192,7 +202,12 @@ async function _handleInvestOrdersCollected(event: SubstrateEvent<InvestOrdersCo
amount: payoutInvestmentInvest.toBigInt(),
}

const trancheBalance = await TrancheBalanceService.getOrInit(orderData.address, orderData.poolId, orderData.trancheId)
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId,
timestamp
)

if (orderData.amount > 0) {
const it = InvestorTransactionService.collectInvestOrder(orderData)
Expand Down Expand Up @@ -248,7 +263,12 @@ async function _handleRedeemOrdersCollected(event: SubstrateEvent<RedeemOrdersCo
amount: payoutInvestmentRedeem.toBigInt(),
}

const trancheBalance = await TrancheBalanceService.getOrInit(orderData.address, orderData.poolId, orderData.trancheId)
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId,
timestamp
)

if (orderData.amount > 0) {
const it = InvestorTransactionService.collectRedeemOrder(orderData)
Expand Down
3 changes: 2 additions & 1 deletion src/mappings/handlers/poolsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId
orderData.trancheId,
timestamp
)

if (oo.investAmount > BigInt(0) && epochState.investFulfillmentPercentage! > BigInt(0)) {
Expand Down
7 changes: 4 additions & 3 deletions src/mappings/services/trancheBalanceService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { TrancheBalance } from '../../types/models/TrancheBalance'

export class TrancheBalanceService extends TrancheBalance {
static init(address: string, poolId: string, trancheId: string) {
static init(address: string, poolId: string, trancheId: string, timestamp: Date) {
logger.info(`Initialising new TrancheBalance: ${address}-${poolId}-${trancheId}`)
const trancheBalance = new this(
`${address}-${poolId}-${trancheId}`,
address,
poolId,
`${poolId}-${trancheId}`,
timestamp,
BigInt(0),
BigInt(0),
BigInt(0),
Expand All @@ -24,10 +25,10 @@ export class TrancheBalanceService extends TrancheBalance {
return trancheBalance as TrancheBalanceService | undefined
}

static getOrInit = async (address: string, poolId: string, trancheId: string) => {
static async getOrInit(address: string, poolId: string, trancheId: string, timestamp: Date) {
let trancheBalance = await this.getById(address, poolId, trancheId)
if (!trancheBalance) {
trancheBalance = this.init(address, poolId, trancheId)
trancheBalance = this.init(address, poolId, trancheId, timestamp)
await trancheBalance.save()
}
return trancheBalance as TrancheBalanceService
Expand Down

0 comments on commit e9faa04

Please sign in to comment.