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

@@ -1,6 +1,11 @@
import { useEtherStore } from "@/store/ether";
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 etherStore = useEtherStore();
const window_ = window as any;
@@ -9,7 +14,10 @@ const updateWalletStatus = async (walletAddress: string) => {
if (!connection) return;
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.setWalletAddress(walletAddress);
@@ -22,10 +30,13 @@ const connectProvider = async () => {
let provider: ethers.providers.Web3Provider | null = null;
if (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 balance = await provider.getBalance(walletAddress[0]);
const balance = await contract.balanceOf(walletAddress[0]);
etherStore.setWalletAddress(walletAddress[0]);
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 formatted = ethers.utils.formatEther(balance);
return formatted;
};
export default { connectProvider, formatEther };
export default { connectProvider, formatEther, makeTransaction };