From c90f468d3c325bf259864dce669c5503b6f3928e Mon Sep 17 00:00:00 2001 From: Filipe Soccol Date: Mon, 2 Dec 2024 12:17:47 -0300 Subject: [PATCH] Finished refactoring for Sellet flow. --- src/blockchain/sellerMethods.ts | 17 +++++++++++------ src/components/SellerSteps/SellerComponent.vue | 4 ++-- src/store/ether.ts | 9 +++++++++ src/views/SellerView.vue | 17 ++++++----------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/blockchain/sellerMethods.ts b/src/blockchain/sellerMethods.ts index b67633a..fd96b06 100644 --- a/src/blockchain/sellerMethods.ts +++ b/src/blockchain/sellerMethods.ts @@ -5,12 +5,14 @@ import { encodeBytes32String, Contract, parseEther } from "ethers"; import mockToken from "../utils/smart_contract_files/MockToken.json"; import { useEtherStore } from "@/store/ether"; +import { createParticipant, Participant } from "@/utils/bbPay"; -const approveTokens = async (tokenQty: string): Promise => { +const approveTokens = async (participant: Participant): Promise => { const provider = getProvider(); const signer = await provider?.getSigner(); const etherStore = useEtherStore(); + etherStore.setSeller(participant); const tokenContract = new Contract( getTokenAddress(etherStore.selectedToken), mockToken.abi, @@ -22,11 +24,11 @@ const approveTokens = async (tokenQty: string): Promise => { await signer?.getAddress(), getP2PixAddress() ); - if (approved < parseEther(tokenQty)) { + if (approved < parseEther(participant.offer)) { // Approve tokens const apprv = await tokenContract.approve( getP2PixAddress(), - parseEther(tokenQty) + parseEther(participant.offer) ); await apprv.wait(); return true; @@ -34,15 +36,18 @@ const approveTokens = async (tokenQty: string): Promise => { return true; }; -const addDeposit = async (tokenQty: string, pixKey: string): Promise => { +const addDeposit = async (): Promise => { const p2pContract = await getContract(); const etherStore = useEtherStore(); + const sellerId = await createParticipant(etherStore.seller); + etherStore.setSellerId(sellerId.id); + const deposit = await p2pContract.deposit( - pixKey, + sellerId, encodeBytes32String(""), getTokenAddress(etherStore.selectedToken), - parseEther(tokenQty), + parseEther(etherStore.seller.offer), true ); diff --git a/src/components/SellerSteps/SellerComponent.vue b/src/components/SellerSteps/SellerComponent.vue index 2f3713e..9eaab1a 100644 --- a/src/components/SellerSteps/SellerComponent.vue +++ b/src/components/SellerSteps/SellerComponent.vue @@ -40,6 +40,7 @@ const selectedBank = ref(null); // Import the bank list import bankList from "@/utils/files/isbpList.json"; +import { Participant } from "@/utils/bbPay"; const filteredBanks = computed(() => { if (!bankSearchQuery.value) return []; @@ -70,7 +71,7 @@ const handleSubmit = (e: Event): void => { const processedIdentification = postProcessKey(identification.value); - const data = { + const data: Participant = { offer: offer.value, fullName: fullName.value, identification: processedIdentification, @@ -78,7 +79,6 @@ const handleSubmit = (e: Event): void => { accountType: accountType.value, account: account.value, branch: branch.value, - savingsVariation: savingsVariation.value || "", }; diff --git a/src/store/ether.ts b/src/store/ether.ts index c4bba46..f908abb 100644 --- a/src/store/ether.ts +++ b/src/store/ether.ts @@ -1,5 +1,6 @@ import { NetworkEnum, TokenEnum } from "../model/NetworkEnum"; import type { ValidDeposit } from "@/model/ValidDeposit"; +import { Participant } from "@/utils/bbPay"; import { defineStore } from "pinia"; export const useEtherStore = defineStore("ether", { @@ -13,6 +14,8 @@ export const useEtherStore = defineStore("ether", { depositsValidList: [] as ValidDeposit[], loadingWalletTransactions: false, loadingNetworkLiquidity: false, + seller: {} as Participant, + sellerId: "", }), actions: { setWalletAddress(walletAddress: string) { @@ -42,6 +45,12 @@ export const useEtherStore = defineStore("ether", { setLoadingNetworkLiquidity(isLoadingNetworkLiquidity: boolean) { this.loadingNetworkLiquidity = isLoadingNetworkLiquidity; }, + setSeller(seller: Participant) { + this.seller = seller; + }, + setSellerId(sellerId: string) { + this.sellerId = sellerId; + }, }, getters: { getValidDepositByWalletAddress: (state) => { diff --git a/src/views/SellerView.vue b/src/views/SellerView.vue index 489ad30..02c9ad1 100644 --- a/src/views/SellerView.vue +++ b/src/views/SellerView.vue @@ -7,7 +7,7 @@ import { approveTokens, addDeposit } from "@/blockchain/sellerMethods"; import { ref } from "vue"; import { useEtherStore } from "@/store/ether"; import CustomAlert from "@/components/CustomAlert/CustomAlert.vue"; -import { createParticipant, Participant } from "@/utils/bbPay"; +import { Participant } from "@/utils/bbPay"; enum Step { Search, @@ -21,16 +21,13 @@ etherStore.setSellerView(true); const flowStep = ref(Step.Sell); const loading = ref(false); -const seller = ref(); -const sellerId = ref(); const showAlert = ref(false); // Verificar tipagem const approveOffer = async (args: Participant) => { loading.value = true; try { - seller.value = args; - await approveTokens(args.offer); + await approveTokens(args); flowStep.value = Step.Network; loading.value = false; } catch (err) { @@ -43,10 +40,8 @@ const approveOffer = async (args: Participant) => { const sendNetwork = async () => { loading.value = true; try { - if (seller.value) { - const participantWithId = await createParticipant(seller.value); - sellerId.value = participantWithId.id; - await addDeposit(String(seller.value.offer), participantWithId.id); + if (etherStore.seller) { + await addDeposit(); flowStep.value = Step.Sell; loading.value = false; showAlert.value = true; @@ -75,8 +70,8 @@ const sendNetwork = async () => { />