Skip to content

Commit

Permalink
Remove optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
ef1rspb authored Aug 5, 2021
1 parent ceb38cd commit efdd63b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/mappings/HistoryElements.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {SubstrateEvent, SubstrateExtrinsic} from '@subql/types';
import {SubstrateExtrinsic} from '@subql/types';
import {HistoryElement, Transfer} from "../types";
import {
callFromProxy, callsFromBatch,
exportFeeFromDepositEvent,
exportFeeFromDepositEventAsString,
extrinsicIdFromBlockAndIdx, isBatch, isProxy,
isTransfer,
timestamp
} from "./common";
import {Balance} from "@polkadot/types/interfaces";
import {CallBase} from "@polkadot/types/types/calls";
import {AnyTuple} from "@polkadot/types/types/codec";
import {u64} from "@polkadot/types";
Expand Down Expand Up @@ -50,7 +49,7 @@ async function saveExtrinsic(extrinsic: SubstrateExtrinsic): Promise<void> {
module: extrinsic.extrinsic.method.section,
call: extrinsic.extrinsic.method.method,
success: extrinsic.success,
fee: exportFeeFromDepositEvent(extrinsic)?.toString()
fee: exportFeeFromDepositEventAsString(extrinsic)
}
await element.save()
}
Expand All @@ -74,7 +73,7 @@ function findFailedTransferCalls(extrinsic: SubstrateExtrinsic): Transfer[] | nu
from: sender.toString(),
to: tuple[0],
block: blockNumber,
fee: exportFeeFromDepositEvent(extrinsic).toString(),
fee: exportFeeFromDepositEventAsString(extrinsic),
extrinsicId: extrinsicIdFromBlockAndIdx(blockNumber, extrinsic.idx.toString()),
success: false
}
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/Rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async function handleSlashForTxHistory(slashEvent: SubstrateEvent): Promise<void
})
const validatorsSet = new Set(validatorsInSlashEra)

const initialValidator: string | null = null
const initialValidator: string = ""

await buildRewardEvents(
slashEvent.block,
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/StakeChanged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function handleBonded(event: SubstrateEvent): Promise<void> {
let accumulatedAmount = await handleAccumulatedStake(address, amountBalance)

const element = new StakeChange(eventId(event));
element.timestamp = timestamp(event.extrinsic.block)
element.timestamp = timestamp(event.block)
element.address = address
element.amount = amountBalance.toString()
element.accumulatedAmount = accumulatedAmount.toString()
Expand All @@ -29,7 +29,7 @@ export async function handleUnbonded(event: SubstrateEvent): Promise<void> {
let accumulatedAmount = await handleAccumulatedStake(address, -amountBalance)

const element = new StakeChange(eventId(event));
element.timestamp = timestamp(event.extrinsic.block)
element.timestamp = timestamp(event.block)
element.address = address
element.amount = amountBalance.toString()
element.accumulatedAmount = accumulatedAmount.toString()
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/Transfers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {HistoryElement} from '../types';
import {SubstrateEvent} from "@subql/types";
import {blockNumber, eventId, exportFeeFromDepositEvent, extrinsicId, timestamp} from "./common";
import {blockNumber, eventId, exportFeeFromDepositEventAsString, extrinsicId, timestamp} from "./common";

export async function handleTransfer(event: SubstrateEvent): Promise<void> {
const {event: {data: [from, to, ]}} = event;
Expand All @@ -27,7 +27,7 @@ async function populateTransfer(element: HistoryElement, event: SubstrateEvent):
from: from.toString(),
to: to.toString(),
block: blockNumber(event),
fee: exportFeeFromDepositEvent(event.extrinsic).toString(),
fee: exportFeeFromDepositEventAsString(event.extrinsic),
extrinsicId: extrinsicId(event),
success: true
}
Expand Down
14 changes: 11 additions & 3 deletions src/mappings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {Balance} from "@polkadot/types/interfaces";
import {CallBase} from "@polkadot/types/types/calls";
import {AnyTuple} from "@polkadot/types/types/codec";
import { Vec } from '@polkadot/types';
import {EventRecord} from "@polkadot/types/interfaces/system/types"


const batchCalls = ["batch", "batchAll"]
const transferCalls = ["transfer", "transferKeepAlive"]
Expand Down Expand Up @@ -43,7 +41,8 @@ export function eventIdFromBlockAndIdx(blockNumber: string, eventIdx: string) {
}

export function extrinsicId(event: SubstrateEvent): string {
return `${blockNumber(event)}-${event.extrinsic.idx.toString()}`
let idx: string = event.extrinsic ? event.extrinsic.idx.toString() : event.idx.toString()
return `${blockNumber(event)}-${idx}`
}

export function blockNumber(event: SubstrateEvent): string {
Expand Down Expand Up @@ -71,3 +70,12 @@ export function exportFeeFromDepositEvent(extrinsic: SubstrateExtrinsic): Balanc
return null
}
}

export function exportFeeFromDepositEventAsString(extrinsic?: SubstrateExtrinsic): string {
if (extrinsic) {
let fee = exportFeeFromDepositEvent(extrinsic)
return fee ? fee.toString() : "0"
} else {
return "0"
}
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"target": "es2020"
"target": "es2020",
"skipLibCheck": true,
"strictNullChecks": true
},
"include": [
"src/**/*",
Expand Down

0 comments on commit efdd63b

Please sign in to comment.