From d686c0b9bc606c1177d59174f7c12018b2b07768 Mon Sep 17 00:00:00 2001 From: RcleydsonR Date: Fri, 24 Feb 2023 00:50:32 -0300 Subject: [PATCH] add method to get active lock amount from a seller wallet --- src/blockchain/wallet.ts | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/blockchain/wallet.ts b/src/blockchain/wallet.ts index 15271d8..e896b05 100644 --- a/src/blockchain/wallet.ts +++ b/src/blockchain/wallet.ts @@ -155,6 +155,22 @@ const listLockTransactionByWalletAddress = async ( }); }; +const listLockTransactionBySellerAddress = async ( + sellerAddress: string +): Promise => { + const p2pContract = getContract(true); + + const filterAddedLocks = p2pContract.filters.LockAdded(); + const eventsReleasedLocks = await p2pContract.queryFilter(filterAddedLocks); + + return eventsReleasedLocks.filter((lock) => + lock.args?.seller + .toHexString() + .substring(3) + .includes(sellerAddress.substring(2).toLowerCase()) + ); +}; + const checkUnreleasedLock = async ( walletAddress: string ): Promise => { @@ -187,10 +203,37 @@ const checkUnreleasedLock = async ( } }; +const getActiveLockAmount = async (walletAddress: string): Promise => { + const p2pContract = getContract(); + const lockSeller = await listLockTransactionBySellerAddress(walletAddress); + + const lockStatus = await p2pContract.getLocksStatus( + lockSeller.map((lock) => lock.args?.lockID) + ); + + const activeLockAmount = await lockStatus[1].reduce( + async (sumValue: Promise, currentStatus: number, index: number) => { + const currValue = await sumValue; + let valueToSum = 0; + + if (currentStatus == 1) { + const lock = await p2pContract.mapLocks(lockStatus[0][index]); + valueToSum = Number(formatEther(lock?.amount)); + } + + return currValue + valueToSum; + }, + Promise.resolve(0) + ); + + return activeLockAmount; +}; + export { updateWalletStatus, listValidDepositTransactionsByWalletAddress, listAllTransactionByWalletAddress, listReleaseTransactionByWalletAddress, checkUnreleasedLock, + getActiveLockAmount, };