将参数传递给函数的可靠性问题

Solidity issue passing parameters to a function

我有一个具有以下功能的智能合约:

contract Example {
     event claimed(address owner);
     function claimStar() public {
          owner = msg.sender;
          emit claimed(msg.sender);
     }
}

我使用 Truffle V5.0 和 Webpack box 作为样板代码。

在我的 truffle-config.js 文件中我有网络配置:

development:{
  host:"127.0.0.1",
  port: 9545,
  network_id:"*"
}

一切编译正常使用: - truffle develop - compile - migrate --reset 它说 Truffle Develop started at http://127.0.0.1:9545

在我的 index.js 文件中,我有以下代码:

import Web3 from "web3";
import starNotaryArtifact from "../../build/contracts/StarNotary.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 = starNotaryArtifact.networks[networkId];
      this.meta = new web3.eth.Contract(
        starNotaryArtifact.abi,
        deployedNetwork.address,
      );

      // get accounts
      const accounts = await web3.eth.getAccounts();
      this.account = accounts[0];
    } catch (error) {
      console.error("Could not connect to contract or chain.");
    }
  },

  setStatus: function(message) {
    const status = document.getElementById("status");
    status.innerHTML = message;
  },

  claimStarFunc: async function(){
    const { claimStar } = this.meta.methods;
    await claimStar();
    App.setStatus("New Star Owner is " + this.account + ".");
  }

};

window.App = App;

window.addEventListener("load", async function() {
  if (window.ethereum) {
    // use MetaMask's provider
    App.web3 = new Web3(window.ethereum);
    await window.ethereum.enable(); // get permission to access accounts
  } else {
    console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. 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:9545"),);
  }

  App.start();
});

在我的浏览器中,我安装了 Metamask,我添加了一个具有相同 URL 的专用网络,还导入了两个帐户。 当我启动应用程序并在浏览器中打开时,它会打开 Metamask 以请求权限,因为我正在使用 window.ethereum.enable();。 但是当我点击 claim 按钮时,它什么也没做。 正常行为是 Metamask 打开一个提示要求确认,但它从未发生过。 我还在测试合同中创建了一个新的 属性,它工作正常,向我显示了合同构造函数中分配的值。 我的问题是,我错过了什么吗?

我也尝试将函数 await claimStar(); 更改为 await claimStar({from: this.account});,但在这种情况下,我收到一条错误消息,指出 claimStar 不需要参数。

如有任何帮助,我将不胜感激。谢谢

我解决了问题,问题出在函数中claimStarFunc 应该是这样的:

claimStarFunc: async function(){
    const { claimStar } = this.meta.methods;
    await claimStar().send({from:this.account});
    App.setStatus("New Star Owner is " + this.account + ".");
  }

因为我正在发送交易。 谢谢