From ad5f69e86fc10c4a355314313da944c40f0d897a Mon Sep 17 00:00:00 2001 From: Eldar Gabdullin Date: Fri, 1 Dec 2023 20:52:16 +0400 Subject: [PATCH] fix DataFrame mapping --- evm/evm-processor/src/ds-rpc/schema.ts | 7 ++--- evm/evm-processor/src/mapping/schema.ts | 2 +- test/erc20-transfers/src/processor.ts | 1 - .../src/composite/default.ts | 27 +++++++++++++++++++ util/util-internal-validation/src/dsl.ts | 9 +++++++ 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 util/util-internal-validation/src/composite/default.ts diff --git a/evm/evm-processor/src/ds-rpc/schema.ts b/evm/evm-processor/src/ds-rpc/schema.ts index 7f59b3ed5..604397095 100644 --- a/evm/evm-processor/src/ds-rpc/schema.ts +++ b/evm/evm-processor/src/ds-rpc/schema.ts @@ -11,7 +11,8 @@ import { SMALL_QTY, STRING, taggedUnion, - Validator + Validator, + withDefault } from '@subsquid/util-internal-validation' import {Bytes, Bytes20} from '../interfaces/base' import {FieldSelection} from '../interfaces/data' @@ -114,7 +115,7 @@ function getDebugFrameValidator(fields: FieldSelection['trace']) { gas: QTY, input: BYTES, gasUsed: QTY, - output: BYTES, + output: withDefault('0x', BYTES), to: BYTES }) }) @@ -133,7 +134,7 @@ function getDebugFrameValidator(fields: FieldSelection['trace']) { from: BYTES, value: option(QTY), gas: QTY, - output: BYTES, + output: withDefault('0x', BYTES), gasUsed: QTY }) }) diff --git a/evm/evm-processor/src/mapping/schema.ts b/evm/evm-processor/src/mapping/schema.ts index dee613d91..f2fc1a357 100644 --- a/evm/evm-processor/src/mapping/schema.ts +++ b/evm/evm-processor/src/mapping/schema.ts @@ -43,7 +43,7 @@ export function getTxProps(fields: FieldSelection['transaction'], forArchive: bo maxFeePerGas: option(QTY), maxPriorityFeePerGas: option(QTY), input: BYTES, - nonce: withSentinel('Transaction.nonce', -1, NAT), + nonce: withSentinel('Transaction.nonce', -1, natural), value: withSentinel('Transaction.value', -1n, QTY), v: withSentinel('Transaction.v', -1n, QTY), r: withSentinel('Transaction.r', '0x', BYTES), diff --git a/test/erc20-transfers/src/processor.ts b/test/erc20-transfers/src/processor.ts index a5d2c8ee0..75e438a07 100644 --- a/test/erc20-transfers/src/processor.ts +++ b/test/erc20-transfers/src/processor.ts @@ -25,7 +25,6 @@ processor.run(new TypeormDatabase({supportHotBlocks: true}), async ctx => { let transfers: Transfer[] = [] for (let block of ctx.blocks) { - ctx.log.info(block) for (let log of block.logs) { if (log.address == CONTRACT && log.topics[0] === erc20.events.Transfer.topic) { let {from, to, value} = erc20.events.Transfer.decode(log) diff --git a/util/util-internal-validation/src/composite/default.ts b/util/util-internal-validation/src/composite/default.ts new file mode 100644 index 000000000..8da51c67e --- /dev/null +++ b/util/util-internal-validation/src/composite/default.ts @@ -0,0 +1,27 @@ +import {ValidationFailure} from '../error' +import {Validator} from '../interface' + + +export class Default implements Validator { + constructor( + public readonly value: T, + public readonly item: Validator + ) {} + + cast(value: unknown): ValidationFailure | T { + if (value == null) { + return this.value + } else { + return this.item.cast(value) + } + } + + validate(value: unknown): ValidationFailure | undefined { + if (value == null) return + return this.item.validate(value) + } + + phantom(): S | undefined | null { + return undefined + } +} diff --git a/util/util-internal-validation/src/dsl.ts b/util/util-internal-validation/src/dsl.ts index a707cc915..150e8da4c 100644 --- a/util/util-internal-validation/src/dsl.ts +++ b/util/util-internal-validation/src/dsl.ts @@ -1,5 +1,6 @@ import {ArrayValidator} from './composite/array' import {ConstantValidator} from './composite/constant' +import {Default} from './composite/default' import {GetKeyTaggedUnionCast, GetKeyTaggedUnionSrc, KeyTaggedUnionValidator} from './composite/key-tagged-union' import {NullableValidator} from './composite/nullable' import {GetPropsCast, GetPropsSrc, ObjectValidator} from './composite/object' @@ -83,6 +84,14 @@ export function withSentinel>( } +export function withDefault>( + value: GetCastType, + validator: V +): Validator, GetSrcType | undefined | null> { + return new Default(value, validator) +} + + export function ref>(get: () => V): Validator, GetSrcType> { return new RefValidator(get) }