Last active
May 30, 2023 02:17
-
-
Save danicuki/882259a049077bc8c8d228405b6c8c12 to your computer and use it in GitHub Desktop.
Seção 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from "react"; | |
import { ethers } from "ethers"; | |
import './App.css'; | |
import abi from "./utils/WavePortal.json" | |
export default function App() { | |
const [currentAccount, setCurrentAccount] = useState(""); | |
const contractAddress = "0xF2482AEDB6bfF7Cc73772fCBCeAA9157ff00c287"; | |
const contractABI = abi.abi; | |
const checkIfWalletIsConnected = async () => { | |
try { | |
const { ethereum } = window; | |
if (!ethereum) { | |
console.log("Garanta que possua a Metamask instalada!"); | |
return; | |
} else { | |
console.log("Temos o objeto ethereum", ethereum); | |
} | |
const accounts = await ethereum.request({ method: "eth_accounts" }); | |
if (accounts.length !== 0) { | |
const account = accounts[0]; | |
console.log("Encontrada a conta autorizada:", account); | |
setCurrentAccount(account) | |
} else { | |
console.log("Nenhuma conta autorizada foi encontrada") | |
} | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
/** | |
* Implemente aqui o seu método connectWallet | |
*/ | |
const connectWallet = async () => { | |
try { | |
const { ethereum } = window; | |
if (!ethereum) { | |
alert("MetaMask encontrada!"); | |
return; | |
} | |
const accounts = await ethereum.request({ method: "eth_requestAccounts" }); | |
console.log("Conectado", 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("Recuperado o número de tchauzinhos...", count.toNumber()); | |
/* | |
* Executar o aceno a partir do contrato inteligente | |
*/ | |
const waveTxn = await wavePortalContract.wave(); | |
console.log("Minerando...", waveTxn.hash); | |
await waveTxn.wait(); | |
console.log("Minerado -- ", waveTxn.hash); | |
count = await wavePortalContract.getTotalWaves(); | |
console.log("Total de tchauzinhos recuperado...", count.toNumber()); | |
} else { | |
console.log("Objeto Ethereum não encontrado!"); | |
} | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
useEffect(() => { | |
checkIfWalletIsConnected(); | |
}, []) | |
return ( | |
<div className="mainContainer"> | |
<div className="dataContainer"> | |
<div className="header"> | |
👋 Olá Pessoal! | |
</div> | |
<div className="bio"> | |
Eu sou o danicuki e já trabalhei com música, sabia? Legal, né? Conecte sua carteira Ethereum wallet e me manda um tchauzinho! | |
</div> | |
<button className="waveButton" onClick={wave}> | |
Mandar Tchauzinho 🌟 | |
</button> | |
{/* | |
* Se não existir currentAccount, apresente este botão | |
*/} | |
{!currentAccount && ( | |
<button className="waveButton" onClick={connectWallet}> | |
Conectar carteira | |
</button> | |
)} | |
</div> | |
</div> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('@nomiclabs/hardhat-waffle'); | |
module.export = { | |
solidity: '0.8.0', | |
networks: { | |
rinkeby: { | |
url: 'SUA_CHAVE_API_ALCHEMY', | |
accounts: ['SUA_CHAVE_PRIVADA_RINKEBY'], | |
}, | |
}, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
CONTEUDO_DO_SEU_ABI_FILE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Até aqui tudo certo. Obrigado Web3Dev por essa oportunidade!!!