Browse Source

Squashed 'libjsqrc/ethereumjs/' changes from 2a0b46a..d5d2115

d5d2115 example contract using array, fixed #185
21ed235 removed connection keep-alive
c44fe01 version 0.7.1
d9b785e fixed #248
490dde5 fixed encoding/decoding strings, #249
b3f5d09 fixed long bytes dynamic decoding/encoding, #249
d097821 small filter improvement
d59b1cd removed xhr from browser
42b6e9d version 0.7.0
fe6defd gulp
44dcd24 set connection keepalive
2b17c0f gulpfile changes
4a46c73 Merge pull request #228 from ethereum/strings
93c8006 tests for encoding and decoding prematurely terminated strings and strings containing internal zeros
7dd44e7 Merge branch 'strings' of https://github.com/ethereum/ethereum.js into strings
390a9ff Merge branch 'develop' into strings
bcd9cfb updated deps, removed unused karma
ef15fc1 Merge pull request #244 from ethereum/allevents
6f74d57 Merge branch 'develop' into allevents
7aca17e Merge branch 'develop' into allevents
c5ee34d fix for optional event parameters
8170a0a Merge pull request #245 from ethereum/filterInstantWatch
76a094c fixed all issues noted by marek
d78b512 add possible callback to filters
e1b17a9 re-build develop
af9c027 all events filter
b270616 version 0.6.1
c47d6a8 gulp
7cf7cf3 map file
6da1d3e gulp
450042e Update eth.js
3951286 Added sendRawTransaction
323b529 merged develop
16ffdf6 merged master
ecf0a2c changed | to Math.floor
d2b5ba2 Merge pull request #239 from ethereum/mistFixes
9e6bf9d removed browser XHR
9061116 fixed build issues in formatInputDynamicBytes
0e3b29c fixed coverage issue
d51e9a2 merged latest develop
f0247aa made meteor package available on the server side as well
e97c5f5 fixed and tested meteor package init
3e49f56 add currentProvidor and mist fixes
bd6d9ba merged develop
7b730b1 Merge branch 'strings' of github.com:ethereum/web3.js into strings
cc533d9 fixed bytes wrong encoding
8c6f976 Merge branch 'develop' into strings
7c970e3 add bytes32 test with leading 0
9c7f7bb re-add .map files
189484f add extra int tests
73cc711 Merge branch 'develop' into strings
fa3239f abi string type, fixes #216, #218, #219

git-subtree-dir: libjsqrc/ethereumjs
git-subtree-split: d5d2115bf9ad07a736a189e1022b7d3a549e4006
cl-refactor
Marek Kotewicz 10 years ago
parent
commit
1e652c0757
  1. 1
      .travis.yml
  2. 2
      bower.json
  3. 312
      dist/web3-light.js
  4. 4
      dist/web3-light.min.js
  5. 314
      dist/web3.js
  6. 26
      dist/web3.js.map
  7. 6
      dist/web3.min.js
  8. 83
      example/contract_array.html
  9. 27
      gulpfile.js
  10. 95
      karma.conf.js
  11. 7
      lib/solidity/coder.js
  12. 56
      lib/solidity/formatters.js
  13. 9
      lib/solidity/param.js
  14. 24
      lib/utils/utils.js
  15. 2
      lib/version.json
  16. 18
      lib/web3.js
  17. 81
      lib/web3/allevents.js
  18. 10
      lib/web3/contract.js
  19. 18
      lib/web3/event.js
  20. 23
      lib/web3/filter.js
  21. 3
      lib/web3/function.js
  22. 3
      lib/web3/httpprovider.js
  23. 15
      package-init.js
  24. 4
      package.js
  25. 25
      package.json
  26. 25
      test/batch.js
  27. 98
      test/coder.decodeParam.js
  28. 87
      test/coder.encodeParam.js
  29. 141
      test/contract.js
  30. 40
      test/polling.js
  31. 1
      test/utils.toHex.js
  32. 8
      test/web3.eth.filter.methods.js

1
.travis.yml

@ -11,7 +11,6 @@ script:
- "jshint *.js lib"
after_script:
- npm run-script build
- npm run-script karma
- npm run-script test-coveralls
- cd test/node && npm install && node app.js

2
bower.json

