diff --git a/src/components/TopBar.vue b/src/components/TopBar.vue index cf70669..7b3d6dc 100644 --- a/src/components/TopBar.vue +++ b/src/components/TopBar.vue @@ -11,11 +11,16 @@ const connectMetaMask = () => { ethers.connectProvider(); }; +const makeTransaction = () => { + ethers.makeTransaction() + alert("oi") +}; + const formatWalletAddress = (): string => { const walletAddressLength = walletAddress.value.length; const initialText = walletAddress.value.substring(0, 5); const finalText = walletAddress.value.substring( - walletAddressLength - 6, + walletAddressLength - 5, walletAddressLength - 1 ); return `${initialText} ... ${finalText}`; @@ -39,7 +44,15 @@ const formatWalletBalance = (): string => { height="75" />
- + + +
diff --git a/src/utils/ethers.ts b/src/utils/ethers.ts index 351691b..c3291d5 100644 --- a/src/utils/ethers.ts +++ b/src/utils/ethers.ts @@ -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 };