-
Hi guys, I met some weird situation.
Then I found out fund button was broken too, it said:
It's weird cuz I did not make any change to fund part, and it worked fine before. Here is my index.js import { ethers } from "./ethers-5.6.esm.min.js"
import { abi, contractAddress } from "./constants.js"
const connectButton = document.getElementById("connectButton")
const fundButton = document.getElementById("fundButton")
const balanceButton = document.getElementById("balanceButton")
const withdrawButton = document.getElementById("withdrawButton")
connectButton.onclick = connect
fundButton.onclick = fund
balanceButton.onclick = getBalance
withdrawButton.onclick = withdraw
console.log(ethers)
async function connect() {
if (typeof window.ethereum !== "undefined") {
await window.ethereum.request({ method: "eth_requestAccounts" })
connectButton.innerHTML = "Connected!"
} else {
connectButton.innerHTML.innerHTML = "Please install metamask!"
}
}
async function fund() {
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with ${ethAmount}...`)
if (typeof window.ethereum !== "undefined") {
//provider / connection to the blockchain
//signer/ wallet /someone with somegas
//contract that we are ingteracting with
// ^ ABI, Address
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(ethAmount),
})
await listenForTransactionMine(transactionResponse, provider)
//Listen for the tx to be mined
console.log("Done!")
} catch (error) {
console.log(error)
}
}
}
async function getBalance() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const balance = await provider.getBalance(contractAddress)
console.log(ethers.utils.formatEther(balance))
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`)
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations`
)
resolve()
})
})
//creat a listener for the blockchain
}
// fund function
//withdraw
async function withdraw() {
if (typeof window.ethereum !== "undefined") {
console.log("Withdrawing....")
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
} catch (error) {
console.log(error)
}
}
} Here is my repo:https://github.com/SakanaSachi/Html-FundMe-App.git |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Your index.js file is fine and I checked through your front-end repo that also works. |
Beta Was this translation helpful? Give feedback.
Your index.js file is fine and I checked through your front-end repo that also works.
I guess you are not entering the amount in the input field or the value entered in the field is not reaching your contract.
What does the console prints for this statement:
console.log('Funding with ${ethAmount}...')