forked from astroport-fi/astroport-classic-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
181 lines (150 loc) · 4.56 KB
/
helpers.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
import { TxInfo } from "@terra-money/terra.js";
import num from "libs/num";
import numeral from "numeral";
import { request } from "graphql-request";
export const handleBigPercentage = (
value: string | number,
format: string = "0,0a.00",
numberSuffix: string = "%"
) => {
if (num(value).gte(100000)) {
return `> 100K${numberSuffix}`;
}
return `${
numeral(value).format(format).toUpperCase() || (0).toFixed(2)
}${numberSuffix}`;
};
export const handleBigAndTinyAmount = (
value: string | number,
format: string = "0,0.00",
includeDollarSign: boolean = false,
includeZero: boolean = false,
numberPrefix: string = ""
) => {
if (includeZero && num(value).eq(0)) {
return `< ${numberPrefix}0.01${includeDollarSign ? " USTC" : ""}`;
}
if (num(value).lt(0.01) && num(value).gt(0)) {
return `< ${numberPrefix}0.01${includeDollarSign ? " USTC" : ""}`;
}
if (num(value).gt(1000000)) {
return `${numberPrefix}${numeral(value)
.format("0.00a", Math.floor)
.toUpperCase()}${includeDollarSign ? " USTC" : ""}`;
}
return `${numberPrefix}${numeral(value).format(format)}${
includeDollarSign ? " USTC " : ""
}`;
};
export const handleTinyAmount = (
value: string | number,
format: string = "0,0.00",
includeZero: boolean = false,
numberPrefix: string = ""
) => {
const bigNumValue = num(value);
if (includeZero && bigNumValue.eq(0)) {
return `< 0.01${numberPrefix}`;
}
// not so necessary but also can check if it's positive number -bignumValue.gt()
if (bigNumValue.lt(0.01)) {
return `< 0.01${numberPrefix}`;
}
return `${numeral(value).format(format)}${numberPrefix}`;
};
export const handleDollarTinyAmount = (
value: string | number,
format: string = "0,0.00",
includeZero: boolean = false
) => {
return handleTinyAmount(value, format, includeZero, " USTC");
};
export const handleAmountWithoutTrailingZeros = (
value: number,
significantDigits: number = 2
) => {
return parseFloat(value.toFixed(significantDigits));
};
export const requestInChunks = async <Item = any, Response = any>(
chunkSize: number,
url: string,
items: Item[],
queryBuilder: (chunk: Item[]) => string
): Promise<Response> => {
const totalChunks = Math.ceil(items.length / chunkSize);
const chunks = await Promise.all(
Array.from(Array(totalChunks).keys()).map((i) => {
const chunk = items.slice(i * chunkSize, (i + 1) * chunkSize);
return request<Response>(url, queryBuilder(chunk));
})
);
return chunks.reduce((all, chunk) => ({ ...all, ...chunk }));
};
export const truncateStr = (str: string, length: number) => {
if (str?.length > length) {
return str.substring(0, length).trim() + "...";
}
return str;
};
export const validateJsonInput = (json: string): Boolean => {
try {
JSON.parse(json);
} catch (e) {
return false;
}
return true;
};
export const getEventsByType = (txInfo: TxInfo, index: number = 0): any => {
if (!txInfo) {
return null;
}
if (txInfo.logs && index < 0) {
index = txInfo.logs.length - index * -1;
}
if (!txInfo.logs || txInfo.logs.length <= index || !txInfo.logs[index]) {
return null;
}
return txInfo.logs[index]?.eventsByType;
};
export const partition = (array: any, isValid: any) => {
return array.reduce(
([pass, fail]: [any, any], elem: any) => {
return isValid(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]];
},
[[], []]
);
};
export const searchTokenAdddres = (search: string, pools: any[]): any[] => {
return pools.filter((row: any) => {
const query = search.toLowerCase();
const assets = row.sortingAssets;
if (!assets) {
return true;
}
for (const asset of assets) {
const string = asset ? String(asset).toLowerCase() : "";
// If the sortingAsset string looks like an address (starts with "terra"),
// then the query must begin with "terra" and also match. If the sortingAsset string
// does not look like an address (e.g. a symbol), then any query can match.
// This allows the search to be useful when the query is short (e.g. 1 character),
// otherwise it'd match lots of pools by address.
if (
(!string.startsWith("terra") || query.startsWith("terra")) &&
string.includes(query)
) {
return true;
}
}
return false;
});
};
export const toggleValueInArray = (value: any, array: any[]) => {
const newArray = [...array];
const valueIndex = array.indexOf(value);
if (valueIndex > -1) {
newArray.splice(valueIndex, 1);
} else {
newArray.push(value);
}
return newArray;
};