send.js 766 B

1234567891011121314151617181920212223242526
  1. const ethjsABI = require('ethjs-abi');
  2. const { ethSendTransaction } = require('./web3');
  3. function findMethod (abi, name, args) {
  4. for (let i = 0; i < abi.length; i++) {
  5. const methodArgs = abi[i].inputs.map(input => input.type).join(',');
  6. if ((abi[i].name === name) && (methodArgs === args)) {
  7. return abi[i];
  8. }
  9. }
  10. }
  11. async function transaction (target, name, argsTypes, argsValues, opts) {
  12. const abiMethod = findMethod(target.abi, name, argsTypes);
  13. const encodedData = ethjsABI.encodeMethod(abiMethod, argsValues);
  14. return target.sendTransaction(Object.assign({ data: encodedData }, opts));
  15. }
  16. function ether (from, to, value) {
  17. return ethSendTransaction({ from, to, value, gasPrice: 0 });
  18. }
  19. module.exports = {
  20. ether,
  21. transaction,
  22. };