From b5959c29f8875bd37bf1bcb5ebef820c904242cb Mon Sep 17 00:00:00 2001 From: DZariusz Date: Thu, 7 Mar 2019 17:50:35 +0100 Subject: [PATCH] linters --- migrations/1_initial_migration.js | 6 +- migrations/2_sales_migration.js | 53 ++-- migrations/3_salable_migration.js | 5 +- migrations/deployers/Salable.js | 109 ++++---- package.json | 4 +- test/SalableTest.js | 14 +- test/SalesTest.js | 273 +++++++++------------ test/inc/helpers.js | 5 +- test/ministro-contracts/ministroSalable.js | 3 - 9 files changed, 218 insertions(+), 254 deletions(-) diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js index 4d5f3f9..42278e7 100644 --- a/migrations/1_initial_migration.js +++ b/migrations/1_initial_migration.js @@ -1,5 +1,3 @@ -var Migrations = artifacts.require("./Migrations.sol"); +const Migrations = artifacts.require('./Migrations.sol'); -module.exports = function(deployer) { - deployer.deploy(Migrations); -}; +module.exports = deployer => deployer.deploy(Migrations); diff --git a/migrations/2_sales_migration.js b/migrations/2_sales_migration.js index fb435b1..b746602 100755 --- a/migrations/2_sales_migration.js +++ b/migrations/2_sales_migration.js @@ -1,28 +1,29 @@ +const abi = require('ethereumjs-abi'); const getConfig = require('./inc/getConfig'); + const Sales = artifacts.require('Sales'); -const abi = require('ethereumjs-abi'); -module.exports = function(deployer, network, accounts) { +module.exports = (deployer, network, accounts) => { const { config, wallet } = getConfig(network, accounts); // assert.ok(wallet); - var args = [ + const args = [ wallet, - config['total'], - config['name'], - config['decimals'], - config['symbol'], - config['price'], - config['startBlock'], - config['freezeBlock'], - config['cap'], - config['locked'] + config.total, + config.name, + config.decimals, + config.symbol, + config.price, + config.startBlock, + config.freezeBlock, + config.cap, + config.locked, ]; - console.log('args: ' + args.join(',')); + console.log(`args: ${args.join(',')}`); - var encoded = abi.rawEncode( + const encoded = abi.rawEncode( [ 'address', 'uint256', @@ -33,24 +34,24 @@ module.exports = function(deployer, network, accounts) { 'uint256', 'uint256', 'uint256', - 'uint256' + 'uint256', ], - args + args, ); - console.log('encoded argument for Sales contract: ' + encoded.toString('hex')); + console.log(`encoded argument for Sales contract: ${encoded.toString('hex')}`); return deployer.deploy( Sales, wallet, - config['total'], - config['name'], - config['decimals'], - config['symbol'], - config['price'], - config['startBlock'], - config['freezeBlock'], - config['cap'], - config['locked'] + config.total, + config.name, + config.decimals, + config.symbol, + config.price, + config.startBlock, + config.freezeBlock, + config.cap, + config.locked, ); }; diff --git a/migrations/3_salable_migration.js b/migrations/3_salable_migration.js index 843a2d7..7086316 100755 --- a/migrations/3_salable_migration.js +++ b/migrations/3_salable_migration.js @@ -1,4 +1,7 @@ const SalableDeployer = require('./deployers/Salable'); + const Salable = artifacts.require('Salable'); -module.exports = (deployer, network, accounts) => SalableDeployer(deployer, network, accounts, Salable); +module.exports = (deployer, network, accounts) => SalableDeployer( + deployer, network, accounts, Salable, +); diff --git a/migrations/deployers/Salable.js b/migrations/deployers/Salable.js index 80f40c8..b885e07 100644 --- a/migrations/deployers/Salable.js +++ b/migrations/deployers/Salable.js @@ -1,58 +1,55 @@ -const getConfig = require('../inc/getConfig'); const abi = require('ethereumjs-abi'); +const getConfig = require('../inc/getConfig'); -module.exports = (deployer, network, accounts, SalableArtifact) => { - return deployer.then(async () => { - const { config, wallet } = getConfig(network, accounts); - - // assert.ok(wallet); - - var args = [ - wallet, - config['total'], - config['name'], - config['decimals'], - config['symbol'], - config['price'], - config['startBlock'], - config['freezeBlock'], - config['cap'], - config['locked'] - ]; - - console.log('args: ' + args.join(',')); - - var encoded = abi.rawEncode( - [ - 'address', - 'uint256', - 'string', - 'uint8', - 'string', - 'uint256', - 'uint256', - 'uint256', - 'uint256', - 'uint256' - ], - args - ); - - console.log('encoded argument for Salable contract: ' + encoded.toString('hex')); - - return deployer.deploy( - SalableArtifact, - wallet, - config['total'], - config['name'], - config['decimals'], - config['symbol'], - config['price'], - config['startBlock'], - config['freezeBlock'], - config['cap'], - config['locked'] - ); - }); -}; - +module.exports = (deployer, network, accounts, SalableArtifact) => deployer.then(async () => { + const { config, wallet } = getConfig(network, accounts); + + // assert.ok(wallet); + + const args = [ + wallet, + config.total, + config.name, + config.decimals, + config.symbol, + config.price, + config.startBlock, + config.freezeBlock, + config.cap, + config.locked, + ]; + + console.log(`args: ${args.join(',')}`); + + const encoded = abi.rawEncode( + [ + 'address', + 'uint256', + 'string', + 'uint8', + 'string', + 'uint256', + 'uint256', + 'uint256', + 'uint256', + 'uint256', + ], + args, + ); + + console.log(`encoded argument for Salable contract: ${encoded.toString('hex')}`); + + return deployer.deploy( + SalableArtifact, + wallet, + config.total, + config.name, + config.decimals, + config.symbol, + config.price, + config.startBlock, + config.freezeBlock, + config.cap, + config.locked, + ); +}); diff --git a/package.json b/package.json index 392dc02..a4da07f 100755 --- a/package.json +++ b/package.json @@ -3,10 +3,9 @@ "version": "0.1.0", "description": "Typical Ethereum based Sale Contracts", "scripts": { - "lint:solium": "solium --dir . --fix", "lint:js": "./node_modules/.bin/eslint ./test --no-ignore --ext .js --fix", "lint:js-migrations": "./node_modules/.bin/eslint ./migrations --no-ignore --ext .js --fix", - "lint": "npm run lint:js && npm run lint:js-migrations && npm run lint:solium", + "lint": "npm run lint:js && npm run lint:js-migrations", "test": "scripts/test.sh" }, "author": "Miguel Morales ", @@ -18,6 +17,7 @@ "dependencies": { "bip39": "2.5.0", "bn.js": "4.11.8", + "bignumber.js": "8.1.1", "ethereumjs-abi": "0.6.6", "ethjs-provider-http": "0.1.6", "ethjs-query": "0.3.8", diff --git a/test/SalableTest.js b/test/SalableTest.js index 53386dd..5ec4afe 100755 --- a/test/SalableTest.js +++ b/test/SalableTest.js @@ -1,5 +1,3 @@ -// const {assert} = require('chai'); - const BN = require('bn.js'); const BigNumber = require('bignumber.js'); @@ -36,15 +34,21 @@ contract('Salable', (accounts) => { }); it('should set the expected total supply', async () => { - assert(BigNumber(conf.total).times(100 - TWENTY).div(100).eq(await ministroSalable.totalSupply())); + assert( + BigNumber(conf.total).times(100 - TWENTY).div(100).eq(await ministroSalable.totalSupply()), + ); }); it('should set the expected number of tokens for the contract as owner', async () => { - assert(BigNumber(conf.total).times(SALE_PERCENT).div(100).eq(await ministroSalable.balanceOf(ministroSalable.instance.address))); + assert(BigNumber(conf.total).times(SALE_PERCENT).div(100).eq( + await ministroSalable.balanceOf(ministroSalable.instance.address), + )); }); it('should set the expected number of tokens for the owner', async () => { - assert(BigNumber(conf.total).times(TWENTY).div(100).eq(await ministroSalable.balanceOf(ownerAccount))); + assert(BigNumber(conf.total).times(TWENTY).div(100).eq( + await ministroSalable.balanceOf(ownerAccount), + )); }); }); diff --git a/test/SalesTest.js b/test/SalesTest.js index 9265255..e464a9a 100755 --- a/test/SalesTest.js +++ b/test/SalesTest.js @@ -1,194 +1,169 @@ +/* eslint-disable */ const Sales = artifacts.require('Sales'); const HumanStandardToken = artifacts.require('HumanStandardToken'); -const conf = require('../conf/development'); const fs = require('fs'); const BN = require('bn.js'); +const conf = require('../conf/development'); -const { moveForward, takeSnapshot, resetSnapshot, mineBlock } = require('./inc/helpers'); +const { + moveForward, takeSnapshot, resetSnapshot, mineBlock, +} = require('./inc/helpers'); -var getTotalSupply = function() { - return new Promise(function(resolve, reject) { +const getTotalSupply = function () { + return new Promise(((resolve, reject) => { Sales .deployed() - .then(function (instance) { - return instance.token.call(); - }) - .then(function (tokenAddr) { - return HumanStandardToken.at(tokenAddr); - }) - .then(function (token) { - return token.totalSupply.call() - }) - .then(function (result) { + .then(instance => instance.token.call()) + .then(tokenAddr => HumanStandardToken.at(tokenAddr)) + .then(token => token.totalSupply.call()) + .then((result) => { resolve(result.toString()); }) - .catch(function(err) { + .catch((err) => { reject(new Error(err)); }); - }); + })); }; -var getBalanceOf = function(address) { - return new Promise(function(resolve, reject) { +const getBalanceOf = function (address) { + return new Promise(((resolve, reject) => { Sales .deployed() - .then(function (instance) { - return instance.token.call(); - }) - .then(function(tokenAddr) { - return HumanStandardToken.at(tokenAddr); - }) - .then(function(token) { - return token.balanceOf.call(address) - }) - .then(function (result) { + .then(instance => instance.token.call()) + .then(tokenAddr => HumanStandardToken.at(tokenAddr)) + .then(token => token.balanceOf.call(address)) + .then((result) => { resolve(result.toString()); }) - .catch(function(err) { + .catch((err) => { reject(new Error(err)); }); - }); + })); }; -contract('Sales', function(accounts) { - var instance; - var snapshotId; - var ownerAccount = accounts[0]; - var buyerAccount = accounts[1]; +contract('Sales', (accounts) => { + let instance; + let snapshotId; + const ownerAccount = accounts[0]; + const buyerAccount = accounts[1]; - beforeEach(function() { + beforeEach(() => { takeSnapshot() - .then(function(_snapshotId) { + .then((_snapshotId) => { snapshotId = _snapshotId; return Sales .deployed() - .then(function(_instance) { + .then((_instance) => { instance = _instance; - }) - }) - }); - - afterEach(function() { - return resetSnapshot(snapshotId); + }); + }); }); - describe('when the contract was deployed', function() { - it('should set the expected total supply', function() { - return getTotalSupply() - .then(function(result) { - assert.equal(result, conf.total); - }); - }); - - it('should set the expected number of tokens for the contract as owner', function() { - return getBalanceOf(instance.address) - .then(function(result) { - const bn = new BN(conf.total, 10); - assert.equal(result, bn.mul(new BN(6, 10)).div(new BN(10, 10)).toString()); - }); - }); - - it('should set the expected number of tokens for the owner', function() { - return getBalanceOf(ownerAccount) - .then(function(result) { - const bn = new BN(conf.total, 10); - assert.equal(result, bn.mul(new BN(2, 10)).div(new BN(10, 10)).toString()); - }); - }); - - it('should set the expected number of tokens in escrow', function() { - return instance.locked.call() - .then(function(address) { - return getBalanceOf(address); - }) - .then(function(result) { - const bn = new BN(conf.total, 10); - assert.equal(result, bn.mul(new BN(2, 10)).div(new BN(10, 10)).toString()); - }); - }); + afterEach(() => resetSnapshot(snapshotId)); + + describe('when the contract was deployed', () => { + it('should set the expected total supply', () => getTotalSupply() + .then((result) => { + assert.equal(result, conf.total); + })); + + it('should set the expected number of tokens for the contract as owner', () => getBalanceOf(instance.address) + .then((result) => { + const bn = new BN(conf.total, 10); + assert.equal(result, bn.mul(new BN(6, 10)).div(new BN(10, 10)).toString()); + })); + + it('should set the expected number of tokens for the owner', () => getBalanceOf(ownerAccount) + .then((result) => { + const bn = new BN(conf.total, 10); + assert.equal(result, bn.mul(new BN(2, 10)).div(new BN(10, 10)).toString()); + })); + + it('should set the expected number of tokens in escrow', () => instance.locked.call() + .then(address => getBalanceOf(address)) + .then((result) => { + const bn = new BN(conf.total, 10); + assert.equal(result, bn.mul(new BN(2, 10)).div(new BN(10, 10)).toString()); + })); }); - describe('a user tries to buy coins within the sale period', function() { - var snapshotId; + describe('a user tries to buy coins within the sale period', () => { + let snapshotId; - beforeEach(function() { - var blockToMine = new BN(conf['startBlock'], 10); + beforeEach(() => { + const blockToMine = new BN(conf.startBlock, 10); return mineBlock(blockToMine); }); - describe('when there are enough tokens left', function() { - it('should set the expected token balance', function() { + describe('when there are enough tokens left', () => { + it('should set the expected token balance', () => { // var wei = web3.toWei('0.0030', 'Ether'); return instance .purchaseTokens({ from: buyerAccount, - value: new BN('10', 10).mul(new BN(conf['price'], 10)) + value: new BN('10', 10).mul(new BN(conf.price, 10)), }) - .then(function() { - return getBalanceOf(buyerAccount); - }) - .then(function(result) { + .then(() => getBalanceOf(buyerAccount) + ) + .then((result) => { assert.equal(result, 10); }); }); }); - describe('when the cap has been reached', function() { - beforeEach(function() { - const bn = new BN(conf['cap'], 10).mul(new BN(conf['price'], 10)); + describe('when the cap has been reached', () => { + beforeEach(() => { + const bn = new BN(conf.cap, 10).mul(new BN(conf.price, 10)); return instance .purchaseTokens( { from: buyerAccount, - value: bn.toString(10) - } + value: bn.toString(10), + }, ) - .then(function() { - return instance.sold.call(); - }).then(function(sold) { - assert.equal(sold.toString(), conf['cap']); + .then(() => instance.sold.call()) + .then((sold) => { + assert.equal(sold.toString(), conf.cap); }); }); - it('should throw an exception', function(done) { - var value = new BN('10', 10); + it('should throw an exception', (done) => { + const value = new BN('10', 10); instance .purchaseTokens({ from: buyerAccount, - value: value.mul(new BN(conf['price'], 10)) + value: value.mul(new BN(conf.price, 10)), }) - .then(res => { + .then((res) => { assert(false, 'should throw'); }) - .catch(function(err) { + .catch((err) => { assert.isDefined(err); done(); }) - .then(function(res) { + .then((res) => { assert(res === undefined); }); }); - describe('when the cap is increased', function() { + describe('when the cap is increased', () => { let blanceBefore; - var tokenBought = 1000000000; - - beforeEach(function() { - + const tokenBought = 1000000000; + beforeEach(() => { return instance .changeCap('300000000000000', { from: ownerAccount - }).then(function() { + }) + .then(function() { return getBalanceOf(buyerAccount); }).then(b => { balanceBefore = new BN(b); - const wei = new BN(tokenBought, 10).mul(new BN(conf.price, 10)); return instance.purchaseTokens({ @@ -198,107 +173,97 @@ contract('Sales', function(accounts) { }); }); - it('should set the expected token balance', function() { + it('should set the expected token balance', () => { return getBalanceOf(buyerAccount) .then(function(result) { assert.equal(result, balanceBefore.add(new BN(tokenBought)).toString(10)); - }); + }) }); }); }); }); - describe('a user tries to buy coins outside period', function() { - describe('after the sale period', function() { - beforeEach(function() { - var blockToMine = new BN(conf['freezeBlock'], 10) - .add(new BN('10', 10)); - + describe('a user tries to buy coins outside period', () => { + describe('after the sale period', () => { + beforeEach(() => { + const blockToMine = new BN(conf.freezeBlock, 10).add(new BN('10', 10)); return mineBlock(blockToMine); }); - it('should throw an exception', function(done) { - var value = new BN('10', 10); + it('should throw an exception', (done) => { + const value = new BN('10', 10); instance .purchaseTokens({ from: buyerAccount, - value: value.mul(new BN(conf['price'], 10)) + value: value.mul(new BN(conf.price, 10)), }) - .then(function() { - return getBalanceOf(buyerAccount); - }) - .catch(function(err) { + .then(() => getBalanceOf(buyerAccount)) + .catch((err) => { assert.isDefined(err); done(); }) - .then(function(res) { + .then((res) => { assert(res === undefined); }); }); }); - describe('before the sale period', function() { - beforeEach(function() { - var blockToMine = new BN(conf['startBlock'], 10) - .sub(new BN('10', 10)); - + describe('before the sale period', () => { + beforeEach(() => { + const blockToMine = new BN(conf.startBlock, 10).sub(new BN('10', 10)); return mineBlock(blockToMine); }); - it('should throw an exception', function(done) { - var value = new BN('100', 10); + it('should throw an exception', (done) => { + const value = new BN('100', 10); instance .purchaseTokens({ from: buyerAccount, - value: value.mul(new BN(conf['price'], 10)) + value: value.mul(new BN(conf.price, 10)), }) - .then(function() { - return getBalanceOf(buyerAccount); - }) - .catch(function(err) { + .then(() => getBalanceOf(buyerAccount)) + .catch((err) => { assert.isDefined(err); done(); }) - .then(function(res) { + .then((res) => { assert(res === undefined); }); }); }); }); - describe('when an owner tries to unlock tokens in escrow', function() { - describe('when the vesting period has not passed', function() { - it('should throw an exception', function(done) { + describe('when an owner tries to unlock tokens in escrow', () => { + describe('when the vesting period has not passed', () => { + it('should throw an exception', (done) => { instance .unlockEscrow({ - from: ownerAccount + from: ownerAccount, }) - .catch(function(err) { + .catch((err) => { assert.isDefined(err); done(); }) - .then(function(res) { + .then((res) => { assert(res === undefined); }); }); }); - describe('when the vesting period has passed', function() { - beforeEach(function() { - return moveForward(conf['locked']); - }); + describe('when the vesting period has passed', () => { + beforeEach(() => moveForward(conf.locked)); - it('should set the expected token value', function() { + it('should set the expected token value', () => { return instance .unlockEscrow({ - from: ownerAccount + from: ownerAccount, }) - .then(function() { + .then(() => { return getBalanceOf(ownerAccount); }) - .then(function(result) { + .then((result) => { // console.log('owner balance after unlock: ' + result); const bn = new BN(conf.total, 10); assert.equal(result, bn.mul(new BN(4, 10)).div(new BN(10, 10)).toString()); diff --git a/test/inc/helpers.js b/test/inc/helpers.js index c616546..0303c81 100644 --- a/test/inc/helpers.js +++ b/test/inc/helpers.js @@ -9,7 +9,7 @@ const ethRPC = new EthRPC(new HttpProvider('http://localhost:8545')); const ethQuery = new EthQuery(new HttpProvider('http://localhost:8545')); const moveForward = seconds => new Promise((resolve, reject) => { - ethRPC.sendAsync({ method: 'evm_increaseTime', params: [seconds] }, (err, res) => { + ethRPC.sendAsync({ method: 'evm_increaseTime', params: [seconds] }, (err) => { if (err !== undefined && err !== null) { reject(err); } @@ -43,12 +43,11 @@ const resetSnapshot = id => new Promise((resolve, reject) => { const mineBlock = newBlockNumber => new Promise((resolve, reject) => { if (!BN.isBN(newBlockNumber)) { - return reject('not a valid block number'); + throw new Error('not a valid block number'); } return ethQuery.blockNumber() .then((blockNumber) => { - // console.log(`current block ${blockNumber.toString()}, target => ${newBlockNumber.toString()}`); if (new BN(blockNumber, 10).lt(newBlockNumber)) { ethRPC.sendAsync({ jsonrpc: '2.0', method: 'evm_mine' }, (err) => { if (err !== undefined && err !== null) { diff --git a/test/ministro-contracts/ministroSalable.js b/test/ministro-contracts/ministroSalable.js index a122254..a28b38a 100644 --- a/test/ministro-contracts/ministroSalable.js +++ b/test/ministro-contracts/ministroSalable.js @@ -1,9 +1,6 @@ -// const chai = require('chai'); const ministroExecute = require('ministro-tool'); -const { formatVerifier, areAddressesEqual } = require('../inc/helpers'); const BN = require('bignumber.js'); -// const { assert } = chai; const CONF = require('../../conf/development');