-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add schemas for working with bigints of all kinds
- Loading branch information
1 parent
bdd23a5
commit fa4c759
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
) | ||
}) | ||
}) | ||
}) |