/
utils.ts
72 lines (61 loc) · 2.08 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
import BN from "bn.js"
import { File } from "formdata-node"
import { Buffer } from "buffer"
import { balanceToNumber } from "../blockchain"
import { Errors } from "../constants"
import { RetryUploadErrorType } from "./types"
/**
* @name convertFileToBuffer
* @summary Converts a File to Buffer.
* @param file File to convert.
* @returns A Buffer.
*/
export const convertFileToBuffer = async (file: File): Promise<Buffer> => {
const arrayBuffer = await file.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
return buffer
}
/**
* @name formatPermill
* @summary Checks that percent is in range 0 to 100 and format to permill.
* @param percent Number in range from 0 to 100 with max 4 decimals.
* @returns The formated percent in permill format.
*/
export const formatPermill = (percent: number): number => {
if (percent > 100 || percent < 0) {
throw new Error(Errors.MUST_BE_PERCENTAGE)
}
return parseFloat(percent.toFixed(4)) * 10000
}
export const roundBalance = (amount: string) =>
Number(balanceToNumber(new BN(amount), { forceUnit: "-", withUnit: false }).split(",").join(""))
export const removeURLSlash = (url: string) => {
if (url.length === 0) return url
const lastChar = url.charAt(url.length - 1)
if (lastChar === "/") {
return url.slice(0, -1)
} else {
return url
}
}
export const retryPost = async <T>(fn: () => Promise<any>, n: number): Promise<T | RetryUploadErrorType> => {
let lastError: RetryUploadErrorType | undefined
for (let i = 0; i < n; i++) {
try {
return await fn()
} catch (err: any) {
lastError = {
isRetryError: true,
status: "SDK_RETRY_POST_ERROR",
message: err?.message ? err.message : JSON.stringify(err),
}
}
}
return lastError as RetryUploadErrorType
}
export const ensureHttps = (url: string) => {
if (!url) throw new Error(Errors.URL_UNDEFINED)
if (url.indexOf("https://") === 0) return url
else if (url.indexOf("http://") === 0) return url.replace("http://", "https://")
else return "https://" + url
}