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

Moving helper functions from dex-react #69

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ export const FEE_PERCENTAGE = (1 / FEE_DENOMINATOR) * 100 // syntatic sugar for

// Amount for an order to be considered unlimited, from contract's point of view: https://github.com/gnosis/dex-contracts/blob/master/contracts/BatchExchange.sol#L35
export const UNLIMITED_ORDER_AMOUNT = TWO.pow(new BN(128)).sub(ONE)

// Batch ID of orders without expiration date set
export const MAX_BATCH_ID = 2 ** 32 - 1
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './time'
export * from './ethereum'
export * from './format'
export * from './orders'
export * from './misc'
7 changes: 7 additions & 0 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AssertionError } from 'assert'

export function assert<T>(val: T, message: string): asserts val is NonNullable<T> {
if (!val) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't exactly a NonNullable assertion, though it may be ok for our purposes
!val will break on every falsy value (0, '')

https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullablet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestion on how to fix this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do sth like val ?? somethingElse where 0 and '' will be accepted but nothing else

nullish coalescense or sth similar in TS 3.7+

throw new AssertionError({ message })
}
}
13 changes: 13 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { MAX_BATCH_ID } from 'const'

/**
* Epoch in seconds
*/
export function getEpoch(): number {
return Math.floor(Date.now() / 1000)
}

/**
* Checks whether batchId is `never expires`
* Using the convention which the fronted creates orders with expiration date set
* to max uint32 when placing orders active indefinitely.
*
* @param batchId The expiration batch id to check
*/
export function isNeverExpiresOrder(batchId: number): boolean {
return MAX_BATCH_ID === batchId
}