-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.ts
More file actions
150 lines (118 loc) · 4.25 KB
/
Copy pathutils.ts
File metadata and controls
150 lines (118 loc) · 4.25 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Address, BigDecimal, BigInt, Bytes } from '@graphprotocol/graph-ts'
import { ADDRESS_LENGTH, DEFAULT_DECIMALS, ZERO_ADDRESS } from './constants'
export namespace address {
// Helpers
export function isZeroAddress(address: Address): boolean {
return address.toHexString() == ZERO_ADDRESS
}
}
export namespace bytes {
// Converters
export function toAddress(address: Bytes): Address {
return Address.fromHexString(address.toHexString()).subarray(-ADDRESS_LENGTH) as Address
}
export function toSignedInt(value: Bytes, bigEndian: boolean = true): BigInt {
return BigInt.fromSignedBytes(bigEndian ? (value.reverse() as Bytes) : value)
}
export function toUnsignedInt(value: Bytes, bigEndian: boolean = true): BigInt {
return BigInt.fromUnsignedBytes(bigEndian ? Bytes.fromUint8Array(value.reverse()) : value)
}
// Helpers
export function hexZeroPad(value: Bytes, length: i32 = 32): string {
let hexstring = value.toHexString()
return hexstring.substr(0, 2) + hexstring.substr(2).padStart(length * 2, '0')
}
}
export namespace decimal {
// Constants
export let NEGATIVE_ONE = BigDecimal.fromString('-1')
export let ZERO = BigDecimal.fromString('0')
export let ONE = BigDecimal.fromString('1')
export let TWO = BigDecimal.fromString('2')
export let ONE_HUNDRED = BigDecimal.fromString('100')
// Factory methods
export function fromBigInt(value: BigInt, decimals: i32 = DEFAULT_DECIMALS): BigDecimal {
let precision = BigInt.fromI32(10)
.pow(<u8>decimals)
.toBigDecimal()
return value.divDecimal(precision)
}
export function fromNumber(value: f64): BigDecimal {
return fromString(value.toString())
}
export function fromString(value: string): BigDecimal {
return BigDecimal.fromString(value)
}
// Converters
export function toBigInt(value: BigDecimal, decimals: u8 = DEFAULT_DECIMALS): BigInt {
return value.times(getPrecision(decimals).toBigDecimal()).truncate(0).digits
}
// Helpers
export function getPrecision(decimals: u8 = DEFAULT_DECIMALS): BigInt {
return BigInt.fromI32(10).pow(decimals)
}
export function min(a: BigDecimal, b: BigDecimal): BigDecimal {
return BigDecimal.compare(a, b) < 1 ? a : b
}
export function max(a: BigDecimal, b: BigDecimal): BigDecimal {
return BigDecimal.compare(a, b) > 1 ? a : b
}
}
export namespace integer {
// Constants
export let NEGATIVE_ONE = BigInt.fromI32(-1)
export let ZERO = BigInt.fromI32(0)
export let ONE = BigInt.fromI32(1)
export let TWO = BigInt.fromI32(2)
export let ONE_THOUSAND = BigInt.fromI32(1000)
export let WEI_PER_ETHER = BigInt.fromI32(<i32>1000000000000000000)
// Factory methods
export function fromNumber(value: i32): BigInt {
return BigInt.fromI32(value)
}
export function fromString(value: string): BigInt {
return fromNumber(parseI32(value))
}
// Converters
export function toBytes(value: BigInt): Bytes {
return (value as Uint8Array) as Bytes
}
// Helpers
export function decrement(value: BigInt, amount: BigInt = ONE): BigInt {
return value.minus(amount)
}
export function increment(value: BigInt, amount: BigInt = ONE): BigInt {
return value.plus(amount)
}
export function min(a: BigInt, b: BigInt): BigInt {
return BigInt.compare(a, b) < 1 ? a : b
}
export function max(a: BigInt, b: BigInt): BigInt {
return BigInt.compare(a, b) > 1 ? a : b
}
}
export namespace units {
export let WAD = BigDecimal.fromString('1000000000000000000')
export let RAY = BigDecimal.fromString('1000000000000000000000000000')
export let RAD = BigDecimal.fromString('1000000000000000000000000000000000000000000000')
// Factory methods
export function fromRad(value: BigInt): BigDecimal {
return value.divDecimal(RAD)
}
export function fromRay(value: BigInt): BigDecimal {
return value.divDecimal(RAY)
}
export function fromWad(value: BigInt): BigDecimal {
return value.divDecimal(WAD)
}
// Converters
export function toRad(value: BigDecimal): BigInt {
return value.times(RAD).truncate(0).digits
}
export function toRay(value: BigDecimal): BigInt {
return value.times(RAY).truncate(0).digits
}
export function toWad(value: BigDecimal): BigInt {
return value.times(WAD).truncate(0).digits
}
}