简体   繁体   English

部署智能合约Web3无效的RPC响应

[英]Deploying smart contract web3 Invalid RPC response

I'm trying to deploy a contract on my node server as follows: 我正在尝试在节点服务器上部署合同,如下所示:

var escrow = 'pragma solidity ^0.4.4;contract Escrow {address public challenger;address public participant;address public arbiter;function Escrow(address _participant, address _arbiter) {challenger = msg.sender;participant = _participant;arbiter = _arbiter;}function payoutToParticipant() {if(msg.sender == challenger || msg.sender == arbiter) {participant.send(this.balance);}}function refundToChallenger() {if(msg.sender == challenger || msg.sender == arbiter) {challenger.send(this.balance);}}function getBalance() constant returns (uint) {return this.balance;}}';

module.exports.createEscrowContract = function(req, res) {
    //Set variables
    var challenger = '0xE6...';
    var participant = '0x4E...';
    var arbiter = '0xe5C...';

    //Compile contract
    var compiled = solc.compile(escrow);

    console.log(compiled);
    const web3 = new Web3();
    web3.setProvider(new 
    web3.providers.HttpProvider("https://mainnet.infura.io/XXXX"));

    //Store bytecode
    var bytecode = compiled.contracts[':Escrow'].bytecode;

    //Store abi
    var abi = JSON.parse(compiled.contracts[':Escrow'].interface);

    var escrowContract = web3.eth.contract(abi);

    //Seems to compile fine.
    console.log(escrowContract);

    //Deploy contract
    var deployed = escrowContract.new(participant, arbiter, {
      from: challenger,
      data: bytecode,
      gas: 470000,
      }, (error, contract) => {

           console.log(error); 
           // Invalid JSON RPC response: ""
           console.log(contract);
           //undefined
      });


}

I've tried several things and read in the documentation but can't seem to find anything! 我尝试了几件事并阅读了文档,但似乎找不到任何东西! I'm sure I can't be the only person with this issue, I'm using web3@0.20. 我确定我不是唯一一个遇到此问题的人,我使用的是web3@0.20。

If anyone can help I'd really appreciate it. 如果有人可以提供帮助,我将非常感激。

Two issues I can spot: 我可以发现两个问题:

  1. You're passing 47,000,000 gas, which is absurdly high. 您正在传递47,000,000的天然气,这是荒谬的。 To put it in context, an entire block can only use 8,000,000 gas. 就上下文而言,整个区块只能使用8,000,000瓦斯。 If you actually used 47 million gas at current gas prices, it would cost over $200 USD. 如果您以当前的天然气价格实际使用了4,700万美元的天然气,那将花费超过200美元。 I believe that this would cause your transaction to be rejected before any node attempted to run it. 我相信这会导致您的交易在任何节点尝试运行之前被拒绝。
  2. Infura , as a public node, can't ever have your private key (in this case, the challenger 's private key), so it can't sign a transaction for you. Infura作为公共节点,永远无法拥有您的私钥(在本例中为challenger的私钥),因此它无法为您签署交易。 If challenger is your account, then you can hold the key and sign the deployment transaction before broadcasting it. 如果challenger是您的帐户,则可以在广播之前按住密钥并签署部署事务。 If challenger is a user of your app, you're better off having the user sign and send the transaction themselves via something like MetaMask . 如果challenger是您应用程序的用户,那么最好让用户签名并通过MetaMask之类的东西自己发送交易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM