forked from guix77/truffle-cost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (62 loc) · 1.61 KB
/
index.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
const fetch = require("node-fetch")
/**
* Data returned to Mocha reporter.
*/
const result = {
gasUsed: null,
fiatSymbol: null,
fiatCost: null,
original: null,
final: null
};
/**
* Fetched prices.
*/
let prices = {
gas: 0.00000002
}
/**
* Reset before each test.
*/
const reset = () => {
result.gasUsed = null;
result.fiatCost = null;
result.fiatSymbol = null;
result.original = null;
result.final = null;
return;
}
/**
* Main function: log gas used for a transaction and optionally cost in fiat.
*/
const log = async(asyncFn, fiatSymbol = "GBP") => {
if (Object.keys(prices).length == 1) {
const res = await fetch('https://api.coinbase.com/v2/exchange-rates?currency=ETH');
const json = await res.json();
prices = Object.assign(prices, json.data.rates)
}
// Transaction result.
const txResult = await asyncFn;
// Used gas for Mocha reporter.
result.gasUsed += txResult.receipt.gasUsed;
// If call has a fiat symbol param,
// Fiat symbol for Mocha.
const symbol = fiatSymbol.toUpperCase()
result.fiatSymbol = fiatSymbol;
let price = parseInt(prices[symbol])
if (result.original == null) {
result.original = txResult.receipt.gasUsed
} else {
result.final = txResult.receipt.gasUsed
}
// Fiat cost for Mocha.
result.fiatCost = (result.gasUsed * prices.gas * price).toFixed(2);
// Return result of transaction to Truffle TestRunner.
return txResult;
}
// Export everything.
module.exports = {
reset: reset,
log: log,
result: () => { return result }
}