Skip to content

Commit

Permalink
Merge f242489 into 61f1ba6
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Jun 5, 2015
2 parents 61f1ba6 + f242489 commit bd3d505
Show file tree
Hide file tree
Showing 12 changed files with 587 additions and 221 deletions.
237 changes: 147 additions & 90 deletions dist/web3-light.js

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions dist/web3-light.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3-light.min.js

Large diffs are not rendered by default.

237 changes: 147 additions & 90 deletions dist/web3.js

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions dist/web3.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ web3.eth.filter = function (fil, eventParams, options, formatter) {
return fil(eventParams, options);
}

// what outputLogFormatter? that's wrong
//return new Filter(fil, watches.eth(), formatters.outputLogFormatter);
// output logs works for blockFilter and pendingTransaction filters?
return new Filter(fil, watches.eth(), formatter || formatters.outputLogFormatter);
};
/*jshint maxparams:3 */
Expand Down
77 changes: 48 additions & 29 deletions lib/web3/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var getOptions = function (options) {
};

var Filter = function (options, methods, formatter) {
var self = this;
var implementation = {};
methods.forEach(function (method) {
method.attachToObject(implementation);
Expand All @@ -83,51 +84,69 @@ var Filter = function (options, methods, formatter) {
this.implementation = implementation;
this.callbacks = [];
this.formatter = formatter;
this.filterId = this.implementation.newFilter(this.options);
this.implementation.newFilter(this.options, function(error, id){
if(error)
self.filterError = error;
else
self.filterId = id;
});
};

Filter.prototype.watch = function (callback) {
this.callbacks.push(callback);
var self = this;

var onMessage = function (error, messages) {
if (error) {
return self.callbacks.forEach(function (callback) {
callback(error);
});
}
// check inf an interval of 10ms if the filter id has arrived
var intervalId = setInterval(function(){

messages.forEach(function (message) {
message = self.formatter ? self.formatter(message) : message;
self.callbacks.forEach(function (callback) {
callback(null, message);
});
});
};
if(self.filterId || self.filterError)
clearInterval(intervalId);

// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
if(!self.filterId)
return;

self.callbacks.push(callback);

var onMessage = function (error, messages) {
if (error) {
return self.callbacks.forEach(function (callback) {
callback(error);
});
}

messages.forEach(function (message) {
callback(null, message);
message = self.formatter ? self.formatter(message) : message;
self.callbacks.forEach(function (callback) {
callback(null, message);
});
});
});
}
};

// call getFilterLogs on start
if (!utils.isString(self.options)) {
self.get(function (err, messages) {
// don't send all the responses to all the watches again... just to self one
if (err) {
callback(err);
}

messages.forEach(function (message) {
callback(null, message);
});
});
}

RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
}, this.filterId, onMessage, this.stopWatching.bind(this));
RequestManager.getInstance().startPolling({
method: self.implementation.poll.call,
params: [self.filterId],
}, self.filterId, onMessage, self.stopWatching.bind(self));

}, 10);
};

Filter.prototype.stopWatching = function () {
RequestManager.getInstance().stopPolling(this.filterId);
this.implementation.uninstallFilter(this.filterId);
// remove filter async
this.implementation.uninstallFilter(this.filterId, function(){});
this.callbacks = [];
};

Expand Down
13 changes: 11 additions & 2 deletions lib/web3/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var web3 = require('../web3');
var coder = require('../solidity/coder');
var utils = require('../utils/utils');
var formatters = require('./formatters');
var sha3 = require('../utils/sha3');

/**
Expand All @@ -46,6 +47,12 @@ SolidityFunction.prototype.extractCallback = function (args) {
}
};

SolidityFunction.prototype.extractDefaultBlock = function (args) {
if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) {
return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array!
}
};

/**
* Should be used to create payload from arguments
*
Expand Down Expand Up @@ -97,15 +104,17 @@ SolidityFunction.prototype.unpackOutput = function (output) {
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var defaultBlock = this.extractDefaultBlock(args);
var payload = this.toPayload(args);


if (!callback) {
var output = web3.eth.call(payload);
var output = web3.eth.call(payload, defaultBlock);
return this.unpackOutput(output);
}

var self = this;
web3.eth.call(payload, function (error, output) {
web3.eth.call(payload, defaultBlock, function (error, output) {
callback(error, self.unpackOutput(output));
});
};
Expand Down
11 changes: 9 additions & 2 deletions lib/web3/requestmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,16 @@ RequestManager.prototype.poll = function () {
}

results.map(function (result, index) {
result.callback = self.polls[index].callback;
return result;
// make sure the filter is still installed after arrival of the request
if(self.polls[index]) {
result.callback = self.polls[index].callback;
return result;
} else
return false;
}).filter(function (result) {
if(!result)
return false;

var valid = Jsonrpc.getInstance().isValidResponse(result);
if (!valid) {
result.callback(errors.InvalidResponse(result));
Expand Down
4 changes: 2 additions & 2 deletions lib/web3/watches.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ var eth = function () {

switch(type) {
case 'latest':
args.pop();
args.shift();
this.params = 0;
return 'eth_newBlockFilter';
case 'pending':
args.pop();
args.shift();
this.params = 0;
return 'eth_newPendingTransactionFilter';
default:
Expand Down
48 changes: 48 additions & 0 deletions test/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('web3.eth.contract', function () {
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)'
var address = '0x1234567890123456789012345678901234567890';

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
Expand All @@ -147,6 +148,28 @@ describe('web3.eth.contract', function () {
assert.deepEqual(new BigNumber(0x32), r);
});

it('should call constant function with default block', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)'
var address = '0x1234567890123456789012345678901234567890';

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
to: address
}, '0xb']);
});

var contract = web3.eth.contract(desc).at(address);

var r = contract.balance(address, 11);
assert.deepEqual(new BigNumber(0x32), r);
});

it('should sendTransaction to contract function', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
Expand Down Expand Up @@ -218,6 +241,31 @@ describe('web3.eth.contract', function () {

});

it('should explicitly make a call with optional params and defaultBlock', function () {

var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
provider.injectResult('0x0000000000000000000000000000000000000000000000000000000000000032');
var signature = 'balance(address)';
var address = '0x1234567890123456789012345678901234567890';
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_call');
assert.deepEqual(payload.params, [{
data: '0x' + sha3(signature).slice(0, 8) + '0000000000000000000000001234567890123456789012345678901234567890',
to: address,
from: address,
gas: '0xc350'
}, '0xb']);
});

var contract = web3.eth.contract(desc).at(address);

var r = contract.balance.call(address, {from: address, gas: 50000}, 11);
assert.deepEqual(new BigNumber(0x32), r);

});

it('should sendTransaction with optional params', function () {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
Expand Down

0 comments on commit bd3d505

Please sign in to comment.