javascript Web3 web3.eth.sendSignedTransaction无效参数

ars1skjm  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(236)

我对以太坊很陌生,我已经使用万神殿客户端建立了一个私有网络。我已经成功地将一个契约部署到网络上,当通过混音使用时,所有与契约的交互都能正常工作。
我试图建立一个中继,在那里一个事务被签署客户端,发送到一个nodeJs服务器,然后服务器代理该事务的合同。然而,当我传递签署的事务到sendSignedTransaction()我得到错误Invalid params,对我来说,这是非常模糊的,我不确定我做错了什么/无效的参数是什么。(任何建议如何调试这个?)

    • 更新**

使用web3 v1.2.0
错误

Error: Returned error: Invalid params
    at Object.ErrorResponse (/Users/ghost/node_modules/web3-core-helpers/src/errors.js:29:16)
    at Object.<anonymous> (/Users/ghost/node_modules/web3-core-requestmanager/src/index.js:140:36)
    at /Users/ghost/node_modules/web3-providers-ws/src/index.js:121:44
    at Array.forEach (<anonymous>)
    at W3CWebSocket.WebsocketProvider.connection.onmessage (/Users/ghost/node_modules/web3-providers-ws/src/index.js:98:36)
    at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/ghost/node_modules/yaeti/lib/EventTarget.js:107:17)
    at W3CWebSocket.onMessage (/Users/ghost/node_modules/websocket/lib/W3CWebSocket.js:234:14)
    at WebSocketConnection.<anonymous> (/Users/ghost/node_modules/websocket/lib/W3CWebSocket.js:205:19)
    at WebSocketConnection.emit (events.js:188:13)
    at WebSocketConnection.processFrame (/Users/ghost/node_modules/websocket/lib/WebSocketConnection.js:552:26)
    at /Users/ghost/node_modules/websocket/lib/WebSocketConnection.js:321:40
    at process.internalTickCallback (internal/process/next_tick.js:70:11)

合同

pragma solidity ^0.5.1;

import "./Ownable.sol";

contract Entry is Ownable { 
    mapping (address => string) hash;

    function addEntry(string memory _hash) public {
        hash[msg.sender] = _hash;
    }

    function getHash() public view returns(string memory){
        return hash[msg.sender];
    }

}

中继服务器

const Web3 = require('web3');
const express = require('express')
const app = express()
const port = 3003
const bodyParser = require('body-parser');
const cors = require('cors')

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json())
app.use(cors())

var web3 = new Web3(Web3.givenProvider || "ws://localhost:7002");

app.post('/transaction/send', async (req, res) => {
    const {tx, data} = req.body;

    web3.eth.sendSignedTransaction(tx, function (err, transactionHash) {
      if(err) console.log(err);
      console.log(transactionHash);
    });
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

前端

import React from 'react';
import './App.css';
import Web3 from 'web3';
import request from 'request-promise';

const Tx = require('ethereumjs-tx').Transaction;
const web3 = new Web3("http://localhost:8545");
const privKey = '[My Priv key here]';
const contractADDRESS = "0x4261d524bc701da4ac49339e5f8b299977045ea5";
const addressFrom = '0x627306090abaB3A6e1400e9345bC60c78a8BEf57';
const contractABI = [{"constant":false,"inputs":[{"name":"_hash","type":"string"}],"name":"addEntry","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getHash","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"}];

function App() {

  async function sendTx(){ 
    const data = await extraData();
    web3.eth.getTransactionCount(addressFrom).then(txCount => {
      const txData = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(25000),
        gasPrice: web3.utils.toHex(10e9),
        to: contractADDRESS,
        from: addressFrom,
        data: data
      }

      sendSigned(txData, function(err, result) {
        if (err) return console.log('error', err)
        console.log('sent', result)
      })

    })
  }

  async function sendSigned(txData, cb) {
    const privateKey = new Buffer(privKey, 'hex')
    const transaction = new Tx(txData)
    transaction.sign(privateKey)
    const serializedTx = transaction.serialize().toString('hex')
    const response = request({
      method: 'POST',
      uri: 'http://127.0.0.1:3003/transaction/send',

      body: {
          tx: serializedTx,
          data: 'somehashhh'
      },
      json: true,
    });
  }

  async function extraData() {
    const contractInstance = new web3.eth.Contract(contractABI, contractADDRESS);
    return await contractInstance.methods.addEntry('somehashhh').encodeABI();
  }

  return (
    <div className="App">
      <header className="App-header">
        <div onClick={() => sendTx()}>Submit transaction</div>
      </header>
    </div>
  );
}

export default App;

这是从前端发送的txData

{
data: "0x17ce42bd0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a736f6d6568617368686800000000000000000000000000000000000000000000"
from: "0x627306090abaB3A6e1400e9345bC60c78a8BEf57"
gasLimit: "0x61a8"
gasPrice: "0x2540be400"
nonce: "0x0"
to: "0x4261d524bc701da4ac49339e5f8b299977045ea5"
}
zzlelutf

zzlelutf1#

经过大量的尝试和错误的堆栈溢出工作的建议,我已经得到了事务签名的工作!.最后,我离开了使用ethereumjs-tx(出于某种原因,这是许多人推荐),并使用纯粹的Web 3。

前端客户端

async function sendTx(){ 
    const { address: from } = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY)
    const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS)
    const query = await contract.methods.updateCount();

    const signed = await web3.eth.accounts.signTransaction({
        to: CONTRACT_ADDRESS,
        from,
        value: '0',
        data: query.encodeABI(),
        gasPrice: web3.utils.toWei('20', 'gwei'),
        gas: Math.round((await query.estimateGas({ from })) * 1.5),
        nonce: await web3.eth.getTransactionCount(from, 'pending')
    }, PRIVATE_KEY)

    const response = await request({
      method: 'POST', json: true, 
      uri: 'http://127.0.0.1:3003/transaction/send', 
      body: {
        tx: signed.rawTransaction,
        data: 'some data'
      }
    });

    console.log(response);
  }

中继服务器

const Web3 = require('web3');
const express = require('express')
const app = express()
const port = 3003
const bodyParser = require('body-parser');
const cors = require('cors')

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json())
app.use(cors())

var web3 = new Web3(Web3.givenProvider || "ws://localhost:7002");

app.post('/transaction/send', async (req, res) => {
    const {tx, data} = req.body;

    web3.eth.sendSignedTransaction(tx)
    .on('transactionHash', (txHash) => res.json({txHash}))
    .on('error', console.log)
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

希望这能帮助到其他人👍

相关问题