forked from octalmage/terrajs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
80 lines (71 loc) · 2.48 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
const ethers = require("ethers")
const _ = require("lodash")
const fetch = require("isomorphic-fetch")
const config = require("./config")
const consoleError = ({ message, err, tags }) => {
const error = new Error(message)
error.extra = JSON.stringify(err.response ? err.response.data.message : err.message ? err.message : err)
error.tags = {
area: "crypto_coverage",
blockchain: "terra",
...(tags || {}),
}
console.error(error)
}
// return Coins
const getGasPrice = async (network) => {
let gasPrices
try {
gasPrices = await (await fetch(_getNetworkDetails(network).gasStation)).json()
} catch (err) {
consoleError({
message: `pricing API ${_getNetworkDetails(network).gasStation} for ${network} is failing see extra for error`,
err,
})
gasPrices = { uluna: "0.15" }
}
return Number(gasPrices.uluna)
}
const _getNetworkDetails = (network) => (network === "main" ? config.networks.main : config.networks.testnet)
// returns Number
const _toDecimal = (amount, decimals) => ethers.utils.formatUnits(amount, decimals)
// returns ethers.BigNumber
const _toCrypto = (amount, decimals) => (typeof amount === "string" ? ethers.utils.parseUnits(amount, decimals) : ethers.utils.parseUnits(amount.toString(), decimals))
// return Number
const _fetchFeeFromTx = (tx) => _toDecimal(`${_.get(tx, "auth_info.fee.amount._coins.uluna.amount", 0)}`, 6)
//return object { to, from, value(inCrypto),denom , fee, nonce }
const _fetchTrasactionData = (tx, decimals = 6) => {
let response
const { to_address, from_address, amount, execute_msg, contract, sender } = tx.body.messages[0]
const fee = _fetchFeeFromTx(tx)
const nonce = _.get(tx, "auth_info.signer_infos")[0].sequence
if (!contract) {
let value, denom
for (const coin in amount._coins) {
denom = coin
value = _toDecimal(`${amount._coins[coin].amount}`, decimals)
}
response = {
to: to_address,
from: from_address,
value,
denom,
}
} else {
response = {
to: sender,
from: _.get(execute_msg, "transfer.recipient"),
value: _toDecimal(_.get(execute_msg, "transfer.amount"), decimals),
}
}
return { ...response, fee, nonce }
}
module.exports = {
consoleError,
getGasPrice,
_getNetworkDetails,
_fetchTrasactionData,
_fetchFeeFromTx,
_toCrypto,
_toDecimal,
}