@ -1,7 +1,7 @@
{
"name": "web3",
"namespace": "ethereum",
"version": "0.6.0",
"version": "0.7.1",
"description": "Ethereum Compatible JavaScript API",
"main": [
"./dist/web3.js",

312
dist/web3-light.js

@ -263,6 +263,13 @@ var coder = new SolidityCoder([
inputFormatter: f.formatInputBytes,
outputFormatter: f.formatOutputBytes
}),
new SolidityType({
name: 'string',
match: 'strict',
mode: 'bytes',
inputFormatter: f.formatInputString,
outputFormatter: f.formatOutputString
}),
new SolidityType({
name: 'real',
match: 'prefix',
@ -328,26 +335,43 @@ var formatInputInt = function (value) {
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
var result = utils.padRight(utils.toHex(value).substr(2), 64);
return new SolidityParam(result);
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputDynamicBytes
* @method formatDynamicInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputDynamicBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
value = utils.toHex(value).substr(2);
var l = Math.floor((value.length + 63) / 64);
var result = utils.padRight(value, l * 64);
var length = Math.floor(value.length / 2);
return new SolidityParam(formatInputInt(length).value + result, 32);
};
/**
* Formats input value to byte representation of string
*
* @method formatInputString
* @param {String}
* @returns {SolidityParam}
*/
var formatInputString = function (value) {
var result = utils.fromAscii(value).substr(2);
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(value.length).value + result, 32);
};
@ -450,27 +474,38 @@ var formatOutputBool = function (param) {
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputBytes = function (param) {
// length might also be important!
return utils.toAscii(param.staticPart());
return '0x' + param.staticPart();
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputDynamicBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputDynamicBytes = function (param) {
// length might also be important!
return utils.toAscii(param.dynamicPart().slice(64));
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return '0x' + param.dynamicPart().substr(64, length);
};
/**
* Should be used to format output string
*
* @method formatOutputString
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
*/
var formatOutputString = function (param) {
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return utils.toAscii(param.dynamicPart().substr(64, length));
};
/**
@ -489,6 +524,7 @@ module.exports = {
formatInputInt: formatInputInt,
formatInputBytes: formatInputBytes,
formatInputDynamicBytes: formatInputDynamicBytes,
formatInputString: formatInputString,
formatInputBool: formatInputBool,
formatInputReal: formatInputReal,
formatOutputInt: formatOutputInt,
@ -498,6 +534,7 @@ module.exports = {
formatOutputBool: formatOutputBool,
formatOutputBytes: formatOutputBytes,
formatOutputDynamicBytes: formatOutputDynamicBytes,
formatOutputString: formatOutputString,
formatOutputAddress: formatOutputAddress
};
@ -687,13 +724,14 @@ var getOffset = function (bytes, index) {
*/
SolidityParam.decodeBytes = function (bytes, index) {
index = index || 0;
//TODO add support for strings longer than 32 bytes
//var length = parseInt('0x' + bytes.substr(offset * 64, 64));
var offset = getOffset(bytes, index);
// 2 * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, 2 * 64), 0);
var l = parseInt('0x' + bytes.substr(offset * 2, 64));
l = Math.floor((l + 31) / 32);
// (1 + l) * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0);
};
/**
@ -846,7 +884,7 @@ module.exports = function (str, isNew) {
};
},{"./utils":7,"crypto-js/sha3":33}],7:[function(require,module,exports){
},{"./utils":7,"crypto-js/sha3":34}],7:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -924,6 +962,19 @@ var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Should be called to pad string to expected length
*
* @method padRight
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padRight = function (string, chars, sign) {
return string + (new Array(chars - string.length + 1).join(sign ? sign : "0"));
};
/**
* Should be called to get sting from it's hex representation
*
@ -940,10 +991,6 @@ var toAscii = function(hex) {
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code);
}
@ -1053,7 +1100,7 @@ var fromDecimal = function (value) {
* @return {String}
*/
var toHex = function (val) {
/*jshint maxcomplexity:7 */
/*jshint maxcomplexity: 8 */
if (isBoolean(val))
return fromDecimal(+val);
@ -1067,9 +1114,11 @@ var toHex = function (val) {
// if its a negative number, pass it through fromDecimal
if (isString(val)) {
if (val.indexOf('-0x') === 0)
return fromDecimal(val);
return fromDecimal(val);
else if (!isFinite(val))
return fromAscii(val);
else if(val.indexOf('0x') === 0)
return val;
}
return fromDecimal(val);
@ -1320,6 +1369,7 @@ var isIBAN = function (iban) {
module.exports = {
padLeft: padLeft,
padRight: padRight,
toHex: toHex,
toDecimal: toDecimal,
fromDecimal: fromDecimal,
@ -1348,7 +1398,7 @@ module.exports = {
},{"bignumber.js":"bignumber.js"}],8:[function(require,module,exports){
module.exports={
"version": "0.6.0"
"version": "0.7.1"
}
},{}],9:[function(require,module,exports){
@ -1434,31 +1484,25 @@ var setupProperties = function (obj, properties) {
/// setups web3 object, and it's in-browser executed methods
var web3 = {};
web3.providers = {};
web3.currentProvider = null;
web3.version = {};
web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
web3.eth.filter = function (fil, callback) {
return new Filter(fil, watches.eth(), formatters.outputLogFormatter, callback);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter, callback);
};
web3.net = {};
web3.db = {};
web3.setProvider = function (provider) {
this.currentProvider = provider;
RequestManager.getInstance().setProvider(provider);
};
web3.reset = function () {
@ -1531,7 +1575,90 @@ setupMethods(web3.shh, shh.methods);
module.exports = web3;
},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":10,"./web3/db":12,"./web3/eth":14,"./web3/filter":16,"./web3/formatters":17,"./web3/method":22,"./web3/net":24,"./web3/property":25,"./web3/requestmanager":27,"./web3/shh":28,"./web3/watches":30}],10:[function(require,module,exports){
},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":11,"./web3/db":13,"./web3/eth":15,"./web3/filter":17,"./web3/formatters":18,"./web3/method":23,"./web3/net":25,"./web3/property":26,"./web3/requestmanager":28,"./web3/shh":29,"./web3/watches":31}],10:[function(require,module,exports){
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file allevents.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var sha3 = require('../utils/sha3');
var SolidityEvent = require('./event');
var formatters = require('./formatters');
var utils = require('../utils/utils');
var Filter = require('./filter');
var watches = require('./watches');
var AllSolidityEvents = function (json, address) {
this._json = json;
this._address = address;
};
AllSolidityEvents.prototype.encode = function (options) {
options = options || {};
var result = {};
['fromBlock', 'toBlock'].filter(function (f) {
return options[f] !== undefined;
}).forEach(function (f) {
result[f] = formatters.inputBlockNumberFormatter(options[f]);
});
result.topics = [null, null, null, null, null]; // match all topics
result.address = this._address;
return result;
};
AllSolidityEvents.prototype.decode = function (data) {
data.data = data.data || '';
data.topics = data.topics || [];
var eventTopic = data.topics[0].slice(2);
var match = this._json.filter(function (j) {
return eventTopic === sha3(utils.transformToFullName(j));
})[0];
if (!match) { // cannot find matching event?
console.warn('cannot find event for log');
return data;
}
var event = new SolidityEvent(match, this._address);
return event.decode(data);
};
AllSolidityEvents.prototype.execute = function (options, callback) {
var o = this.encode(options);
var formatter = this.decode.bind(this);
return new Filter(o, watches.eth(), formatter, callback);
};
AllSolidityEvents.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this);
contract.allEvents = execute;
};
module.exports = AllSolidityEvents;
},{"../utils/sha3":6,"../utils/utils":7,"./event":16,"./filter":17,"./formatters":18,"./watches":31}],11:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1594,7 +1721,7 @@ Batch.prototype.execute = function () {
module.exports = Batch;
},{"./requestmanager":27}],11:[function(require,module,exports){
},{"./requestmanager":28}],12:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1622,6 +1749,7 @@ var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var SolidityEvent = require('./event');
var SolidityFunction = require('./function');
var AllEvents = require('./allevents');
/**
* Should be called to encode constructor params
@ -1667,9 +1795,14 @@ var addFunctionsToContract = function (contract, abi) {
* @param {Array} abi
*/
var addEventsToContract = function (contract, abi) {
abi.filter(function (json) {
var events = abi.filter(function (json) {
return json.type === 'event';
}).map(function (json) {
});
var All = new AllEvents(events, contract.address);
All.attachToContract(contract);
events.map(function (json) {
return new SolidityEvent(json, contract.address);
}).forEach(function (e) {
e.attachToContract(contract);
@ -1776,7 +1909,7 @@ var Contract = function (abi, address) {
module.exports = contract;
},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./event":15,"./function":18}],12:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./allevents":10,"./event":16,"./function":19}],13:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1834,7 +1967,7 @@ module.exports = {
methods: methods
};
},{"./method":22}],13:[function(require,module,exports){
},{"./method":23}],14:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1874,7 +2007,7 @@ module.exports = {
};
},{}],14:[function(require,module,exports){
},{}],15:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2159,7 +2292,7 @@ module.exports = {
};
},{"../utils/utils":7,"./formatters":17,"./method":22,"./property":25}],15:[function(require,module,exports){
},{"../utils/utils":7,"./formatters":18,"./method":23,"./property":26}],16:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2184,9 +2317,10 @@ module.exports = {
var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -2332,10 +2466,21 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
if(arguments.length === 2)
options = null;
if(arguments.length === 1) {
options = null;
indexed = {};
}
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), formatter, callback);
};
/**
@ -2356,7 +2501,7 @@ SolidityEvent.prototype.attachToContract = function (contract) {
module.exports = SolidityEvent;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],16:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"./filter":17,"./formatters":18,"./watches":31}],17:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2486,7 +2631,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, formatter, callback) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -2494,23 +2639,32 @@ var Filter = function (options, methods, formatter) {
});
this.options = getOptions(options);
this.implementation = implementation;
this.filterId = null;
this.callbacks = [];
this.pollFilters = [];
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
// get filter logs for the already existing watch calls
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
if(self.callbacks.length > 0)
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {
@ -2556,7 +2710,7 @@ Filter.prototype.get = function (callback) {
module.exports = Filter;
},{"../utils/utils":7,"./formatters":17,"./requestmanager":27}],17:[function(require,module,exports){
},{"../utils/utils":7,"./formatters":18,"./requestmanager":28}],18:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2778,7 +2932,7 @@ module.exports = {
};
},{"../utils/config":5,"../utils/utils":7}],18:[function(require,module,exports){
},{"../utils/config":5,"../utils/utils":7}],19:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2969,8 +3123,9 @@ SolidityFunction.prototype.request = function () {
var format = this.unpackOutput.bind(this);
return {
method: this._constant ? 'eth_call' : 'eth_sendTransaction',
callback: callback,
payload: payload,
params: [payload],
format: format
};
};
@ -3014,7 +3169,7 @@ SolidityFunction.prototype.attachToContract = function (contract) {
module.exports = SolidityFunction;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],19:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":18}],20:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3041,7 +3196,8 @@ module.exports = SolidityFunction;
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
// resolves the problem for electron/atom shell environments, which use node integration, but have no process variable available
var XMLHttpRequest = (typeof window !== 'undefined' && window.XMLHttpRequest) ? window.XMLHttpRequest : require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {
@ -3108,7 +3264,7 @@ HttpProvider.prototype.sendAsync = function (payload, callback) {
module.exports = HttpProvider;
},{"./errors":13,"xmlhttprequest":4}],20:[function(require,module,exports){
},{"./errors":14,"xmlhttprequest":4}],21:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3218,7 +3374,7 @@ ICAP.prototype.address = function () {
module.exports = ICAP;
},{"../utils/utils":7}],21:[function(require,module,exports){
},{"../utils/utils":7}],22:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3311,7 +3467,7 @@ Jsonrpc.prototype.toBatchPayload = function (messages) {
module.exports = Jsonrpc;
},{}],22:[function(require,module,exports){
},{}],23:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3485,7 +3641,7 @@ Method.prototype.send = function () {
module.exports = Method;
},{"../utils/utils":7,"./errors":13,"./requestmanager":27}],23:[function(require,module,exports){
},{"../utils/utils":7,"./errors":14,"./requestmanager":28}],24:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3533,7 +3689,7 @@ var abi = [
module.exports = contract(abi).at(address);
},{"./contract":11}],24:[function(require,module,exports){
},{"./contract":12}],25:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3583,7 +3739,7 @@ module.exports = {
};
},{"../utils/utils":7,"./property":25}],25:[function(require,module,exports){
},{"../utils/utils":7,"./property":26}],26:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3701,7 +3857,7 @@ Property.prototype.getAsync = function (callback) {
module.exports = Property;
},{"./requestmanager":27}],26:[function(require,module,exports){
},{"./requestmanager":28}],27:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3736,7 +3892,7 @@ QtSyncProvider.prototype.send = function (payload) {
module.exports = QtSyncProvider;
},{}],27:[function(require,module,exports){
},{}],28:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4001,7 +4157,7 @@ RequestManager.prototype.poll = function () {
module.exports = RequestManager;
},{"../utils/config":5,"../utils/utils":7,"./errors":13,"./jsonrpc":21}],28:[function(require,module,exports){
},{"../utils/config":5,"../utils/utils":7,"./errors":14,"./jsonrpc":22}],29:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4071,7 +4227,7 @@ module.exports = {
};
},{"./formatters":17,"./method":22}],29:[function(require,module,exports){
},{"./formatters":18,"./method":23}],30:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4167,7 +4323,7 @@ var deposit = function (from, address, value, client, callback) {
module.exports = transfer;
},{"../web3":9,"./contract":11,"./icap":20,"./namereg":23}],30:[function(require,module,exports){
},{"../web3":9,"./contract":12,"./icap":21,"./namereg":24}],31:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4283,9 +4439,9 @@ module.exports = {
};
},{"./method":22}],31:[function(require,module,exports){
},{"./method":23}],32:[function(require,module,exports){
},{}],32:[function(require,module,exports){
},{}],33:[function(require,module,exports){
;(function (root, factory) {
if (typeof exports === "object") {
// CommonJS
@ -5028,7 +5184,7 @@ module.exports = {
return CryptoJS;
}));
},{}],33:[function(require,module,exports){
},{}],34:[function(require,module,exports){
;(function (root, factory, undef) {
if (typeof exports === "object") {
// CommonJS
@ -5352,7 +5508,7 @@ module.exports = {
return CryptoJS.SHA3;
}));
},{"./core":32,"./x64-core":34}],34:[function(require,module,exports){
},{"./core":33,"./x64-core":35}],35:[function(require,module,exports){
;(function (root, factory) {
if (typeof exports === "object") {
// CommonJS
@ -5657,7 +5813,7 @@ module.exports = {
return CryptoJS;
}));
},{"./core":32}],"bignumber.js":[function(require,module,exports){
},{"./core":33}],"bignumber.js":[function(require,module,exports){
'use strict';
module.exports = BigNumber; // jshint ignore:line
@ -5679,5 +5835,5 @@ if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') {
module.exports = web3;
},{"./lib/web3":9,"./lib/web3/contract":11,"./lib/web3/httpprovider":19,"./lib/web3/namereg":23,"./lib/web3/qtsync":26,"./lib/web3/transfer":29}]},{},["web3"])
},{"./lib/web3":9,"./lib/web3/contract":12,"./lib/web3/httpprovider":20,"./lib/web3/namereg":24,"./lib/web3/qtsync":27,"./lib/web3/transfer":30}]},{},["web3"])
//# sourceMappingURL=web3-light.js.map

4
dist/web3-light.min.js

File diff suppressed because one or more lines are too long

314
dist/web3.js

@ -263,6 +263,13 @@ var coder = new SolidityCoder([
inputFormatter: f.formatInputBytes,
outputFormatter: f.formatOutputBytes
}),
new SolidityType({
name: 'string',
match: 'strict',
mode: 'bytes',
inputFormatter: f.formatInputString,
outputFormatter: f.formatOutputString
}),
new SolidityType({
name: 'real',
match: 'prefix',
@ -328,26 +335,43 @@ var formatInputInt = function (value) {
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
var result = utils.padRight(utils.toHex(value).substr(2), 64);
return new SolidityParam(result);
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputDynamicBytes
* @method formatDynamicInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputDynamicBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
value = utils.toHex(value).substr(2);
var l = Math.floor((value.length + 63) / 64);
var result = utils.padRight(value, l * 64);
var length = Math.floor(value.length / 2);
return new SolidityParam(formatInputInt(length).value + result, 32);
};
/**
* Formats input value to byte representation of string
*
* @method formatInputString
* @param {String}
* @returns {SolidityParam}
*/
var formatInputString = function (value) {
var result = utils.fromAscii(value).substr(2);
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(value.length).value + result, 32);
};
@ -450,27 +474,38 @@ var formatOutputBool = function (param) {
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputBytes = function (param) {
// length might also be important!
return utils.toAscii(param.staticPart());
return '0x' + param.staticPart();
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputDynamicBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputDynamicBytes = function (param) {
// length might also be important!
return utils.toAscii(param.dynamicPart().slice(64));
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return '0x' + param.dynamicPart().substr(64, length);
};
/**
* Should be used to format output string
*
* @method formatOutputString
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
*/
var formatOutputString = function (param) {
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return utils.toAscii(param.dynamicPart().substr(64, length));
};
/**
@ -489,6 +524,7 @@ module.exports = {
formatInputInt: formatInputInt,
formatInputBytes: formatInputBytes,
formatInputDynamicBytes: formatInputDynamicBytes,
formatInputString: formatInputString,
formatInputBool: formatInputBool,
formatInputReal: formatInputReal,
formatOutputInt: formatOutputInt,
@ -498,6 +534,7 @@ module.exports = {
formatOutputBool: formatOutputBool,
formatOutputBytes: formatOutputBytes,
formatOutputDynamicBytes: formatOutputDynamicBytes,
formatOutputString: formatOutputString,
formatOutputAddress: formatOutputAddress
};
@ -687,13 +724,14 @@ var getOffset = function (bytes, index) {
*/
SolidityParam.decodeBytes = function (bytes, index) {
index = index || 0;
//TODO add support for strings longer than 32 bytes
//var length = parseInt('0x' + bytes.substr(offset * 64, 64));
var offset = getOffset(bytes, index);
// 2 * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, 2 * 64), 0);
var l = parseInt('0x' + bytes.substr(offset * 2, 64));
l = Math.floor((l + 31) / 32);
// (1 + l) * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0);
};
/**
@ -846,7 +884,7 @@ module.exports = function (str, isNew) {
};
},{"./utils":7,"crypto-js/sha3":33}],7:[function(require,module,exports){
},{"./utils":7,"crypto-js/sha3":34}],7:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -924,6 +962,19 @@ var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Should be called to pad string to expected length
*
* @method padRight
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padRight = function (string, chars, sign) {
return string + (new Array(chars - string.length + 1).join(sign ? sign : "0"));
};
/**
* Should be called to get sting from it's hex representation
*
@ -940,10 +991,6 @@ var toAscii = function(hex) {
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code);
}
@ -1053,7 +1100,7 @@ var fromDecimal = function (value) {
* @return {String}
*/
var toHex = function (val) {
/*jshint maxcomplexity:7 */
/*jshint maxcomplexity: 8 */
if (isBoolean(val))
return fromDecimal(+val);
@ -1067,9 +1114,11 @@ var toHex = function (val) {
// if its a negative number, pass it through fromDecimal
if (isString(val)) {
if (val.indexOf('-0x') === 0)
return fromDecimal(val);
return fromDecimal(val);
else if (!isFinite(val))
return fromAscii(val);
else if(val.indexOf('0x') === 0)
return val;
}
return fromDecimal(val);
@ -1320,6 +1369,7 @@ var isIBAN = function (iban) {
module.exports = {
padLeft: padLeft,
padRight: padRight,
toHex: toHex,
toDecimal: toDecimal,
fromDecimal: fromDecimal,
@ -1348,7 +1398,7 @@ module.exports = {
},{"bignumber.js":"bignumber.js"}],8:[function(require,module,exports){
module.exports={
"version": "0.6.0"
"version": "0.7.1"
}
},{}],9:[function(require,module,exports){
@ -1434,31 +1484,25 @@ var setupProperties = function (obj, properties) {
/// setups web3 object, and it's in-browser executed methods
var web3 = {};
web3.providers = {};
web3.currentProvider = null;
web3.version = {};
web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
web3.eth.filter = function (fil, callback) {
return new Filter(fil, watches.eth(), formatters.outputLogFormatter, callback);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter, callback);
};
web3.net = {};
web3.db = {};
web3.setProvider = function (provider) {
this.currentProvider = provider;
RequestManager.getInstance().setProvider(provider);
};
web3.reset = function () {
@ -1531,7 +1575,90 @@ setupMethods(web3.shh, shh.methods);
module.exports = web3;
},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":10,"./web3/db":12,"./web3/eth":14,"./web3/filter":16,"./web3/formatters":17,"./web3/method":22,"./web3/net":24,"./web3/property":25,"./web3/requestmanager":27,"./web3/shh":28,"./web3/watches":30}],10:[function(require,module,exports){
},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":11,"./web3/db":13,"./web3/eth":15,"./web3/filter":17,"./web3/formatters":18,"./web3/method":23,"./web3/net":25,"./web3/property":26,"./web3/requestmanager":28,"./web3/shh":29,"./web3/watches":31}],10:[function(require,module,exports){
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file allevents.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var sha3 = require('../utils/sha3');
var SolidityEvent = require('./event');
var formatters = require('./formatters');
var utils = require('../utils/utils');
var Filter = require('./filter');
var watches = require('./watches');
var AllSolidityEvents = function (json, address) {
this._json = json;
this._address = address;
};
AllSolidityEvents.prototype.encode = function (options) {
options = options || {};
var result = {};
['fromBlock', 'toBlock'].filter(function (f) {
return options[f] !== undefined;
}).forEach(function (f) {
result[f] = formatters.inputBlockNumberFormatter(options[f]);
});
result.topics = [null, null, null, null, null]; // match all topics
result.address = this._address;
return result;
};
AllSolidityEvents.prototype.decode = function (data) {
data.data = data.data || '';
data.topics = data.topics || [];
var eventTopic = data.topics[0].slice(2);
var match = this._json.filter(function (j) {
return eventTopic === sha3(utils.transformToFullName(j));
})[0];
if (!match) { // cannot find matching event?
console.warn('cannot find event for log');
return data;
}
var event = new SolidityEvent(match, this._address);
return event.decode(data);
};
AllSolidityEvents.prototype.execute = function (options, callback) {
var o = this.encode(options);
var formatter = this.decode.bind(this);
return new Filter(o, watches.eth(), formatter, callback);
};
AllSolidityEvents.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this);
contract.allEvents = execute;
};
module.exports = AllSolidityEvents;
},{"../utils/sha3":6,"../utils/utils":7,"./event":16,"./filter":17,"./formatters":18,"./watches":31}],11:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1594,7 +1721,7 @@ Batch.prototype.execute = function () {
module.exports = Batch;
},{"./requestmanager":27}],11:[function(require,module,exports){
},{"./requestmanager":28}],12:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1622,6 +1749,7 @@ var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var SolidityEvent = require('./event');
var SolidityFunction = require('./function');
var AllEvents = require('./allevents');
/**
* Should be called to encode constructor params
@ -1667,9 +1795,14 @@ var addFunctionsToContract = function (contract, abi) {
* @param {Array} abi
*/
var addEventsToContract = function (contract, abi) {
abi.filter(function (json) {
var events = abi.filter(function (json) {
return json.type === 'event';
}).map(function (json) {
});
var All = new AllEvents(events, contract.address);
All.attachToContract(contract);
events.map(function (json) {
return new SolidityEvent(json, contract.address);
}).forEach(function (e) {
e.attachToContract(contract);
@ -1776,7 +1909,7 @@ var Contract = function (abi, address) {
module.exports = contract;
},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./event":15,"./function":18}],12:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./allevents":10,"./event":16,"./function":19}],13:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1834,7 +1967,7 @@ module.exports = {
methods: methods
};
},{"./method":22}],13:[function(require,module,exports){
},{"./method":23}],14:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -1874,7 +2007,7 @@ module.exports = {
};
},{}],14:[function(require,module,exports){
},{}],15:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2159,7 +2292,7 @@ module.exports = {
};
},{"../utils/utils":7,"./formatters":17,"./method":22,"./property":25}],15:[function(require,module,exports){
},{"../utils/utils":7,"./formatters":18,"./method":23,"./property":26}],16:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2184,9 +2317,10 @@ module.exports = {
var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -2332,10 +2466,21 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
if(arguments.length === 2)
options = null;
if(arguments.length === 1) {
options = null;
indexed = {};
}
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), formatter, callback);
};
/**
@ -2356,7 +2501,7 @@ SolidityEvent.prototype.attachToContract = function (contract) {
module.exports = SolidityEvent;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],16:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"./filter":17,"./formatters":18,"./watches":31}],17:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2486,7 +2631,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, formatter, callback) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -2494,23 +2639,32 @@ var Filter = function (options, methods, formatter) {
});
this.options = getOptions(options);
this.implementation = implementation;
this.filterId = null;
this.callbacks = [];
this.pollFilters = [];
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
// get filter logs for the already existing watch calls
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
if(self.callbacks.length > 0)
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {
@ -2556,7 +2710,7 @@ Filter.prototype.get = function (callback) {
module.exports = Filter;
},{"../utils/utils":7,"./formatters":17,"./requestmanager":27}],17:[function(require,module,exports){
},{"../utils/utils":7,"./formatters":18,"./requestmanager":28}],18:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2778,7 +2932,7 @@ module.exports = {
};
},{"../utils/config":5,"../utils/utils":7}],18:[function(require,module,exports){
},{"../utils/config":5,"../utils/utils":7}],19:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2969,8 +3123,9 @@ SolidityFunction.prototype.request = function () {
var format = this.unpackOutput.bind(this);
return {
method: this._constant ? 'eth_call' : 'eth_sendTransaction',
callback: callback,
payload: payload,
params: [payload],
format: format
};
};
@ -3014,7 +3169,7 @@ SolidityFunction.prototype.attachToContract = function (contract) {
module.exports = SolidityFunction;
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":17}],19:[function(require,module,exports){
},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":18}],20:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3041,7 +3196,8 @@ module.exports = SolidityFunction;
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
// resolves the problem for electron/atom shell environments, which use node integration, but have no process variable available
var XMLHttpRequest = (typeof window !== 'undefined' && window.XMLHttpRequest) ? window.XMLHttpRequest : require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {
@ -3108,7 +3264,7 @@ HttpProvider.prototype.sendAsync = function (payload, callback) {
module.exports = HttpProvider;
},{"./errors":13,"xmlhttprequest":4}],20:[function(require,module,exports){
},{"./errors":14,"xmlhttprequest":4}],21:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3218,7 +3374,7 @@ ICAP.prototype.address = function () {
module.exports = ICAP;
},{"../utils/utils":7}],21:[function(require,module,exports){
},{"../utils/utils":7}],22:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3311,7 +3467,7 @@ Jsonrpc.prototype.toBatchPayload = function (messages) {
module.exports = Jsonrpc;
},{}],22:[function(require,module,exports){
},{}],23:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3485,7 +3641,7 @@ Method.prototype.send = function () {
module.exports = Method;
},{"../utils/utils":7,"./errors":13,"./requestmanager":27}],23:[function(require,module,exports){
},{"../utils/utils":7,"./errors":14,"./requestmanager":28}],24:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3533,7 +3689,7 @@ var abi = [
module.exports = contract(abi).at(address);
},{"./contract":11}],24:[function(require,module,exports){
},{"./contract":12}],25:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3583,7 +3739,7 @@ module.exports = {
};
},{"../utils/utils":7,"./property":25}],25:[function(require,module,exports){
},{"../utils/utils":7,"./property":26}],26:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3701,7 +3857,7 @@ Property.prototype.getAsync = function (callback) {
module.exports = Property;
},{"./requestmanager":27}],26:[function(require,module,exports){
},{"./requestmanager":28}],27:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3736,7 +3892,7 @@ QtSyncProvider.prototype.send = function (payload) {
module.exports = QtSyncProvider;
},{}],27:[function(require,module,exports){
},{}],28:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4001,7 +4157,7 @@ RequestManager.prototype.poll = function () {
module.exports = RequestManager;
},{"../utils/config":5,"../utils/utils":7,"./errors":13,"./jsonrpc":21}],28:[function(require,module,exports){
},{"../utils/config":5,"../utils/utils":7,"./errors":14,"./jsonrpc":22}],29:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4071,7 +4227,7 @@ module.exports = {
};
},{"./formatters":17,"./method":22}],29:[function(require,module,exports){
},{"./formatters":18,"./method":23}],30:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4167,7 +4323,7 @@ var deposit = function (from, address, value, client, callback) {
module.exports = transfer;
},{"../web3":9,"./contract":11,"./icap":20,"./namereg":23}],30:[function(require,module,exports){
},{"../web3":9,"./contract":12,"./icap":21,"./namereg":24}],31:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -4283,9 +4439,9 @@ module.exports = {
};
},{"./method":22}],31:[function(require,module,exports){
},{"./method":23}],32:[function(require,module,exports){
},{}],32:[function(require,module,exports){
},{}],33:[function(require,module,exports){
;(function (root, factory) {
if (typeof exports === "object") {
// CommonJS
@ -5028,7 +5184,7 @@ module.exports = {
return CryptoJS;
}));
},{}],33:[function(require,module,exports){
},{}],34:[function(require,module,exports){
;(function (root, factory, undef) {
if (typeof exports === "object") {
// CommonJS
@ -5352,7 +5508,7 @@ module.exports = {
return CryptoJS.SHA3;
}));
},{"./core":32,"./x64-core":34}],34:[function(require,module,exports){
},{"./core":33,"./x64-core":35}],35:[function(require,module,exports){
;(function (root, factory) {
if (typeof exports === "object") {
// CommonJS
@ -5657,7 +5813,7 @@ module.exports = {
return CryptoJS;
}));
},{"./core":32}],"bignumber.js":[function(require,module,exports){
},{"./core":33}],"bignumber.js":[function(require,module,exports){
/*! bignumber.js v2.0.7 https://github.com/MikeMcl/bignumber.js/LICENCE */
;(function (global) {
@ -8342,7 +8498,7 @@ module.exports = {
}
})(this);
},{"crypto":31}],"web3":[function(require,module,exports){
},{"crypto":32}],"web3":[function(require,module,exports){
var web3 = require('./lib/web3');
web3.providers.HttpProvider = require('./lib/web3/httpprovider');
web3.providers.QtSyncProvider = require('./lib/web3/qtsync');
@ -8358,5 +8514,5 @@ if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') {
module.exports = web3;
},{"./lib/web3":9,"./lib/web3/contract":11,"./lib/web3/httpprovider":19,"./lib/web3/namereg":23,"./lib/web3/qtsync":26,"./lib/web3/transfer":29}]},{},["web3"])
},{"./lib/web3":9,"./lib/web3/contract":12,"./lib/web3/httpprovider":20,"./lib/web3/namereg":24,"./lib/web3/qtsync":27,"./lib/web3/transfer":30}]},{},["web3"])
//# sourceMappingURL=web3.js.map

