From a5d72650ee6322e8f32a37b29fa6df050232884f Mon Sep 17 00:00:00 2001 From: RcleydsonR Date: Fri, 10 Feb 2023 16:18:08 -0300 Subject: [PATCH] Add check unreleased lock function --- src/blockchain/wallet.ts | 47 +++++++++++++++++++++++++++++++++++++ src/model/UnreleasedLock.ts | 6 +++++ 2 files changed, 53 insertions(+) create mode 100644 src/model/UnreleasedLock.ts diff --git a/src/blockchain/wallet.ts b/src/blockchain/wallet.ts index 75d0470..ec85e67 100644 --- a/src/blockchain/wallet.ts +++ b/src/blockchain/wallet.ts @@ -9,6 +9,8 @@ import { ethers, type Event } from "ethers"; import { formatEther } from "ethers/lib/utils"; import { getValidDeposits } from "./events"; import type { ValidDeposit } from "@/model/ValidDeposit"; +import type { UnreleasedLock } from "@/model/UnreleasedLock"; +import type { Pix } from "@/model/Pix"; const updateWalletStatus = async (): Promise => { const etherStore = useEtherStore(); @@ -97,9 +99,54 @@ const listReleaseTransactionByWalletAddress = async ( }); }; +const listLockTransactionByWalletAddress = async ( + walletAddress: string +): Promise => { + const p2pContract = getContract(true); + + const filterAddedLocks = p2pContract.filters.LockAdded([walletAddress]); + const eventsReleasedLocks = await p2pContract.queryFilter( + filterAddedLocks + ); + + return eventsReleasedLocks.sort((a, b) => { + return b.blockNumber - a.blockNumber; + }); +}; + +const checkUnreleasedLocks = async ( + walletAddress: string +): Promise => { + const p2pContract = getContract(); + const pixData: Pix = { + pixKey: "", + }; + + const addedLocks = await listLockTransactionByWalletAddress(walletAddress); + const lockStatus = await p2pContract.getLocksStatus(addedLocks.map((lock) => lock.args?.lockID)) + const unreleasedLockId = lockStatus.find((lock: any) => lock.status) + + if (unreleasedLockId){ + const lock = await p2pContract.mapLocks(unreleasedLockId); + + const pixTarget = lock.pixTarget; + const amount = formatEther(lock?.amount); + pixData.pixKey = pixTarget; + pixData.value = Number(amount); + + return { + lockID: unreleasedLockId, + pix: pixData, + }; + } + + return; +}; + export { updateWalletStatus, listValidDepositTransactionsByWalletAddress, listAllTransactionByWalletAddress, listReleaseTransactionByWalletAddress, + checkUnreleasedLocks }; diff --git a/src/model/UnreleasedLock.ts b/src/model/UnreleasedLock.ts new file mode 100644 index 0000000..0be47b2 --- /dev/null +++ b/src/model/UnreleasedLock.ts @@ -0,0 +1,6 @@ +import type { Pix } from "./Pix"; + +export type UnreleasedLock = { + lockID: string; + pix: Pix; +}; \ No newline at end of file