diff --git a/src/blockchain/addresses.ts b/src/blockchain/addresses.ts index 4619322..ef486b5 100644 --- a/src/blockchain/addresses.ts +++ b/src/blockchain/addresses.ts @@ -1,7 +1,7 @@ import { useEtherStore } from "@/store/ether"; import { NetworkEnum } from "@/model/NetworkEnum"; -const getTokenAddress = (): string => { +const getTokenAddress = (network?: NetworkEnum): string => { const etherStore = useEtherStore(); const possibleTokenAddresses: { [key: string]: string } = { @@ -9,10 +9,10 @@ const getTokenAddress = (): string => { Polygon: "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29", }; - return possibleTokenAddresses[etherStore.networkName]; + return possibleTokenAddresses[network ? network : etherStore.networkName]; }; -const getP2PixAddress = (): string => { +const getP2PixAddress = (network?: NetworkEnum): string => { const etherStore = useEtherStore(); const possibleP2PixAddresses: { [key: string]: string } = { @@ -20,7 +20,7 @@ const getP2PixAddress = (): string => { Polygon: "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00", }; - return possibleP2PixAddresses[etherStore.networkName]; + return possibleP2PixAddresses[network ? network : etherStore.networkName]; }; const getProviderUrl = (): string => { diff --git a/src/blockchain/events.ts b/src/blockchain/events.ts index 503fd1f..75615a2 100644 --- a/src/blockchain/events.ts +++ b/src/blockchain/events.ts @@ -5,6 +5,8 @@ import p2pix from "../utils/smart_contract_files/P2PIX.json"; import { formatEther } from "ethers/lib/utils"; import { getContract } from "./provider"; import type { ValidDeposit } from "@/model/ValidDeposit"; +import { getP2PixAddress, getTokenAddress } from "./addresses"; +import { NetworkEnum } from "@/model/NetworkEnum"; const getNetworksLiquidity = async (): Promise => { const etherStore = useEtherStore(); @@ -20,23 +22,23 @@ const getNetworksLiquidity = async (): Promise => { ); // mumbai provider const p2pContractGoerli = new ethers.Contract( - "0x2414817FF64A114d91eCFA16a834d3fCf69103d4", + getP2PixAddress(NetworkEnum.ethereum), p2pix.abi, goerliProvider ); const p2pContractMumbai = new ethers.Contract( - "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00", + getP2PixAddress(NetworkEnum.polygon), p2pix.abi, mumbaiProvider ); const depositListGoerli = await getValidDeposits( - "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00", + getTokenAddress(NetworkEnum.ethereum), p2pContractGoerli ); const depositListMumbai = await getValidDeposits( - "0xC86042E9F2977C62Da8c9dDF7F9c40fde4796A29", + getTokenAddress(NetworkEnum.polygon), p2pContractMumbai ); diff --git a/src/blockchain/wallet.ts b/src/blockchain/wallet.ts index e708e77..a4ac1fa 100644 --- a/src/blockchain/wallet.ts +++ b/src/blockchain/wallet.ts @@ -112,7 +112,7 @@ const listLockTransactionByWalletAddress = async ( }); }; -const checkUnreleasedLocks = async ( +const checkUnreleasedLock = async ( walletAddress: string ): Promise => { const p2pContract = getContract(); @@ -124,18 +124,22 @@ const checkUnreleasedLocks = async ( const lockStatus = await p2pContract.getLocksStatus( addedLocks.map((lock) => lock.args?.lockID) ); - const unreleasedLockId = lockStatus.find((lock: any) => lock.status); + const unreleasedLockId = lockStatus[1].findIndex( + (lockStatus: number) => lockStatus == 1 + ); - if (unreleasedLockId) { - const lock = await p2pContract.mapLocks(unreleasedLockId); + console.log(lockStatus); + if (unreleasedLockId != -1) { + const _lockID = lockStatus[0][unreleasedLockId]; + const lock = await p2pContract.mapLocks(_lockID); const pixTarget = lock.pixTarget; const amount = formatEther(lock?.amount); - pixData.pixKey = pixTarget; + pixData.pixKey = String(Number(pixTarget)); pixData.value = Number(amount); return { - lockID: unreleasedLockId, + lockID: _lockID, pix: pixData, }; } @@ -148,5 +152,5 @@ export { listValidDepositTransactionsByWalletAddress, listAllTransactionByWalletAddress, listReleaseTransactionByWalletAddress, - checkUnreleasedLocks, + checkUnreleasedLock, }; diff --git a/src/components/QrCodeComponent.vue b/src/components/QrCodeComponent.vue index 20de029..2e9357f 100644 --- a/src/components/QrCodeComponent.vue +++ b/src/components/QrCodeComponent.vue @@ -169,7 +169,7 @@ onUnmounted(() => { /> diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 907856a..0b8b932 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -2,7 +2,7 @@ import SearchComponent from "@/components/SearchComponent.vue"; import LoadingComponent from "@/components/LoadingComponent/LoadingComponent.vue"; import BuyConfirmedComponent from "@/components/BuyConfirmedComponent/BuyConfirmedComponent.vue"; -import { ref, onMounted } from "vue"; +import { ref, onMounted, watch } from "vue"; import { useEtherStore } from "@/store/ether"; import QrCodeComponent from "@/components/QrCodeComponent.vue"; import CustomModal from "@/components/CustomModal/CustomModal.vue"; @@ -11,6 +11,7 @@ import { addLock, releaseLock } from "@/blockchain/buyerMethods"; import { updateWalletStatus, listReleaseTransactionByWalletAddress, + checkUnreleasedLock, } from "@/blockchain/wallet"; import { getNetworksLiquidity } from "@/blockchain/events"; import type { Event } from "ethers"; @@ -26,7 +27,7 @@ const etherStore = useEtherStore(); etherStore.setSellerView(false); // States -const { loadingLock, walletAddress } = storeToRefs(etherStore); +const { loadingLock, walletAddress, networkName } = storeToRefs(etherStore); const flowStep = ref(Step.Search); const pixTarget = ref(); const tokenAmount = ref(); @@ -86,30 +87,31 @@ const releaseTransaction = async (e2eId: string) => { } }; -// const checkForUnreleasedLocks = async () => { -// const walletLocks = await checkUnreleasedLocks(walletAddress.value); -// if (walletLocks) { -// lockID.value = walletLocks.lockID; -// tokenAmount.value = walletLocks.pix.value; -// pixTarget.value = Number(walletLocks.pix.pixKey); -// showModal.value = true; -// } else { -// flowStep.value = Step.Search; -// showModal.value = false; -// } -// }; +const checkForUnreleasedLocks = async (): Promise => { + const walletLocks = await checkUnreleasedLock(walletAddress.value); + console.log(walletLocks); + if (walletLocks) { + lockID.value = walletLocks.lockID; + tokenAmount.value = walletLocks.pix.value; + pixTarget.value = Number(walletLocks.pix.pixKey); + showModal.value = true; + } else { + flowStep.value = Step.Search; + showModal.value = false; + } +}; -// if (walletAddress) { -// await checkForUnreleasedLocks(); -// } +if (walletAddress.value) { + await checkForUnreleasedLocks(); +} -// watch(walletAddress, async () => { -// await checkForUnreleasedLocks(); -// }); +watch(walletAddress, async () => { + await checkForUnreleasedLocks(); +}); -// watch(networkName, async () => { -// await checkForUnreleasedLocks(); -// }); +watch(networkName, async () => { + if (walletAddress.value) await checkForUnreleasedLocks(); +}); onMounted(async () => { await getNetworksLiquidity(); diff --git a/src/views/SellerView.vue b/src/views/SellerView.vue index 97dc494..ac67dc5 100644 --- a/src/views/SellerView.vue +++ b/src/views/SellerView.vue @@ -23,7 +23,10 @@ const offerValue = ref(""); const pixKeyBuyer = ref(""); // Verificar tipagem -const approveOffer = async (args: { offer: string; postProcessedPixKey: string }) => { +const approveOffer = async (args: { + offer: string; + postProcessedPixKey: string; +}) => { loading.value = true; try { offerValue.value = args.offer;