sendTransaction.js 774 B

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