-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
91 lines (84 loc) · 3.42 KB
/
utility.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
const request = require('request');
const Web3 = require('web3');
const config = require('./config.js');
const web3 = new Web3(new Web3.providers.HttpProvider(config.ethRPC));
const SolidityFunction = require('web3');
module.exports = {
weiToEth: function (wei, divisorIn) {
const divisor = !divisorIn ? 1000000000000000000 : divisorIn;
return (wei / divisor).toFixed(3);
},
ethToWei: function (eth, divisorIn) {
const divisor = !divisorIn ? 1000000000000000000 : divisorIn;
return parseFloat((eth * divisor).toPrecision(10));
},
roundToNearest: function (numToRound, numToRoundToIn) {
const numToRoundTo = 1 / numToRoundToIn;
return Math.round(numToRound * numToRoundTo) / numToRoundTo;
},
getURL: function (url, callback, options) {
request.get(url, options, (err, httpResponse, body) => {
if (err) {
callback(err, undefined);
} else {
callback(undefined, body);
}
})
},
postURL: function (url, formData, callback) {
request.post({url, form: formData}, (err, httpResponse, body) => {
if (err) {
callback(err, undefined);
} else {
callback(undefined, body);
}
})
},
call: function (web3In, contract, address, functionName, args, callback) {
function proxy(retries) {
const data = contract[functionName].getData.apply(null, args);
let url = config.etherscanAPI`/api?module=proxy&action=eth_Call&to=${address}&data=${data}`;
if (config.etherscanAPIKey) url += `&apikey=${config.etherscanAPIKey}`;
getURL(url, (err, body) => {
if (!err) {
try {
const result = JSON.parse(body);
const functionAbi = contract.abi.find(element => element.name === functionName);
const solidityFunction = new SolidityFunction(web3.Eth, functionAbi, address);
const resultUnpacked = solidityFunction.unpackOutput(result.result);
callback(undefined, resultUnpacked);
} catch (errJson) {
if (retries > 0) {
setTimeout(() => {
proxy(retries - 1)
}, 1000)
} else {
callback(err, undefined);
}
}
} else {
callback(err, undefined);
}
})
}
try {
const data = contract[functionName].getData.apply(null, args);
web3In.eth.call({to: address, data}, (err, result) => {
if (!err) {
const functionAbi = contract.abi.find(element => element.name === functionName);
const solidityFunction = new SolidityFunction(web3In.Eth, functionAbi, address);
try {
const resultUnpacked = solidityFunction.unpackOutput(result);
callback(undefined, resultUnpacked);
} catch (errJson) {
proxy(1);
}
} else {
proxy(1);
}
})
} catch (err) {
proxy(1);
}
},
};