Skip to content

Commit

Permalink
add mingo compare operator for address
Browse files Browse the repository at this point in the history
  • Loading branch information
XY-Wang committed Jun 20, 2024
1 parent 198e36d commit 47a1b4a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
64 changes: 64 additions & 0 deletions packages/core/src/subjects/mingo-ops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,70 @@ describe('mingo query ops', () => {
}).toThrow()
})

it('should compare eq addresses', () => {
const q = new Query({
'args.dest.id': { $address_eq: '14NEHDwc5PPQfEjzLVDbVbi4djQLQZ9u7mMU3BPhTFJf4cD6' },
})

expect(q.test(data)).toBeTruthy()
})

it('should compare eq addresses with different ss58 formats', () => {
const q = new Query({
'args.dest.id': { $address_eq: 'dfZvF6iz8qvsdGEWTHBoo2daJWq386QYfDXwfsycTJhicxLcc' },
})

expect(q.test(data)).toBeTruthy()
})

it('should compare eq addresses falsy case', () => {
const q = new Query({
'args.dest.id': { $address_eq: '12Dw5aURKmAG8fCRtBa28tEvBVDChnaoKsjpewv53d6e6766' },
})

expect(q.test(data)).toBeFalsy()
})

it('should compare neq addresses', () => {
const q = new Query({
'args.dest.id': { $address_neq: '12Dw5aURKmAG8fCRtBa28tEvBVDChnaoKsjpewv53d6e6766' },
})

expect(q.test(data)).toBeTruthy()
})

it('should compare neq addresses with different ss58 formats', () => {
const q = new Query({
'args.dest.id': { $address_neq: '7LxmCfshfK8TeiJWUmwZr6NqCWbGeyeAoEi61SvKX4Ea3UNn' },
})

expect(q.test(data)).toBeTruthy()
})

it('should compare neq addresses with different ss58 falsy case', () => {
const q = new Query({
'args.dest.id': { $address_neq: 'dfZvF6iz8qvsdGEWTHBoo2daJWq386QYfDXwfsycTJhicxLcc' },
})

expect(q.test(data)).toBeFalsy()
})

it('should return neq if is not valid address', () => {
const q = new Query({
'args.dest.id': { $address_neq: 'foobar' },
})

expect(q.test(data)).toBeFalsy()
})

it('should return neq if value is not string', () => {
const q = new Query({
'args.dest.id': { $address_neq: 111 },
})

expect(q.test(data)).toBeFalsy()
})

it('should be idempotent on ops registration', () => {
expect(() => {
installOperators()
Expand Down
27 changes: 26 additions & 1 deletion packages/core/src/subjects/mingo-ops.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2023-2024 SO/DA zone
// SPDX-License-Identifier: Apache-2.0

import { bnToBn, logger } from '@polkadot/util'
import { bnToBn, isU8a, logger } from '@polkadot/util'
import { addressEq } from '@polkadot/util-crypto'

import { OperatorType, Options, QueryOperator, getOperator, useOperators } from 'mingo/core'
import { BASIC_CONTEXT } from 'mingo/init/basic'
Expand Down Expand Up @@ -49,6 +50,28 @@ function $bn_neq(a: AnyVal, b: AnyVal): boolean {
return compare(a, b, (x: AnyVal, y: AnyVal) => !bn(x).eq(bn(y)))
}

function $address_eq(a: AnyVal, b: AnyVal): boolean {
if ((typeof a === 'string' || isU8a(a)) && (typeof b === 'string' || isU8a(b))) {
try {
return addressEq(a, b)
} catch (_) {
return false
}
}
return false
}

function $address_neq(a: AnyVal, b: AnyVal): boolean {
if ((typeof a === 'string' || isU8a(a)) && (typeof b === 'string' || isU8a(b))) {
try {
return !addressEq(a, b)
} catch (_) {
return false
}
}
return false
}

function createQueryOperator(predicate: Predicate<AnyVal>): QueryOperator {
const f = (selector: string, value: AnyVal, options: Options) => {
const opts = { unwrapArray: true }
Expand Down Expand Up @@ -80,6 +103,8 @@ export function installOperators() {
$bn_gte: createQueryOperator($bn_gte),
$bn_eq: createQueryOperator($bn_eq),
$bn_neq: createQueryOperator($bn_neq),
$address_eq: createQueryOperator($address_eq),
$address_neq: createQueryOperator($address_neq),
})
}
}

0 comments on commit 47a1b4a

Please sign in to comment.