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
I have done same as told in video but got different result in browser console.
Here is my javascript code
// in nodejs we use require()// But in front-end we use import(), as we can't use require()import{ethers}from"https://cdnjs.cloudflare.com/ajax/libs/ethers/6.7.0/ethers.min.js";import{contractAddress,abi}from"./constants.js";constconnectButton=document.getElementById("connectButton");constfundButton=document.getElementById("fundButton");constbalanceButton=document.getElementById("balanceButton");constwithdrawButton=document.getElementById("withdrawButton");connectButton.onclick=connect;fundButton.onclick=fund;balanceButton.onclick=getBalance;withdrawButton.onclick=withdraw;asyncfunctionconnect(){if(typeofwindow.etherum!=undefined){awaitethereum.request({method: "eth_requestAccounts",params: []});connectButton.innerHTML="Connected";}else{connectButton.innerHTML="Please install Metamask";}}asyncfunctiongetBalance(){if(typeofwindow.etherum!=undefined){constprovider=newethers.BrowserProvider(window.ethereum);constbalance=awaitprovider.getBalance(contractAddress);console.log(ethers.formatEther(balance));}}asyncfunctionfund(){constethAmount=document.getElementById("ethAmount").value;if(typeofwindow.ethereum!=undefined){constprovider=newethers.BrowserProvider(window.ethereum);constsigner=awaitprovider.getSigner();constcontract=newethers.Contract(contractAddress,abi,signer);console.log(`Funding with ${ethAmount}...`);try{consttransactionResponse=awaitcontract.fund({value: ethers.parseEther(ethAmount),});awaitlistenForTransactionMine(transactionResponse,provider);console.log("Done!");}catch(error){console.log(error);}}}asyncfunctionwithdraw(){if(typeofwindow.ethereum!="undefined"){console.log("Withdrawing...");constprovider=newethers.BrowserProvider(window.ethereum);constsigner=awaitprovider.getSigner();constcontract=newethers.Contract(contractAddress,abi,signer);try{consttransactionResponse=awaitcontract.withdraw();awaitlistenForTransactionMine(transactionResponse,provider);console.log("Done!");}catch(error){console.log(error);}}}functionlistenForTransactionMine(transactionResponse,provider){console.log(`Mining ${transactionResponse.hash}....`);// Listen for transaction to finishreturnnewPromise((resolve,reject)=>{provider.once(transactionResponse.hash,(transactionReciept)=>{console.log(`Completed with ${transactionReciept.confirmations} confirmations.`);resolve();});});}
Now what I am getting after running fund() function
Completed with async confirmations(){return await this.provider.getBlockNumber()-this.blockNumber+1} confirmations.
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.
-
I have done same as told in video but got different result in browser console.
Here is my javascript code
Now what I am getting after running fund() function
Completed with async confirmations(){return await this.provider.getBlockNumber()-this.blockNumber+1} confirmations.
Beta Was this translation helpful? Give feedback.
All reactions