Skip to content

Commit

Permalink
fix: missingId error (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 authored Nov 13, 2024
1 parent 268d1c9 commit 99480da
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 209 deletions.
12 changes: 10 additions & 2 deletions src/helpers/stateSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export async function statesSnapshotter<T extends SnapshottableEntity, U extends
entitySaves.push(stateEntity.save())
}
entitySaves.push(snapshotEntity.save())
//logger.info(JSON.stringify(stateEntity))
//logger.info(JSON.stringify(snapshotEntity))
}
return Promise.all(entitySaves)
}
Expand Down Expand Up @@ -88,3 +86,13 @@ export interface BlockInfo {
number: number
timestamp: Date
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function logObject(object: any) {
return logger.info(
JSON.stringify(
object,
(_key, value) => (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged
)
)
}
359 changes: 182 additions & 177 deletions src/mappings/handlers/blockHandlers.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/mappings/handlers/ethHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function _handleEthBlock(block: EthereumBlock): Promise<void> {
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}`)
}

Expand All @@ -131,7 +131,7 @@ async function _handleEthBlock(block: EthereumBlock): Promise<void> {
: 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}`)
}

Expand Down
2 changes: 1 addition & 1 deletion src/mappings/handlers/poolFeesHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function _handleFeeRemoved(event: SubstrateEvent<PoolFeesRemovedEvent>): P
}

const poolFee = await PoolFeeService.delete(poolFeeData)
await poolFee.save()
if(poolFee) await poolFee.save()

const poolFeeTransaction = PoolFeeTransactionService.delete(poolFeeData)
await poolFeeTransaction.save()
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/accountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export class AccountService extends Account {
}

static async getOrInit(address: string, blockchainService = BlockchainService): Promise<AccountService> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/currencyService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssetMetadata>
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/services/epochService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/investorTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions src/mappings/services/poolFeeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<PoolFeeData, 'amount'> & Required<Pick<PoolFeeData, 'amount' | 'pending'>>) {
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/services/poolService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
13 changes: 4 additions & 9 deletions src/mappings/services/poolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/trancheBalanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/trancheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrancheService[]> {
Expand Down

0 comments on commit 99480da

Please sign in to comment.