Implemented communication with MockToken contract and deployed local network

Co-authored-by: RcleydsonR <rafael.cleydson@gmail.com>
Co-authored-by: geovanne97 <geovannessaraiva97@gmail.com>
This commit is contained in:
brunoedcf 2022-11-18 17:05:11 -03:00
parent d7f6294e6d
commit 328bb9ad62
2 changed files with 49 additions and 6 deletions

View File

@ -11,11 +11,16 @@ const connectMetaMask = () => {
ethers.connectProvider(); ethers.connectProvider();
}; };
const makeTransaction = () => {
ethers.makeTransaction()
alert("oi")
};
const formatWalletAddress = (): string => { const formatWalletAddress = (): string => {
const walletAddressLength = walletAddress.value.length; const walletAddressLength = walletAddress.value.length;
const initialText = walletAddress.value.substring(0, 5); const initialText = walletAddress.value.substring(0, 5);
const finalText = walletAddress.value.substring( const finalText = walletAddress.value.substring(
walletAddressLength - 6, walletAddressLength - 5,
walletAddressLength - 1 walletAddressLength - 1
); );
return `${initialText} ... ${finalText}`; return `${initialText} ... ${finalText}`;
@ -39,7 +44,15 @@ const formatWalletBalance = (): string => {
height="75" height="75"
/> />
<div class="flex gap-4 items-center"> <div class="flex gap-4 items-center">
<button type="button" class="p-2 text-gray-50">Quero vender</button>
<button
type="button"
class="p-2 rounded text-gray-50"
@click="makeTransaction()"
>
Quero vender
</button>
<button <button
type="button" type="button"
v-if="!walletAddress" v-if="!walletAddress"
@ -52,7 +65,7 @@ const formatWalletBalance = (): string => {
<span class="text-gray-50"> <span class="text-gray-50">
{{ formatWalletAddress() }} {{ formatWalletAddress() }}
</span> </span>
<span class="text-gray-50"> ETH: {{ formatWalletBalance() }} </span> <span class="text-gray-50"> MBRZ: {{ formatWalletBalance() }} </span>
</div> </div>
</div> </div>
</header> </header>

View File

@ -1,6 +1,11 @@
import { useEtherStore } from "@/store/ether"; import { useEtherStore } from "@/store/ether";
import { ethers } from "ethers"; import { ethers } from "ethers";
// smart contract imports
import mocktoken from "../../../p2pix-smart-contracts/artifacts/contracts/mockToken.sol/MockToken.json"
import p2pix from "../../../p2pix-smart-contracts/artifacts/contracts/p2pix.sol/P2PIX.json"
import addresses from "../../../p2pix-smart-contracts/deploys/localhost.json"
const updateWalletStatus = async (walletAddress: string) => { const updateWalletStatus = async (walletAddress: string) => {
const etherStore = useEtherStore(); const etherStore = useEtherStore();
const window_ = window as any; const window_ = window as any;
@ -9,7 +14,10 @@ const updateWalletStatus = async (walletAddress: string) => {
if (!connection) return; if (!connection) return;
const provider = new ethers.providers.Web3Provider(connection); const provider = new ethers.providers.Web3Provider(connection);
const balance = await provider.getBalance(walletAddress); const signer = provider.getSigner();
const contract = new ethers.Contract(addresses.token, mocktoken.abi, signer);
const balance = await contract.balanceOf(walletAddress);
etherStore.setBalance(String(balance)); etherStore.setBalance(String(balance));
etherStore.setWalletAddress(walletAddress); etherStore.setWalletAddress(walletAddress);
@ -22,10 +30,13 @@ const connectProvider = async () => {
let provider: ethers.providers.Web3Provider | null = null; let provider: ethers.providers.Web3Provider | null = null;
if (connection) { if (connection) {
provider = new ethers.providers.Web3Provider(connection); provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
const contract = new ethers.Contract(addresses.token, mocktoken.abi, signer);
const walletAddress = await provider.send("eth_requestAccounts", []); const walletAddress = await provider.send("eth_requestAccounts", []);
const balance = await provider.getBalance(walletAddress[0]); const balance = await contract.balanceOf(walletAddress[0]);
etherStore.setWalletAddress(walletAddress[0]); etherStore.setWalletAddress(walletAddress[0]);
etherStore.setBalance(String(balance)); etherStore.setBalance(String(balance));
@ -36,9 +47,28 @@ const connectProvider = async () => {
} }
}; };
const makeTransaction = async () => {
const etherStore = useEtherStore();
const window_ = window as any;
const connection = window_.ethereum;
let provider: ethers.providers.Web3Provider | null = null;
if (!connection) return;
provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
const contract = new ethers.Contract(addresses.token, mocktoken.abi, signer);
const fixedAccount1Address = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"
const tx = await contract.transfer(fixedAccount1Address, ethers.utils.parseEther("100.0"));
await tx.wait()
updateWalletStatus(etherStore.walletAddress);
};
const formatEther = (balance: string) => { const formatEther = (balance: string) => {
const formatted = ethers.utils.formatEther(balance); const formatted = ethers.utils.formatEther(balance);
return formatted; return formatted;
}; };
export default { connectProvider, formatEther }; export default { connectProvider, formatEther, makeTransaction };