我想使用.jsx显示网页上NFT项目铸造的NFT的总量
这是我的基本网页代码,该网页允许用户使用其MetAmask钱包造币。我要做的是在屏幕上显示铸造的总量供用户查看。为此,我只想说我希望它是X/4756。我有一个在下面显示的总铸造的地方,我有****希望X/4756在这里。我的问题是,我似乎无法将X显示为铸造的金额。我将其显示在Console.log中,但无法将其显示在屏幕上。我在网络上搜索,并尝试了诸如将{}放在变量“ count”周围之类的事情上,但它一直说我放置在{}中的变量是未定义的。有没有办法将变量放入其中,然后只键入 /4756将其显示为X /4756,甚至将其从Console.log中抽出的方法?让我知道是否有人有任何想法!谢谢你!
import React, { useEffect, useState } from "react";
import "./App.css";
import { ethers } from "ethers";
import abi from "./util/WavePortal.json";
const App = () => {
const [currentAccount, setCurrentAccount] = useState("");
const contractAddress = "0xbA0d7f21F06C329188580864e06DD6cdb03F1e5d"
const contractABI = abi.abi;
const checkIfWalletIsConnected = async () => {
try {
const { ethereum } = window;
if (!ethereum) {
console.log("Make sure you have metamask!");
return;
} else {
console.log("We have the ethereum object", ethereum);
}
const accounts = await ethereum.request({ method: "eth_accounts" });
if (accounts.length !== 0) {
const account = accounts[0];
console.log("Found an authorized account:", account);
setCurrentAccount(account);
} else {
console.log("No authorized account found")
}
} catch (error) {
console.log(error);
}
}
/**
* Implement your connectWallet method here
*/
const connectWallet = async () => {
try {
const { ethereum } = window;
if (!ethereum) {
alert("Get MetaMask!");
return;
}
const accounts = await ethereum.request({ method: "eth_requestAccounts" });
console.log("Connected", accounts[0]);
setCurrentAccount(accounts[0]);
} catch (error) {
console.log(error)
}
}
const wave = async () => {
try {
const { ethereum } = window;
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const wavePortalContract = new ethers.Contract(contractAddress, contractABI, signer);
let count = await wavePortalContract.getTotalWaves();
console.log("Retrieved total wave count...", count.toNumber());
let count2 = count.toNumber();
const waveTxn = await wavePortalContract.wave();
console.log("Mining...", waveTxn.hash);
await waveTxn.wait();
console.log("Mined -- ", waveTxn.hash);
count = await wavePortalContract.getTotalWaves();
console.log("Retrieved total wave count...", count.toNumber());
} else {
console.log("Ethereum object doesn't exist!");
}
} catch (error) {
console.log(error);
}
}
useEffect(() => {
checkIfWalletIsConnected();
}, [])
return (
<div className="mainContainer">
<div className="dataContainer">
<div className="header">
This is my code for a basic webpage that allows the user to mint using their metamask wallet. What I want to do is to display the Total amount minted on the screen for the user to see. For this I'll just say I would want it to be x/4756. I have the place for me that displays the total minted down below and I have **** where I would like the x/4756 to be at. My problem is that I cannot seem to get the x to show as the amount minted. I have it showing up in console.log, but cannot get it to be displayed on the screen. I searched on the web and tried out things like putting {} around the variable "count" but it keeps saying that the variable I place in the {} is undefined. Is there a way that I can either place the variable in there and then just type /4756 to have it displayed as x/4756, or even a way to pull it out of the console.log? Let me know if anyone has any ideas! Thank you!
import React, { useEffect, useState } from "react";
import "./App.css";
import { ethers } from "ethers";
import abi from "./util/WavePortal.json";
const App = () => {
const [currentAccount, setCurrentAccount] = useState("");
const contractAddress = "0xbA0d7f21F06C329188580864e06DD6cdb03F1e5d"
const contractABI = abi.abi;
const checkIfWalletIsConnected = async () => {
try {
const { ethereum } = window;
if (!ethereum) {
console.log("Make sure you have metamask!");
return;
} else {
console.log("We have the ethereum object", ethereum);
}
const accounts = await ethereum.request({ method: "eth_accounts" });
if (accounts.length !== 0) {
const account = accounts[0];
console.log("Found an authorized account:", account);
setCurrentAccount(account);
} else {
console.log("No authorized account found")
}
} catch (error) {
console.log(error);
}
}
/**
* Implement your connectWallet method here
*/
const connectWallet = async () => {
try {
const { ethereum } = window;
if (!ethereum) {
alert("Get MetaMask!");
return;
}
const accounts = await ethereum.request({ method: "eth_requestAccounts" });
console.log("Connected", accounts[0]);
setCurrentAccount(accounts[0]);
} catch (error) {
console.log(error)
}
}
const wave = async () => {
try {
const { ethereum } = window;
if (ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
const wavePortalContract = new ethers.Contract(contractAddress, contractABI, signer);
let count = await wavePortalContract.getTotalWaves();
console.log("Retrieved total wave count...", count.toNumber());
let count2 = count.toNumber();
const waveTxn = await wavePortalContract.wave();
console.log("Mining...", waveTxn.hash);
await waveTxn.wait();
console.log("Mined -- ", waveTxn.hash);
count = await wavePortalContract.getTotalWaves();
console.log("Retrieved total wave count...", count.toNumber());
} else {
console.log("Ethereum object doesn't exist!");
}
} catch (error) {
console.log(error);
}
}
useEffect(() => {
checkIfWalletIsConnected();
}, [])
return (
<div className="mainContainer">
<div className="dataContainer">
<div className="header">
???? Welcome to the Project!
</div>
<div className="bio">
This is my first site and I am proud of it.
</div>
<div className="bio">
Total Mints: ****
</div>
<button className="waveButton" onClick={wave}>
Mint
</button>
{/*
* If there is no currentAccount render this button
*/}
{!currentAccount && (
<button className="waveButton" onClick={connectWallet}>
Connect Wallet
</button>
)}
</div>
</div>
);
}
export default App
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用Usestate钩进行计数。
从请求中获得计数后,用setCount设置了计数后
,最终将count var插入JSX中。
希望它有帮助:)
Try to use useState hook for count.
after you get count from request set it with setCount
Finally just insert count var in JSX.
Hope it helps:)