From 2994eb4e630c2498c2aff272965fbc0a215cf055 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 29 Jan 2015 15:26:42 +0100 Subject: [PATCH] Squashed 'libjsqrc/ethereumjs/' changes from 6d59047..94e0e5a 94e0e5a Merge branch 'cpp' into cpp2 8613382 moved comment df17c33 event example c8ee08c contract.js simplified 842b8cf event.js e1c0862 Fix for API. 61e8ae2 events init 2544d2c tests for abi.filters ea7c2fc abi function type 63d9c07 fixed incoming messages 1345a8c log error on console, if api returns an error 83fad0f removed fromFixed, toFixed && offset from tests c2cb2be removed web3.eth.account, fixed #37 09f6335 fixed #23 42a25f2 evaluating solidity method input params git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: 94e0e5ab7d8ec9adcd03fedc3abe5cf6444a5123 --- abi.filters.js | 49 ++++++++++++ abi.parsers.js | 37 +++++++++ eth.contract.js | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ eth.methods.js | 1 - event.js | 22 ++++++ web3.methods.js | 3 - 6 files changed, 309 insertions(+), 4 deletions(-) create mode 100644 abi.filters.js create mode 100644 eth.contract.js create mode 100644 event.js diff --git a/abi.filters.js b/abi.filters.js new file mode 100644 index 000000000..42385fd2a --- /dev/null +++ b/abi.filters.js @@ -0,0 +1,49 @@ +var assert = require('assert'); +var abi = require('../lib/abi.js'); + +describe('abi', function() { + it('should filter functions and events from input array properly', function () { + + // given + var description = [{ + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ], + }, { + "name": "test2", + "type": "event", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + // when + var events = abi.filterEvents(description); + var functions = abi.filterFunctions(description); + + // then + assert.equal(events.length, 1); + assert.equal(events[0].name, 'test2'); + assert.equal(functions.length, 1); + assert.equal(functions[0].name, 'test'); + + }); +}); diff --git a/abi.parsers.js b/abi.parsers.js index 19fa1d4cf..12bccf5a5 100644 --- a/abi.parsers.js +++ b/abi.parsers.js @@ -5,6 +5,7 @@ var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; var description = [{ "name": "test", + "type": "function", "inputs": [{ "name": "a", "type": "uint256" @@ -339,10 +340,12 @@ describe('abi', function() { // given var d = [{ name: "test", + type: "function", inputs: [{ type: "int" }], outputs: [{ type: "int" }] },{ name: "test2", + type: "function", inputs: [{ type: "string" }], outputs: [{ type: "string" }] }]; @@ -775,10 +778,12 @@ describe('abi', function() { // given var d = [{ name: "test", + type: "function", inputs: [{ type: "int" }], outputs: [{ type: "int" }] },{ name: "test2", + type: "function", inputs: [{ type: "string" }], outputs: [{ type: "string" }] }]; @@ -823,6 +828,38 @@ describe('abi', function() { }); + it('should parse 0x value', function () { + + // given + var d = clone(description); + d[0].outputs = [ + { type: 'int' } + ]; + + // when + var parser = abi.outputParser(d); + + // then + assert.equal(parser.test("0x")[0], 0); + + }); + + it('should parse 0x value', function () { + + // given + var d = clone(description); + d[0].outputs = [ + { type: 'uint' } + ]; + + // when + var parser = abi.outputParser(d); + + // then + assert.equal(parser.test("0x")[0], 0); + + }); + }); }); diff --git a/eth.contract.js b/eth.contract.js new file mode 100644 index 000000000..1a92ec88f --- /dev/null +++ b/eth.contract.js @@ -0,0 +1,201 @@ +var assert = require('assert'); +var contract = require('../lib/contract.js'); + +describe('contract', function() { + it('should create simple contract with one method from abi with explicit type name', function () { + + // given + var description = [{ + "name": "test(uint256)", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + // when + var con = contract(null, description); + + // then + assert.equal('function', typeof con.test); + assert.equal('function', typeof con.test['uint256']); + }); + + it('should create simple contract with one method from abi with implicit type name', function () { + + // given + var description = [{ + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + // when + var con = contract(null, description); + + // then + assert.equal('function', typeof con.test); + assert.equal('function', typeof con.test['uint256']); + }); + + it('should create contract with multiple methods', function () { + + // given + var description = [{ + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ], + }, { + "name": "test2", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + // when + var con = contract(null, description); + + // then + assert.equal('function', typeof con.test); + assert.equal('function', typeof con.test['uint256']); + assert.equal('function', typeof con.test2); + assert.equal('function', typeof con.test2['uint256']); + }); + + it('should create contract with overloaded methods', function () { + + // given + var description = [{ + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ], + }, { + "name": "test", + "type": "function", + "inputs": [{ + "name": "a", + "type": "string" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + // when + var con = contract(null, description); + + // then + assert.equal('function', typeof con.test); + assert.equal('function', typeof con.test['uint256']); + assert.equal('function', typeof con.test['string']); + }); + + it('should create contract with no methods', function () { + + // given + var description = [{ + "name": "test(uint256)", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + + // when + var con = contract(null, description); + + // then + assert.equal('undefined', typeof con.test); + + }); + + it('should create contract with one event', function () { + + // given + var description = [{ + "name": "test", + "type": "event", + "inputs": [{ + "name": "a", + "type": "uint256" + } + ], + "outputs": [ + { + "name": "d", + "type": "uint256" + } + ] + }]; + + + // when + var con = contract(null, description); + + // then + assert.equal('function', typeof con.test); + assert.equal('function', typeof con.test['uint256']); + + }); + +}); + diff --git a/eth.methods.js b/eth.methods.js index 002268565..7a031c4c8 100644 --- a/eth.methods.js +++ b/eth.methods.js @@ -24,7 +24,6 @@ describe('web3', function() { u.propertyExists(web3.eth, 'listening'); u.propertyExists(web3.eth, 'mining'); u.propertyExists(web3.eth, 'gasPrice'); - u.propertyExists(web3.eth, 'account'); u.propertyExists(web3.eth, 'accounts'); u.propertyExists(web3.eth, 'peerCount'); u.propertyExists(web3.eth, 'defaultBlock'); diff --git a/event.js b/event.js new file mode 100644 index 000000000..781f42e5e --- /dev/null +++ b/event.js @@ -0,0 +1,22 @@ +var assert = require('assert'); +var event = require('../lib/event.js'); + +describe('event', function () { + it('should create filter input object from given', function () { + + // given + var address = '0x012345'; + var signature = '0x987654'; + + // when + var impl = event(address, signature); + var result = impl(); + + // then + assert.equal(result.address, address); + assert.equal(result.topics.length, 1); + assert.equal(result.topics[0], signature); + + }); +}); + diff --git a/web3.methods.js b/web3.methods.js index 1b5792110..d08495dd9 100644 --- a/web3.methods.js +++ b/web3.methods.js @@ -6,8 +6,5 @@ describe('web3', function() { u.methodExists(web3, 'sha3'); u.methodExists(web3, 'toAscii'); u.methodExists(web3, 'fromAscii'); - u.methodExists(web3, 'toFixed'); - u.methodExists(web3, 'fromFixed'); - u.methodExists(web3, 'offset'); });