diff --git a/contract.js b/contract.js index 00f9cbcc8..2632575f5 100644 --- a/contract.js +++ b/contract.js @@ -66,7 +66,7 @@ describe('web3.eth.contract', function () { provider.injectValidation(function (payload) { if (step === 0) { step = 1; - provider.injectResult(3); + provider.injectResult('0x3'); assert.equal(payload.jsonrpc, '2.0'); assert.equal(payload.method, 'eth_newFilter'); assert.deepEqual(payload.params[0], { @@ -105,7 +105,7 @@ describe('web3.eth.contract', function () { '0000000000000000000000000000000000000000000000000000000000000008' }]]); var r = payload.filter(function (p) { - return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === 3; + return p.jsonrpc === '2.0' && p.method === 'eth_getFilterChanges' && p.params[0] === '0x3'; }); assert.equal(r.length > 0, true); } @@ -114,7 +114,8 @@ describe('web3.eth.contract', function () { var contract = web3.eth.contract(desc).at(address); var res = 0; - contract.Changed({from: address}).watch(function(err, result) { + var event = contract.Changed({from: address}); + event.watch(function(err, result) { assert.equal(result.args.from, address); assert.equal(result.args.amount, 1); assert.equal(result.args.t1, 1); @@ -133,6 +134,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, [{ @@ -147,6 +149,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); @@ -218,6 +242,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); diff --git a/formatters.outputBlockFormatter.js b/formatters.outputBlockFormatter.js index 272250651..a94c87b37 100644 --- a/formatters.outputBlockFormatter.js +++ b/formatters.outputBlockFormatter.js @@ -8,38 +8,72 @@ describe('formatters', function () { it('should return the correct value', function () { assert.deepEqual(formatters.outputBlockFormatter({ - hash: '0x34234kjh23kj4234', - parentHash: '0x34234kjh23kj4234', - miner: '0x34234kjh23kj4234', - stateRoot: '0x34234kjh23kj4234', - sha3Uncles: '0x34234kjh23kj4234', - bloom: '0x34234kjh23kj4234', + hash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + parentHash: '0x83ffb245cfced97ccc5c75253d6960376d6c6dea93647397a543a72fdaea5265', + miner: '0xdcc6960376d6c6dea93647383ffb245cfced97cf', + stateRoot: '0x54dda68af07643f68739a6e9612ad157a26ae7e2ce81f77842bb5835fbcde583', + sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', + bloom: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', difficulty: '0x3e8', totalDifficulty: '0x3e8', number: '0x3e8', gasLimit: '0x3e8', gasUsed: '0x3e8', timestamp: '0x3e8', - extraData: '0x34234kjh23kj4234', - nonce: '0x34234kjh23kj4234', - children: ['0x34234kjh23kj4234'], + extraData: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + nonce: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', size: '0x3e8' }), { - hash: '0x34234kjh23kj4234', - parentHash: '0x34234kjh23kj4234', - miner: '0x34234kjh23kj4234', - stateRoot: '0x34234kjh23kj4234', - sha3Uncles: '0x34234kjh23kj4234', - bloom: '0x34234kjh23kj4234', + hash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + parentHash: '0x83ffb245cfced97ccc5c75253d6960376d6c6dea93647397a543a72fdaea5265', + miner: '0xdcc6960376d6c6dea93647383ffb245cfced97cf', + stateRoot: '0x54dda68af07643f68739a6e9612ad157a26ae7e2ce81f77842bb5835fbcde583', + sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', + bloom: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', difficulty: new BigNumber(1000), totalDifficulty: new BigNumber(1000), number: 1000, gasLimit: 1000, gasUsed: 1000, timestamp: 1000, - extraData: '0x34234kjh23kj4234', - nonce: '0x34234kjh23kj4234', - children: ['0x34234kjh23kj4234'], + extraData: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + nonce: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + size: 1000 + }); + }); + it('should return the correct value, when null values are present', function () { + + assert.deepEqual(formatters.outputBlockFormatter({ + hash: null, + parentHash: '0x83ffb245cfced97ccc5c75253d6960376d6c6dea93647397a543a72fdaea5265', + miner: null, + stateRoot: '0x54dda68af07643f68739a6e9612ad157a26ae7e2ce81f77842bb5835fbcde583', + sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', + bloom: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + difficulty: '0x3e8', + totalDifficulty: '0x3e8', + number: null, + gasLimit: '0x3e8', + gasUsed: '0x3e8', + timestamp: '0x3e8', + extraData: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + nonce: null, + size: '0x3e8' + }), { + hash: null, + parentHash: '0x83ffb245cfced97ccc5c75253d6960376d6c6dea93647397a543a72fdaea5265', + miner: null, + stateRoot: '0x54dda68af07643f68739a6e9612ad157a26ae7e2ce81f77842bb5835fbcde583', + sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', + bloom: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + difficulty: new BigNumber(1000), + totalDifficulty: new BigNumber(1000), + number: null, + gasLimit: 1000, + gasUsed: 1000, + timestamp: 1000, + extraData: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + nonce: null, size: 1000 }); }); diff --git a/formatters.outputLogFormatter.js b/formatters.outputLogFormatter.js index 495a64620..432bfb0d2 100644 --- a/formatters.outputLogFormatter.js +++ b/formatters.outputLogFormatter.js @@ -9,17 +9,37 @@ describe('formatters', function () { transactionIndex: '0x3e8', logIndex: '0x3e8', blockNumber: '0x3e8', - transactionHash: '0x7b2274657374223a2274657374227d', - blockHash: '0x7b2274657374223a2274657374227d', - data: '0x7b2274657374223a2274657374227d', + transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + data: '0x7b2274657374223a2274657374227', topics: ['0x68656c6c6f','0x6d79746f70696373'] }), { transactionIndex: 1000, logIndex: 1000, blockNumber: 1000, - transactionHash: '0x7b2274657374223a2274657374227d', - blockHash: '0x7b2274657374223a2274657374227d', - data: '0x7b2274657374223a2274657374227d', + transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', + data: '0x7b2274657374223a2274657374227', + topics: ['0x68656c6c6f','0x6d79746f70696373'] + }); + }); + it('should return the correct value, when null values are present', function () { + + assert.deepEqual(formatters.outputLogFormatter({ + transactionIndex: null, + logIndex: null, + blockNumber: null, + transactionHash: null, + blockHash: null, + data: '0x7b2274657374223a2274657374227', + topics: ['0x68656c6c6f','0x6d79746f70696373'] + }), { + transactionIndex: null, + logIndex: null, + blockNumber: null, + transactionHash: null, + blockHash: null, + data: '0x7b2274657374223a2274657374227', topics: ['0x68656c6c6f','0x6d79746f70696373'] }); }); diff --git a/formatters.outputTransactionFormatter.js b/formatters.outputTransactionFormatter.js index ef19b6da7..db4831602 100644 --- a/formatters.outputTransactionFormatter.js +++ b/formatters.outputTransactionFormatter.js @@ -7,7 +7,7 @@ describe('formatters', function () { it('should return the correct value', function () { assert.deepEqual(formatters.outputTransactionFormatter({ - input: '0x34234kjh23kj4234', + input: '0x3454645634534', from: '0x00000', to: '0x00000', value: '0x3e8', @@ -16,9 +16,9 @@ describe('formatters', function () { nonce: '0xb', transactionIndex: '0x1', blockNumber: '0x3e8', - blockHash: '0x34234bf23bf4234' + blockHash: '0xc9b9cdc2092a9d6589d96662b1fd6949611163fb3910cf8a173cd060f17702f9' }), { - input: '0x34234kjh23kj4234', + input: '0x3454645634534', from: '0x00000', to: '0x00000', value: new BigNumber(1000), @@ -26,9 +26,36 @@ describe('formatters', function () { gasPrice: new BigNumber(1000), nonce: 11, blockNumber: 1000, - blockHash: '0x34234bf23bf4234', + blockHash: '0xc9b9cdc2092a9d6589d96662b1fd6949611163fb3910cf8a173cd060f17702f9', transactionIndex: 1 }); }); + + it('should return the correct value, when null values are present', function () { + + assert.deepEqual(formatters.outputTransactionFormatter({ + input: '0x3454645634534', + from: '0x00000', + to: null, + value: '0x3e8', + gas: '0x3e8', + gasPrice: '0x3e8', + nonce: '0xb', + transactionIndex: null, + blockNumber: null, + blockHash: null + }), { + input: '0x3454645634534', + from: '0x00000', + to: null, + value: new BigNumber(1000), + gas: 1000, + gasPrice: new BigNumber(1000), + nonce: 11, + blockNumber: null, + blockHash: null, + transactionIndex: null + }); + }); }); }); diff --git a/helpers/FakeHttpProvider2.js b/helpers/FakeHttpProvider2.js index 0f26d84b7..e287a0745 100644 --- a/helpers/FakeHttpProvider2.js +++ b/helpers/FakeHttpProvider2.js @@ -15,11 +15,21 @@ FakeHttpProvider2.prototype.injectResultList = function (list) { FakeHttpProvider2.prototype.getResponse = function () { var result = this.resultList[this.counter]; this.counter++; + + // add fallback result value + if(!result) + result = { + result: undefined + }; + if (result.type === 'batch') { this.injectBatchResults(result.result); } else { this.injectResult(result.result); } + + this.counter = 0; + return this.response; }; diff --git a/helpers/FakeXMLHttpRequest.js b/helpers/FakeXMLHttpRequest.js index a67d7f74a..6dc35b98a 100644 --- a/helpers/FakeXMLHttpRequest.js +++ b/helpers/FakeXMLHttpRequest.js @@ -6,6 +6,9 @@ var FakeXMLHttpRequest = function () { this.readyState = 4; this.onreadystatechange = null; this.async = false; + this.headers = { + 'Content-Type': 'text/plain' + }; }; FakeXMLHttpRequest.prototype.open = function (method, host, async) { @@ -15,6 +18,10 @@ FakeXMLHttpRequest.prototype.open = function (method, host, async) { this.async = async; }; +FakeXMLHttpRequest.prototype.setRequestHeader = function(name, value) { + this.headers[name] = value; +}; + FakeXMLHttpRequest.prototype.send = function (payload) { assert.equal(typeof payload, 'string'); if (this.async) { diff --git a/web3.extend.js b/web3.extend.js new file mode 100644 index 000000000..dfa863a23 --- /dev/null +++ b/web3.extend.js @@ -0,0 +1,76 @@ +var chai = require('chai'); +var assert = chai.assert; +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var web3 = require('../lib/web3'); + + +var tests = [{ + properties: [new web3._extend.Property({ + name: 'gasPrice', + getter: 'eth_gasPrice', + outputFormatter: web3._extend.formatters.outputBigNumberFormatter + })] +},{ + methods: [new web3._extend.Method({ + name: 'getBalance', + call: 'eth_getBalance', + params: 2, + inputFormatter: [web3._extend.utils.toAddress, web3._extend.formatters.inputDefaultBlockNumberFormatter], + outputFormatter: web3._extend.formatters.outputBigNumberFormatter + })] +},{ + property: 'admin', + properties: [new web3._extend.Property({ + name: 'gasPrice', + getter: 'eth_gasPrice', + outputFormatter: web3._extend.formatters.outputBigNumberFormatter + })], + methods: [new web3._extend.Method({ + name: 'getBalance', + call: 'eth_getBalance', + params: 2, + inputFormatter: [web3._extend.utils.toAddress, web3._extend.formatters.inputDefaultBlockNumberFormatter], + outputFormatter: web3._extend.formatters.outputBigNumberFormatter + })] +}]; + +describe('web3', function () { + describe('_extend', function () { + tests.forEach(function (test, index) { + it('test no: ' + index, function () { + web3._extend(test); + + + if(test.properties) + test.properties.forEach(function(property){ + + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(''); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, property.getter); + }); + + if(test.property) { + assert.isObject(web3[test.property][property.name]); + assert.isFunction(web3[test.property]['get'+ property.name.charAt(0).toUpperCase() + property.name.slice(1)]); + } else { + assert.isObject(web3[property.name]); + assert.isFunction(web3['get'+ property.name.charAt(0).toUpperCase() + property.name.slice(1)]); + } + }); + + if(test.methods) + test.methods.forEach(function(property){ + if(test.property) + assert.isFunction(web3[test.property][property.name]); + else + assert.isFunction(web3[property.name]); + }); + + }); + }); + }); +}); +