/
utils.js
318 lines (293 loc) · 8.39 KB
/
utils.js
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
import { staking } from '@oasisprotocol/client';
import BigNumber from "bignumber.js";
import validUrl from 'valid-url';
import { cointypes } from '../../config';
import { getLocal } from "../background/storage/localStorage";
import { NET_WORK_CONFIG } from "../constant/storageKey";
/**
* Address interception
* @param {*} address
*/
export function addressSlice(address, sliceLength = 10) {
if (address) {
return `${address.slice(0, sliceLength)}...${address.slice(-sliceLength)}`
}
return address
}
/**
* Remove scientific notation
* @param {*} num_str
*/
export function toNonExponential(num_str) {
num_str = num_str.toString();
if (num_str.indexOf("+") != -1) {
num_str = num_str.replace("+", "");
}
if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) {
var resValue = "",
power = "",
result = null,
dotIndex = 0,
resArr = [],
sym = "";
var numStr = num_str.toString();
if (numStr[0] == "-") {
numStr = numStr.substr(1);
sym = "-";
}
if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) {
var regExp = new RegExp(
"^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$",
"ig"
);
result = regExp.exec(numStr);
if (result != null) {
resValue = result[2];
power = result[5];
result = null;
}
if (!resValue && !power) {
return false;
}
dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf(".");
resValue = resValue.replace(".", "");
resArr = resValue.split("");
if (Number(power) >= 0) {
var subres = resValue.substr(dotIndex);
power = Number(power);
for (var i = 0; i <= power - subres.length; i++) {
resArr.push("0");
}
if (power - subres.length < 0) {
resArr.splice(dotIndex + power, 0, ".");
}
} else {
power = power.replace("-", "");
power = Number(power);
for (var i = 0; i < power - dotIndex; i++) {
resArr.unshift("0");
}
var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
resArr.splice(n, 0, ".");
}
}
resValue = resArr.join("");
return sym + resValue;
} else {
return num_str;
}
}
/**
* Accuracy conversion No accuracy to accuracy
* @param {*} amount
* @param {*} decimal
*/
export function amountDecimals(amount, decimal = cointypes.decimals) {
let realBalance = new BigNumber(amount)
.dividedBy(new BigNumber(10).pow(decimal))
.toString();
return realBalance;
}
/**
* Accuracy conversion There is accuracy to no accuracy
* @param {*} amount
* @param {*} decimal
*/
export function amountToNoDecimals(amount, decimal = cointypes.decimals) {
let realBalance = new BigNumber(amount)
.multipliedBy(new BigNumber(10).pow(decimal))
.toString();
return realBalance;
}
/**
* Impression amount conversion. Default 4 decimal places
* @param {*} number
* @param {*} fixed
*/
export function getDisplayAmount(number, fixed = 4) {
if (isNaN(parseFloat(number)) || number === 0) {
return '0.00';
}
let showAmount = new BigNumber(number).toFixed(fixed, 1).toString()
return toNonExponential(showAmount)
}
export function getAmountDisplay(amount, decimal = 0, fixed = 4) {
let res1 = amountDecimals(amount, decimal)
let res2 = getDisplayAmount(res1, fixed)
return res2
}
export function getAmountForUI(rawAmount, decimal = cointypes.decimals) {
return new BigNumber(rawAmount)
.dividedBy(new BigNumber(10).pow(decimal))
.toFormat(2,
BigNumber.ROUND_DOWN,
{
groupSeparator: ',',
groupSize: 3,
decimalSeparator: '.',
});
}
/**
* Remove the spaces before and after the string
* @param {*} str
*/
export function trimSpace(str) {
let res = str.replace(/(^\s*)|(\s*$)/g, "")
res = res.replace(/[\r\n]/g, "")
return res
}
/**
* Verify that the address is valid
* @param {*} url
*/
export function urlValid(url) {
if (validUrl.isWebUri(url)) {
return true
}
return false
}
/**
* Determine if it is a number
* @param n
* @param includeE Whether to include scientific notation
*/
export function isNumber(n, includeE = false) {
let isNum = !!String(n).match(/^\d+\.?(?:\.\d+)?$/);
if (!isNum && includeE) {
return !!String(n).match(/^\d+e(-)?\d+$/);
}
return isNum;
}
/**
* Check whether it is an integer greater than 0
* @param {*} n
* @param {*} includeE
* @returns
*/
export function isTrueNumber(n) {
let isNum = !!String(n).match(/^([1-9][0-9]*)$/);
return isNum;
}
/**
* Check the length of the user name, the default is 16 bits
* @param {*} name
* @param {*} defaultLength
*/
export function nameLengthCheck(name, defaultLength = 16) {
let realLength = 0
let len = name.length
let charCode = -1;
for (let i = 0; i < len; i++) {
charCode = name.charCodeAt(i);
if (charCode >= 0 && charCode <= 128) {
realLength += 1;
} else {
realLength += 2;
}
}
if (realLength > defaultLength) {
return false
}
return true;
}
/**
* Copy text
*/
export function copyText(text) {
return navigator.clipboard.writeText(text)
.catch((error) => { alert(`Copy failed! ${error}`) })
}
/**
* Get digital precision
* @param {*} number
* @returns
*/
export function getNumberDecimals(number) {
if (isNumber(number)) {
let newNumer = new BigNumber(number).minus(new BigNumber(number).toFixed(0, 1).toString()).toString()
let splitList = newNumer.split('.');
if (splitList.length > 1) {
let littleNumber = splitList[1]
return littleNumber.length
} else {
return 0
}
} else {
return 0
}
}
export function getExplorerUrl() {
let localNetConfig = getLocal(NET_WORK_CONFIG)
let config = {}
if (localNetConfig) {
localNetConfig = JSON.parse(localNetConfig)
let currentList = localNetConfig.currentNetList
currentList = currentList.filter((item, index) => {
return item.isSelect
})
config = currentList && currentList.length > 0 ? currentList[0] : ""
}
return config.explorer
}
export const uint2hex = (uint) => Buffer.from(uint).toString('hex')
export const hex2uint = (hex) => new Uint8Array(Buffer.from(hex, 'hex'))
export const publicKeyToAddress = async (publicKey) => {
const data = await staking.addressFromPublicKey(publicKey)
return staking.addressToBech32(data)
}
export function getPrettyAddress(address) {
let prettyAddress = address
if (address.match(/^oasis1/)) {
const parts = address.split('1')
const hrp = parts[0]
const publicKey = parts[1].match(/.{1,4}/g)?.join(' ')
prettyAddress = `${hrp}1 ${publicKey}`
}
return prettyAddress
}
/**
* get params from input url
* @param {*} url
* @returns
*/
export function getQueryStringArgs(url) {
let qs = url || ""
let paramSplit = qs.split("?")
let paramStr = ''
if (paramSplit.length > 1) {
paramStr = paramSplit[1]
}
var args = {};
var items = paramStr.length > 0 ? paramStr.split("&") : [],
item = null, name = null, value = null;
var len = items.length;
for (var i = 0; i < len; i++) {
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
if (name.length > 0) {
args[name] = value
}
}
return args;
}
export function getOriginFromUrl(url) {
var origin = new URL(url).origin;
return origin
}
/**
* format connectAccount
* @param {*} account
* @returns
*/
export function connectAccountDataFilter(account){
return {
address: account.address,
accountName: account.accountName,
publicKey: account.publicKey,
type: account.type,
ledgerHdIndex:account.ledgerHdIndex,
isConnected:account.isConnected,
isConnecting:account.isConnecting,
}
}