Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eth-gas-reporter

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eth-gas-reporter - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

CHANGELOG.md

154

index.js

@@ -1,92 +0,106 @@

var mocha = require('mocha');
var inherits = require('util').inherits;
var Base = mocha.reporters.Base;
var color = Base.color;
const mocha = require('mocha')
const inherits = require('util').inherits
const stats = require('./gasStats.js')
const Base = mocha.reporters.Base
const color = Base.color
const log = console.log
module.exports = Gas
module.exports = Gas;
// Based on 'Spec'. Please smoke outside while running your suite. For safety.
// Based on the 'Spec' reporter
function Gas (runner) {
Base.call(this, runner);
Base.call(this, runner)
var self = this;
var indents = 0;
var n = 0;
var startBlock;
const self = this
let indents = 0
let n = 0
let startBlock
let methodMap
function indent () {
return Array(indents).join(' ');
}
// ------------------------------------ Helpers -------------------------------------------------
const indent = () => Array(indents).join(' ')
function calculateGasUsed(){
let gasUsed = 0;
const endBlock = web3.eth.blockNumber;
while(startBlock <= endBlock){
let block = web3.eth.getBlock(startBlock);
if (block)
gasUsed += block.gasUsed;
startBlock++;
const gasAnalytics = (methodMap) => {
let gasUsed = 0
const endBlock = web3.eth.blockNumber
while (startBlock <= endBlock) {
let block = web3.eth.getBlock(startBlock)
if (block) {
// Add to running tally for this test
gasUsed += block.gasUsed
// Compile per method stats
methodMap && block.transactions.forEach(tx => {
const input = web3.eth.getTransaction(tx).input;
const receipt = web3.eth.getTransactionReceipt(tx);
const id = stats.getMethodID( input );
if (methodMap[id]){
methodMap[id].gasData.push(receipt.gasUsed);
}
})
}
startBlock++
}
return gasUsed;
};
return gasUsed
}
runner.on('start', function () {
console.log();
});
// ------------------------------------ Runners -------------------------------------------------
runner.on('start', () => {
methodMap = stats.mapMethodsToContracts(artifacts)
log()
})
runner.on('suite', function (suite) {
++indents;
console.log(color('suite', '%s%s'), indent(), suite.title);
});
runner.on('suite', suite => {
++indents
log(color('suite', '%s%s'), indent(), suite.title)
})
runner.on('suite end', function () {
--indents;
runner.on('suite end', () => {
--indents
if (indents === 1) {
console.log();
log()
}
});
})
runner.on('hook end', function () {
startBlock = web3.eth.blockNumber + 1;
runner.on('pending', test => {
let fmt = indent() + color('pending', ' - %s')
log(fmt, test.title)
})
runner.on('test', function() {});
runner.on('hook end', () => { startBlock = web3.eth.blockNumber + 1 })
runner.on('pending', function (test) {
var fmt = indent() + color('pending', ' - %s');
console.log(fmt, test.title);
});
runner.on('pass', test => {
let fmt
let limitString
let gasUsed = gasAnalytics(methodMap)
//let percent = 0
let percent = stats.gasToPercentOfLimit(gasUsed);
runner.on('pass', function (test) {
let fmt;
let gasUsed = calculateGasUsed();
if (test.speed === 'fast') {
fmt = indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s')+
color('checkmark', ' (%d gas)');
console.log(fmt, test.title, gasUsed);
if (percent >= 100){
limitString = color('fail', ' (%d% of limit) ')
} else {
fmt = indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
color(test.speed, ' (%dms)') +
color('checkmark', ' (%d gas)');
console.log(fmt, test.title, test.duration, gasUsed);
limitString = color('pass', ' (%d% of limit) ')
}
});
runner.on('fail', function (test) {
let gasUsed = calculateGasUsed();
fmt = indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
color('checkmark', ' (%d gas)') +
limitString
log(fmt, test.title, gasUsed, percent)
})
runner.on('fail', test => {
let gasUsed = gasAnalytics()
let fmt = indent() +
color('fail', ' %d) %s') +
color('pass', ' (%d gas)');
console.log()
color('pass', ' (%d gas)')
log()
log(fmt, ++n, test.title, gasUsed)
})
console.log(fmt, ++n, test.title, gasUsed);
});
runner.on('end', self.epilogue.bind(self));
runner.on('end', () => {
self.epilogue();
stats.generateGasStatsReport (methodMap)
})
}

@@ -97,2 +111,2 @@

*/
inherits(Gas, Base);
inherits(Gas, Base)

@@ -1,5 +0,5 @@

var Migrations = artifacts.require("./Migrations.sol");
var Migrations = artifacts.require('./Migrations.sol')
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
module.exports = function (deployer) {
deployer.deploy(Migrations)
}

@@ -1,8 +0,12 @@

var ConvertLib = artifacts.require("./ConvertLib.sol");
var MetaCoin = artifacts.require("./MetaCoin.sol");
var ConvertLib = artifacts.require('./ConvertLib.sol')
var MetaCoin = artifacts.require('./MetaCoin.sol')
var Wallet = artifacts.require('./Wallets/Wallet.sol')
var VariableCosts = artifacts.require('./VariableCosts.sol')
module.exports = function(deployer) {
module.exports = function (deployer) {
deployer.deploy(ConvertLib);
deployer.link(ConvertLib, MetaCoin);
deployer.deploy(MetaCoin);
};
deployer.deploy(Wallet);
deployer.deploy(VariableCosts);
}
{
"name": "mock",
"version": "1.0.0",
"description": "",
"main": "truffle.js",
"directories": {
"test": "test"
},
"name": "eth-gas-reporter",
"version": "0.0.4",
"description": "Mocha reporter which shows gas used per unit test.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "./mock/test.sh"
},
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/cgewecke/eth-gas-reporter.git"
},
"keywords": [
"Ethereum",
"solidity",
"unit-testing",
"truffle",
"gas."
],
"author": "cgewecke <github.com/cgewecke>",
"license": "MIT",
"bugs": {
"url": "https://github.com/cgewecke/eth-gas-reporter/issues"
},
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme",
"dependencies": {
"abi-decoder": "^1.0.8",
"cli-table2": "^0.2.0",
"colors": "^1.1.2",
"lodash": "^4.17.4",
"req-cwd": "^2.0.0",
"request": "^2.83.0",
"request-promise-native": "^1.0.5",
"shelljs": "^0.7.8"
},
"devDependencies": {
"eth-gas-reporter": "0.0.3",
"ethereumjs-testrpc": "^4.1.3",

@@ -17,0 +38,0 @@ "truffle": "^3.4.11"

@@ -1,73 +0,72 @@

var MetaCoin = artifacts.require("./MetaCoin.sol");
var MetaCoin = artifacts.require('./MetaCoin.sol')
contract('MetaCoin', function(accounts) {
beforeEach(async function(){
await MetaCoin.new();
await MetaCoin.new();
});
afterEach(async function(){
await MetaCoin.new();
await MetaCoin.new();
});
contract('MetaCoin', function (accounts) {
beforeEach(async function () {
await MetaCoin.new()
await MetaCoin.new()
})
afterEach(async function () {
await MetaCoin.new()
await MetaCoin.new()
})
it("should put 10000 MetaCoin in the first account", function() {
return MetaCoin.deployed().then(function(instance) {
return instance.getBalance.call(accounts[0]);
}).then(function(balance) {
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
});
});
it("should call a function that depends on a linked library", function() {
var meta;
var metaCoinBalance;
var metaCoinEthBalance;
it('should put 10000 MetaCoin in the first account', function () {
return MetaCoin.deployed().then(function (instance) {
return instance.getBalance.call(accounts[0])
}).then(function (balance) {
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account")
})
})
it('should call a function that depends on a linked library', function () {
var meta
var metaCoinBalance
var metaCoinEthBalance
return MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.getBalance.call(accounts[0]);
}).then(function(outCoinBalance) {
metaCoinBalance = outCoinBalance.toNumber();
return meta.getBalanceInEth.call(accounts[0]);
}).then(function(outCoinBalanceEth) {
metaCoinEthBalance = outCoinBalanceEth.toNumber();
}).then(function() {
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken");
});
});
it("should send coin correctly", function() {
var meta;
return MetaCoin.deployed().then(function (instance) {
meta = instance
return meta.getBalance.call(accounts[0])
}).then(function (outCoinBalance) {
metaCoinBalance = outCoinBalance.toNumber()
return meta.getBalanceInEth.call(accounts[0])
}).then(function (outCoinBalanceEth) {
metaCoinEthBalance = outCoinBalanceEth.toNumber()
}).then(function () {
assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, 'Library function returned unexpected function, linkage may be broken')
})
})
it('should send coin correctly', function () {
var meta
// Get initial balances of first and second account.
var account_one = accounts[0];
var account_two = accounts[1];
var account_one = accounts[0]
var account_two = accounts[1]
var account_one_starting_balance;
var account_two_starting_balance;
var account_one_ending_balance;
var account_two_ending_balance;
var account_one_starting_balance
var account_two_starting_balance
var account_one_ending_balance
var account_two_ending_balance
var amount = 10;
var amount = 10
return MetaCoin.deployed().then(function(instance) {
meta = instance;
return meta.getBalance.call(account_one);
}).then(function(balance) {
account_one_starting_balance = balance.toNumber();
return meta.getBalance.call(account_two);
}).then(function(balance) {
account_two_starting_balance = balance.toNumber();
return meta.sendCoin(account_two, amount, {from: account_one});
}).then(function() {
return meta.getBalance.call(account_one);
}).then(function(balance) {
account_one_ending_balance = balance.toNumber();
return meta.getBalance.call(account_two);
}).then(function(balance) {
account_two_ending_balance = balance.toNumber();
return MetaCoin.deployed().then(function (instance) {
meta = instance
return meta.getBalance.call(account_one)
}).then(function (balance) {
account_one_starting_balance = balance.toNumber()
return meta.getBalance.call(account_two)
}).then(function (balance) {
account_two_starting_balance = balance.toNumber()
return meta.sendCoin(account_two, amount, {from: account_one})
}).then(function () {
return meta.getBalance.call(account_one)
}).then(function (balance) {
account_one_ending_balance = balance.toNumber()
return meta.getBalance.call(account_two)
}).then(function (balance) {
account_two_ending_balance = balance.toNumber()
assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender");
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
});
});
});
assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender")
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver")
})
})
})
module.exports = {
networks: {
development: {
host: "localhost",
host: 'localhost',
port: 8545,
network_id: "*", // Match any network id
network_id: '*' // Match any network id
}

@@ -12,2 +12,2 @@ },

}
};
}
{
"name": "eth-gas-reporter",
"version": "0.0.4",
"version": "0.0.5",
"description": "Mocha reporter which shows gas used per unit test.",
"main": "index.js",
"scripts": {
"test": "./mock/test.sh",
"postinstall": "cd mock && npm install"
"test": "./mock/test.sh"
},

@@ -26,3 +25,17 @@ "repository": {

},
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme"
"homepage": "https://github.com/cgewecke/eth-gas-reporter#readme",
"dependencies": {
"abi-decoder": "^1.0.8",
"cli-table2": "^0.2.0",
"colors": "^1.1.2",
"lodash": "^4.17.4",
"req-cwd": "^2.0.0",
"request": "^2.83.0",
"request-promise-native": "^1.0.5",
"shelljs": "^0.7.8"
},
"devDependencies": {
"ethereumjs-testrpc": "^4.1.3",
"truffle": "^3.4.11"
}
}
# eth-gas-reporter
Gas usage per unit test. Average gas usage per method. A mocha reporter.
![metacoingas2](https://user-images.githubusercontent.com/7332026/30790579-874eccc6-a161-11e7-90f9-3a08de72b1c7.png)
![screen shot 2017-10-12 at 8 02 57 pm](https://user-images.githubusercontent.com/7332026/31528529-b0d52178-af88-11e7-9c58-004ba4a7b360.png)

@@ -9,6 +10,6 @@

```javascript
// Mocha installed globally
// Truffle/mocha installed globally
npm install -g eth-gas-reporter
// Mocha installed locally
// Truffle/mocha installed locally
npm install --save-dev eth-gas-reporter

@@ -32,7 +33,7 @@ ```

```
### Thanks
### Run without truffle
```
mocha --reporter eth-gas-reporter
```
Everything here has been cannibalized from elsewhere and dropped into mocha's reporter facility. Many thanks to:
+ [@maurelian](https://github.com/maurelian) - once posted a slack picture of mocha reporting gas output instead time. Idea from him.
+ [@cag](https://github.com/cag) - wrote a really nice gas stats utility for the Gnosis suites. The table borrows from / is based his work.
+ [Neufund](https://github.com/Neufund/ico-contracts) - expressing gas usage as a ratio of the block limit in their ico suite. That comes from them.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc