Updated Ethers to last version.

This commit is contained in:
Filipe Soccol
2024-11-05 16:59:30 -03:00
parent 65c1dc0f06
commit 9205909f9f
15 changed files with 10830 additions and 593 deletions

View File

@@ -1,12 +1,19 @@
import {
Contract,
formatEther,
getAddress,
Interface,
Log,
LogDescription,
} from "ethers";
import { useEtherStore } from "@/store/ether";
import { getContract, getProvider } from "./provider";
import { getTokenAddress, isPossibleNetwork } from "./addresses";
import mockToken from "@/utils/smart_contract_files/MockToken.json";
import p2pix from "@/utils/smart_contract_files/P2PIX.json";
import { ethers, type Event, type BigNumber } from "ethers";
import { formatEther } from "ethers/lib/utils";
import { getValidDeposits } from "./events";
import type { ValidDeposit } from "@/model/ValidDeposit";
@@ -14,20 +21,20 @@ import type { WalletTransaction } from "@/model/WalletTransaction";
import type { UnreleasedLock } from "@/model/UnreleasedLock";
import type { Pix } from "@/model/Pix";
const updateWalletStatus = async (): Promise<void> => {
export const updateWalletStatus = async (): Promise<void> => {
const etherStore = useEtherStore();
const provider = getProvider();
const signer = provider.getSigner();
const provider = await getProvider();
const signer = await provider.getSigner();
const { chainId } = await provider.getNetwork();
if (!isPossibleNetwork(chainId)) {
if (!isPossibleNetwork(Number(chainId))) {
window.alert("Invalid chain!:" + chainId);
return;
}
etherStore.setNetworkName(chainId);
etherStore.setNetworkName(Number(chainId));
const mockTokenContract = new ethers.Contract(
const mockTokenContract = new Contract(
getTokenAddress(etherStore.selectedToken),
mockToken.abi,
signer
@@ -37,14 +44,16 @@ const updateWalletStatus = async (): Promise<void> => {
const balance = await mockTokenContract.balanceOf(walletAddress[0]);
etherStore.setBalance(formatEther(balance));
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
etherStore.setWalletAddress(getAddress(walletAddress[0]));
};
const listValidDepositTransactionsByWalletAddress = async (
export const listValidDepositTransactionsByWalletAddress = async (
walletAddress: string
): Promise<ValidDeposit[]> => {
const etherStore = useEtherStore();
const walletDeposits = await getValidDeposits(getTokenAddress(etherStore.selectedToken));
const walletDeposits = await getValidDeposits(
getTokenAddress(etherStore.selectedToken)
);
if (walletDeposits) {
return walletDeposits
@@ -57,60 +66,72 @@ const listValidDepositTransactionsByWalletAddress = async (
return [];
};
const getLockStatus = async (id: [BigNumber]): Promise<number> => {
const p2pContract = getContract();
const getLockStatus = async (id: [BigInt]): Promise<number> => {
const p2pContract = await getContract();
const res = await p2pContract.getLocksStatus([id]);
return res[1][0];
};
const filterLockStatus = async (
transactions: Event[]
transactions: Log[]
): Promise<WalletTransaction[]> => {
const txs = await Promise.all(
transactions.map(async (transaction) => {
const tx: WalletTransaction = {
token: transaction.args?.token ? transaction.args?.token : "",
blockNumber: transaction.blockNumber ? transaction.blockNumber : -1,
amount: transaction.args?.amount
? Number(formatEther(transaction.args?.amount))
const txs: WalletTransaction[] = [];
for (const transaction of transactions) {
const IPix2Pix = new Interface(p2pix.abi);
const decoded = IPix2Pix.parseLog({
topics: transaction.topics,
data: transaction.data,
});
if (!decoded) continue;
const tx: WalletTransaction = {
token: decoded.args.token ? decoded.args.token : "",
blockNumber: transaction.blockNumber,
amount: decoded.args.amount
? Number(formatEther(decoded.args.amount))
: -1,
seller: decoded.args.seller ? decoded.args.seller : "",
buyer: decoded.args.buyer ? decoded.args.buyer : "",
event: decoded.name,
lockStatus:
decoded.name == "LockAdded"
? await getLockStatus(decoded.args.lockID)
: -1,
seller: transaction.args?.seller ? transaction.args?.seller : "",
buyer: transaction.args?.buyer ? transaction.args?.buyer : "",
event: transaction.event ? transaction.event : "",
lockStatus:
transaction.event == "LockAdded"
? await getLockStatus(transaction.args?.lockID)
: -1,
transactionHash: transaction.transactionHash
? transaction.transactionHash
: "",
transactionID: transaction.args?.lockID
? String(transaction.args?.lockID)
: "",
};
return tx;
})
);
transactionHash: transaction.transactionHash
? transaction.transactionHash
: "",
transactionID: decoded.args.lockID ? decoded.args.lockID.toString() : "",
};
txs.push(tx);
}
return txs;
};
const listAllTransactionByWalletAddress = async (
export const listAllTransactionByWalletAddress = async (
walletAddress: string
): Promise<WalletTransaction[]> => {
const p2pContract = getContract(true);
const p2pContract = await getContract(true);
const filterDeposits = p2pContract.filters.DepositAdded([walletAddress]);
const eventsDeposits = await p2pContract.queryFilter(filterDeposits);
const eventsDeposits = await p2pContract.queryFilter(
filterDeposits,
0,
"latest"
);
const filterAddedLocks = p2pContract.filters.LockAdded([walletAddress]);
const eventsAddedLocks = await p2pContract.queryFilter(filterAddedLocks);
const eventsAddedLocks = await p2pContract.queryFilter(
filterAddedLocks,
0,
"latest"
);
const filterReleasedLocks = p2pContract.filters.LockReleased([walletAddress]);
const eventsReleasedLocks = await p2pContract.queryFilter(
filterReleasedLocks
filterReleasedLocks,
0,
"latest"
);
const filterWithdrawnDeposits = p2pContract.filters.DepositWithdrawn([
@@ -135,51 +156,83 @@ const listAllTransactionByWalletAddress = async (
};
// get wallet's release transactions
const listReleaseTransactionByWalletAddress = async (
export const listReleaseTransactionByWalletAddress = async (
walletAddress: string
): Promise<Event[]> => {
const p2pContract = getContract(true);
): Promise<LogDescription[]> => {
const p2pContract = await getContract(true);
const filterReleasedLocks = p2pContract.filters.LockReleased([walletAddress]);
const eventsReleasedLocks = await p2pContract.queryFilter(
filterReleasedLocks
filterReleasedLocks,
0,
"latest"
);
return eventsReleasedLocks.sort((a, b) => {
return b.blockNumber - a.blockNumber;
});
return eventsReleasedLocks
.sort((a, b) => {
return b.blockNumber - a.blockNumber;
})
.map((lock) => {
const IPix2Pix = new Interface(p2pix.abi);
const decoded = IPix2Pix.parseLog({
topics: lock.topics,
data: lock.data,
});
return decoded;
})
.filter((lock) => lock !== null);
};
const listLockTransactionByWalletAddress = async (
walletAddress: string
): Promise<Event[]> => {
const p2pContract = getContract(true);
): Promise<LogDescription[]> => {
const p2pContract = await getContract(true);
const filterAddedLocks = p2pContract.filters.LockAdded([walletAddress]);
const eventsReleasedLocks = await p2pContract.queryFilter(filterAddedLocks);
return eventsReleasedLocks.sort((a, b) => {
return b.blockNumber - a.blockNumber;
});
return eventsReleasedLocks
.sort((a, b) => {
return b.blockNumber - a.blockNumber;
})
.map((lock) => {
const IPix2Pix = new Interface(p2pix.abi);
const decoded = IPix2Pix.parseLog({
topics: lock.topics,
data: lock.data,
});
return decoded;
})
.filter((lock) => lock !== null);
};
const listLockTransactionBySellerAddress = async (
sellerAddress: string
): Promise<Event[]> => {
const p2pContract = getContract(true);
): Promise<LogDescription[]> => {
const p2pContract = await getContract(true);
const filterAddedLocks = p2pContract.filters.LockAdded();
const eventsReleasedLocks = await p2pContract.queryFilter(filterAddedLocks);
return eventsReleasedLocks.filter((lock) =>
lock.args?.seller.toLowerCase() == sellerAddress.toLowerCase()
);
return eventsReleasedLocks
.map((lock) => {
const IPix2Pix = new Interface(p2pix.abi);
const decoded = IPix2Pix.parseLog({
topics: lock.topics,
data: lock.data,
});
return decoded;
})
.filter((lock) => lock !== null)
.filter(
(lock) => lock.args.seller.toLowerCase() == sellerAddress.toLowerCase()
);
};
const checkUnreleasedLock = async (
export const checkUnreleasedLock = async (
walletAddress: string
): Promise<UnreleasedLock | undefined> => {
const p2pContract = getContract();
const p2pContract = await getContract();
const pixData: Pix = {
pixKey: "",
};
@@ -208,8 +261,10 @@ const checkUnreleasedLock = async (
}
};
const getActiveLockAmount = async (walletAddress: string): Promise<number> => {
const p2pContract = getContract();
export const getActiveLockAmount = async (
walletAddress: string
): Promise<number> => {
const p2pContract = await getContract();
const lockSeller = await listLockTransactionBySellerAddress(walletAddress);
const lockStatus = await p2pContract.getLocksStatus(
@@ -233,12 +288,3 @@ const getActiveLockAmount = async (walletAddress: string): Promise<number> => {
return activeLockAmount;
};
export {
updateWalletStatus,
listValidDepositTransactionsByWalletAddress,
listAllTransactionByWalletAddress,
listReleaseTransactionByWalletAddress,
checkUnreleasedLock,
getActiveLockAmount,
};