From 631cb0215188a31066d5366d6a3a03b1495e1d07 Mon Sep 17 00:00:00 2001 From: Filippo Fontana Date: Wed, 13 Nov 2024 22:50:22 +0100 Subject: [PATCH] fix: missingId error --- src/helpers/stateSnapshot.ts | 12 +- src/mappings/handlers/blockHandlers.ts | 359 +++++++++--------- src/mappings/handlers/ethHandlers.ts | 4 +- src/mappings/handlers/poolFeesHandlers.ts | 2 +- src/mappings/services/accountService.ts | 4 +- src/mappings/services/assetService.ts | 4 +- src/mappings/services/currencyService.ts | 4 +- src/mappings/services/epochService.ts | 2 +- .../services/investorTransactionService.ts | 4 +- src/mappings/services/poolFeeService.ts | 11 +- src/mappings/services/poolService.test.ts | 2 +- src/mappings/services/poolService.ts | 13 +- .../services/trancheBalanceService.ts | 4 +- src/mappings/services/trancheService.ts | 4 +- 14 files changed, 220 insertions(+), 209 deletions(-) diff --git a/src/helpers/stateSnapshot.ts b/src/helpers/stateSnapshot.ts index 4ac095e..e635015 100644 --- a/src/helpers/stateSnapshot.ts +++ b/src/helpers/stateSnapshot.ts @@ -47,8 +47,6 @@ export async function statesSnapshotter (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged + ) + ) +} diff --git a/src/mappings/handlers/blockHandlers.ts b/src/mappings/handlers/blockHandlers.ts index 0b05f2f..9c30518 100644 --- a/src/mappings/handlers/blockHandlers.ts +++ b/src/mappings/handlers/blockHandlers.ts @@ -14,6 +14,8 @@ import { EpochService } from '../services/epochService' import { SnapshotPeriodService } from '../services/snapshotPeriodService' import { TrancheBalanceService } from '../services/trancheBalanceService' import { InvestorPositionService } from '../services/investorPositionService' +import { CurrencyService } from '../services/currencyService' +import { BlockchainService } from '../services/blockchainService' const timekeeper = TimekeeperService.init() @@ -25,200 +27,203 @@ async function _handleBlock(block: SubstrateBlock): Promise { const blockNumber = block.block.header.number.toNumber() const newPeriod = (await timekeeper).processBlock(block.timestamp) - if (newPeriod) { - const specVersion = api.runtimeVersion.specVersion.toNumber() - logger.info( - `# It's a new period on block ${blockNumber}: ${block.timestamp.toISOString()} (specVersion: ${specVersion})` - ) - - const period = SnapshotPeriodService.init(blockPeriodStart) - await period.save() - - const lastPeriodStart = new Date(period.start.valueOf() - SNAPSHOT_INTERVAL_SECONDS * 1000) - const daysAgo7 = new Date(period.start.valueOf() - 7 * 24 * 3600 * 1000) - const daysAgo30 = new Date(period.start.valueOf() - 30 * 24 * 3600 * 1000) - const daysAgo90 = new Date(period.start.valueOf() - 90 * 24 * 3600 * 1000) - const beginningOfMonth = new Date(period.year, period.month, 1) - const quarter = Math.floor(period.month / 3) - const beginningOfQuarter = new Date(period.year, quarter * 3, 1) - const beginningOfYear = new Date(period.year, 0, 1) - - const poolsToSnapshot: PoolService[] = [] - const tranchesToSnapshot: TrancheService[] = [] - const assetsToSnapshot: AssetService[] = [] - const poolFeesToSnapshot: PoolFeeService[] = [] - - // Update Pool States - const pools = await PoolService.getCfgActivePools() - for (const pool of pools) { - logger.info(` ## Updating pool ${pool.id} states...`) - - if (!pool.currentEpoch) { - logger.error(`Pool currentEpoch not set for ${pool.id}, skipping...`) - continue - } + if (!newPeriod) return + + const specVersion = api.runtimeVersion.specVersion.toNumber() + logger.info( + `# It's a new period on block ${blockNumber}: ${block.timestamp.toISOString()} (specVersion: ${specVersion})` + ) + const _blockchain = BlockchainService.getOrInit(chainId) + const period = SnapshotPeriodService.init(blockPeriodStart) + await period.save() + + const lastPeriodStart = new Date(period.start.valueOf() - SNAPSHOT_INTERVAL_SECONDS * 1000) + const daysAgo7 = new Date(period.start.valueOf() - 7 * 24 * 3600 * 1000) + const daysAgo30 = new Date(period.start.valueOf() - 30 * 24 * 3600 * 1000) + const daysAgo90 = new Date(period.start.valueOf() - 90 * 24 * 3600 * 1000) + const beginningOfMonth = new Date(period.year, period.month, 1) + const quarter = Math.floor(period.month / 3) + const beginningOfQuarter = new Date(period.year, quarter * 3, 1) + const beginningOfYear = new Date(period.year, 0, 1) + + const poolsToSnapshot: PoolService[] = [] + const tranchesToSnapshot: TrancheService[] = [] + const assetsToSnapshot: AssetService[] = [] + const poolFeesToSnapshot: PoolFeeService[] = [] + + // Update Pool States + const pools = await PoolService.getCfgActivePools() + for (const pool of pools) { + const currency = (await CurrencyService.getByPoolId(pool.id, { limit: 1 })).pop() + logger.info(` ## Updating pool ${pool.id} states...`) + + if (!pool.currentEpoch) { + logger.error(`Pool currentEpoch not set for ${pool.id}, skipping...`) + continue + } - const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) - if (!currentEpoch) { - logger.error(`Current epoch ${pool.currentEpoch} for pool ${pool.id} not found, skipping pool`) - continue - } + const currentEpoch = await EpochService.getById(pool.id, pool.currentEpoch) + if (!currentEpoch) { + logger.error(`Current epoch ${pool.currentEpoch} for pool ${pool.id} not found, skipping pool`) + continue + } - await pool.updateState() - await pool.resetDebtOverdue() + await pool.updateState() + await pool.resetDebtOverdue() - // Update tranche states - const tranches = await TrancheService.getActivesByPoolId(pool.id) - const trancheData = await pool.getTranches() - const trancheTokenPrices = await pool.getTrancheTokenPrices() - for (const tranche of tranches) { - if (typeof tranche.index !== 'number') { - logger.error('Tranche index not set, skipping tranche') + // Update tranche states + const tranches = await TrancheService.getActivesByPoolId(pool.id) + const trancheData = await pool.getTranches() + const trancheTokenPrices = await pool.getTrancheTokenPrices() + for (const tranche of tranches) { + if (typeof tranche.index !== 'number') { + logger.error('Tranche index not set, skipping tranche') + continue + } + if (!trancheTokenPrices) { + logger.error('trancheTokenPrices not available, skipping tranche updates') + break + } + await tranche.updatePrice(trancheTokenPrices[tranche.index].toBigInt(), block.block.header.number.toNumber()) + await tranche.updateSupply() + await tranche.updateDebt(trancheData[tranche.trancheId].debt) + await tranche.computeYield('yieldSinceLastPeriod', lastPeriodStart) + await tranche.computeYield('yieldSinceInception') + await tranche.computeYield('yieldYTD', beginningOfYear) + await tranche.computeYield('yieldQTD', beginningOfQuarter) + await tranche.computeYield('yieldMTD', beginningOfMonth) + await tranche.computeYieldAnnualized('yield7DaysAnnualized', period.start, daysAgo7) + await tranche.computeYieldAnnualized('yield30DaysAnnualized', period.start, daysAgo30) + await tranche.computeYieldAnnualized('yield90DaysAnnualized', period.start, daysAgo90) + await tranche.save() + tranchesToSnapshot.push(tranche) + + // Compute TrancheBalances Unrealized Profit + const trancheBalances = (await TrancheBalanceService.getByTrancheId(tranche.id, { + limit: 100, + })) as TrancheBalanceService[] + for (const trancheBalance of trancheBalances) { + if (typeof tranche.tokenPrice !== 'bigint') { + console.warn(`tokenPrice not set, unable to update unrealizedProfit for trancheBalance ${trancheBalance.id}`) continue } - if (!trancheTokenPrices) { - logger.error('trancheTokenPrices not available, skipping tranche updates') - break - } - await tranche.updatePrice(trancheTokenPrices[tranche.index].toBigInt(), block.block.header.number.toNumber()) - await tranche.updateSupply() - await tranche.updateDebt(trancheData[tranche.trancheId].debt) - await tranche.computeYield('yieldSinceLastPeriod', lastPeriodStart) - await tranche.computeYield('yieldSinceInception') - await tranche.computeYield('yieldYTD', beginningOfYear) - await tranche.computeYield('yieldQTD', beginningOfQuarter) - await tranche.computeYield('yieldMTD', beginningOfMonth) - await tranche.computeYieldAnnualized('yield7DaysAnnualized', period.start, daysAgo7) - await tranche.computeYieldAnnualized('yield30DaysAnnualized', period.start, daysAgo30) - await tranche.computeYieldAnnualized('yield90DaysAnnualized', period.start, daysAgo90) - await tranche.save() - tranchesToSnapshot.push(tranche) - - // Compute TrancheBalances Unrealized Profit - const trancheBalances = (await TrancheBalanceService.getByTrancheId(tranche.id, { - limit: 100, - })) as TrancheBalanceService[] - for (const trancheBalance of trancheBalances) { - if (typeof tranche.tokenPrice !== 'bigint') { - console.warn( - `tokenPrice not set, unable to update unrealizedProfit for trancheBalance ${trancheBalance.id}` - ) - continue - } - const unrealizedProfit = await InvestorPositionService.computeUnrealizedProfitAtPrice( - trancheBalance.accountId, - tranche.id, - tranche.tokenPrice - ) - await trancheBalance.updateUnrealizedProfit(unrealizedProfit) - await trancheBalance.save() - } + const unrealizedProfit = await InvestorPositionService.computeUnrealizedProfitAtPrice( + trancheBalance.accountId, + tranche.id, + tranche.tokenPrice + ) + await trancheBalance.updateUnrealizedProfit(unrealizedProfit) + await trancheBalance.save() + } + } + // Asset operations + const activeAssetData = await pool.getPortfolio() + pool.resetOffchainCashValue() + pool.resetUnrealizedProfit() + for (const assetId in activeAssetData) { + const asset = await AssetService.getById(pool.id, assetId) + if (!asset) continue + await asset.loadSnapshot(lastPeriodStart) + await asset.updateActiveAssetData(activeAssetData[assetId]) + if (asset.notional && asset.currentPrice) { + await asset.updateUnrealizedProfit( + await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.currentPrice), + await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.notional) + ) + } else { + console.warn(`Missing current price or notional, unable to update unrealized profit for asset ${assetId}`) + } + await asset.save() + assetsToSnapshot.push(asset) + + if (typeof asset.interestAccruedByPeriod === 'bigint') { + await pool.increaseInterestAccrued(asset.interestAccruedByPeriod) + } else { + logger.warn(`interestAccruedByPeriod not set, unable to compute accrued interest for asset ${assetId}`) } - // Asset operations - const activeAssetData = await pool.getPortfolio() - pool.resetOffchainCashValue() - pool.resetUnrealizedProfit() - for (const assetId in activeAssetData) { - const asset = await AssetService.getById(pool.id, assetId) - if (!asset) continue - await asset.loadSnapshot(lastPeriodStart) - await asset.updateActiveAssetData(activeAssetData[assetId]) - if (asset.notional && asset.currentPrice) { - await asset.updateUnrealizedProfit( - await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.currentPrice), - await AssetPositionService.computeUnrealizedProfitAtPrice(asset.id, asset.notional) + if (asset.isNonCash()) { + if ( + typeof asset.unrealizedProfitAtMarketPrice === 'bigint' && + typeof asset.unrealizedProfitAtNotional === 'bigint' && + typeof asset.unrealizedProfitByPeriod === 'bigint' + ) { + pool.increaseUnrealizedProfit( + asset.unrealizedProfitAtMarketPrice, + asset.unrealizedProfitAtNotional, + asset.unrealizedProfitByPeriod ) } else { - console.warn(`Missing current price or notional, unable to update unrealized profit for asset ${assetId}`) + logger.warn(`Missing unrealized profit figures, unable to increase unrealized profit for asset ${assetId}`) } - await asset.save() - assetsToSnapshot.push(asset) - - if (typeof asset.interestAccruedByPeriod === 'bigint') { - await pool.increaseInterestAccrued(asset.interestAccruedByPeriod) + } + if (asset.isBeyondMaturity(block.timestamp)) { + if (typeof asset.outstandingDebt === 'bigint') { + pool.increaseDebtOverdue(asset.outstandingDebt) } else { - logger.warn(`interestAccruedByPeriod not set, unable to compute accrued interest for asset ${assetId}`) - } - if (asset.isNonCash()) { - if ( - typeof asset.unrealizedProfitAtMarketPrice === 'bigint' && - typeof asset.unrealizedProfitAtNotional === 'bigint' && - typeof asset.unrealizedProfitByPeriod === 'bigint' - ) { - pool.increaseUnrealizedProfit( - asset.unrealizedProfitAtMarketPrice, - asset.unrealizedProfitAtNotional, - asset.unrealizedProfitByPeriod - ) - } else { - logger.warn(`Missing unrealized profit figures, unable to increase unrealized profit for asset ${assetId}`) - } - } - if (asset.isBeyondMaturity(block.timestamp)) { - if (typeof asset.outstandingDebt === 'bigint') { - pool.increaseDebtOverdue(asset.outstandingDebt) - } else { - logger.warn(`Unable to increase debt overdue, missing outstandingDebt for ${assetId}`) - } - } - if (asset.isOffchainCash()) { - if (typeof asset.presentValue === 'bigint') { - pool.increaseOffchainCashValue(asset.presentValue) - } else { - logger.warn(`Asset present value not set, unable to increase offchain cash value for ${assetId}`) - } + logger.warn(`Unable to increase debt overdue, missing outstandingDebt for ${assetId}`) } } - await pool.updateNumberOfActiveAssets(BigInt(Object.keys(activeAssetData).length)) - - // NAV update requires updated offchain cash value - await pool.updateNAV() - - //PoolFees operations - const accruedFees = await pool.getAccruedFees() - for (const accruals of accruedFees) { - const [feeId, pending, disbursement] = accruals - const poolFee = await PoolFeeService.getById(pool.id, feeId) - if (!poolFee) { - logger.error(`Unable to retrieve PoolFee ${pool.id}-${feeId}, skipping accruals!`) - continue - } - await poolFee.updateAccruals(pending, disbursement) - await poolFee.save() - poolFeesToSnapshot.push(poolFee) - - if (typeof poolFee.sumAccruedAmountByPeriod === 'bigint') { - await pool.increaseAccruedFees(poolFee.sumAccruedAmountByPeriod) - const poolFeeTransaction = PoolFeeTransactionService.accrue({ - poolId: pool.id, - feeId, - blockNumber, - amount: poolFee.sumAccruedAmountByPeriod, - epochId: currentEpoch.id, - hash: block.hash.toHex(), - timestamp: block.timestamp, - }) - await poolFeeTransaction.save() + if (asset.isOffchainCash()) { + if (typeof asset.presentValue === 'bigint') { + pool.increaseOffchainCashValue(asset.presentValue) } else { - logger.warn(`sumAccruedAmountByPeriod not set. unable to increase accrued fees for ${poolFee.id}`) + logger.warn(`Asset present value not set, unable to increase offchain cash value for ${assetId}`) } } - const sumPoolFeesPendingAmount = await PoolFeeService.computeSumPendingFees(pool.id) - await pool.updateSumPoolFeesPendingAmount(sumPoolFeesPendingAmount) - await pool.save() - poolsToSnapshot.push(pool) - logger.info(`## Pool ${pool.id} states update completed!`) } + await pool.updateNumberOfActiveAssets(BigInt(Object.keys(activeAssetData).length)) - logger.info('## Performing snapshots...') - const blockInfo = { number: block.block.header.number.toNumber(), timestamp: block.timestamp } - await statesSnapshotter('periodId', period.id, poolsToSnapshot, PoolSnapshot, blockInfo, 'poolId') - await statesSnapshotter('periodId', period.id, tranchesToSnapshot, TrancheSnapshot, blockInfo, 'trancheId') - await statesSnapshotter('periodId', period.id, assetsToSnapshot, AssetSnapshot, blockInfo, 'assetId') - await statesSnapshotter('periodId', period.id, poolFeesToSnapshot, PoolFeeSnapshot, blockInfo, 'poolFeeId') - logger.info('## Snapshotting completed!') + // NAV update requires updated offchain cash value + if (currency) { + await pool.updateNAV(currency.decimals) + } else { + logger.warn(`Cannot update NAV for pool ${pool.id} as NO currency found / initialised`) + } - //Update tracking of period and continue - await (await timekeeper).update(period.start) + //PoolFees operations + const accruedFees = await pool.getAccruedFees() + for (const accruals of accruedFees) { + const [feeId, pending, disbursement] = accruals + const poolFee = await PoolFeeService.getById(pool.id, feeId) + if (!poolFee) { + logger.error(`Unable to retrieve PoolFee ${pool.id}-${feeId}, skipping accruals!`) + continue + } + await poolFee.updateAccruals(pending, disbursement) + await poolFee.save() + poolFeesToSnapshot.push(poolFee) + + if (typeof poolFee.sumAccruedAmountByPeriod === 'bigint') { + await pool.increaseAccruedFees(poolFee.sumAccruedAmountByPeriod) + const poolFeeTransaction = PoolFeeTransactionService.accrue({ + poolId: pool.id, + feeId, + blockNumber, + amount: poolFee.sumAccruedAmountByPeriod, + epochId: currentEpoch.id, + hash: block.hash.toHex(), + timestamp: block.timestamp, + }) + await poolFeeTransaction.save() + } else { + logger.warn(`sumAccruedAmountByPeriod not set. unable to increase accrued fees for ${poolFee.id}`) + } + } + const sumPoolFeesPendingAmount = await PoolFeeService.computeSumPendingFees(pool.id) + await pool.updateSumPoolFeesPendingAmount(sumPoolFeesPendingAmount) + await pool.save() + poolsToSnapshot.push(pool) + logger.info(`## Pool ${pool.id} states update completed!`) } + + logger.info('## Performing snapshots...') + const blockInfo = { number: block.block.header.number.toNumber(), timestamp: block.timestamp } + await statesSnapshotter('periodId', period.id, poolsToSnapshot, PoolSnapshot, blockInfo, 'poolId') + await statesSnapshotter('periodId', period.id, tranchesToSnapshot, TrancheSnapshot, blockInfo, 'trancheId') + await statesSnapshotter('periodId', period.id, assetsToSnapshot, AssetSnapshot, blockInfo, 'assetId') + await statesSnapshotter('periodId', period.id, poolFeesToSnapshot, PoolFeeSnapshot, blockInfo, 'poolFeeId') + logger.info('## Snapshotting completed!') + + //Update tracking of period and continue + await (await timekeeper).update(period.start) } diff --git a/src/mappings/handlers/ethHandlers.ts b/src/mappings/handlers/ethHandlers.ts index c6b6de9..f90faa3 100644 --- a/src/mappings/handlers/ethHandlers.ts +++ b/src/mappings/handlers/ethHandlers.ts @@ -119,7 +119,7 @@ async function _handleEthBlock(block: EthereumBlock): Promise { pool.id === ALT_1_POOL_ID && blockNumber > ALT_1_END_BLOCK ? BigInt(0) : (pool.portfolioValuation ?? BigInt(0)) + (pool.totalReserve ?? BigInt(0)) - await pool.updateNormalizedNAV() + await pool.updateNormalizedNAV(currency.decimals) logger.info(`Updating pool ${pool.id} with portfolioValuation: ${pool.portfolioValuation}`) } @@ -131,7 +131,7 @@ async function _handleEthBlock(block: EthereumBlock): Promise { : ReserveAbi__factory.createInterface().decodeFunctionResult('totalBalance', callResult.result)[0].toBigInt() pool.totalReserve = totalBalance pool.netAssetValue = (pool.portfolioValuation ?? BigInt(0)) + (pool.totalReserve ?? BigInt(0)) - await pool.updateNormalizedNAV() + await pool.updateNormalizedNAV(currency.decimals) logger.info(`Updating pool ${pool.id} with totalReserve: ${pool.totalReserve}`) } diff --git a/src/mappings/handlers/poolFeesHandlers.ts b/src/mappings/handlers/poolFeesHandlers.ts index 4aed302..ac5c38c 100644 --- a/src/mappings/handlers/poolFeesHandlers.ts +++ b/src/mappings/handlers/poolFeesHandlers.ts @@ -105,7 +105,7 @@ async function _handleFeeRemoved(event: SubstrateEvent): P } const poolFee = await PoolFeeService.delete(poolFeeData) - await poolFee.save() + if(poolFee) await poolFee.save() const poolFeeTransaction = PoolFeeTransactionService.delete(poolFeeData) await poolFeeTransaction.save() diff --git a/src/mappings/services/accountService.ts b/src/mappings/services/accountService.ts index abcd0e6..ef0da75 100644 --- a/src/mappings/services/accountService.ts +++ b/src/mappings/services/accountService.ts @@ -17,13 +17,13 @@ export class AccountService extends Account { } static async getOrInit(address: string, blockchainService = BlockchainService): Promise { - let account = (await this.get(address)) as AccountService | undefined + let account = (await this.get(address)) if (!account) { account = await this.init(address) await blockchainService.getOrInit(account.chainId) await account.save() } - return account + return account as AccountService } static evmToSubstrate(evmAddress: string, chainId: string) { diff --git a/src/mappings/services/assetService.ts b/src/mappings/services/assetService.ts index 8ee9910..43a7646 100644 --- a/src/mappings/services/assetService.ts +++ b/src/mappings/services/assetService.ts @@ -81,8 +81,8 @@ export class AssetService extends Asset { } static async getById(poolId: string, assetId: string) { - const asset = (await this.get(`${poolId}-${assetId}`)) as AssetService | undefined - return asset + const asset = (await this.get(`${poolId}-${assetId}`)) + return asset as AssetService } static async getByNftId(collectionId: string, itemId: string) { diff --git a/src/mappings/services/currencyService.ts b/src/mappings/services/currencyService.ts index eabca03..1d11b3d 100644 --- a/src/mappings/services/currencyService.ts +++ b/src/mappings/services/currencyService.ts @@ -17,7 +17,7 @@ export class CurrencyService extends Currency { static async getOrInit(chainId: string, currencyType: string, ...currencyValue: string[]) { const currencyId = currencyValue.length > 0 ? `${currencyType}-${currencyValue.join('-')}` : currencyType const id = `${chainId}-${currencyId}` - let currency = (await this.get(id)) as CurrencyService | undefined + let currency = (await this.get(id)) if (!currency) { const enumPayload = formatEnumPayload(currencyType, ...currencyValue) const assetMetadata = (await api.query.ormlAssetRegistry.metadata(enumPayload)) as Option @@ -33,7 +33,7 @@ export class CurrencyService extends Currency { static async getOrInitEvm(chainId: string, currencyType: string, symbol?: string, name?: string) { const currencyId = currencyType const id = `${chainId}-${currencyId}` - let currency: CurrencyService = (await this.get(id)) as CurrencyService + let currency = (await this.get(id)) if (!currency) { currency = this.init(chainId, currencyId, WAD_DIGITS, symbol, name) await currency.save() diff --git a/src/mappings/services/epochService.ts b/src/mappings/services/epochService.ts index ae3edd8..b3d6f20 100644 --- a/src/mappings/services/epochService.ts +++ b/src/mappings/services/epochService.ts @@ -43,7 +43,7 @@ export class EpochService extends Epoch { } static async getById(poolId: string, epochNr: number) { - const epoch = (await this.get(`${poolId}-${epochNr.toString()}`)) as EpochService + const epoch = (await this.get(`${poolId}-${epochNr.toString()}`)) as EpochService | undefined if (!epoch) return undefined const epochStates = await EpochState.getByEpochId(`${poolId}-${epochNr.toString(10)}`, { limit: 100 }) epoch.states = epochStates diff --git a/src/mappings/services/investorTransactionService.ts b/src/mappings/services/investorTransactionService.ts index 116b02c..3766a5a 100644 --- a/src/mappings/services/investorTransactionService.ts +++ b/src/mappings/services/investorTransactionService.ts @@ -162,8 +162,8 @@ export class InvestorTransactionService extends InvestorTransaction { } static async getById(hash: string) { - const tx = (await this.get(hash)) as InvestorTransactionService | undefined - return tx + const tx = await this.get(hash) + return tx as InvestorTransactionService | undefined } static computeTokenAmount(data: InvestorTransactionData) { diff --git a/src/mappings/services/poolFeeService.ts b/src/mappings/services/poolFeeService.ts index 0fbfe8f..53e4a4c 100644 --- a/src/mappings/services/poolFeeService.ts +++ b/src/mappings/services/poolFeeService.ts @@ -40,13 +40,13 @@ export class PoolFeeService extends PoolFee { blockchain = '0' ) { const { poolId, feeId } = data - let poolFee = (await this.get(`${poolId}-${feeId}`)) as PoolFeeService | undefined + let poolFee = await this.get(`${poolId}-${feeId}`) if (!poolFee) { poolFee = this.init(data, type, status, blockchain) } else { poolFee.status = PoolFeeStatus[status] } - return poolFee + return poolFee as PoolFeeService } static getById(poolId: string, feeId: string) { @@ -70,9 +70,12 @@ export class PoolFeeService extends PoolFee { logger.info(`Removing PoolFee ${data.feeId}`) const { poolId, feeId } = data const poolFee = await this.get(`${poolId}-${feeId}`) - if (!poolFee) throw new Error('Unable to remove PoolFee. PoolFee does not exist.') + if (!poolFee) { + logger.error('Unable to remove PoolFee. PoolFee does not exist.') + return + } poolFee.isActive = false - return poolFee + return poolFee as PoolFeeService } public charge(data: Omit & Required>) { diff --git a/src/mappings/services/poolService.test.ts b/src/mappings/services/poolService.test.ts index a9d7b5b..d81b468 100644 --- a/src/mappings/services/poolService.test.ts +++ b/src/mappings/services/poolService.test.ts @@ -86,7 +86,7 @@ describe('Given a new pool, when initialised', () => { describe('Given an existing pool,', () => { test.skip('when the nav is updated, then the value is fetched and set correctly', async () => { - await pool.updateNAV() + await pool.updateNAV(14) expect(api.query.loans.portfolioValuation).toHaveBeenCalled() expect(pool.portfolioValuation).toBe(BigInt(100000000000000)) }) diff --git a/src/mappings/services/poolService.ts b/src/mappings/services/poolService.ts index 0c6128d..14d3202 100644 --- a/src/mappings/services/poolService.ts +++ b/src/mappings/services/poolService.ts @@ -13,7 +13,6 @@ import { Pool } from '../../types' import { cid, readIpfs } from '../../helpers/ipfsFetch' import { EpochService } from './epochService' import { WAD_DIGITS } from '../../config' -import { CurrencyService } from './currencyService' import { assertPropInitialized } from '../../helpers/validation' export class PoolService extends Pool { @@ -214,7 +213,7 @@ export class PoolService extends Pool { return this } - public async updateNAV() { + public async updateNAV(poolCurrencyDecimals: number) { const specVersion = api.runtimeVersion.specVersion.toNumber() const specName = api.runtimeVersion.specName.toString() switch (specName) { @@ -225,7 +224,7 @@ export class PoolService extends Pool { await (specVersion < 1025 ? this.updateNAVQuery() : this.updateNAVCall()) break } - await this.updateNormalizedNAV() + await this.updateNormalizedNAV(poolCurrencyDecimals) return this } @@ -275,13 +274,9 @@ export class PoolService extends Pool { return this } - public async updateNormalizedNAV() { - assertPropInitialized(this, 'currencyId', 'string') + public async updateNormalizedNAV(poolCurrencyDecimals: number) { assertPropInitialized(this, 'netAssetValue', 'bigint') - - const currency = await CurrencyService.get(this.currencyId!) - if (!currency) throw new Error(`No currency with Id ${this.currencyId} found!`) - const digitsMismatch = WAD_DIGITS - currency.decimals + const digitsMismatch = WAD_DIGITS - poolCurrencyDecimals if (digitsMismatch === 0) { this.normalizedNAV = this.netAssetValue return this diff --git a/src/mappings/services/trancheBalanceService.ts b/src/mappings/services/trancheBalanceService.ts index 1056a6d..9cc45fb 100644 --- a/src/mappings/services/trancheBalanceService.ts +++ b/src/mappings/services/trancheBalanceService.ts @@ -26,11 +26,11 @@ export class TrancheBalanceService extends TrancheBalance { static getOrInit = async (address: string, poolId: string, trancheId: string) => { let trancheBalance = await this.getById(address, poolId, trancheId) - if (trancheBalance === undefined) { + if (!trancheBalance) { trancheBalance = this.init(address, poolId, trancheId) await trancheBalance.save() } - return trancheBalance + return trancheBalance as TrancheBalanceService } public investOrder(currencyAmount: bigint) { diff --git a/src/mappings/services/trancheService.ts b/src/mappings/services/trancheService.ts index 8ae371e..7a79d47 100644 --- a/src/mappings/services/trancheService.ts +++ b/src/mappings/services/trancheService.ts @@ -57,8 +57,8 @@ export class TrancheService extends Tranche { } static async getById(poolId: string, trancheId: string) { - const tranche = (await this.get(`${poolId}-${trancheId}`)) as TrancheService | undefined - return tranche + const tranche = (await this.get(`${poolId}-${trancheId}`)) + return tranche as TrancheService | undefined } static async getByPoolId(poolId: string): Promise {