Skip to content

Commit

Permalink
feat: add initialised at timestamp to tranche balances
Browse files Browse the repository at this point in the history
  • Loading branch information
hieronx committed Nov 11, 2024
1 parent 9ecc2ae commit e357a81
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 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
7 changes: 6 additions & 1 deletion src/mappings/handlers/evmHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,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 @@ -67,7 +67,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,
event.block.timestamp
)
await trancheBalance.investOrder(orderData.amount)
await trancheBalance.save()
}
Expand Down Expand Up @@ -130,7 +135,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,
event.block.timestamp
)
await trancheBalance.redeemOrder(orderData.amount)
await trancheBalance.save()
}
Expand Down Expand Up @@ -175,7 +185,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,
event.block.timestamp
)

if (orderData.amount > 0) {
const it = InvestorTransactionService.collectInvestOrder(orderData)
Expand Down Expand Up @@ -227,7 +242,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,
event.block.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 @@ -231,7 +231,8 @@ async function _handleEpochExecuted(event: SubstrateEvent<EpochClosedExecutedEve
const trancheBalance = await TrancheBalanceService.getOrInit(
orderData.address,
orderData.poolId,
orderData.trancheId
orderData.trancheId,
event.block.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
}

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

0 comments on commit e357a81

Please sign in to comment.