diff options
author | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-11-15 00:20:05 +0800 |
---|---|---|
committer | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-11-15 00:20:05 +0800 |
commit | 42713c6e9995a131601186363ba2c49ffd093acc (patch) | |
tree | c02fbe94c1205ce270b36b6aa65de33af6178d54 /lib/main.js | |
parent | 7ffba22f2d3e246ded4ac8f6df3d9eab5137e838 (diff) | |
parent | ea8db7a4aecb034c6a967ccd3b17c50f423cb77c (diff) | |
download | go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar.gz go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar.bz2 go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar.lz go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar.xz go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.tar.zst go-tangerine-42713c6e9995a131601186363ba2c49ffd093acc.zip |
Merge pull request #12 from debris/master
simplifie contract creation && calls, improved added examples
Diffstat (limited to 'lib/main.js')
-rw-r--r-- | lib/main.js | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/lib/main.js b/lib/main.js index c3ed22f8b..7990691de 100644 --- a/lib/main.js +++ b/lib/main.js @@ -22,6 +22,8 @@ * @date 2014 */ +var abi = require('./abi'); + function flattenPromise (obj) { if (obj instanceof Promise) { return Promise.resolve(obj); @@ -77,6 +79,7 @@ var ethMethods = function () { var methods = [ { name: 'balanceAt', call: 'eth_balanceAt' }, { name: 'stateAt', call: 'eth_stateAt' }, + { name: 'storageAt', call: 'eth_storageAt' }, { name: 'countAt', call: 'eth_countAt'}, { name: 'codeAt', call: 'eth_codeAt' }, { name: 'transact', call: 'eth_transact' }, @@ -84,8 +87,9 @@ var ethMethods = function () { { name: 'block', call: blockCall }, { name: 'transaction', call: transactionCall }, { name: 'uncle', call: uncleCall }, - { name: 'compile', call: 'eth_compile' }, - { name: 'lll', call: 'eth_lll' } + { name: 'compilers', call: 'eth_compilers' }, + { name: 'lll', call: 'eth_lll' }, + { name: 'solidity', call: 'eth_solidity' } ]; return methods; }; @@ -290,9 +294,8 @@ var web3 = { } }; -var eth = web3.eth; -setupMethods(eth, ethMethods()); -setupProperties(eth, ethProperties()); +setupMethods(web3.eth, ethMethods()); +setupProperties(web3.eth, ethProperties()); setupMethods(web3.db, dbMethods()); setupMethods(web3.shh, shhMethods()); @@ -452,5 +455,40 @@ function messageHandler(data) { } } +web3.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 = web3; |