diff options
author | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-16 18:10:28 +0800 |
---|---|---|
committer | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-16 18:10:28 +0800 |
commit | e9db2d1b1877bdf49cb76de46849a1426ded288c (patch) | |
tree | fcad7bcf7df6fdb72e69d8282d07fa570d9b399b /lib/contract.js | |
parent | f6ee8e52dd50ba4e7c2ff2de0ca3bdf2aba9ce3a (diff) | |
parent | b402484e41c9579f33d70f8f8b191ab0074dad62 (diff) | |
download | dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar.gz dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar.bz2 dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar.lz dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar.xz dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.tar.zst dexon-e9db2d1b1877bdf49cb76de46849a1426ded288c.zip |
Merge pull request #15 from debris/master
changes from cpp-ethereum
Diffstat (limited to 'lib/contract.js')
-rw-r--r-- | lib/contract.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/contract.js b/lib/contract.js new file mode 100644 index 000000000..10ceaf869 --- /dev/null +++ b/lib/contract.js @@ -0,0 +1,63 @@ +/* + This file is part of ethereum.js. + + ethereum.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. + + ethereum.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 ethereum.js. If not, see <http://www.gnu.org/licenses/>. +*/ +/** @file contract.js + * @authors: + * Marek Kotewicz <marek@ethdev.com> + * @date 2014 + */ + +if (process.env.NODE_ENV !== 'build') { + var web3 = require('./web3'); // jshint ignore:line +} +var abi = require('./abi'); + +var contract = function (address, desc) { + var inputParser = abi.inputParser(desc); + var outputParser = abi.outputParser(desc); + + var contract = {}; + + desc.forEach(function (method) { + contract[method.name] = function () { + var params = Array.prototype.slice.call(arguments); + var parsed = inputParser[method.name].apply(null, params); + + var onSuccess = function (result) { + return outputParser[method.name](result); + }; + + return { + call: function (extra) { + extra = extra || {}; + extra.to = address; + extra.data = parsed; + return web3.eth.call(extra).then(onSuccess); + }, + transact: function (extra) { + extra = extra || {}; + extra.to = address; + extra.data = parsed; + return web3.eth.transact(extra).then(onSuccess); + } + }; + }; + }); + + return contract; +}; + +module.exports = contract; |