大神您好,使用webpack+truffle官方的box # MetaCoin学习,部署合约到测试网后,电脑上运行一切正常,然后用手机钱包打开发现token转账后状态不更新。 也就是执行index.js的sendCoin函数 await sendCoin(receiver, amount).send({ from: this.account }); 这段语句后面的代码没有执行(去钱包查询确实转账成功了),不知道是处理,求解答,谢谢! 以下是合约文件:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ConvertLib.sol";
// This is a simple example of a coin-like contract.
// It is not standards compatible and cannot be expected to talk to other
// coin/token contracts. If you want to create a standards-compliant
// token, see: https://github.com/ConsenSys/Tokens. Cheers!
contract MetaCoin {
    mapping (address => uint) balances;
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    constructor() public {
        balances[msg.sender] = 10000;
    }
    function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
        if (balances[msg.sender] < amount) return false;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Transfer(msg.sender, receiver, amount);
        return true;
    }
    function getBalanceInEth(address addr) public view returns(uint) {
        return ConvertLib.convert(getBalance(addr),2);
    }
    function getBalance(address addr) public view returns(uint) {
        return balances[addr];
    }
}下面是index.js:
import Web3 from "web3";
import metaCoinArtifact from "../../build/contracts/MetaCoin.json";
const App = {
  web3: null,
  account: null,
  meta: null,
  start: async function() {
    const { web3 } = this;
    try {
      // get contract instance
      const networkId = await web3.eth.net.getId();
      const deployedNetwork = metaCoinArtifact.networks[networkId];
      this.meta = new web3.eth.Contract(
        metaCoinArtifact.abi,
        deployedNetwork.address,
      );
      // get accounts
      const accounts = await web3.eth.getAccounts();
      this.account = accounts[0];
      this.refreshBalance();
    } catch (error) {
      console.error("Could not connect to contract or chain.");
    }
  },
  refreshBalance: async function() {
    const { getBalance } = this.meta.methods;
    const balance = await getBalance(this.account).call();
    const balanceElement = document.getElementsByClassName("balance")[0];
    balanceElement.innerHTML = balance;
  },
  sendCoin: async function() {
    const amount = parseInt(document.getElementById("amount").value);
    const receiver = document.getElementById("receiver").value;
    this.setStatus("Initiating transaction... (please wait)");
    const { sendCoin } = this.meta.methods;
    await sendCoin(receiver, amount).send({ from: this.account });
    this.setStatus("Transaction complete!");
    this.refreshBalance();
  },
  setStatus: function(message) {
    const status = document.getElementById("status");
    status.innerHTML = message;
  },
};
window.App = App;
window.addEventListener("load", function() {
  if (window.ethereum) {
    // use MetaMask's provider
    App.web3 = new Web3(window.ethereum);
    window.ethereum.enable(); // get permission to access accounts
  } else {
    console.warn(
      "No web3 detected. Falling back to http://127.0.0.1:8545. You should remove this fallback when you deploy live",
    );
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    App.web3 = new Web3(
      new Web3.providers.HttpProvider("http://127.0.0.1:8545"),
    );
  }
  App.start();
});