You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I have been struggling with the units visualization in my front end. This is a practise code I made to acknowledge everything but it uses the same principles as the Lesson 10.
First I call an start price of 0.02 ETH in my helper hardhatcode to initialize the variable in the constructor:
In the deployment and testing everything works fine, even the contract deplyed in the rinkeby testnet shows the Price accordingly:
But when i call it in the frint end using the same code as the Lesson 10, whatever number I use it just deplys the last single digit of weis at the end.
Does anyone know why it shows the data like this??
This is the frontEnd code. The entranceFee is the variable that calls the getPrice function and it shows visualized in the frontend.
import{contractAddresses,abi}from"../constants"// dont export from moralis when using reactimport{useMoralis,useWeb3Contract}from"react-moralis"import{useEffect,useState}from"react"import{useNotification}from"web3uikit"import{ethers}from"ethers"exportdefaultfunctionCodecall(){const{ Moralis, isWeb3Enabled,chainId: chainIdHex}=useMoralis()// These get re-rendered every time due to our connect button!constchainId=parseInt(chainIdHex)console.log(`ChainId is ${chainId}`)constraffleAddress=chainIdincontractAddresses ? contractAddresses[chainId][0] : null// State hooks// https://stackoverflow.com/questions/58252454/react-hooks-using-usestate-vs-just-variablesconst[entranceFee,setEntranceFee]=useState("0")const[numberOfPlayers,setNumberOfPlayers]=useState("0")const[recentWinner,setRecentWinner]=useState("0")constdispatch=useNotification()const{runContractFunction: Subscription,data: enterTxResponse,
isLoading,
isFetching,}=useWeb3Contract({abi: abi,contractAddress: raffleAddress,functionName: "Subscription",msgValue: entranceFee,params: {},})/* View Functions */const{runContractFunction: getInterval}=useWeb3Contract({abi: abi,contractAddress: raffleAddress,// specify the networkIdfunctionName: "getInterval",params: {},})const{runContractFunction: getPrice}=useWeb3Contract({abi: abi,contractAddress: raffleAddress,functionName: "getPrice",params: {},})const{runContractFunction: getSubscribers}=useWeb3Contract({abi: abi,contractAddress: raffleAddress,functionName: "getSubscribers",params: {},})asyncfunctionupdateUIValues(){// Another way we could make a contract call:// const options = { abi, contractAddress: raffleAddress } // const fee = await Moralis.executeFunction({// functionName: "getEntranceFee",// ...options,// })const[entranceFeeFromCall,b]=(awaitgetPrice()).toString()console.log(`Value: ${entranceFeeFromCall}`)constnumPlayersFromCall=(awaitgetSubscribers()).toString()constrecentWinnerFromCall=(awaitgetInterval()).toString()setEntranceFee(entranceFeeFromCall)setNumberOfPlayers(numPlayersFromCall)setRecentWinner(recentWinnerFromCall)}useEffect(()=>{if(isWeb3Enabled){updateUIValues()}},[isWeb3Enabled])consthandleNewNotification=()=>{dispatch({type: "info",message: "Transaction Complete!",title: "Transaction Notification",position: "topR",icon: "bell",})}// Probably could add some error handlingconsthandleSuccess=async(tx)=>{awaittx.wait(1)updateUIValues()handleNewNotification(tx)}return(<divclassName="p-5"><h1className="py-4 px-4 font-bold text-3xl">Subscription</h1>{raffleAddress ? (<div><buttonclassName="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded ml-auto"onClick={async()=>awaitSubscription({// onComplete:// onError:onSuccess: handleSuccess,onError: (error)=>console.log(error),})}disabled={isLoading||isFetching}>{isLoading||isFetching ? (<divclassName="animate-spin spinner-border h-8 w-8 border-b-2 rounded-full"></div>) : ("Enter the subscription"//ethers.utils.formatUnits(entranceFee, "ether"))}</button><div>Entrance Fee: {ethers.utils.formatUnits(entranceFee,"ether")} ETH</div> // HERE!!
<div>The current number of players is: {numberOfPlayers}</div><div>Number is: {recentWinner}</div></div>) : (<div>Please connect to a supported chain </div>)}</div>)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I have been struggling with the units visualization in my front end. This is a practise code I made to acknowledge everything but it uses the same principles as the Lesson 10.
First I call an start price of 0.02 ETH in my helper hardhatcode to initialize the variable in the constructor:
In the deployment and testing everything works fine, even the contract deplyed in the rinkeby testnet shows the Price accordingly:

But when i call it in the frint end using the same code as the Lesson 10, whatever number I use it just deplys the last single digit of weis at the end.

Does anyone know why it shows the data like this??
This is the frontEnd code. The entranceFee is the variable that calls the getPrice function and it shows visualized in the frontend.
Beta Was this translation helpful? Give feedback.
All reactions