1

我正在尝试让我的第一个 dapp 工作;我知道我已经接近了,但一直遇到 web3 的问题。

我在Windows 10上工作,通过PowerShell运行testrpc节点。我使用truffle来设置我的文件夹和示例文件,然后编译和迁移。

我认为我没有更改由 truffle 构建的app.js文件的任何内容......这是该代码:

var accounts;
var account;

function setStatus(message) {
  var status = document.getElementById("status");
  status.innerHTML = message;
};

function refreshBalance() {
  var meta = MetaCoin.deployed();

  meta.getBalance.call(account, {from: account}).then(function(value) {
    var balance_element = document.getElementById("balance");
    balance_element.innerHTML = value.valueOf();
  }).catch(function(e) {
    console.log(e);
    setStatus("Error getting balance; see log.");
  });
};

function calcPremium() {
    var premium = parseInt(document.getElementById("benefit").value)/10000;
    document.getElementById("monthlyPremium").innerHTML = "    Monthly Premium: $"+premium.toFixed(2);

};

function sendCoin() {
  var meta = MetaCoin.deployed();

  var amount = parseInt(document.getElementById("monthlyPremium").value);
  var receiver = document.getElementById("receiver").value;

  setStatus("Initiating transaction... (please wait)");

  meta.sendCoin(receiver, amount, {from: account}).then(function() {
    setStatus("Transaction complete!");
    refreshBalance();
  }).catch(function(e) {
    console.log(e);
    setStatus("Error sending coin; see log.");
  });
};

window.onload = function() {
  web3.eth.getAccounts(function(err, accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];

    refreshBalance();
  });
}

我可以在启用了MetaMask插件的Chrome浏览器中打开 html 文件。但是,由于 web3 错误问题,我似乎无法以任何方式与合同进行交互。确切的信息是这篇文章的主题行。

提前感谢您的任何帮助或指导!

4

1 回答 1

2

你能不能试试看。我认为onload给出了问题。

    $(window).load function() {
  web3.eth.getAccounts(function(err,accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];

    refreshBalance();
  });
}
于 2017-01-02T03:04:03.800 回答