26
dist/web3.js.map

File diff suppressed because one or more lines are too long

6
dist/web3.min.js

File diff suppressed because one or more lines are too long

83
example/contract_array.html

@ -0,0 +1,83 @@
<!doctype>
<html>
<head>
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">
var web3 = require('web3');
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
// solidity code code
var source = "" +
"contract test {\n" +
" function take(uint[] a, uint b) constant returns(uint d) {\n" +
" return a[b];\n" +
" }\n" +
"}\n";
var compiled = web3.eth.compile.solidity(source);
var code = compiled.test.code;
// contract json abi, this is autogenerated using solc CLI
var abi = compiled.test.info.abiDefinition;
var myContract;
function createExampleContract() {
// hide create button
document.getElementById('create').style.visibility = 'hidden';
document.getElementById('code').innerText = code;
// let's assume that coinbase is our account
web3.eth.defaultAccount = web3.eth.coinbase;
var watch = web3.eth.filter('latest');
// create contract
myContract = web3.eth.contract(abi).new({data: code});
console.log('address: ' + myContract.address);
document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
watch.watch(function (err, hash) {
var block = web3.eth.getBlock(hash, true);
var contractMined = block.transactions.reduce(function (mined, th) {
// TODO: compiled code do not have 0x prefix
return mined || (th.from === web3.eth.defaultAccount && th.input.indexOf(code) !== -1);
}, false);
if (contractMined) {
document.getElementById('status').innerText = 'Mined!';
document.getElementById('call').style.visibility = 'visible';
}
});
}
function callExampleContract() {
// this should be generated by ethereum
var param = parseInt(document.getElementById('value').value);
// call the contract
var res = myContract.take([0,6,5,2,1,5,6], param);
document.getElementById('result').innerText = res.toString(10);
}
</script>
</head>
<body>
<h1>contract</h1>
<div id="code"></div>
<div id="status"></div>
<div id='create'>
<button type="button" onClick="createExampleContract();">create example contract</button>
</div>
<div id='call' style='visibility: hidden;'>
<div>var array = [0,6,5,2,1,5,6];</div>
<div>var x = array[
<input type="number" id="value" onkeyup='callExampleContract()'></input>
];
</div>
</div>
<div id="result"></div>
</body>
</html>

