[英]Smart Contract : .methods.request().send() doesn't display anything
我有以下问题:
我在 remix 上写了一个智能合约:
pragma solidity ^0.5.1;
contract anotherTry {
function Hello() external pure returns (string memory) {
return "Hello World !!!";
}
}
我编译了它,然后我得到了它的 WEB3DEPLOY 数据并将它添加到我的 Geth 控制台中。我收到的合同地址和来自 remix 网站的 ABI 我就像这样使用它们:
//addr = the contract address received from the Geth console
//abit = obviously,the ABI
//account = a random & valid account address(I've tried with addr too...who knows maybe it could work)
let MyContract = new web3.eth.Contract(abi, addr);
MyContract.methods.Hello().send({ from: account }).then(receipt => { console.log("SUCCESS"); });
问题是我没有收到任何消息,但是当我尝试时,我收到了带有函数和内容的循环结构
console.log(MyContract.methods);
知道为什么我不能“连接”到我的智能合约吗? 我在 web3 1.0.0 & Geth 中使用 NodeJS
我解决了
这实际上是web3提供程序声明的问题,我应该调用method.request()。call()而不是.send()
无论如何,谢谢您的帮助!
要为上述正确答案添加更多详细信息,要确定是否使用call
与send
请查看合同方法签名,如果它是view
或pure
函数,则它不会改变状态,您必须使用call
。 其他没有这样的签名或payable
会改变区块链上的状态,因此是一笔交易,必须支付以太币才能获得 gas。 必须使用send
调用这些类型的方法。
Ire 在这里写得很清楚https://bitsofco.de/calling-smart-contract-functions-using-web3-js-call-vs-send/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.