我正在学习建立一个简单的前端网站,在测试环境中执行智能合同方法,但是即使MetaMask确认了合同的部署,事务也没有被执行。
Deploy a Remix Contract
Ethereum Secret Messenger
This site writes a secret message to the Ethereum blockchain!
Set secret message
// Connect a the web3 provider
if (typeof web3 !== "undefined") {
web3 = new Web3(web3.currentProvider);
} else {
web3 = new Web3(
new Web3.providers.HttpProvider(
"https://ropsten.infura.io/v3/3934271dae4126bf3bffc1244d2f6f"
)
);
}
// Set a default account
web3.eth.defaultAccount = web3.eth.accounts[0];
// Get the contract address
var RemixContract = new web3.eth.Contract([
{
constant: false,
inputs: [
{
name: "x",
type: "string",
},
],
name: "setMessage",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function",
},
{
constant: true,
inputs: [],
name: "getMessage",
outputs: [
{
name: "",
type: "string",
},
],
payable: false,
stateMutability: "view",
type: "function",
},
]);
// Get the contract abi
RemixContract.options.address =
"0xbB2a0A4c3Fa4611eF34b260Bbb4932548fF38947";
$("#setMessageButton").click(function () {
message = $("#userInput").val();
RemixContract.methods
.setMessage($("#userInput").val())
.call()
.then(console.log);
console.log($("#userInput").val());
});如前所述,MetaMask显示了正在确认的合同部署日志,但是当我尝试使用setMessage方法时,MetaMask弹出不会出现,其他任何事情都不会发生。该网站通过VSC的Live扩展在本地端口5500上运行,在日志中没有显示错误。
发布于 2020-05-08 17:08:09
您使用的是.call(),它不会创建事务。它用于查询合同。
如果要修改合同,则需要使用.send()。
发布于 2020-05-09 08:21:18
似乎,您的代码无法连接到元问询,请尝试更改
web3 = new Web3(web3.currentProvider);
至
web3 = new Web3(window.web3.currentProvider);
https://ethereum.stackexchange.com/questions/83188
复制相似问题