学习dapp

image.png

const displayGreeting = async (greeting, contract) => {
  greeting = await contract.methods.sayHello().call();
  $("h2").html(greeting);
};

const updateGreeting = (greeting, contract, accounts) => {
  let input;
  $("#input").on("change", (e) => {
    input = e.target.value;
  });
  $("#form").on("submit", async (e) => {
    e.preventDefault();
    await contract.methods
      .updateGreeting(input)
      .send({ from: accounts[0], gas: 40000 });
    displayGreeting(greeting, contract);
  });
};

async function greetingApp() {
  const web3 = await getWeb3();
  const accounts = await web3.eth.getAccounts();
  const contract = await getContract(web3);
  let greeting;

  displayGreeting(greeting, contract);
  updateGreeting(greeting, contract, accounts);
}

greetingApp();

各位大佬,请问一下这些代码都是些什么意思?想知道如何在前端调用智能合约

请先 登录 后评论

最佳答案 2022-05-12 11:42

你这个就是正常的合约调用,就是前端调用合约了,不过你这个还是jq操作,太麻烦了,建议你看react的框架,web3-react这个库的运用

请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 0 收藏,2108 浏览
  • Alan 提出于 2022-05-12 10:17