add withdraw deposit function

This commit is contained in:
RcleydsonR 2022-12-27 17:25:47 -03:00
parent 0e8ca7f803
commit c6067fb4eb
3 changed files with 67 additions and 28 deletions

View File

@ -77,15 +77,16 @@ const emit = defineEmits(["cancelDeposit", "withdrawDeposit"]);
:key="item?.blockNumber" :key="item?.blockNumber"
> >
<span class="last-release-info"> <span class="last-release-info">
{{ formatEventsAmount(item?.args.amount) }} BRZ {{ formatEventsAmount(item?.args ? item?.args.amount : item?.remaining) }} BRZ
</span> </span>
<span class="last-release-info"> 20 out 2022 </span> <!-- TODO: change this hardcoded date -->
<span class="last-release-info"> 20 out 2022 </span>
<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.args.depositID)" @click="emit('cancelDeposit', item.depositID)"
> >
<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" />
@ -115,7 +116,7 @@ const emit = defineEmits(["cancelDeposit", "withdrawDeposit"]);
<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')" @click="emit('withdrawDeposit', item.depositID)"
> >
<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" />

View File

@ -93,6 +93,25 @@ const listDepositTransactionByWalletAddress = async (
}); });
}; };
// get wallet's deposit transactions
// const listValidDepositTransactionsByWalletAddress = async (
// walletAddress: string
// ): Promise<any[]> => {
// const provider = getProvider();
// const etherStore = useEtherStore();
// if (!provider) return [];
// const signer = provider.getSigner();
// const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
// const filterDeposits = p2pContract.filters.DepositAdded([walletAddress]);
// const eventsDeposits = await p2pContract.queryFilter(filterDeposits);
// console.log()
// };
// get wallet's lock transactions // get wallet's lock transactions
const listLockTransactionByWalletAddress = async ( const listLockTransactionByWalletAddress = async (
walletAddress: string walletAddress: string
@ -155,10 +174,10 @@ const updateValidDeposits = async () => {
remaining: formatBigNumber(mappedDeposit.remaining), remaining: formatBigNumber(mappedDeposit.remaining),
seller: mappedDeposit.seller, seller: mappedDeposit.seller,
pixKey: mappedDeposit.pixTarget, pixKey: mappedDeposit.pixTarget,
valid: mappedDeposit.valid,
}; };
depositList.push(validDeposit); if (mappedDeposit.valid)
depositList.push(validDeposit);
}); });
etherStore.setDepositsValidList(depositList); etherStore.setDepositsValidList(depositList);
@ -275,7 +294,7 @@ const addDeposit = async (tokenQty: Number, pixKey: string) => {
await updateValidDeposits(); await updateValidDeposits();
}; };
// cancel a deposit by ots Id // cancel a deposit by its Id
const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => { const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => {
const provider = getProvider(); const provider = getProvider();
@ -286,7 +305,21 @@ const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => {
await contract.cancelDeposit(depositId); await contract.cancelDeposit(depositId);
await updateWalletStatus(); await updateWalletStatus();
await updateDepositAddedEvents(); await updateValidDeposits();
return true;
};
// withdraw a deposit by its Id
const withdrawDeposit = async (depositId: BigNumber): Promise<Boolean> => {
const provider = getProvider();
if (!provider) return false;
const signer = provider.getSigner();
const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
await contract.withdraw(depositId, []);
await updateWalletStatus();
await updateValidDeposits(); await updateValidDeposits();
return true; return true;
}; };
@ -409,6 +442,7 @@ export default {
listLockTransactionByWalletAddress, listLockTransactionByWalletAddress,
addDeposit, addDeposit,
cancelDeposit, cancelDeposit,
withdrawDeposit,
mapDeposits, mapDeposits,
formatBigNumber, formatBigNumber,
addLock, addLock,

View File

@ -7,46 +7,50 @@ import type { BigNumber } from "ethers";
import { ref } from "vue"; import { ref } from "vue";
const etherStore = useEtherStore(); const etherStore = useEtherStore();
const { walletAddress } = storeToRefs(etherStore); const { walletAddress, depositsValidList } = storeToRefs(etherStore);
const depositList = ref<any[]>([]); const depositList = ref<any[]>([]);
if (walletAddress.value) { const listValidDepositTransactionsByWalletAddress = (walletAddress: string, deposits: any[]) => {
await blockchain depositList.value = deposits.filter((deposit) => deposit.seller === walletAddress)
.listDepositTransactionByWalletAddress(walletAddress.value)
.then((value) => (depositList.value = value));
} }
const handleCancelDeposit = async (depositID: BigNumber) => { const handleCancelDeposit = async (depositID: BigNumber) => {
console.log(depositID);
const response = await blockchain.cancelDeposit(depositID); const response = await blockchain.cancelDeposit(depositID);
if (response == true) console.log("Depósito cancelado com sucesso."); if (response == true) console.log("Depósito cancelado com sucesso.");
}; };
etherStore.$subscribe(async (mutation, state) => { const handleWithDrawDeposit = async (depositID: BigNumber) => {
if (mutation.events.key == "walletAddress") { const response = await blockchain.withdrawDeposit(depositID);
if (state.walletAddress) { if (response == true) console.log("Token retirado com sucesso.");
await blockchain };
.listDepositTransactionByWalletAddress(state.walletAddress)
.then((value) => (depositList.value = value)); if (walletAddress.value) {
} listValidDepositTransactionsByWalletAddress(walletAddress.value, depositsValidList.value)
} }
etherStore.$subscribe((_mutation, state) => {
if(state.walletAddress != "")
listValidDepositTransactionsByWalletAddress(state.walletAddress, state.depositsValidList)
}); });
</script> </script>
<template> <template>
<div class="page"> <div class="page">
<div class="header">Gerenciar ofertas</div> <div class="header">Gerenciar ofertas</div>
<ListingComponent <div class="w-full max-w-4xl">
:wallet-transactions="depositList" <ListingComponent
:is-manage-mode="true" :wallet-transactions="depositList"
@cancel-deposit="handleCancelDeposit" :is-manage-mode="true"
></ListingComponent> @cancel-deposit="handleCancelDeposit"
@withdraw-deposit="handleWithDrawDeposit"
></ListingComponent>
</div>
</div> </div>
</template> </template>
<style scoped> <style scoped>
.page { .page {
@apply flex flex-col gap-10 mt-20 w-full; @apply flex flex-col items-center gap-10 mt-20 w-full;
} }
.header { .header {