-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathestimateGas.js
26 lines (20 loc) · 989 Bytes
/
estimateGas.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
var artifacts = require('truffle-artifactor');
var TestContract = artifacts.require("./contracts/Flashloan.sol");
// Run unit tests to populate my contract
// ...
// ...
// getGasPrice returns the gas price on the current network
TestContract.web3.eth.getGasPrice(function(error, result){
var gasPrice = Number(result);
console.log("Gas Price is " + gasPrice + " wei"); // "10000000000000"
// Get Contract instance
TestContract.deployed().then(function(instance) {
// Use the keyword 'estimateGas' after the function name to get the gas estimation for this particular function
return instance.giveAwayDividend.estimateGas(1);
}).then(function(result) {
var gas = Number(result);
console.log("gas estimation = " + gas + " units");
console.log("gas cost estimation = " + (gas * gasPrice) + " wei");
console.log("gas cost estimation = " + TestContract.web3.fromWei((gas * gasPrice), 'ether') + " ether");
});
});