Skip to content

Commit

Permalink
fix(hydra-indexer): add default values in case SubstrateExtrinsic pro…
Browse files Browse the repository at this point in the history
…perties are undefined (e.g. tip

affects: @subsquid/hydra-indexer

ISSUES CLOSED: #70
  • Loading branch information
dzhelezov committed Aug 18, 2021
1 parent 93532a5 commit 3d1dd9b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
@@ -0,0 +1,46 @@
import { expect } from 'chai'
import { Extrinsic } from '@polkadot/types/interfaces'
import { SubstrateBlockEntity } from './SubstrateBlockEntity'
import { fromBlockExtrinsic } from './SubstrateExtrinsicEntity'

const testData: {
e: Extrinsic
blockEntity: SubstrateBlockEntity
indexInBlock: number
} = {
e: {
isSigned: true,
hash: { toString: () => '0xdcdca' } as any,
signature: { toString: () => '0xbbbbb' } as any,
signer: { toString: () => '0xaaaaa' } as any,
nonce: { toNumber: () => 12332 } as any,
method: {
method: 'method',
section: 'section',
args: [],
} as any,
} as any,
blockEntity: {
height: 6666,
hash: '0xaaaaaa',
} as any,
indexInBlock: 1,
}

describe('SubstrateExtrinsicEntity', () => {
it('handles non-standard tips', () => {
const fromTestData = fromBlockExtrinsic(testData)

expect(fromTestData.tip).eq(BigInt(0), 'should set tip to zero by default')
})

it('creates block extrinsics', () => {
const fromTestData = fromBlockExtrinsic(testData)

expect(fromTestData.hash).eq('0xdcdca', 'should set hash')
expect(fromTestData.signature).eq('0xbbbbb', 'should set signature')
expect(fromTestData.signer).eq('0xaaaaa', 'should set signer')
expect(fromTestData.meta).eql({}, 'should set default meta')
expect(fromTestData.nonce).eq(12332, 'should set nonce')
})
})
21 changes: 11 additions & 10 deletions packages/hydra-indexer/src/entities/SubstrateExtrinsicEntity.ts
Expand Up @@ -124,21 +124,22 @@ export function fromBlockExtrinsic(data: {
extr.blockNumber = height
extr.blockHash = hash
extr.indexInBlock = indexInBlock
extr.signature = e.signature.toString()
extr.signer = e.signer.toString()
extr.signature = e.signature ? e.signature.toString() : ''
extr.signer = e.signer ? e.signer.toString() : ''

extr.method = e.method.method || 'NO_METHOD'
extr.section = e.method.section || 'NO_SECTION'
extr.method = (e.method && e.method.method) || 'NO_METHOD'
extr.section = (e.method && e.method.section) || 'NO_SECTION'
extr.name = fullName(e.method)

extr.meta = (e.meta.toJSON() || {}) as AnyJson
extr.hash = e.hash.toString()
extr.meta =
e.meta && e.meta.toJSON ? ((e.meta.toJSON() || {}) as AnyJson) : {}
extr.hash = e.hash ? e.hash.toString() : ''

extr.isSigned = e.isSigned
extr.tip = e.tip.toBigInt()
extr.versionInfo = e.version.toString()
extr.nonce = e.nonce.toNumber()
extr.era = (e.era.toJSON() || {}) as AnyJson
extr.tip = e.tip ? e.tip.toBigInt() : BigInt(0)
extr.versionInfo = e.version ? e.version.toString() : ''
extr.nonce = e.nonce ? e.nonce.toNumber() : 0
extr.era = e.era ? ((e.era.toJSON() || {}) as AnyJson) : {}

extr.args = []

Expand Down

0 comments on commit 3d1dd9b

Please sign in to comment.