diff --git a/README.md b/README.md index 1dce393..7625dbb 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,12 @@ $ npm install --save wait-transaction ## Usage ```js -import waitTransaction from 'wait-transaction' -const wait = waitTransaction(web3) +// pass a valid web3 instance that the providers set +const waitTransaction = require('wait-transaction')(web3) -it('should add funds sent by the seller to sellerFunds', () => { - const contract = MyContract.deployed() - return wait({ from: web3.eth.accounts[0], to: contract.address, value: 1000 }) - .then(() => web3.eth.getBalance(contract.address)) - .then(balance => assert.equal(1000, balance)) -}) +waitTransaction({ from: web3.eth.accounts[0], to: emptyAccount.address, value: 1000 }) + .then(() => web3.eth.getBalance(emptyAccount.address).valueOf()) + .then(balance => assert.equal(1000, balance)) ``` ## License diff --git a/package.json b/package.json index 0ddcf7d..6227063 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,11 @@ "name": "wait-transaction", "description": "A promisified web3.eth.sendTransaction that waits for confirmation ", "scripts": { - "lint": "standard \"test/**/*.js\" \"src/**/*.js\"", + "lint": "standard \"test.js\" \"src/**/*.js\"", "build": "babel src -d lib", - "watch": "chokidar \"test/**/*.js\" \"src/**/*.js\" -c \"npm run lint ; npm run build && npm run test\"" + "test": "npm run lint && npm run testrunner", + "testrunner": "mocha --compilers js:babel-register", + "watch": "chokidar \"test.js\" \"src/**/*.js\" -c \"npm run lint ; npm run build && npm run testrunner\"" }, "main": "lib/index.js", "private": true, @@ -17,6 +19,7 @@ "devDependencies": { "babel-cli": "^6.9.0", "babel-preset-es2015": "^6.9.0", + "babel-register": "^6.9.0", "chai": "^3.5.0", "chokidar": "^1.5.0", "mocha": "^2.4.5", diff --git a/test.js b/test.js new file mode 100644 index 0000000..3eb1a43 --- /dev/null +++ b/test.js @@ -0,0 +1,25 @@ +/* global describe, it */ + +const { assert } = require('chai') +const Web3 = require('web3') +const web3 = new Web3() +web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) +const waitTransaction = require('./lib')(web3) + +/** Returns true if the difference between the two values is less than the given margin of error */ +const within = (margin, a, b) => Math.abs(a - b) < margin + +describe('wait-transaction', () => { + it('should send ether from one account to another', () => { + const [account1, account2] = web3.eth.accounts + const balance1 = +web3.eth.getBalance(account1).valueOf() + const balance2 = +web3.eth.getBalance(account2).valueOf() + + return waitTransaction({ from: account1, to: account2, value: balance1 / 2 }).then(() => { + const newBalance1 = +web3.eth.getBalance(account1).valueOf() + const newBalance2 = +web3.eth.getBalance(account2).valueOf() + assert(within(balance1 / 1e12, newBalance1, balance1 / 2)) + assert(within(balance2 / 1e12, newBalance2, balance2 + balance1 / 2)) + }) + }) +})