implement logic to await all validDeposits correctly and improve state rendering from listing component

This commit is contained in:
RcleydsonR 2022-12-30 02:03:23 -03:00
parent 59c9eabd30
commit 6279acc20d
4 changed files with 94 additions and 65 deletions

View File

@ -12,8 +12,8 @@ const props = defineProps<{
const itemsToShow = ref<any[]>([]); const itemsToShow = ref<any[]>([]);
// Methods // Methods
const showInitialItems = (items: any[]) => { const showInitialItems = () => {
return items.length > 3 ? items.slice(0, 3) : items; itemsToShow.value = props.walletTransactions.slice(0, 3);
}; };
const formatEventsAmount = (amount: any) => { const formatEventsAmount = (amount: any) => {
@ -31,31 +31,27 @@ const openEtherscanUrl = (url: string) => {
const loadMore = () => { const loadMore = () => {
const itemsShowing = itemsToShow.value.length; const itemsShowing = itemsToShow.value.length;
if (props.walletTransactions?.length > itemsShowing + 3)
itemsToShow.value?.push( itemsToShow.value?.push(
...props.walletTransactions.slice(itemsShowing, itemsShowing + 3) ...props.walletTransactions.slice(itemsShowing, itemsShowing + 3)
); );
else itemsToShow.value = props.walletTransactions;
}; };
// watch props changes // watch props changes
watch(props, async () => {
watch(props, (newProps) => {
const itemsToShowQty = itemsToShow.value.length; const itemsToShowQty = itemsToShow.value.length;
if (itemsToShowQty == 0) if (itemsToShowQty == 0) showInitialItems();
itemsToShow.value = showInitialItems(props.walletTransactions);
else else
itemsToShow.value = itemsToShow.value =
newProps?.walletTransactions.length > itemsToShowQty props.walletTransactions.length > itemsToShowQty
? newProps.walletTransactions.slice(0, itemsToShowQty) ? props.walletTransactions.slice(0, itemsToShowQty)
: newProps.walletTransactions; : props.walletTransactions;
}); });
//emits //emits
const emit = defineEmits(["cancelDeposit", "withdrawDeposit"]); const emit = defineEmits(["cancelDeposit", "withdrawDeposit"]);
// initial itemToShow value // initial itemsToShow value
itemsToShow.value = showInitialItems(props.walletTransactions); showInitialItems();
</script> </script>
<template> <template>
@ -79,8 +75,8 @@ itemsToShow.value = showInitialItems(props.walletTransactions);
</div> </div>
<div <div
class="grid grid-cols-4 grid-flow-row w-full bg-white px-6 py-4 rounded-lg" class="grid grid-cols-4 grid-flow-row w-full bg-white px-6 py-4 rounded-lg"
v-for="item in itemsToShow" v-for="(item, index) in itemsToShow"
:key="item?.blockNumber" :key="item.depositID"
> >
<span class="last-release-info"> <span class="last-release-info">
{{ {{
@ -95,7 +91,7 @@ itemsToShow.value = showInitialItems(props.walletTransactions);
<div <div
v-if="props.isManageMode" v-if="props.isManageMode"
class="flex gap-2 cursor-pointer items-center justify-self-center" class="flex gap-2 cursor-pointer items-center justify-self-center"
@click="emit('cancelDeposit', item.depositID)" @click="emit('cancelDeposit', item.depositID, index)"
> >
<span class="last-release-info">Cancelar</span> <span class="last-release-info">Cancelar</span>
<img alt="Cancel image" src="@/assets/cancel.svg" /> <img alt="Cancel image" src="@/assets/cancel.svg" />
@ -125,7 +121,7 @@ itemsToShow.value = showInitialItems(props.walletTransactions);
<div <div
v-if="props.isManageMode" v-if="props.isManageMode"
class="flex gap-2 cursor-pointer items-center justify-self-center" class="flex gap-2 cursor-pointer items-center justify-self-center"
@click="emit('withdrawDeposit', item.depositID)" @click="emit('withdrawDeposit', item.depositID, index)"
> >
<span class="last-release-info">Retirar</span> <span class="last-release-info">Retirar</span>
<img alt="Cancel image" src="@/assets/withdraw.svg" /> <img alt="Cancel image" src="@/assets/withdraw.svg" />
@ -142,11 +138,26 @@ itemsToShow.value = showInitialItems(props.walletTransactions);
<img alt="Redirect image" src="@/assets/redirect.svg" /> <img alt="Redirect image" src="@/assets/redirect.svg" />
</div> </div>
</div> </div>
<div class="flex justify-center w-full mt-2" v-if="itemsToShow.length != 0"> <div
<button type="button" class="text-white" @click="loadMore()"> class="flex flex-col justify-center items-center w-full mt-2 gap-2"
v-if="
itemsToShow.length != 0 &&
itemsToShow.length != props.walletTransactions.length
"
>
<button
type="button"
class="text-white font-semibold"
@click="loadMore()"
>
Carregar mais Carregar mais
</button> </button>
<span class="text-gray-300">
({{ itemsToShow.length }} de
{{ props.walletTransactions.length }} transações)
</span>
</div> </div>
<span class="font-bold text-gray-900" v-if="itemsToShow.length == 0"> <span class="font-bold text-gray-900" v-if="itemsToShow.length == 0">
Não nenhuma transação anterior Não nenhuma transação anterior
</span> </span>

View File

@ -26,6 +26,21 @@ const updateWalletStatus = async () => {
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0])); 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));
};
// Split tokens between wallets in wallets.json // Split tokens between wallets in wallets.json
const splitTokens = async () => { const splitTokens = async () => {
const provider = getProvider(); const provider = getProvider();
@ -101,7 +116,7 @@ const listValidDepositTransactionsByWalletAddress = async (
const walletDeposits = await getValidDeposits(); const walletDeposits = await getValidDeposits();
if (walletDeposits) { if (walletDeposits) {
return walletDeposits return walletDeposits
.filter((deposit) => deposit.seller == String(walletAddress)) .filter((deposit) => deposit.seller == walletAddress)
.sort((a, b) => { .sort((a, b) => {
return b.blockNumber - a.blockNumber; return b.blockNumber - a.blockNumber;
}); });
@ -152,7 +167,9 @@ const getValidDeposits = async (): Promise<any[] | undefined> => {
const window_ = window as any; const window_ = window as any;
const connection = window_.ethereum; const connection = window_.ethereum;
let provider: ethers.providers.Web3Provider | null = null; let provider: ethers.providers.Web3Provider | null = null;
if (!connection) return;
if (!connection) return [];
provider = new ethers.providers.Web3Provider(connection); provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner(); const signer = provider.getSigner();
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer); const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
@ -160,21 +177,26 @@ const getValidDeposits = async (): Promise<any[] | undefined> => {
const filterDeposits = p2pContract.filters.DepositAdded(null); const filterDeposits = p2pContract.filters.DepositAdded(null);
const eventsDeposits = await p2pContract.queryFilter(filterDeposits); const eventsDeposits = await p2pContract.queryFilter(filterDeposits);
const depositList = [] as any[]; const depositList: any[] = await Promise.all(
eventsDeposits
eventsDeposits.forEach(async (deposit) => { .map(async (deposit) => {
const mappedDeposit = await mapDeposits(deposit.args?.depositID); const mappedDeposit = await mapDeposits(deposit.args?.depositID);
let validDeposit = {};
const validDeposit = { if (mappedDeposit.valid) {
validDeposit = {
blockNumber: deposit.blockNumber, blockNumber: deposit.blockNumber,
depositID: deposit.args?.depositID, depositID: deposit.args?.depositID,
remaining: formatBigNumber(mappedDeposit.remaining), remaining: formatBigNumber(mappedDeposit.remaining),
seller: mappedDeposit.seller, seller: mappedDeposit.seller,
pixKey: mappedDeposit.pixTarget, pixKey: mappedDeposit.pixTarget,
}; };
}
if (mappedDeposit.valid) depositList.push(validDeposit); return validDeposit;
}); })
.filter((deposit) => deposit)
);
return depositList; return depositList;
}; };
@ -306,7 +328,7 @@ const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => {
const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer); const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
await contract.cancelDeposit(depositId); await contract.cancelDeposit(depositId);
await updateWalletStatus(); await updateWalletBalance();
await updateValidDeposits(); await updateValidDeposits();
return true; return true;
}; };
@ -321,7 +343,7 @@ const withdrawDeposit = async (depositId: BigNumber): Promise<Boolean> => {
const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer); const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
await contract.withdraw(depositId, []); await contract.withdraw(depositId, []);
await updateWalletStatus(); await updateWalletBalance();
await updateValidDeposits(); await updateValidDeposits();
return true; return true;
}; };

View File

@ -10,9 +10,9 @@ const { walletAddress } = storeToRefs(etherStore);
const allUserTransactions = ref<any[]>([]); const allUserTransactions = ref<any[]>([]);
watch(walletAddress, async (newValue) => { watch(walletAddress, async (newValue) => {
await blockchain await blockchain.listAllTransactionByWalletAddress(newValue).then((res) => {
.listAllTransactionByWalletAddress(newValue) if (res) allUserTransactions.value = res;
.then((res) => {if(res) (allUserTransactions.value = res)}); });
}); });
watch(walletAddress, async (newValue) => { watch(walletAddress, async (newValue) => {

View File

@ -4,49 +4,45 @@ import { storeToRefs } from "pinia";
import blockchain from "../utils/blockchain"; import blockchain from "../utils/blockchain";
import ListingComponent from "@/components/ListingComponent.vue"; import ListingComponent from "@/components/ListingComponent.vue";
import type { BigNumber } from "ethers"; import type { BigNumber } from "ethers";
import { ref } from "vue"; import { ref, watch } from "vue";
const etherStore = useEtherStore(); const etherStore = useEtherStore();
const { walletAddress } = storeToRefs(etherStore); const { walletAddress } = storeToRefs(etherStore);
const depositList = ref<any[]>([]); const depositList = ref<any[]>([]);
const handleCancelDeposit = async (depositID: BigNumber) => { const handleCancelDeposit = async (depositID: BigNumber, index: number) => {
const response = await blockchain.cancelDeposit(depositID); const response = await blockchain.cancelDeposit(depositID);
if (response == true) { if (response == true) {
console.log("Depósito cancelado com sucesso."); console.log("Depósito cancelado com sucesso.");
await blockchain depositList.value.splice(index, 1);
.listValidDepositTransactionsByWalletAddress(walletAddress.value)
.then((deposits) => {
if (deposits) depositList.value = deposits;
});
} }
}; };
const handleWithDrawDeposit = async (depositID: BigNumber) => { const handleWithDrawDeposit = async (depositID: BigNumber, index: number) => {
const response = await blockchain.withdrawDeposit(depositID); const response = await blockchain.withdrawDeposit(depositID);
if (response == true) { if (response == true) {
console.log("Token retirado com sucesso."); console.log("Token retirado com sucesso.");
await blockchain depositList.value.splice(index, 1);
.listValidDepositTransactionsByWalletAddress(walletAddress.value)
.then((deposits) => {
if (deposits) depositList.value = deposits;
});
} }
}; };
if (walletAddress.value) { if (walletAddress.value) {
depositList.value = etherStore.getValidDepositByWalletAddress( const walletDeposits =
await blockchain.listValidDepositTransactionsByWalletAddress(
walletAddress.value walletAddress.value
); );
if (walletDeposits) {
depositList.value = walletDeposits;
}
} }
etherStore.$subscribe(async (_mutation, state) => { watch(walletAddress, async () => {
if (state.walletAddress != "" && state.depositsValidList.length > 0) { const walletDeposits =
await blockchain await blockchain.listValidDepositTransactionsByWalletAddress(
.listValidDepositTransactionsByWalletAddress(walletAddress.value) walletAddress.value
.then((deposits) => { );
if (deposits) depositList.value = deposits; if (walletDeposits) {
}); depositList.value = walletDeposits;
} }
}); });
</script> </script>