Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect ethers.js version changed from 5.6 to 5.2 and applied prettier formatting #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
110 changes: 54 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "./ethers-5.6.esm.min.js"
import { ethers } from "./ethers-5.2.esm.min.js"
import { abi, contractAddress } from "./constants.js"

const connectButton = document.getElementById("connectButton")
Expand All @@ -11,81 +11,79 @@ fundButton.onclick = fund
balanceButton.onclick = getBalance

async function connect() {
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({ method: "eth_requestAccounts" })
} catch (error) {
console.log(error)
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({ method: "eth_requestAccounts" })
} catch (error) {
console.log(error)
}
connectButton.innerHTML = "Connected"
const accounts = await ethereum.request({ method: "eth_accounts" })
console.log(accounts)
} else {
connectButton.innerHTML = "Please install MetaMask"
}
connectButton.innerHTML = "Connected"
const accounts = await ethereum.request({ method: "eth_accounts" })
console.log(accounts)
} else {
connectButton.innerHTML = "Please install MetaMask"
}
}

async function withdraw() {
console.log(`Withdrawing...`)
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
await provider.send('eth_requestAccounts', [])
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
// await transactionResponse.wait(1)
} catch (error) {
console.log(error)
console.log(`Withdrawing...`)
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
await provider.send("eth_requestAccounts", [])
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
// await transactionResponse.wait(1)
} catch (error) {
console.log(error)
}
} else {
withdrawButton.innerHTML = "Please install MetaMask"
}
} else {
withdrawButton.innerHTML = "Please install MetaMask"
}
}

async function fund() {
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with ${ethAmount}...`)
if (typeof window.ethereum !== "undefined") {
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)
} catch (error) {
console.log(error)
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with ${ethAmount}...`)
if (typeof window.ethereum !== "undefined") {
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)
} catch (error) {
console.log(error)
}
} else {
fundButton.innerHTML = "Please install MetaMask"
}
} else {
fundButton.innerHTML = "Please install MetaMask"
}
}

async function getBalance() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
try {
const balance = await provider.getBalance(contractAddress)
console.log(ethers.utils.formatEther(balance))
} catch (error) {
console.log(error)
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
try {
const balance = await provider.getBalance(contractAddress)
console.log(ethers.utils.formatEther(balance))
} catch (error) {
console.log(error)
}
} else {
balanceButton.innerHTML = "Please install MetaMask"
}
} else {
balanceButton.innerHTML = "Please install MetaMask"
}
}

function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}`)
return new Promise((resolve, reject) => {
try {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations. `
)
console.log(`Completed with ${transactionReceipt.confirmations} confirmations. `)
resolve()
})
} catch (error) {
Expand Down