Refactor of wallet and provider methods
Co-authored-by: brunoedcf <brest.dallacosta@outlook.com>
This commit is contained in:
parent
8b0a212b78
commit
86131b5641
0
src/blockchain/events.ts
Normal file
0
src/blockchain/events.ts
Normal file
@ -1,15 +1,62 @@
|
||||
import { ethers } from "ethers";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import { NetworkEnum } from "@/model/NetworkEnum";
|
||||
import { updateWalletStatus } from "./wallet";
|
||||
|
||||
const getProvider = (): ethers.providers.Web3Provider | null => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const getProvider = (): ethers.providers.Web3Provider | ethers.providers.JsonRpcProvider => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
|
||||
if (!connection) return null;
|
||||
if (!connection)
|
||||
return new ethers.providers.JsonRpcProvider("provider_url"); // alchemy provider
|
||||
|
||||
return new ethers.providers.Web3Provider(connection);
|
||||
return new ethers.providers.Web3Provider(connection); // metamask provider
|
||||
};
|
||||
|
||||
export { getProvider }
|
||||
const connectProvider = async (): Promise<void | null> => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
const provider = getProvider();
|
||||
|
||||
if(!(provider instanceof ethers.providers.Web3Provider)){
|
||||
window.alert("Please, connect to metamask extension");
|
||||
return null;
|
||||
}
|
||||
|
||||
await updateWalletStatus();
|
||||
// await updateValidDeposits();
|
||||
// await updateDepositAddedEvents();
|
||||
// await updateLockAddedEvents();
|
||||
// await updateLockReleasedEvents();
|
||||
|
||||
listenToNetworkChange(connection);
|
||||
listenToWalletChange(connection);
|
||||
};
|
||||
|
||||
const listenToWalletChange = (connection: any): void => {
|
||||
connection.on("accountsChanged", async () => {
|
||||
await updateWalletStatus();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const listenToNetworkChange = (connection: any) => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleNetworks: {[key: string]: NetworkEnum} = {
|
||||
"0x5": NetworkEnum.ethereum,
|
||||
"0x13881": NetworkEnum.polygon,
|
||||
"0x7a69": NetworkEnum.localhost,
|
||||
}
|
||||
|
||||
connection.on("chainChanged", (networkChain: string) => {
|
||||
if (Object.keys(possibleNetworks).includes(networkChain)){
|
||||
etherStore.setNetworkName(possibleNetworks[networkChain]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
getProvider,
|
||||
listenToNetworkChange
|
||||
}
|
0
src/blockchain/transfer.ts
Normal file
0
src/blockchain/transfer.ts
Normal file
43
src/blockchain/wallet.ts
Normal file
43
src/blockchain/wallet.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { ethers } from "ethers";
|
||||
import { getProvider } from "./provider";
|
||||
import blockchain from "../utils/blockchain"
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
|
||||
import mockToken from "../utils/smart_contract_files/MockToken.json";
|
||||
import addresses from "../utils/smart_contract_files/localhost.json";
|
||||
|
||||
const updateWalletStatus = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(blockchain.formatBigNumber(balance));
|
||||
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
|
||||
};
|
||||
|
||||
const updateWalletBalance = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(blockchain.formatBigNumber(balance));
|
||||
};
|
||||
|
||||
export {
|
||||
updateWalletStatus,
|
||||
updateWalletBalance
|
||||
}
|
@ -8,40 +8,7 @@ import addresses from "./smart_contract_files/localhost.json";
|
||||
// Mock wallets import
|
||||
import { wallets } from "./smart_contract_files/wallets.json";
|
||||
import { getProvider } from "../blockchain/provider";
|
||||
import { NetworkEnum } from "@/model/NetworkEnum";
|
||||
|
||||
// Wallet methods
|
||||
// Update wallet state (balance and address)
|
||||
const updateWalletStatus = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(formatBigNumber(balance));
|
||||
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
|
||||
};
|
||||
|
||||
const updateWalletBalance = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.token, mockToken.abi, signer);
|
||||
|
||||
const walletAddress = await provider.send("eth_requestAccounts", []);
|
||||
|
||||
const balance = await contract.balanceOf(walletAddress[0]);
|
||||
etherStore.setBalance(formatBigNumber(balance));
|
||||
};
|
||||
import { updateWalletBalance, updateWalletStatus } from "../blockchain/wallet";
|
||||
|
||||
// Split tokens between wallets in wallets.json
|
||||
const splitTokens = async () => {
|
||||
@ -264,39 +231,6 @@ const updateLockReleasedEvents = async () => {
|
||||
console.log("RELEASES", eventsReleases);
|
||||
};
|
||||
|
||||
// Provider methods
|
||||
const connectProvider = async () => {
|
||||
const window_ = window as any;
|
||||
const connection = window_.ethereum;
|
||||
|
||||
await updateWalletStatus();
|
||||
await updateValidDeposits();
|
||||
await updateDepositAddedEvents();
|
||||
await updateLockAddedEvents();
|
||||
await updateLockReleasedEvents();
|
||||
listenToNetworkChange(connection);
|
||||
|
||||
connection.on("accountsChanged", async () => {
|
||||
await updateWalletStatus();
|
||||
});
|
||||
};
|
||||
|
||||
const listenToNetworkChange = (connection: any) => {
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
const possibleNetworks: {[key: string]: NetworkEnum} = {
|
||||
"0x5": NetworkEnum.ethereum,
|
||||
"0x13881": NetworkEnum.polygon,
|
||||
"0x7a69": NetworkEnum.localhost,
|
||||
}
|
||||
|
||||
connection.on("chainChanged", (networkChain: string) => {
|
||||
if (Object.keys(possibleNetworks).includes(networkChain)){
|
||||
etherStore.setNetworkName(possibleNetworks[networkChain]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Deposit methods
|
||||
const approveTokens = async (tokenQty: Number) => {
|
||||
const provider = getProvider();
|
||||
@ -516,9 +450,7 @@ const formatBigNumber = (num: BigNumber) => {
|
||||
};
|
||||
|
||||
export default {
|
||||
connectProvider,
|
||||
formatEther,
|
||||
updateWalletStatus,
|
||||
splitTokens,
|
||||
listValidDepositTransactionsByWalletAddress,
|
||||
listAllTransactionByWalletAddress,
|
||||
|
Loading…
x
Reference in New Issue
Block a user