[英]Call Revert Exception
Following a tutorial on Solidity and am working on the front end and end up getting hit with this error遵循关于 Solidity 的教程并在前端工作并最终遇到此错误
It won't let me fetch the greeting but I can set the greeting everything else works except for the fetch greeting, it won't let me see it even if I set the greeting to something else I can't see it in the console only the error here is the Solidity tutorial它不会让我获取问候语,但我可以设置问候语除了获取问候语之外的所有其他工作,即使我将问候语设置为其他我在控制台中看不到的东西,它也不会让我看到它只有这里的错误是Solidity 教程
Here is my solidity code这是我的可靠代码
pragma solidity ^0.8.4;
import "hardhat/console.sol";
contract Greeter {
string greeting;
constructor(string memory _greeting) {
console.log("Deploying a Greeter with greeting:", _greeting);
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
greeting = _greeting;
}
}
And here is my react code:这是我的反应代码:
import './App.css';
import { useState } from 'react';
import { ethers } from 'ethers'
import Greeter from './artifacts/contracts/Greeter.sol/Greeter.json'
// Update with the contract address logged out to the CLI when it was deployed
const greeterAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
function App() {
// store greeting in local state
const [greeting, setGreetingValue] = useState()
// request access to the user's MetaMask account
async function requestAccount() {
await window.ethereum.request({ method: 'eth_requestAccounts' });
}
// call the smart contract, read the current greeting value
async function fetchGreeting() {
if (typeof window.ethereum !== 'undefined') {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const contract = new ethers.Contract(greeterAddress, Greeter.abi, provider)
try {
const data = await contract.greet()
console.log('data: ', data)
} catch (err) {
console.log("Error: ", err)
}
}
}
// call the smart contract, send an update
async function setGreeting() {
if (!greeting) return
if (typeof window.ethereum !== 'undefined') {
await requestAccount()
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner()
const contract = new ethers.Contract(greeterAddress, Greeter.abi, signer)
const transaction = await contract.setGreeting(greeting)
await transaction.wait()
fetchGreeting()
}
}
return (
<div className="App">
<header className="App-header">
<button onClick={fetchGreeting}>Fetch Greeting</button>
<button onClick={setGreeting}>Set Greeting</button>
<input onChange={e => setGreetingValue(e.target.value)} placeholder="Set greeting" />
</header>
</div>
);
}
export default App;
Any advice would help thx任何建议都会有所帮助
You need insert the attribute value您需要插入属性值
value = {greeting}
in the input:在输入中:
<input onChange={e => setGreetingValue(e.target.value)}
placeholder="Introduce uno nuevo"
value={greeting}
/>
Also initialize with two quotes useState ():同样用两个引号初始化useState():
const [greeting, setGreetingValue] = useState ('')
You will probably now have a issue with the metamask nonce, which will tell you that it is too high like message error like this:您现在可能会遇到元掩码 nonce 的问题,它会告诉您它太高了,就像这样的消息错误:
Nonce too high. Expected nonce to be 1 but got 2
You will reset account in metamask, or import a new account.您将在 metamask 中重置帐户,或导入一个新帐户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.