diff options
author | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-22 21:37:34 +0800 |
---|---|---|
committer | Marek Kotewicz <marek.kotewicz@gmail.com> | 2015-01-22 21:37:34 +0800 |
commit | 0202b05a5dc328522697085f493ef06572997993 (patch) | |
tree | a35989ea27e39c82e34bd887428b29099744aa0c /lib | |
parent | 22c77c607ee1ef181bca56123a95f99b96753895 (diff) | |
download | dexon-0202b05a5dc328522697085f493ef06572997993.tar.gz dexon-0202b05a5dc328522697085f493ef06572997993.tar.zst dexon-0202b05a5dc328522697085f493ef06572997993.zip |
implicit solidity method call
Diffstat (limited to 'lib')
-rw-r--r-- | lib/contract.js | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/lib/contract.js b/lib/contract.js index aa4188d40..550ad8bd3 100644 --- a/lib/contract.js +++ b/lib/contract.js @@ -36,18 +36,20 @@ var abi = require('./abi'); * * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object * - * myContract.myMethod('this is test string param for call').call(); // myMethod call - * myContract.myMethod('this is test string param for transact').transact() // myMethod transact + * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default) + * myContract.myMethod('this is test string param for call').call(); // myMethod call (explicit) + * myContract.transact().myMethod('this is test string param for transact'); // myMethod transact * * @param address - address of the contract, which should be called * @param desc - abi json description of the contract, which is being created * @returns contract object */ -var contract = function (address, desc) { + +var contract = function contract (address, desc) { var inputParser = abi.inputParser(desc); var outputParser = abi.outputParser(desc); - var contract = {}; + var result = {}; desc.forEach(function (method) { @@ -56,43 +58,52 @@ var contract = function (address, desc) { var impl = function () { var params = Array.prototype.slice.call(arguments); - var parsed = inputParser[displayName][typeName].apply(null, params); var signature = abi.methodSignature(method.name); + var parsed = inputParser[displayName][typeName].apply(null, params); + + var options = contract._options || {}; + options.to = address; + options.data = signature + parsed; + + var output = ""; + if (contract._isTransact) { + // it's used byt natspec.js + // TODO: figure out better way to solve this + web3._currentContractAbi = desc; + web3._currentContractAddress = address; + + output = web3.eth.transact(options); + } else { + output = web3.eth.call(options); + } - return { - call: function (extra) { - extra = extra || {}; - extra.to = address; - extra.data = signature + parsed; - - var result = web3.eth.call(extra); - return outputParser[displayName][typeName](result); - }, - transact: function (extra) { - extra = extra || {}; - extra.to = address; - extra.data = signature + parsed; - - /// it's used by natspec.js - /// TODO: figure a better way to solve this - web3._currentContractAbi = desc; - web3._currentContractAddress = address; - - var result = web3.eth.transact(extra); - return outputParser[displayName][typeName](result); - } - }; + return outputParser[displayName][typeName](output); }; - if (contract[displayName] === undefined) { - contract[displayName] = impl; + if (result[displayName] === undefined) { + result[displayName] = impl; } - contract[displayName][typeName] = impl; + result[displayName][typeName] = impl; + }); + return result; +}; + +var transact = function (options) { + contract._isTransact = true; + contract._options = options; + return contract; +}; +var call = function (options) { + contract._isTransact = false; + contract._options = options; return contract; }; +contract.transact = transact; +contract.call = call; + module.exports = contract; |