Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
improve log filter options
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Mar 23, 2022
1 parent 5dab52c commit 2163f08
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 82 deletions.
131 changes: 73 additions & 58 deletions src/utils/option-formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { EventCriteriaSet, LogQueryBody, LogQueryOptions, LogQueryRange, Order,

import * as utils from './'

export const fromETHBlockNumber = function(blockNumber: StringOrNumber): StringOrNumber {
const OBJhas = (obj: Object, p: string)=>{
return obj.hasOwnProperty(p)
}

export const fromETHBlockNumber = function (blockNumber: StringOrNumber): StringOrNumber {
if (typeof blockNumber === 'number') {
return blockNumber
} else if (typeof blockNumber === 'string') {
Expand All @@ -20,55 +24,84 @@ export const fromETHBlockNumber = function(blockNumber: StringOrNumber): StringO
}
}

export const fromETHBlockNumberOrHash = function(blockRevision: any): StringOrNumber {
export const fromETHBlockNumberOrHash = function (blockRevision: any): StringOrNumber {
if (/^(-0x|0x)?[0-9a-fA-F]{64}$/i.test(blockRevision)) {
return blockRevision
} else {
return fromETHBlockNumber(blockRevision)
}
}

export const formatRange = function(range: any): LogQueryRange | null {
const ret: LogQueryRange = {}
if (range.unit !== 'block' && range.unit !== 'time') {
return null
} else {
ret.unit = range.unit
export const formatRange = function (range: any): LogQueryRange {
const defaultUnit = 'block'

const ret: LogQueryRange = {
unit: range.unit || defaultUnit,
from: 0,
to: Number.MAX_SAFE_INTEGER
}
if (range.hasOwnProperty('from')) {
const temp = fromETHBlockNumber(range.from)
if (temp !== 'best') { ret.from = temp as number } else { ret.from = 0 }
} else {
ret.from = 0

if (ret.unit !== 'block' && ret.unit !== 'time') {
ret.unit = defaultUnit
}
if (range.hasOwnProperty('to')) {
const temp = utils.fromETHBlockNumber(range.to)
if (temp !== 'best') { ret.to = temp as number } else { ret.to = Number.MAX_SAFE_INTEGER }
} else {
ret.to = Number.MAX_SAFE_INTEGER


if (range.unit === 'block') {
if (OBJhas(range, 'from')) {
const t = fromETHBlockNumber(range.from)
if (t === 'best') {
ret.from = Number.MAX_SAFE_INTEGER
} else {
ret.from = t as number
}
}
if (OBJhas(range, 'to')) {
const t = fromETHBlockNumber(range.to)
if (t === 'best') {
ret.to = Number.MAX_SAFE_INTEGER
} else {
ret.to = t as number
}
}
return ret
}

if (OBJhas(range, 'from')) {
const t = utils.toInteger(range.from)
if (t !== null) {
ret.from = t
}
}

if (OBJhas(range, 'to')) {
const t = utils.toInteger(range.to)
if (t !== null) {
ret.to = t
}
}

return ret
}

export const formatOptions = function(options: any): LogQueryOptions | null {
const ret: LogQueryOptions = {}
if (options.hasOwnProperty('limit')) {
const temp = utils.toInteger(options.limit)
if (temp) { ret.limit = temp }
export const formatOptions = function (options: any): LogQueryOptions {
const ret: LogQueryOptions = {
offset: 0,
limit: utils.params.defaultLogLimit,
}
if (options.hasOwnProperty('offset')) {

if (OBJhas(options, 'offset')) {
const temp = utils.toInteger(options.offset)
if (temp) { ret.offset = temp }
}
if (ret.hasOwnProperty('limit') || ret.hasOwnProperty('offset')) {
return ret
} else {
return null
if (OBJhas(options, 'limit')) {
const temp = utils.toInteger(options.limit)
if (temp) { ret.limit = temp }
}

return ret
}

export const formatLogQuery = function(params: any): LogQueryBody {
export const formatLogQuery = function (params: any): LogQueryBody {
let address = ''
let order: Order = 'ASC'
if (params.address) {
Expand All @@ -80,39 +113,21 @@ export const formatLogQuery = function(params: any): LogQueryBody {

const body: LogQueryBody = {
criteriaSet: [],
order,
order
}

if (params.range) {
const ret = formatRange(params.range)
if (ret) {
body.range = ret
}
}
body.options = formatOptions(params.options||{})

if (params.options) {
const ret = formatOptions(params.options)
if (ret) {
body.options = ret
}
}

if (!body.range && (params.hasOwnProperty('fromBlock') || params.hasOwnProperty('toBlock'))) {

body.range = {
// discard fromBlock and toBlock if range presents
if (params.range) {
body.range = formatRange(params.range)
} else {
const range = {
unit: 'block',
from: params.fromBlock || 0,
to: params.toBlock || Number.MAX_SAFE_INTEGER
}

if (params.hasOwnProperty('fromBlock')) {
body.range.from = params.fromBlock
}

if (params.hasOwnProperty('toBlock')) {
body.range.to = params.toBlock
}

body.range = formatRange(body.range) as LogQueryRange

body.range = formatRange(range)
}

const topics: TopicItem[] = []
Expand All @@ -133,7 +148,7 @@ export const formatLogQuery = function(params: any): LogQueryBody {
}
}

const outputTopic = function(inputTopics: TopicItem[], index: number, receiver: EventCriteriaSet[], current: EventCriteriaSet) {
const outputTopic = function (inputTopics: TopicItem[], index: number, receiver: EventCriteriaSet[], current: EventCriteriaSet) {
if (index === inputTopics.length) {
const o = {}
if (address) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const params = {
defaultGasPriceCoef: 0,
defaultExpiration: 720,
defaultLogLimit: 50,
}

export {
Expand Down
12 changes: 11 additions & 1 deletion test/extend/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { expect } from 'chai'
import { web3, xhrUtility } from '../test-utils/init'
import * as utils from '../../src/utils'

const ABI = [{ anonymous: false, inputs: [{ indexed: true, name: '_from', type: 'address' }, { indexed: true, name: '_to', type: 'address' }, { indexed: false, name: '_value', type: 'uint256' }], name: 'Transfer', type: 'event' }]
const Address = '0x0000000000000000000000000000456e65726779'
Expand All @@ -21,7 +22,9 @@ describe('web3.contract', () => {
expect((body as any).criteriaSet).to.be.an('array').to.have.lengthOf(1)
expect((body as any).criteriaSet[0]).to.have.property('address', Address)

expect(body).to.not.have.property('options')
expect(body).to.have.property('options')
expect((body as any).options).to.have.property('offset', 0)
expect((body as any).options).to.have.property('limit', utils.params.defaultLogLimit)
})

it('getPastEvents', async () => {
Expand All @@ -31,6 +34,13 @@ describe('web3.contract', () => {

expect(url).to.be.equal('/logs/event')
expect(body).to.have.property('order', 'ASC')
expect(body).to.have.property('range')
expect((body as any).range).to.have.property('unit', 'block')
expect((body as any).range).to.have.property('from', 0)
expect((body as any).range).to.have.property('to', Number.MAX_SAFE_INTEGER)
expect(body).to.have.property('options')
expect((body as any).options).to.have.property('offset', 0)
expect((body as any).options).to.have.property('limit', utils.params.defaultLogLimit)
})

})
55 changes: 32 additions & 23 deletions test/utils/option-formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ describe('utils:fromETHBlockNumberOrHash', () => {
})

describe('utils:formatRange', () => {

it('empty input', () => {
expect(utils.formatRange({})).to.be.equal(null)
})

it('minimal input', () => {
const ret = utils.formatRange({ unit: 'block' })
expect(ret.unit).to.be.equal('block')
Expand All @@ -71,45 +66,57 @@ describe('utils:formatRange', () => {
expect(ret.from).to.be.equal(0)
expect(ret.to).to.be.equal(Number.MAX_SAFE_INTEGER)
})
})

describe('utils:formatOptions', () => {
it('time range input', () => {
const ret = utils.formatRange({ unit: 'time', from: 0, to: 1000 })
console.log(ret)
expect(ret.unit).to.be.equal('time')
expect(ret.from).to.be.equal(0)
expect(ret.to).to.be.equal(1000)
})

it('empty input', () => {
const ret = utils.formatOptions({})
expect(ret).to.be.equal(null)
it('time: invalid input', () => {
const ret = utils.formatRange({ unit: 'time', from: 'invalid', to: 'invalid' })
expect(ret.unit).to.be.equal('time')
expect(ret.from).to.be.equal(0)
expect(ret.to).to.be.equal(Number.MAX_SAFE_INTEGER)
})
})

describe('utils:formatOptions', () => {
it('valid input', () => {
const ret = utils.formatOptions({ limit: 100, offset: 100 })
expect(ret.limit).to.be.equal(100)
expect(ret.offset).to.be.equal(100)
})

it('valid invalid input', () => {
it('invalid input', () => {
const ret = utils.formatOptions({ limit: 'invalid', offset: 'invalid' })
expect(ret).to.be.equal(null)
expect(ret).to.have.property('offset', 0)
expect(ret).to.have.property('limit', utils.params.defaultLogLimit)
})

})

describe('utils:formatLogQuery', () => {

it('empty input', () => {
const ret = utils.formatLogQuery({})
expect(ret).to.not.have.property('options')
expect(ret).to.not.have.property('range')
})

it('valid options', () => {
const ret = utils.formatLogQuery({ options: { limit: 100, offset: 100 } })
expect(ret.options.limit).to.be.equal(100)
expect(ret.options.offset).to.be.equal(100)
})

it('invalid options', () => {
it('invalid options should result default options', () => {
const ret = utils.formatLogQuery({ options: {} })
expect(ret).to.not.have.property('options')
expect(ret).to.property('options')
expect(ret.options).to.have.property('offset', 0)
expect(ret.options).to.have.property('limit', utils.params.defaultLogLimit)
})

it('no options should result default', () => {
const ret = utils.formatLogQuery({})
expect(ret).to.property('options')
expect(ret.options).to.have.property('offset', 0)
expect(ret.options).to.have.property('limit', utils.params.defaultLogLimit)
})

it('valid range', () => {
Expand All @@ -121,7 +128,9 @@ describe('utils:formatLogQuery', () => {

it('invalid range', () => {
const ret = utils.formatLogQuery({ range: { unit: 'invalid' } })
expect(ret).to.not.have.property('range')
expect(ret).to.have.property('range')
expect(ret.range.from).to.be.equal(0)
expect(ret.range.to).to.be.equal(Number.MAX_SAFE_INTEGER)
})

it('valid from block', () => {
Expand All @@ -133,7 +142,7 @@ describe('utils:formatLogQuery', () => {

it('invalid from and to block', () => {
const ret = utils.formatLogQuery({ fromBlock: 'latest', toBlock: 'latest' })
expect(ret.range.from).to.be.equal(0)
expect(ret.range.from).to.be.equal(Number.MAX_SAFE_INTEGER)
expect(ret.range.to).to.be.equal(Number.MAX_SAFE_INTEGER)
})

Expand Down

0 comments on commit 2163f08

Please sign in to comment.