This repository has been archived by the owner on Feb 14, 2025. It is now read-only.
forked from Uniswap/v2-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.ts
86 lines (76 loc) · 2.7 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import invariant from 'tiny-invariant'
import warning from 'tiny-warning'
import JSBI from 'jsbi'
import { getAddress } from '@ethersproject/address'
import { BigintIsh, ZERO, ONE, TWO, THREE, SolidityType, SOLIDITY_TYPE_MAXIMA, ZERO_ADDRESS } from './constants'
export function isETH(address: string): boolean {
return address === ZERO_ADDRESS
}
export function validateSolidityTypeInstance(value: JSBI, solidityType: SolidityType): void {
invariant(JSBI.greaterThanOrEqual(value, ZERO), `${value} is not a ${solidityType}.`)
invariant(JSBI.lessThanOrEqual(value, SOLIDITY_TYPE_MAXIMA[solidityType]), `${value} is not a ${solidityType}.`)
}
// warns if addresses are not checksummed
export function validateAndParseAddress(address: string): string {
try {
const checksummedAddress = getAddress(address)
warning(address === checksummedAddress, `${address} is not checksummed.`)
return checksummedAddress
} catch (error) {
invariant(false, `${address} is not a valid address.`)
}
}
export function parseBigintIsh(bigintIsh: BigintIsh): JSBI {
return bigintIsh instanceof JSBI
? bigintIsh
: typeof bigintIsh === 'bigint'
? JSBI.BigInt(bigintIsh.toString())
: JSBI.BigInt(bigintIsh)
}
// mock the on-chain sqrt function
export function sqrt(y: JSBI): JSBI {
validateSolidityTypeInstance(y, SolidityType.uint256)
let z: JSBI = ZERO
let x: JSBI
if (JSBI.greaterThan(y, THREE)) {
z = y
x = JSBI.add(JSBI.divide(y, TWO), ONE)
while (JSBI.lessThan(x, z)) {
z = x
x = JSBI.divide(JSBI.add(JSBI.divide(y, x), x), TWO)
}
} else if (JSBI.notEqual(y, ZERO)) {
z = ONE
}
return z
}
// given an array of items sorted by `comparator`, insert an item into its sort index and constrain the size to
// `maxSize` by removing the last item
export function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator: (a: T, b: T) => number): T | null {
invariant(maxSize > 0, 'MAX_SIZE_ZERO')
// this is an invariant because the interface cannot return multiple removed items if items.length exceeds maxSize
invariant(items.length <= maxSize, 'ITEMS_SIZE')
// short circuit first item add
if (items.length === 0) {
items.push(add)
return null
} else {
const isFull = items.length === maxSize
// short circuit if full and the additional item does not come before the last item
if (isFull && comparator(items[items.length - 1], add) <= 0) {
return add
}
let lo = 0,
hi = items.length
while (lo < hi) {
const mid = (lo + hi) >>> 1
if (comparator(items[mid], add) <= 0) {
lo = mid + 1
} else {
hi = mid
}
}
items.splice(lo, 0, add)
return isFull ? items.pop()! : null
}
}