Web3js

無法連接到 testrpc

  • September 14, 2017

我有一個在虛擬機(Ubuntu 16.04)上執行的 testrpc,當我使用 truffle 控制台與它互動時它可以正常工作。

我唯一的目標是呼叫一個智能合約,簡單地將乙太幣從一個賬戶發送到另一個賬戶。這是智能合約:

pragma solidity ^0.4.4;

contract Transfer {
   address owner;

   function Transfer () {
       owner = msg.sender;
   }

   function sendEther (address dst) payable {
       if(msg.value<=0) revert();
       dst.transfer(msg.value);
   }
}

我正在嘗試使用 javascript 從網路訪問函式 sendEther。當我在本地訪問我的測試網時,一切正常,但現在我無法連接到它。

該腳本使用 browserify 進行轉換,然後作為簡單的腳本標記包含在 htm 文件中。

腳本是這樣的:

var Web3 = require('web3');
let web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

function getParameterByName(name, url) {
   /* irrelevant code here */
}

try
{
   // I have to define "source" this way because web3.eth.accounts[0] is undefined, most likely because there's no connection :(
   var source = "0x8691202f56859bd5362f1033fa64e71cb4eae579"; 
   var abi = [{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}];
   var contract_addr = getParameterByName("contract");
   var amount = getParameterByName("amount");
   var destination = getParameterByName("destination");
   var contract = new web3.eth.Contract(abi, contract_addr, {from: source, to: destination, value: parseInt(amount)});
   var hash = contract.methods.sendEther(destinatario);


   document.write("<br/> Source: ");
   document.write(source);
   document.write("<br/> Destination: ");
   document.write(destination);
   document.write("<br/><br/><b>ABI:</b> ");
   document.write(JSON.stringify(abi));
   document.write("<br/><br/><b>Contract:</b> ");
   document.write(JSON.stringify(contract));
   document.write("<br/><br/><b>Contract address:</b> ");
   document.write(contract_addr);
   document.write("<br/><br/><b>Amount (weis):</b> ");
   document.write(amount);
   document.write("<br/><br/><b>Transaction hash:</b> ");
   document.write(JSON.stringify(hash));
   document.write("<br/><br/><b>Transaction details:</b> ");
   document.write(JSON.stringify(web3.eth.getTransaction(hash)));
} catch (e) {
   document.write("<br/></br><b>Error: ");
   document.write(e.message);
   document.write("</b>");
}

這是我得到的輸出:

Source: 0x8691202f56859bd5362f1033fa64e71cb4eae579

Destination: 0x09b74dc58118140deec480d562651e42ae3b21ad

ABI: [{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]

Contract: {"currentProvider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_requestManager":{"provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"providers":{},"subscriptions":{}},"givenProvider":null,"providers":{},"_provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_ethereumCall":{},"options":{"from":"0x8691202F56859BD5362f1033FA64E71cb4EaE579","to":"0x09b74dc58118140deec480d562651e42ae3b21ad","value":1000000000000,"address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]},"defaultAccount":null,"defaultBlock":"latest","methods":{},"events":{},"_address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","_jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]}

Contract address: 0x377d5848c8a7b68d20cce114f23ad959a62cc9f7

Amount (weis): 1000000000000

Transaction hash: {"arguments":["0x09b74dc58118140deec480d562651e42ae3b21ad"],"_method":{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},"_parent":{"currentProvider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_requestManager":{"provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"providers":{},"subscriptions":{}},"givenProvider":null,"providers":{},"_provider":{"host":"http://localhost:8545","timeout":0,"connected":false},"_ethereumCall":{},"options":{"from":"0x8691202F56859BD5362f1033FA64E71cb4EaE579","to":"0x09b74dc58118140deec480d562651e42ae3b21ad","value":1000000000000,"address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]},"defaultAccount":null,"defaultBlock":"latest","methods":{},"events":{},"_address":"0x377D5848c8A7B68D20CcE114f23AD959a62CC9f7","_jsonInterface":[{"constant":false,"inputs":[{"name":"dst","type":"address"}],"name":"sendEther","outputs":[],"payable":true,"type":"function","signature":"0x48c981e2"},{"inputs":[],"payable":false,"type":"constructor"}]}}

Transaction details: {"isFulfilled":false,"isRejected":false} 

這就是問題所在……當我與 truffle 控制台互動時,一切正常。當我嘗試通過網路執行相同操作時,它無法連接。

關於這裡發生了什麼的任何想法?

非常感謝您!:)

好的,我找到了答案。我會把它留在這裡給任何可能覺得有用的人……

我不知道truffle serve,當我嘗試使用它時,它引發了一個錯誤。

可以按照此處指示的說明解決該錯誤:https ://github.com/trufflesuite/truffle/issues/448

我終於可以從瀏覽器看到我的 testrpc 了!:)

引用自:https://ethereum.stackexchange.com/questions/25399