diff --git a/gulpfile.js b/gulpfile.js index d8a03dc..913f09f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -62,8 +62,12 @@ var packages = [{ src: './packages/web3-bzz/src/index.js' },{ fileName: 'web3-eth-iban', - expose: 'Iban', + expose: 'EthIban', src: './packages/web3-eth-iban/src/index.js' +},{ + fileName: 'web3-eth-abi', + expose: 'EthAbi', + src: './packages/web3-eth-abi/src/index.js' }]; var browserifyOptions = { diff --git a/packages/web3-eth-abi/src/index.js b/packages/web3-eth-abi/src/index.js index b00c2d9..c3626ea 100644 --- a/packages/web3-eth-abi/src/index.js +++ b/packages/web3-eth-abi/src/index.js @@ -1,38 +1,259 @@ /* - This file is part of web3.js. + This file is part of web3.js. - web3.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. + web3.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. - web3.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. + web3.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 web3.js. If not, see . -*/ + You should have received a copy of the GNU Lesser General Public License + along with web3.js. If not, see . + */ /** - * @file contract.js - * - * To initialize a contract use: - * - * var Contract = require('web3-eth-contract'); - * Contract.prototype._eth = needsAEthInstance; - * var contract = new Contract(abi, address, ...); - * - * @author Fabian Vogelsteller - * @date 2016 + * @file coder.js + * @author Marek Kotewicz + * @date 2015 */ +var f = require('./types/formatters'); -"use strict"; +var SolidityTypeAddress = require('./types/address'); +var SolidityTypeBool = require('./types/bool'); +var SolidityTypeInt = require('./types/int'); +var SolidityTypeUInt = require('./types/uint'); +var SolidityTypeDynamicBytes = require('./types/dynamicbytes'); +var SolidityTypeString = require('./types/string'); +var SolidityTypeBytes = require('./types/bytes'); -var solidity = require('./solidity/coder'); - -module.exports = { - solidity: solidity +var isDynamic = function (solidityType, type) { + return solidityType.isDynamicType(type) || + solidityType.isDynamicArray(type); }; +/** + * SolidityCoder prototype should be used to encode/decode solidity params of any type + */ +var SolidityCoder = function (types) { + this._types = types; +}; + +/** + * This method should be used to transform type to SolidityType + * + * @method _requireType + * @param {String} type + * @returns {SolidityType} + * @throws {Error} throws if no matching type is found + */ +SolidityCoder.prototype._requireType = function (type) { + var solidityType = this._types.filter(function (t) { + return t.isType(type); + })[0]; + + if (!solidityType) { + throw Error('invalid solidity type!: ' + type); + } + + return solidityType; +}; + +/** + * Should be used to encode plain param + * + * @method encodeParam + * @param {String} type + * @param {Object} plain param + * @return {String} encoded plain param + */ +SolidityCoder.prototype.encodeParam = function (type, param) { + return this.encodeParams([type], [param]); +}; + +/** + * Should be used to encode list of params + * + * @method encodeParams + * @param {Array} types + * @param {Array} params + * @return {String} encoded list of params + */ +SolidityCoder.prototype.encodeParams = function (types, params) { + var solidityTypes = this.getSolidityTypes(types); + + var encodeds = solidityTypes.map(function (solidityType, index) { + return solidityType.encode(params[index], types[index]); + }); + + var dynamicOffset = solidityTypes.reduce(function (acc, solidityType, index) { + var staticPartLength = solidityType.staticPartLength(types[index]); + var roundedStaticPartLength = Math.floor((staticPartLength + 31) / 32) * 32; + + return acc + (isDynamic(solidityTypes[index], types[index]) ? + 32 : + roundedStaticPartLength); + }, 0); + + var result = this.encodeMultiWithOffset(types, solidityTypes, encodeds, dynamicOffset); + + return result; +}; + +SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) { + var result = ""; + var self = this; + + types.forEach(function (type, i) { + if (isDynamic(solidityTypes[i], types[i])) { + result += f.formatInputInt(dynamicOffset).encode(); + var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); + dynamicOffset += e.length / 2; + } else { + // don't add length to dynamicOffset. it's already counted + result += self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); + } + + // TODO: figure out nested arrays + }); + + types.forEach(function (type, i) { + if (isDynamic(solidityTypes[i], types[i])) { + var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); + dynamicOffset += e.length / 2; + result += e; + } + }); + return result; +}; + +// TODO: refactor whole encoding! +SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) { + var self = this; + if (solidityType.isDynamicArray(type)) { + return (function () { + // offset was already set + var nestedName = solidityType.nestedName(type); + var nestedStaticPartLength = solidityType.staticPartLength(nestedName); + var result = encoded[0]; + + (function () { + var previousLength = 2; // in int + if (solidityType.isDynamicArray(nestedName)) { + for (var i = 1; i < encoded.length; i++) { + previousLength += +(encoded[i - 1])[0] || 0; + result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); + } + } + })(); + + // first element is length, skip it + (function () { + for (var i = 0; i < encoded.length - 1; i++) { + var additionalOffset = result / 2; + result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset); + } + })(); + + return result; + })(); + + } else if (solidityType.isStaticArray(type)) { + return (function () { + var nestedName = solidityType.nestedName(type); + var nestedStaticPartLength = solidityType.staticPartLength(nestedName); + var result = ""; + + + if (solidityType.isDynamicArray(nestedName)) { + (function () { + var previousLength = 0; // in int + for (var i = 0; i < encoded.length; i++) { + // calculate length of previous item + previousLength += +(encoded[i - 1] || [])[0] || 0; + result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); + } + })(); + } + + (function () { + for (var i = 0; i < encoded.length; i++) { + var additionalOffset = result / 2; + result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset); + } + })(); + + return result; + })(); + } + + return encoded; +}; + +/** + * Should be used to decode bytes to plain param + * + * @method decodeParam + * @param {String} type + * @param {String} bytes + * @return {Object} plain param + */ +SolidityCoder.prototype.decodeParam = function (type, bytes) { + return this.decodeParams([type], bytes)[0]; +}; + +/** + * Should be used to decode list of params + * + * @method decodeParam + * @param {Array} types + * @param {String} bytes + * @return {Array} array of plain params + */ +SolidityCoder.prototype.decodeParams = function (types, bytes) { + var solidityTypes = this.getSolidityTypes(types); + var offsets = this.getOffsets(types, solidityTypes); + + return solidityTypes.map(function (solidityType, index) { + return solidityType.decode(bytes, offsets[index], types[index], index); + }); +}; + +SolidityCoder.prototype.getOffsets = function (types, solidityTypes) { + var lengths = solidityTypes.map(function (solidityType, index) { + return solidityType.staticPartLength(types[index]); + }); + + for (var i = 1; i < lengths.length; i++) { + // sum with length of previous element + lengths[i] += lengths[i - 1]; + } + + return lengths.map(function (length, index) { + // remove the current length, so the length is sum of previous elements + var staticPartLength = solidityTypes[index].staticPartLength(types[index]); + return length - staticPartLength; + }); +}; + +SolidityCoder.prototype.getSolidityTypes = function (types) { + var self = this; + return types.map(function (type) { + return self._requireType(type); + }); +}; + +var coder = new SolidityCoder([ + new SolidityTypeAddress(), + new SolidityTypeBool(), + new SolidityTypeInt(), + new SolidityTypeUInt(), + new SolidityTypeDynamicBytes(), + new SolidityTypeBytes(), + new SolidityTypeString() +]); + +module.exports = coder; diff --git a/packages/web3-eth-abi/src/solidity/coder.js b/packages/web3-eth-abi/src/solidity/coder.js deleted file mode 100644 index 3920a97..0000000 --- a/packages/web3-eth-abi/src/solidity/coder.js +++ /dev/null @@ -1,259 +0,0 @@ -/* - This file is part of web3.js. - - web3.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. - - web3.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 web3.js. If not, see . -*/ -/** - * @file coder.js - * @author Marek Kotewicz - * @date 2015 - */ - -var f = require('./formatters'); - -var SolidityTypeAddress = require('./address'); -var SolidityTypeBool = require('./bool'); -var SolidityTypeInt = require('./int'); -var SolidityTypeUInt = require('./uint'); -var SolidityTypeDynamicBytes = require('./dynamicbytes'); -var SolidityTypeString = require('./string'); -var SolidityTypeBytes = require('./bytes'); - -var isDynamic = function (solidityType, type) { - return solidityType.isDynamicType(type) || - solidityType.isDynamicArray(type); -}; - -/** - * SolidityCoder prototype should be used to encode/decode solidity params of any type - */ -var SolidityCoder = function (types) { - this._types = types; -}; - -/** - * This method should be used to transform type to SolidityType - * - * @method _requireType - * @param {String} type - * @returns {SolidityType} - * @throws {Error} throws if no matching type is found - */ -SolidityCoder.prototype._requireType = function (type) { - var solidityType = this._types.filter(function (t) { - return t.isType(type); - })[0]; - - if (!solidityType) { - throw Error('invalid solidity type!: ' + type); - } - - return solidityType; -}; - -/** - * Should be used to encode plain param - * - * @method encodeParam - * @param {String} type - * @param {Object} plain param - * @return {String} encoded plain param - */ -SolidityCoder.prototype.encodeParam = function (type, param) { - return this.encodeParams([type], [param]); -}; - -/** - * Should be used to encode list of params - * - * @method encodeParams - * @param {Array} types - * @param {Array} params - * @return {String} encoded list of params - */ -SolidityCoder.prototype.encodeParams = function (types, params) { - var solidityTypes = this.getSolidityTypes(types); - - var encodeds = solidityTypes.map(function (solidityType, index) { - return solidityType.encode(params[index], types[index]); - }); - - var dynamicOffset = solidityTypes.reduce(function (acc, solidityType, index) { - var staticPartLength = solidityType.staticPartLength(types[index]); - var roundedStaticPartLength = Math.floor((staticPartLength + 31) / 32) * 32; - - return acc + (isDynamic(solidityTypes[index], types[index]) ? - 32 : - roundedStaticPartLength); - }, 0); - - var result = this.encodeMultiWithOffset(types, solidityTypes, encodeds, dynamicOffset); - - return result; -}; - -SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) { - var result = ""; - var self = this; - - types.forEach(function (type, i) { - if (isDynamic(solidityTypes[i], types[i])) { - result += f.formatInputInt(dynamicOffset).encode(); - var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); - dynamicOffset += e.length / 2; - } else { - // don't add length to dynamicOffset. it's already counted - result += self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); - } - - // TODO: figure out nested arrays - }); - - types.forEach(function (type, i) { - if (isDynamic(solidityTypes[i], types[i])) { - var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset); - dynamicOffset += e.length / 2; - result += e; - } - }); - return result; -}; - -// TODO: refactor whole encoding! -SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) { - var self = this; - if (solidityType.isDynamicArray(type)) { - return (function () { - // offset was already set - var nestedName = solidityType.nestedName(type); - var nestedStaticPartLength = solidityType.staticPartLength(nestedName); - var result = encoded[0]; - - (function () { - var previousLength = 2; // in int - if (solidityType.isDynamicArray(nestedName)) { - for (var i = 1; i < encoded.length; i++) { - previousLength += +(encoded[i - 1])[0] || 0; - result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); - } - } - })(); - - // first element is length, skip it - (function () { - for (var i = 0; i < encoded.length - 1; i++) { - var additionalOffset = result / 2; - result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset); - } - })(); - - return result; - })(); - - } else if (solidityType.isStaticArray(type)) { - return (function () { - var nestedName = solidityType.nestedName(type); - var nestedStaticPartLength = solidityType.staticPartLength(nestedName); - var result = ""; - - - if (solidityType.isDynamicArray(nestedName)) { - (function () { - var previousLength = 0; // in int - for (var i = 0; i < encoded.length; i++) { - // calculate length of previous item - previousLength += +(encoded[i - 1] || [])[0] || 0; - result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); - } - })(); - } - - (function () { - for (var i = 0; i < encoded.length; i++) { - var additionalOffset = result / 2; - result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset); - } - })(); - - return result; - })(); - } - - return encoded; -}; - -/** - * Should be used to decode bytes to plain param - * - * @method decodeParam - * @param {String} type - * @param {String} bytes - * @return {Object} plain param - */ -SolidityCoder.prototype.decodeParam = function (type, bytes) { - return this.decodeParams([type], bytes)[0]; -}; - -/** - * Should be used to decode list of params - * - * @method decodeParam - * @param {Array} types - * @param {String} bytes - * @return {Array} array of plain params - */ -SolidityCoder.prototype.decodeParams = function (types, bytes) { - var solidityTypes = this.getSolidityTypes(types); - var offsets = this.getOffsets(types, solidityTypes); - - return solidityTypes.map(function (solidityType, index) { - return solidityType.decode(bytes, offsets[index], types[index], index); - }); -}; - -SolidityCoder.prototype.getOffsets = function (types, solidityTypes) { - var lengths = solidityTypes.map(function (solidityType, index) { - return solidityType.staticPartLength(types[index]); - }); - - for (var i = 1; i < lengths.length; i++) { - // sum with length of previous element - lengths[i] += lengths[i - 1]; - } - - return lengths.map(function (length, index) { - // remove the current length, so the length is sum of previous elements - var staticPartLength = solidityTypes[index].staticPartLength(types[index]); - return length - staticPartLength; - }); -}; - -SolidityCoder.prototype.getSolidityTypes = function (types) { - var self = this; - return types.map(function (type) { - return self._requireType(type); - }); -}; - -var coder = new SolidityCoder([ - new SolidityTypeAddress(), - new SolidityTypeBool(), - new SolidityTypeInt(), - new SolidityTypeUInt(), - new SolidityTypeDynamicBytes(), - new SolidityTypeBytes(), - new SolidityTypeString() -]); - -module.exports = coder; diff --git a/packages/web3-eth-abi/src/solidity/address.js b/packages/web3-eth-abi/src/types/address.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/address.js rename to packages/web3-eth-abi/src/types/address.js diff --git a/packages/web3-eth-abi/src/solidity/bool.js b/packages/web3-eth-abi/src/types/bool.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/bool.js rename to packages/web3-eth-abi/src/types/bool.js diff --git a/packages/web3-eth-abi/src/solidity/bytes.js b/packages/web3-eth-abi/src/types/bytes.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/bytes.js rename to packages/web3-eth-abi/src/types/bytes.js diff --git a/packages/web3-eth-abi/src/solidity/dynamicbytes.js b/packages/web3-eth-abi/src/types/dynamicbytes.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/dynamicbytes.js rename to packages/web3-eth-abi/src/types/dynamicbytes.js diff --git a/packages/web3-eth-abi/src/solidity/formatters.js b/packages/web3-eth-abi/src/types/formatters.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/formatters.js rename to packages/web3-eth-abi/src/types/formatters.js diff --git a/packages/web3-eth-abi/src/solidity/int.js b/packages/web3-eth-abi/src/types/int.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/int.js rename to packages/web3-eth-abi/src/types/int.js diff --git a/packages/web3-eth-abi/src/solidity/param.js b/packages/web3-eth-abi/src/types/param.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/param.js rename to packages/web3-eth-abi/src/types/param.js diff --git a/packages/web3-eth-abi/src/solidity/string.js b/packages/web3-eth-abi/src/types/string.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/string.js rename to packages/web3-eth-abi/src/types/string.js diff --git a/packages/web3-eth-abi/src/solidity/type.js b/packages/web3-eth-abi/src/types/type.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/type.js rename to packages/web3-eth-abi/src/types/type.js diff --git a/packages/web3-eth-abi/src/solidity/uint.js b/packages/web3-eth-abi/src/types/uint.js similarity index 100% rename from packages/web3-eth-abi/src/solidity/uint.js rename to packages/web3-eth-abi/src/types/uint.js diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index 7dacbde..5673fe2 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -37,7 +37,7 @@ var utils = require('web3-utils'); var Subscription = require('web3-core-subscriptions').subscription; var formatters = require('web3-core-helpers').formatters; var promiEvent = require('web3-core-promiEvent'); -var coder = require('web3-eth-abi').solidity; +var coder = require('web3-eth-abi'); /** diff --git a/packages/web3-eth/package.json b/packages/web3-eth/package.json index 9b83258..5c6a4fd 100644 --- a/packages/web3-eth/package.json +++ b/packages/web3-eth/package.json @@ -11,6 +11,7 @@ "web3-core-subscriptions": "^1.0.0", "web3-core-method": "^1.0.0", "web3-eth-iban": "^1.0.0", + "web3-eth-abi": "^1.0.0", "web3-eth-contract": "^1.0.0", "web3-utils": "^1.0.0", "underscore": "^1.8.3" diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 3c0facb..561aea3 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -28,6 +28,7 @@ var helpers = require('web3-core-helpers'); var Subscriptions = require('web3-core-subscriptions').subscriptions; var utils = require('web3-utils'); var Method = require('web3-core-method'); +var abi = require('web3-eth-abi'); var Contract = require('web3-eth-contract'); var Iban = require('web3-eth-iban'); @@ -77,6 +78,9 @@ var Eth = function Eth() { // add IBAN this.Iban = Iban; + // add ABI + this.abi = abi; + // add guess chain this.net.getNetworkType = getNetworkType.bind(this); diff --git a/src/index.js b/src/index.js index c55ea69..780383c 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,12 @@ var Bzz = require('../packages/web3-bzz'); var utils = require('../packages/web3-utils'); +// providers +var providers = { + WebsocketProvider: require('../packages/web3-providers-ws'), + HttpProvider: require('../packages/web3-providers-http'), + IpcProvider: require('../packages/web3-providers-ipc') +} var Web3 = function Web3() { @@ -45,6 +51,9 @@ var Web3 = function Web3() { core.packageInit(this, arguments); + this.providers = providers; + + this.eth = new Eth(this); this.personal = new Personal(this); // move to -> web3.eth.accounts this.shh = new Shh(this); @@ -63,6 +72,7 @@ var Web3 = function Web3() { }; +Web3.providers = providers; core.addProviders(Web3); diff --git a/test/coder.decodeParam-ethjs.js_ b/test/abi.decodeParam-ethjs.js_ similarity index 100% rename from test/coder.decodeParam-ethjs.js_ rename to test/abi.decodeParam-ethjs.js_ diff --git a/test/coder.decodeParam.js b/test/abi.decodeParam.js similarity index 99% rename from test/coder.decodeParam.js rename to test/abi.decodeParam.js index bacce23..f064454 100644 --- a/test/coder.decodeParam.js +++ b/test/abi.decodeParam.js @@ -1,6 +1,6 @@ var chai = require('chai'); var assert = chai.assert; -var coder = require('../packages/web3-eth-abi').solidity; +var coder = require('../packages/web3-eth-abi'); // TODO check line 108 again! diff --git a/test/coder.encodeParam-ethjs.js_ b/test/abi.encodeParam-ethjs.js_ similarity index 100% rename from test/coder.encodeParam-ethjs.js_ rename to test/abi.encodeParam-ethjs.js_ diff --git a/test/coder.encodeParam.js b/test/abi.encodeParam.js similarity index 99% rename from test/coder.encodeParam.js rename to test/abi.encodeParam.js index cda3fbc..90ec399 100644 --- a/test/coder.encodeParam.js +++ b/test/abi.encodeParam.js @@ -1,6 +1,6 @@ var chai = require('chai'); var assert = chai.assert; -var coder = require('../packages/web3-eth-abi').solidity; +var coder = require('../packages/web3-eth-abi'); describe('lib/solidity/coder', function () { diff --git a/test/abi.formatters.formatInputInt.js b/test/abi.formatters.formatInputInt.js new file mode 100644 index 0000000..e81f10a --- /dev/null +++ b/test/abi.formatters.formatInputInt.js @@ -0,0 +1,21 @@ +var chai = require('chai'); +var assert = chai.assert; +var formatters = require('../packages/web3-eth-abi/src/types/formatters.js'); +var Param = require('../packages/web3-eth-abi/src/types/param'); + +var tests = [ + { input: 1, result: new Param('0000000000000000000000000000000000000000000000000000000000000001') }, + { input: 1.1, result: new Param('0000000000000000000000000000000000000000000000000000000000000001') }, + { input: -1.1, result: new Param('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') }, + { input: -1, result: new Param('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') } +]; + +describe('formatters', function () { + describe('formatInputInt', function () { + tests.forEach(function(test){ + it('should return the correct value', function () { + assert.deepEqual(formatters.formatInputInt(test.input), test.result); + }); + }); + }); +}); diff --git a/test/soldity.formatters.formatInputInt.js b/test/soldity.formatters.formatInputInt.js deleted file mode 100644 index fd57a1b..0000000 --- a/test/soldity.formatters.formatInputInt.js +++ /dev/null @@ -1,21 +0,0 @@ -var chai = require('chai'); -var assert = chai.assert; -var formatters = require('../packages/web3-eth-abi/src/solidity/formatters.js'); -var SolidityParam = require('../packages/web3-eth-abi/src/solidity/param'); - -var tests = [ - { input: 1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') }, - { input: 1.1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') }, - { input: -1.1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') }, - { input: -1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') } -]; - -describe('formatters', function () { - describe('formatInputInt', function () { - tests.forEach(function(test){ - it('should return the correct value', function () { - assert.deepEqual(formatters.formatInputInt(test.input), test.result); - }); - }); - }); -});