Skip to content

Commit

Permalink
fix: improve handling of undefined get services
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 committed Nov 12, 2024
1 parent c1619eb commit 13d7a55
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 200 deletions.
11 changes: 10 additions & 1 deletion src/@types/gobal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export {}
import { ApiPromise } from '@polkadot/api'
import type { Provider } from '@ethersproject/providers'
import { ApiDecoration } from '@polkadot/api/types'
import '@subql/types-core/dist/global'
export type ApiAt = ApiDecoration<'promise'> & {
rpc: ApiPromise['rpc']
}
declare global {
const api: ApiAt | Provider
const unsafeApi: ApiPromise | undefined
function getNodeEvmChainId(): Promise<string | undefined>
}
export {}
5 changes: 3 additions & 2 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AugmentedCall, AugmentedRpc, PromiseRpcResult } from '@polkadot/api/typ
import { Enum, Null, Struct, u128, u32, u64, U8aFixed, Option, Vec, Bytes, Result, bool } from '@polkadot/types'
import { AccountId32, Perquintill, Balance } from '@polkadot/types/interfaces'
import { ITuple, Observable } from '@polkadot/types/types'
import type { ApiAt } from '../@types/gobal'

export interface PoolDetails extends Struct {
reserve: { total: u128; available: u128; max: u128 }
Expand Down Expand Up @@ -503,7 +504,7 @@ export type PoolFeesList = Vec<PoolFeesOfBucket>

export type OracleFedEvent = ITuple<[feeder: DevelopmentRuntimeOriginCaller, key: OracleKey, value: u128]>

export type ExtendedRpc = typeof api.rpc & {
export type ExtendedRpc = ApiAt['rpc'] & {
pools: {
trancheTokenPrice: PromiseRpcResult<
AugmentedRpc<(poolId: number | string, trancheId: number[]) => Observable<u128>>
Expand All @@ -512,7 +513,7 @@ export type ExtendedRpc = typeof api.rpc & {
}
}

export type ExtendedCall = typeof api.call & {
export type ExtendedCall = ApiAt['call'] & {
loansApi: {
portfolio: AugmentedCall<'promise', (poolId: string) => Observable<Vec<ITuple<[u64, LoanInfoActivePortfolio]>>>>
expectedCashflows: AugmentedCall<
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { u64 } from '@polkadot/types'
import type { Provider } from '@ethersproject/providers'

const isSubstrateNode = 'query' in api
const isEvmNode = typeof (api as unknown as Provider).getNetwork === 'function'
const ethNetworkProm = isEvmNode ? (api as unknown as Provider).getNetwork() : null
const isEvmNode = typeof (api as Provider).getNetwork === 'function'
const ethNetworkProm = isEvmNode ? (api as Provider).getNetwork() : null

global.fetch = fetch as unknown as typeof global.fetch
global.atob = atob as typeof global.atob
Expand Down
5 changes: 4 additions & 1 deletion src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { EpochService } from '../services/epochService'
import { SnapshotPeriodService } from '../services/snapshotPeriodService'
import { TrancheBalanceService } from '../services/trancheBalanceService'
import { InvestorPositionService } from '../services/investorPositionService'
import { ApiAt } from '../../@types/gobal'

const cfgApi = api as ApiAt
const timekeeper = TimekeeperService.init()

export const handleBlock = errorHandler(_handleBlock)
Expand All @@ -35,7 +37,7 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
const newPeriod = (await timekeeper).processBlock(block.timestamp)

if (newPeriod) {
const specVersion = api.runtimeVersion.specVersion.toNumber()
const specVersion = cfgApi.runtimeVersion.specVersion.toNumber()
logger.info(
`# It's a new period on block ${blockNumber}: ${block.timestamp.toISOString()} (specVersion: ${specVersion})`
)
Expand Down Expand Up @@ -103,6 +105,7 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
pool.resetUnrealizedProfit()
for (const loanId in activeLoanData) {
const asset = await AssetService.getById(pool.id, loanId)
if(!asset) continue
if (!asset.currentPrice) throw new Error('Asset current price not set')
if (!asset.notional) throw new Error('Asset notional not set')
await asset.loadSnapshot(lastPeriodStart)
Expand Down
Loading

0 comments on commit 13d7a55

Please sign in to comment.