contract.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. This file is part of ethereum.js.
  3. ethereum.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethereum.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file contract.js
  15. * @authors:
  16. * Marek Kotewicz <marek@ethdev.com>
  17. * @date 2014
  18. */
  19. // TODO: work out which of the following two lines it is supposed to be...
  20. //if (process.env.NODE_ENV !== 'build') {
  21. if ("build" !== 'build') {/*
  22. var web3 = require('./web3'); // jshint ignore:line
  23. */}
  24. var abi = require('./abi');
  25. var contract = function (address, desc) {
  26. var inputParser = abi.inputParser(desc);
  27. var outputParser = abi.outputParser(desc);
  28. var contract = {};
  29. desc.forEach(function (method) {
  30. contract[method.name] = function () {
  31. var params = Array.prototype.slice.call(arguments);
  32. var parsed = inputParser[method.name].apply(null, params);
  33. var onSuccess = function (result) {
  34. return outputParser[method.name](result);
  35. };
  36. return {
  37. call: function (extra) {
  38. extra = extra || {};
  39. extra.to = address;
  40. extra.data = parsed;
  41. return web3.eth.call(extra).then(onSuccess);
  42. },
  43. transact: function (extra) {
  44. extra = extra || {};
  45. extra.to = address;
  46. extra.data = parsed;
  47. return web3.eth.transact(extra).then(onSuccess);
  48. }
  49. };
  50. };
  51. });
  52. return contract;
  53. };
  54. module.exports = contract;