Skip to content

Commit

Permalink
chore: Add schemas for working with bigints of all kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 5, 2023
1 parent bdd23a5 commit fa4c759
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/utils-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.0",
"@layerzerolabs/lz-definitions": "~1.5.68",
Expand All @@ -53,7 +54,8 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsup": "~8.0.1",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"zod": "^3.22.4"
},
"peerDependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
Expand All @@ -62,6 +64,7 @@
"@ethersproject/providers": "^5.7.0",
"@layerzerolabs/lz-definitions": "~1.5.67",
"@layerzerolabs/ua-utils": "~0.1.0",
"@layerzerolabs/utils": "~0.0.1"
"@layerzerolabs/utils": "~0.0.1",
"zod": "^3.22.4"
}
}
1 change: 1 addition & 0 deletions packages/utils-evm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './address'
export * from './omnigraph'
export * from './provider'
export * from './schema'
export * from './signer'
6 changes: 6 additions & 0 deletions packages/utils-evm/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from 'zod'
import { BigNumber, BigNumberish, isBigNumberish } from '@ethersproject/bignumber/lib/bignumber'

export const BigNumberishSchema = z.custom<BigNumberish>((value: unknown) => isBigNumberish(value))

export const BigNumberishBigintSchema = BigNumberishSchema.transform(BigNumber.from).transform((bn) => bn.toBigInt())
44 changes: 44 additions & 0 deletions packages/utils-evm/test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fc from 'fast-check'
import { AddressZero } from '@ethersproject/constants'
import { evmAddressArbitrary } from '@layerzerolabs/test-utils'
import { ignoreZero, makeZero } from '@/address'
import { BigNumber, BigNumberish } from '@ethersproject/bignumber'
import { BigNumberishBigintSchema, BigNumberishSchema } from '@/schema'

describe('schema', () => {
const bigIntArbitrary = fc.bigInt()
const uintArbitrary = fc.integer({ min: 0 })
const bigIntStringArbitrary = bigIntArbitrary.map(String)
const bigNumberArbitrary = bigIntStringArbitrary.map(BigNumber.from)
const bytesArbitrary = fc.array(fc.integer({ min: 0, max: 255 }), { minLength: 1 })
const bigNumberishArbitrary: fc.Arbitrary<BigNumberish> = fc.oneof(
bigIntArbitrary,
uintArbitrary,
bigIntStringArbitrary,
bigNumberArbitrary,
bytesArbitrary
)

describe('BigNumberishSchema', () => {
it('should parse BigNumberish', () => {
fc.assert(
fc.property(bigNumberishArbitrary, (bigNumberish) => {
expect(BigNumberishSchema.parse(bigNumberish)).toBe(bigNumberish)
})
)
})
})

describe('BigNumberishBigintSchema', () => {
it('should parse BigNumberish into a bigint', () => {
fc.assert(
fc.property(bigNumberishArbitrary, (bigNumberish) => {
const parsed = BigNumberishBigintSchema.parse(bigNumberish)

expect(typeof parsed).toBe('bigint')
expect(BigNumber.from(parsed)).toEqual(BigNumber.from(bigNumberish))
})
)
})
})
})

0 comments on commit fa4c759

Please sign in to comment.