forked from akure/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.ts
378 lines (341 loc) · 11.2 KB
/
conversion.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { fromBech32, toBech32, toHex } from '@cosmjs/encoding'
import { TFunction } from 'next-i18next'
import { Loadable } from 'recoil'
import {
CachedLoadable,
Duration,
DurationUnits,
DurationWithUnits,
LoadingData,
LoadingDataWithError,
} from '@dao-dao/types'
import { Expiration } from '@dao-dao/types/contracts/common'
import { getChainForChainId } from './chain'
import { IPFS_GATEWAY_TEMPLATE, SITE_URL } from './constants'
export function convertMicroDenomToDenomWithDecimals(
amount: number | string,
decimals: number
) {
if (typeof amount === 'string') {
amount = Number(amount)
}
amount = amount / Math.pow(10, decimals)
return isNaN(amount) ? 0 : amount
}
export function convertDenomToMicroDenomWithDecimals(
amount: number | string,
decimals: number
) {
if (typeof amount === 'string') {
amount = Number(amount)
}
// Need to round. Example: `8.029409 * Math.pow(10, 6)`.
amount = Math.round(amount * Math.pow(10, decimals))
return isNaN(amount) ? 0 : amount
}
// Using BigInt.toString() ensures the value is not abbreviated. The
// Number.toString() function abbreviates large numbers like 1e20.
export const convertDenomToMicroDenomStringWithDecimals = (
...params: Parameters<typeof convertDenomToMicroDenomWithDecimals>
) => BigInt(convertDenomToMicroDenomWithDecimals(...params)).toString()
export function convertFromMicroDenom(denom: string) {
return denom?.substring(1).toUpperCase()
}
export function convertToFixedDecimals(amount: number | string): string {
if (typeof amount === 'string') {
amount = Number(amount)
}
if (amount > 0.01) {
return amount.toFixed(2)
} else return String(amount)
}
export const expirationAtTimeToSecondsFromNow = (exp: Expiration) => {
if (!('at_time' in exp)) {
return
}
const end = Number(exp['at_time'])
const nowSeconds = new Date().getTime() / 1000
const endSeconds = end / 1000000000
return endSeconds - nowSeconds
}
export const zeroPad = (num: number, target: number) => {
const s = num.toString()
if (s.length >= target) {
return s
}
return '0'.repeat(target - s.length) + s
}
export const spacePad = (number: string, target: number) =>
number.length >= length ? number : ' '.repeat(target - number.length) + number
export const convertDurationWithUnitsToSeconds = ({
units,
value,
}: DurationWithUnits): number => {
switch (units) {
case DurationUnits.Seconds:
return value
case DurationUnits.Minutes:
return value * 60
case DurationUnits.Hours:
return value * 60 * 60
case DurationUnits.Days:
return value * 60 * 60 * 24
case DurationUnits.Weeks:
return value * 60 * 60 * 24 * 7
case DurationUnits.Months:
return value * 60 * 60 * 24 * 30
case DurationUnits.Years:
return value * 60 * 60 * 24 * 365
default:
throw new Error(`Unsupported duration unit: ${units}`)
}
}
export const convertDurationWithUnitsToDuration = (
durationWithUnits: DurationWithUnits
): { time: number } => ({
time: convertDurationWithUnitsToSeconds(durationWithUnits),
})
// Use largest whole-number unit possible.
export const convertSecondsToDurationWithUnits = (
seconds: number
): DurationWithUnits => {
if (seconds % (60 * 60 * 24 * 365) === 0) {
return {
value: seconds / (60 * 60 * 24 * 365),
units: DurationUnits.Years,
}
} else if (seconds % (60 * 60 * 24 * 30) === 0) {
return {
value: seconds / (60 * 60 * 24 * 30),
units: DurationUnits.Months,
}
} else if (seconds % (60 * 60 * 24 * 7) === 0) {
return {
value: seconds / (60 * 60 * 24 * 7),
units: DurationUnits.Weeks,
}
} else if (seconds % (60 * 60 * 24) === 0) {
return {
value: seconds / (60 * 60 * 24),
units: DurationUnits.Days,
}
} else if (seconds % (60 * 60) === 0) {
return {
value: seconds / (60 * 60),
units: DurationUnits.Hours,
}
} else if (seconds % 60 === 0) {
return {
value: seconds / 60,
units: DurationUnits.Minutes,
}
} else {
return {
value: seconds,
units: DurationUnits.Seconds,
}
}
}
export const convertDurationWithUnitsToHumanReadableString = (
t: TFunction,
{ units, value }: DurationWithUnits
): string =>
`${value} ${t(`unit.${units}`, {
count: value,
}).toLocaleLowerCase()}`
// Convert Recoil loadable into our generic data loader type with a default
// value on error. See the comment above the LoadingData type for more details.
export const loadableToLoadingData = <T>(
loadable: CachedLoadable<T> | Loadable<T>,
defaultValue: T,
onError?: (error: any) => void
): LoadingData<T> => {
if (loadable.state === 'hasError') {
onError?.(loadable.contents)
}
return loadable.state === 'loading' ||
// If on server, start by loading to prevent hyration error.
typeof window === 'undefined'
? { loading: true }
: loadable.state === 'hasValue'
? {
loading: false,
updating: 'updating' in loadable ? loadable.updating : undefined,
data: loadable.contents,
}
: {
loading: false,
data: defaultValue,
}
}
// Combine many data loaders into one.
export const combineLoadingDatas = <T>(
...loadables: LoadingData<T[]>[]
): LoadingData<T[]> =>
loadables.some((l) => l.loading)
? {
loading: true,
}
: {
loading: false,
data: loadables.flatMap((l) => (l.loading ? [] : l.data)),
}
// Combine many data with error loaders into one.
export const combineLoadingDataWithErrors = <T>(
...loadables: LoadingDataWithError<T[]>[]
): LoadingDataWithError<T[]> =>
loadables.some((l) => l.loading)
? {
loading: true,
errored: false,
}
: loadables.some((l) => l.errored)
? {
loading: false,
errored: true,
// First error.
error: loadables.flatMap((l) => (l.errored ? l.error : []))[0],
}
: {
loading: false,
errored: false,
data: loadables.flatMap((l) => (l.loading || l.errored ? [] : l.data)),
}
// Convert Recoil loadable into our generic data loader with error type. See the
// comment above the LoadingData type for more details.
export const loadableToLoadingDataWithError = <T>(
loadable: CachedLoadable<T> | Loadable<T>
): LoadingDataWithError<T> => {
return loadable.state === 'loading' ||
// If on server, start by loading to prevent hyration error.
typeof window === 'undefined'
? { loading: true, errored: false }
: loadable.state === 'hasValue'
? {
loading: false,
errored: false,
updating: 'updating' in loadable ? loadable.updating : undefined,
data: loadable.contents,
}
: {
loading: false,
errored: true,
error: !loadable.contents
? new Error('Unknown error')
: loadable.contents instanceof Error
? loadable.contents
: new Error(`${loadable.contents}`),
}
}
export const convertExpirationToDate = (
blocksPerYear: number,
expiration: Expiration,
// For converting height to rough date.
currentBlockHeight: number
): Date | undefined =>
'at_height' in expiration && currentBlockHeight > 0
? new Date(
Date.now() +
convertBlocksToSeconds(
blocksPerYear,
expiration.at_height - currentBlockHeight
) *
1000
)
: 'at_time' in expiration
? // Timestamp is in nanoseconds, convert to microseconds.
new Date(Number(expiration.at_time) / 1e6)
: undefined
export const convertBlocksToSeconds = (blocksPerYear: number, blocks: number) =>
Math.round((blocks / blocksPerYear) * 365 * 24 * 60 * 60)
export const convertSecondsToBlocks = (
blocksPerYear: number,
seconds: number
) => Math.round((seconds * blocksPerYear) / (365 * 24 * 60 * 60))
export const durationToSeconds = (blocksPerYear: number, duration: Duration) =>
'height' in duration
? convertBlocksToSeconds(blocksPerYear, duration.height)
: duration.time
// Convert IPFS protocol URL to HTTPS protocol URL using IPFS gateway.
export const transformIpfsUrlToHttpsIfNecessary = (ipfsUrl: string) =>
ipfsUrl.startsWith('ipfs://')
? IPFS_GATEWAY_TEMPLATE.replace('PATH', ipfsUrl.replace('ipfs://', ''))
: ipfsUrl
// Transform image URLs to ensure they can be accessed. They need to be using
// https protocol, not ipfs, and potentially from a whitelisted IPFS gateway.
// They only need to be from a whitelisted IPFS gateway if being used in a
// NextJS Image component (in which case proxy should also be set to true so
// that non-IPFS images are proxied through our whitelisted proxy domain).
export const toAccessibleImageUrl = (
url: string,
{ proxy, replaceRelative }: { proxy?: boolean; replaceRelative?: boolean } = {
proxy: false,
replaceRelative: false,
}
) => {
// If hosted locally, passthrough (probably development/test env).
if (url.startsWith('/')) {
return replaceRelative ? SITE_URL + url : url
}
url = transformIpfsUrlToHttpsIfNecessary(url)
// Convert `https://CID.ipfs.nftstorage.link` to
// `https://nftstorage.link/ipfs/CID` because we have to explicitly whitelist
// domains, and the CID is the part that changes.
if (url.includes('.ipfs.nftstorage.link')) {
const matches = url.match(/([a-zA-Z0-9]+)\.ipfs\.nftstorage\.link(.*)$/)
if (matches?.length === 3) {
url = `https://nftstorage.link/ipfs/${matches[1]}${matches[2]}`
return url
}
}
// Convert `https://CID.ipfs.cf-ipfs.com` to `https://cf-ipfs.com/ipfs/CID`
// because we have to explicitly whitelist domains, and the CID is the part
// that changes.
if (url.includes('.ipfs.cf-ipfs.com')) {
const matches = url.match(/([a-zA-Z0-9]+)\.ipfs\.cf-ipfs\.com(.*)$/)
if (matches?.length === 3) {
url = `https://cf-ipfs.com/ipfs/${matches[1]}${matches[2]}`
return url
}
}
// If this is not an IPFS image, we can't enforce that it is coming from one
// of our NextJS allowed image sources. Thus proxy it through a whitelisted
// domain. This only needs to be used for images that are displayed in the
// NextJS Image component, which is why it is optional and off by default.
if (proxy && !url.includes('ipfs')) {
url = `https://img-proxy.ekez.workers.dev/${url}`
}
return url
}
// Converts an address to its corresponding validator address.
export const toValidatorAddress = (address: string, bech32Prefix: string) => {
try {
return toBech32(bech32Prefix + 'valoper', fromBech32(address).data)
} catch (err) {
return ''
}
}
// Convert bech32 address to general hex bech32 hash.
export const toBech32Hash = (address: string) => {
try {
return toHex(fromBech32(address).data)
} catch (err) {
return ''
}
}
export const concatAddressStartEnd = (
address: string,
takeStart: number,
takeEnd: number
) => {
const first = address.substring(0, takeStart)
const last = address.substring(address.length - takeEnd, address.length)
return [first, last].filter(Boolean).join('..')
}
export const concatAddressBoth = (address: string, takeN = 7): string =>
address && concatAddressStartEnd(address, takeN, takeN)
export const transformBech32Address = (address: string, toChainId: string) =>
toBech32(
getChainForChainId(toChainId).bech32_prefix,
fromBech32(address).data
)