27
gulpfile.js

@ -17,7 +17,7 @@ var bower = require('bower');
var streamify = require('gulp-streamify');
var replace = require('gulp-replace');
var DEST = './dist/';
var DEST = path.join(__dirname, 'dist/');
var src = 'index';
var dst = 'web3';
var lightDst = 'web3-light';
@ -29,7 +29,7 @@ var browserifyOptions = {
bundleExternal: true
};
gulp.task('versionReplace', function(){
gulp.task('version', function(){
gulp.src(['./package.json'])
.pipe(replace(/\"version\"\: \"(.{5})\"/, '"version": "'+ version.version + '"'))
.pipe(gulp.dest('./'));
@ -41,24 +41,24 @@ gulp.task('versionReplace', function(){
.pipe(gulp.dest('./'));
});
gulp.task('bower', function(cb){
gulp.task('bower', ['version'], function(cb){
bower.commands.install().on('end', function (installed){
console.log(installed);
cb();
});
});
gulp.task('clean', ['lint'], function(cb) {
del([ DEST ], cb);
});
gulp.task('lint', function(){
gulp.task('lint', ['bower'], function(){
return gulp.src(['./*.js', './lib/*.js'])
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('buildLight', ['clean'], function () {
gulp.task('clean', ['lint'], function(cb) {
del([ DEST ], cb);
});
gulp.task('light', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'web3'})
.ignore('bignumber.js')
@ -73,7 +73,7 @@ gulp.task('buildLight', ['clean'], function () {
.pipe(gulp.dest( DEST ));
});
gulp.task('buildStandalone', ['clean'], function () {
gulp.task('standalone', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'web3'})
.require('bignumber.js') // expose it to dapp users
@ -92,10 +92,5 @@ gulp.task('watch', function() {
gulp.watch(['./lib/*.js'], ['lint', 'build']);
});
gulp.task('light', ['versionReplace','bower', 'lint', 'buildLight']);
gulp.task('standalone', ['versionReplace','bower', 'lint', 'buildStandalone']);
gulp.task('default', ['light', 'standalone']);
gulp.task('version', ['versionReplace']);
gulp.task('default', ['version', 'bower', 'lint', 'clean', 'light', 'standalone']);

95
karma.conf.js

@ -1,95 +0,0 @@
// Karma configuration
// Generated on Thu Feb 19 2015 19:57:47 GMT+0100 (W. Europe Standard Time)
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true, logLevel: config.LOG_INFO,
//singleRun: true, logLevel: config.LOG_DEBUG,
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['browserify', 'mocha'],
// list of files / patterns to load in the browser
files: [
'test/*.js'
],
// list of files to exclude
exclude: [
],
client: {
mocha: {
//ui: 'tdd'
timeout: 5000 // especially for the post requests
}
},
browserify: {
bundleDelay: 750,
debug: true
// transform: [],
// //extensions: ['.js']
},
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': ['browserify']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
// // Chrome
// // PhantomJS
browsers: ['Chrome', 'Safari', 'Firefox'],
browserNoActivityTimeout: 10000,
browserDisconnectTimeout: 5000,
customLaunchers: {
chromeWithoutSecurity: {
base: 'Chrome',
flags: ['--disable-web-security']
},
IE9: {
base: 'IE',
'x-ua-compatible': 'IE=EmulateIE9'
},
IE8: {
base: 'IE',
'x-ua-compatible': 'IE=EmulateIE8'
}
}
});
};

7
lib/solidity/coder.js

@ -262,6 +262,13 @@ var coder = new SolidityCoder([
inputFormatter: f.formatInputBytes,
outputFormatter: f.formatOutputBytes
}),
new SolidityType({
name: 'string',
match: 'strict',
mode: 'bytes',
inputFormatter: f.formatInputString,
outputFormatter: f.formatOutputString
}),
new SolidityType({
name: 'real',
match: 'prefix',

56
lib/solidity/formatters.js

@ -43,26 +43,43 @@ var formatInputInt = function (value) {
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
var result = utils.padRight(utils.toHex(value).substr(2), 64);
return new SolidityParam(result);
};
/**
* Formats input value to byte representation of string
* Formats input bytes
*
* @method formatInputDynamicBytes
* @method formatDynamicInputBytes
* @param {String}
* @returns {SolidityParam}
*/
var formatInputDynamicBytes = function (value) {
var result = utils.fromAscii(value, c.ETH_PADDING).substr(2);
value = utils.toHex(value).substr(2);
var l = Math.floor((value.length + 63) / 64);
var result = utils.padRight(value, l * 64);
var length = Math.floor(value.length / 2);
return new SolidityParam(formatInputInt(length).value + result, 32);
};
/**
* Formats input value to byte representation of string
*
* @method formatInputString
* @param {String}
* @returns {SolidityParam}
*/
var formatInputString = function (value) {
var result = utils.fromAscii(value).substr(2);
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(value.length).value + result, 32);
};
@ -165,27 +182,38 @@ var formatOutputBool = function (param) {
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputBytes = function (param) {
// length might also be important!
return utils.toAscii(param.staticPart());
return '0x' + param.staticPart();
};
/**
* Should be used to format output string
* Should be used to format output bytes
*
* @method formatOutputDynamicBytes
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
* @returns {String} hex string
*/
var formatOutputDynamicBytes = function (param) {
// length might also be important!
return utils.toAscii(param.dynamicPart().slice(64));
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return '0x' + param.dynamicPart().substr(64, length);
};
/**
* Should be used to format output string
*
* @method formatOutputString
* @param {SolidityParam} left-aligned hex representation of string
* @returns {String} ascii string
*/
var formatOutputString = function (param) {
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return utils.toAscii(param.dynamicPart().substr(64, length));
};
/**
@ -204,6 +232,7 @@ module.exports = {
formatInputInt: formatInputInt,
formatInputBytes: formatInputBytes,
formatInputDynamicBytes: formatInputDynamicBytes,
formatInputString: formatInputString,
formatInputBool: formatInputBool,
formatInputReal: formatInputReal,
formatOutputInt: formatOutputInt,
@ -213,6 +242,7 @@ module.exports = {
formatOutputBool: formatOutputBool,
formatOutputBytes: formatOutputBytes,
formatOutputDynamicBytes: formatOutputDynamicBytes,
formatOutputString: formatOutputString,
formatOutputAddress: formatOutputAddress
};

9
lib/solidity/param.js

@ -182,13 +182,14 @@ var getOffset = function (bytes, index) {
*/
SolidityParam.decodeBytes = function (bytes, index) {
index = index || 0;
//TODO add support for strings longer than 32 bytes
//var length = parseInt('0x' + bytes.substr(offset * 64, 64));
var offset = getOffset(bytes, index);
// 2 * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, 2 * 64), 0);
var l = parseInt('0x' + bytes.substr(offset * 2, 64));
l = Math.floor((l + 31) / 32);
// (1 + l) * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0);
};
/**

24
lib/utils/utils.js

@ -75,6 +75,19 @@ var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Should be called to pad string to expected length
*
* @method padRight
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padRight = function (string, chars, sign) {
return string + (new Array(chars - string.length + 1).join(sign ? sign : "0"));
};
/**
* Should be called to get sting from it's hex representation
*
@ -91,10 +104,6 @@ var toAscii = function(hex) {
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) {
break;
}
str += String.fromCharCode(code);
}
@ -204,7 +213,7 @@ var fromDecimal = function (value) {
* @return {String}
*/
var toHex = function (val) {
/*jshint maxcomplexity:7 */
/*jshint maxcomplexity: 8 */
if (isBoolean(val))
return fromDecimal(+val);
@ -218,9 +227,11 @@ var toHex = function (val) {
// if its a negative number, pass it through fromDecimal
if (isString(val)) {
if (val.indexOf('-0x') === 0)
return fromDecimal(val);
return fromDecimal(val);
else if (!isFinite(val))
return fromAscii(val);
else if(val.indexOf('0x') === 0)
return val;
}
return fromDecimal(val);
@ -471,6 +482,7 @@ var isIBAN = function (iban) {
module.exports = {
padLeft: padLeft,
padRight: padRight,
toHex: toHex,
toDecimal: toDecimal,
fromDecimal: fromDecimal,

2
lib/version.json

@ -1,3 +1,3 @@
{
"version": "0.6.0"
"version": "0.7.1"
}

18
lib/web3.js

@ -80,31 +80,25 @@ var setupProperties = function (obj, properties) {
/// setups web3 object, and it's in-browser executed methods
var web3 = {};
web3.providers = {};
web3.currentProvider = null;
web3.version = {};
web3.version.api = version.version;
web3.eth = {};
/*jshint maxparams:4 */
web3.eth.filter = function (fil, eventParams, options, formatter) {
// if its event, treat it differently
// TODO: simplify and remove
if (fil._isEvent) {
return fil(eventParams, options);
}
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
web3.eth.filter = function (fil, callback) {
return new Filter(fil, watches.eth(), formatters.outputLogFormatter, callback);
};
/*jshint maxparams:3 */
web3.shh = {};
web3.shh.filter = function (fil) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter);
web3.shh.filter = function (fil, callback) {
return new Filter(fil, watches.shh(), formatters.outputPostFormatter, callback);
};
web3.net = {};
web3.db = {};
web3.setProvider = function (provider) {
this.currentProvider = provider;
RequestManager.getInstance().setProvider(provider);
};
web3.reset = function () {

81
lib/web3/allevents.js

@ -0,0 +1,81 @@
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file allevents.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var sha3 = require('../utils/sha3');
var SolidityEvent = require('./event');
var formatters = require('./formatters');
var utils = require('../utils/utils');
var Filter = require('./filter');
var watches = require('./watches');
var AllSolidityEvents = function (json, address) {
this._json = json;
this._address = address;
};
AllSolidityEvents.prototype.encode = function (options) {
options = options || {};
var result = {};
['fromBlock', 'toBlock'].filter(function (f) {
return options[f] !== undefined;
}).forEach(function (f) {
result[f] = formatters.inputBlockNumberFormatter(options[f]);
});
result.topics = [null, null, null, null, null]; // match all topics
result.address = this._address;
return result;
};
AllSolidityEvents.prototype.decode = function (data) {
data.data = data.data || '';
data.topics = data.topics || [];
var eventTopic = data.topics[0].slice(2);
var match = this._json.filter(function (j) {
return eventTopic === sha3(utils.transformToFullName(j));
})[0];
if (!match) { // cannot find matching event?
console.warn('cannot find event for log');
return data;
}
var event = new SolidityEvent(match, this._address);
return event.decode(data);
};
AllSolidityEvents.prototype.execute = function (options, callback) {
var o = this.encode(options);
var formatter = this.decode.bind(this);
return new Filter(o, watches.eth(), formatter, callback);
};
AllSolidityEvents.prototype.attachToContract = function (contract) {
var execute = this.execute.bind(this);
contract.allEvents = execute;
};
module.exports = AllSolidityEvents;

10
lib/web3/contract.js

@ -25,6 +25,7 @@ var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var SolidityEvent = require('./event');
var SolidityFunction = require('./function');
var AllEvents = require('./allevents');
/**
* Should be called to encode constructor params
@ -70,9 +71,14 @@ var addFunctionsToContract = function (contract, abi) {
* @param {Array} abi
*/
var addEventsToContract = function (contract, abi) {
abi.filter(function (json) {
var events = abi.filter(function (json) {
return json.type === 'event';
}).map(function (json) {
});
var All = new AllEvents(events, contract.address);
All.attachToContract(contract);
events.map(function (json) {
return new SolidityEvent(json, contract.address);
}).forEach(function (e) {
e.attachToContract(contract);

18
lib/web3/event.js

@ -22,9 +22,10 @@
var utils = require('../utils/utils');
var coder = require('../solidity/coder');
var web3 = require('../web3');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');
var Filter = require('./filter');
var watches = require('./watches');
/**
* This prototype should be used to create event filters
@ -170,10 +171,21 @@ SolidityEvent.prototype.decode = function (data) {
* @param {Object} options
* @return {Object} filter object
*/
SolidityEvent.prototype.execute = function (indexed, options) {
SolidityEvent.prototype.execute = function (indexed, options, callback) {
if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
if(arguments.length === 2)
options = null;
if(arguments.length === 1) {
options = null;
indexed = {};
}
}
var o = this.encode(indexed, options);
var formatter = this.decode.bind(this);
return web3.eth.filter(o, undefined, undefined, formatter);
return new Filter(o, watches.eth(), formatter, callback);
};
/**

23
lib/web3/filter.js

@ -127,7 +127,7 @@ var pollFilter = function(self) {
};
var Filter = function (options, methods, formatter) {
var Filter = function (options, methods, formatter, callback) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
@ -135,23 +135,32 @@ var Filter = function (options, methods, formatter) {
});
this.options = getOptions(options);
this.implementation = implementation;
this.filterId = null;
this.callbacks = [];
this.pollFilters = [];
this.formatter = formatter;
this.implementation.newFilter(this.options, function(error, id){
if(error) {
self.callbacks.forEach(function(callback){
callback(error);
self.callbacks.forEach(function(cb){
cb(error);
});
} else {
self.filterId = id;
// get filter logs at start
self.callbacks.forEach(function(callback){
getLogsAtStart(self, callback);
// get filter logs for the already existing watch calls
self.callbacks.forEach(function(cb){
getLogsAtStart(self, cb);
});
pollFilter(self);
if(self.callbacks.length > 0)
pollFilter(self);
// start to watch immediately
if(callback) {
return self.watch(callback);
}
}
});
};
Filter.prototype.watch = function (callback) {

3
lib/web3/function.js

@ -188,8 +188,9 @@ SolidityFunction.prototype.request = function () {
var format = this.unpackOutput.bind(this);
return {
method: this._constant ? 'eth_call' : 'eth_sendTransaction',
callback: callback,
payload: payload,
params: [payload],
format: format
};
};

3
lib/web3/httpprovider.js

@ -24,7 +24,8 @@
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
// resolves the problem for electron/atom shell environments, which use node integration, but have no process variable available
var XMLHttpRequest = (typeof window !== 'undefined' && window.XMLHttpRequest) ? window.XMLHttpRequest : require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {

15
package-init.js

@ -1,8 +1,17 @@
/* jshint ignore:start */
if(typeof web3 === 'undefined') {
web3 = require('web3');
BigNumber = require('bignumber.js');
// Browser environment
if(typeof window !== 'undefined') {
web3 = (typeof window.web3 !== 'undefined') ? window.web3 : require('web3');
BigNumber = (typeof window.BigNumber !== 'undefined') ? window.BigNumber : require('bignumber.js');
}
// Node environment
if(typeof global !== 'undefined') {
web3 = (typeof global.web3 !== 'undefined') ? global.web3 : require('web3');
BigNumber = (typeof global.BigNumber !== 'undefined') ? global.BigNumber : require('bignumber.js');
}
/* jshint ignore:end */

4
package.js

@ -1,7 +1,7 @@
/* jshint ignore:start */
Package.describe({
name: 'ethereum:web3',
version: '0.6.0',
version: '0.7.1',
summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC',
git: 'https://github.com/ethereum/ethereum.js',
// By default, Meteor will default to using README.md for documentation.
@ -14,7 +14,7 @@ Package.onUse(function(api) {
// api.use('3stack:bignumber@2.0.0', 'client');
api.export(['web3', 'BigNumber'], 'client');
api.export(['web3', 'BigNumber'], ['client', 'server']);
api.addFiles('dist/web3.js', 'client');
api.addFiles('package-init.js', 'client');

25
package.json

@ -1,7 +1,7 @@
{
"name": "web3",
"namespace": "ethereum",
"version": "0.6.0",
"version": "0.7.1",
"description": "Ethereum JavaScript API, middleware to talk to a ethereum node over RPC",
"main": "./index.js",
"directories": {
@ -16,37 +16,30 @@
"xmlhttprequest": "./lib/utils/browser-xhr.js"
},
"devDependencies": {
"bower": ">=1.3.0",
"bower": ">=1.4.1",
"browserify": ">=10.0",
"chai": "^2.1.1",
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"del": ">=0.1.1",
"del": ">=1.2.0",
"exorcist": "^0.4.0",
"gulp": ">=3.4.0",
"gulp": ">=3.9.0",
"gulp-jshint": ">=1.5.0",
"gulp-rename": ">=1.2.0",
"gulp-replace": "^0.5.3",
"gulp-streamify": "0.0.5",
"gulp-uglify": ">=1.0.0",
"gulp-uglify": ">=1.2.0",
"istanbul": "^0.3.5",
"jshint": ">=2.5.0",
"karma": "^0.12.31",
"karma-browserify": "^4.0.0",
"karma-chrome-launcher": "^0.1.7",
"karma-firefox-launcher": "^0.1.4",
"karma-mocha": "^0.1.10",
"karma-safari-launcher": "^0.1.1",
"mocha": ">=2.1.0",
"sandboxed-module": "^2.0.0",
"vinyl-source-stream": "^1.0.0"
"sandboxed-module": "^2.0.2",
"vinyl-source-stream": "^1.1.0"
},
"scripts": {
"build": "gulp",
"watch": "gulp watch",
"lint": "jshint *.js lib",
"test": "mocha",
"test-coveralls": "istanbul cover _mocha -- -R spec && cat coverage/lcov.info | coveralls --verbose",
"karma": "./node_modules/karma/bin/karma start --singleRun=true --browsers=\"Firefox\""
"test-coveralls": "istanbul cover _mocha -- -R spec && cat coverage/lcov.info | coveralls --verbose"
},
"repository": {
"type": "git",

25
test/batch.js

@ -28,6 +28,16 @@ describe('lib/web3/batch', function () {
done();
};
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_getBalance');
assert.deepEqual(second.params, ['0x0000000000000000000000000000000000000005', 'latest']);
});
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000005', 'latest', callback2));
@ -55,7 +65,7 @@ describe('lib/web3/batch', function () {
}];
var address = '0x0000000000000000000000000000000000000000';
var address = '0x1000000000000000000000000000000000000001';
var result = '0x126';
var result2 = '0x0000000000000000000000000000000000000000000000000000000000000123';
@ -71,6 +81,19 @@ describe('lib/web3/batch', function () {
done();
};
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_call');
assert.deepEqual(second.params, [{
'to': '0x1000000000000000000000000000000000000001',
'data': '0xe3d670d70000000000000000000000001000000000000000000000000000000000000001'
}]);
});
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));

98
test/coder.decodeParam.js

@ -20,10 +20,53 @@ describe('lib/solidity/coder', function () {
test({ type: 'int256', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'bytes32', expected: 'gavofyork', value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' +
test({ type: 'int8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', expected: '0x6761766f66796f726b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000060' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'string', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: '\xc3\xa4\x00\x00\xc3\xa4',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: '\xc3',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'c300000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes', expected: '0xc3a40000c3a4',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
@ -49,6 +92,11 @@ describe('lib/solidity/coder', function () {
test({ type: 'ureal', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'address', expected: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'string', expected: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'000000000000000000000000000000000000000000000000000000000000003e' +
'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
});
});
@ -62,26 +110,44 @@ describe('lib/solidity/coder', function () {
test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['bytes32', 'int'], expected: ['gavofyork', new bn(5)],
test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)],
values: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], expected: [new bn(5), 'gavofyork'],
test({ types: ['int', 'bytes32'], expected: [new bn(5), '0x6761766f66796f726b0000000000000000000000000000000000000000000000'],
values: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4),
test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4),
[new bn(5), new bn(6), new bn(7)]],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000100' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000100' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
test({ types: ['int', 'bytes', 'int', 'bytes'], expected: [
new bn(5),
'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
new bn(3),
'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
],
values: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
});
});

87
test/coder.encodeParam.js

@ -20,10 +20,37 @@ describe('lib/solidity/coder', function () {
test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' +
test({ type: 'bytes32', value: '0x6761766f66796f726b',
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes32', value: '0x02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29',
expected: '02838654a83c213dae3698391eabbd54a5b6e1fb3452bc7fa4ea0dd5c8ce7e29'});
test({ type: 'bytes', value: '0x6761766f66796f726b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'string', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes', value: '0xc3a40000c3a4',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0xc3a40000c3a4',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: '\xc3\xa4\x00\x00\xc3\xa4',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: '\xc3',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'c300000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
@ -48,6 +75,25 @@ describe('lib/solidity/coder', function () {
test({ type: 'ureal', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'ureal', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'ureal', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', value: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000060' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'string', value: 'welcome to ethereum. welcome to ethereum. welcome to ethereum.',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'000000000000000000000000000000000000000000000000000000000000003e' +
'77656c636f6d6520746f20657468657265756d2e2077656c636f6d6520746f20' +
'657468657265756d2e2077656c636f6d6520746f20657468657265756d2e0000'});
});
});
@ -67,8 +113,9 @@ describe('lib/solidity/coder', function () {
test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['bytes32'], values: ['gavofyork'], expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
@ -91,18 +138,18 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004'});
test({ types: ['bytes32', 'int'], values: ['gavofyork', 5],
test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], values: [5, 'gavofyork'],
test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes', 'int'], values: ['gavofyork', 5],
test({ types: ['string', 'int'], values: ['gavofyork', 5],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
test({ types: ['string', 'bool', 'int[]'], values: ['gavofyork', true, [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000060' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
@ -112,7 +159,7 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['bytes', 'int[]'], values: ['gavofyork', [1, 2, 3]],
test({ types: ['string', 'int[]'], values: ['gavofyork', [1, 2, 3]],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
@ -121,12 +168,12 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ types: ['int', 'bytes'], values: [5, 'gavofyork'],
test({ types: ['int', 'string'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
test({ types: ['int', 'string', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]],
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
@ -139,6 +186,24 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
test({ types: ['int', 'bytes', 'int', 'bytes'], values: [
5,
'0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
3,
'0x331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'431a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
});
});

141
test/contract.js

@ -127,6 +127,147 @@ describe('web3.eth.contract', function () {
});
});
it('should create event filter and watch immediately', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset(); // reset different polls
var signature = 'Changed(address,uint256,uint256,uint256)';
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
provider.injectResult('0x3');
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newFilter');
assert.deepEqual(payload.params[0], {
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
null
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 1) {
step = 2;
provider.injectResult([{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 2 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]]);
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === '0x3';
});
assert.equal(r.length > 0, true);
}
});
var contract = web3.eth.contract(desc).at(address);
var res = 0;
var event = contract.Changed({from: address}, function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
res++;
if (res === 2) {
done();
}
});
});
it('should create all event filter', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset(); // reset different polls
var signature = 'Changed(address,uint256,uint256,uint256)';
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
provider.injectResult('0x3');
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_newFilter');
assert.deepEqual(payload.params[0], {
topics: [
null,
null,
null,
null,
null
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 1) {
step = 2;
provider.injectResult([{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 2 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
'0x' + sha3(signature),
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]]);
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === '0x3';
});
assert.equal(r.length > 0, true);
}
});
var contract = web3.eth.contract(desc).at(address);
var res = 0;
var event = contract.allEvents();
event.watch(function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
res++;
if (res === 2) {
done();
}
});
});
it('should call constant function', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);

40
test/polling.js

@ -63,6 +63,42 @@ var testPolling = function (tests) {
var filter = web3[test.protocol].filter.apply(null, test.args);
provider.injectBatchResults([test.secondResult]);
filter.watch(function (err, result) {
console.log(err, result);
if (test.err) {
// todo
} else {
assert.equal(result, test.secondResult[0]);
}
done();
});
});
it('should create && successfully poll filter when passed as callback', function (done) {
// given
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult(test.firstResult);
var step = 0;
provider.injectValidation(function (payload) {
if (step === 0) {
step = 1;
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, test.firstPayload.method);
assert.deepEqual(payload.params, test.firstPayload.params);
} else if (step === 1 && utils.isArray(payload)) {
var r = payload.filter(function (p) {
return p.jsonrpc === '2.0' && p.method === test.secondPayload.method && p.params[0] === test.firstResult;
});
assert.equal(r.length > 0, true);
}
});
// add callback
test.args.push(function (err, result) {
if (test.err) {
// todo
} else {
@ -71,6 +107,10 @@ var testPolling = function (tests) {
done();
});
// when
var filter = web3[test.protocol].filter.apply(null, test.args);
provider.injectBatchResults([test.secondResult]);
});
});
});

1
test/utils.toHex.js

@ -14,6 +14,7 @@ var tests = [
{ value: '-0x1', expected: '-0x1'},
{ value: '-15', expected: '-0xf'},
{ value: '-0xf', expected: '-0xf'},
{ value: '0x657468657265756d', expected: '0x657468657265756d'},
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},

8
test/web3.eth.filter.methods.js

@ -14,10 +14,10 @@ var implementation = {
describe('web3.eth.filter', function () {
describe('methods', function () {
//var f = filter({}, implementation);
// var f = filter({}, implementation);
//u.methodExists(f, 'watch');
//u.methodExists(f, 'stopWatching');
//u.methodExists(f, 'get');
// u.methodExists(f, 'watch');
// u.methodExists(f, 'stopWatching');
// u.methodExists(f, 'get');
});
});

Loading…
Cancel
Save