Finished refactoring for Sellet flow.
This commit is contained in:
parent
c4dae86b5f
commit
c90f468d3c
@ -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<any> => {
|
||||
const approveTokens = async (participant: Participant): Promise<any> => {
|
||||
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<any> => {
|
||||
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<any> => {
|
||||
return true;
|
||||
};
|
||||
|
||||
const addDeposit = async (tokenQty: string, pixKey: string): Promise<any> => {
|
||||
const addDeposit = async (): Promise<any> => {
|
||||
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
|
||||
);
|
||||
|
||||
|
@ -40,6 +40,7 @@ const selectedBank = ref<Bank | null>(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 || "",
|
||||
};
|
||||
|
||||
|
@ -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) => {
|
||||
|
@ -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>(Step.Sell);
|
||||
const loading = ref<boolean>(false);
|
||||
|
||||
const seller = ref<Participant>();
|
||||
const sellerId = ref<string>();
|
||||
const showAlert = ref<boolean>(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 () => {
|
||||
/>
|
||||
<div v-if="flowStep == Step.Network">
|
||||
<SendNetwork
|
||||
:sellerId="sellerId"
|
||||
:offer="Number(seller?.offer)"
|
||||
:sellerId="etherStore.sellerId"
|
||||
:offer="Number(etherStore.seller.offer)"
|
||||
:selected-token="etherStore.selectedToken"
|
||||
v-if="!loading"
|
||||
@send-network="sendNetwork"
|
||||
|
Loading…
x
Reference in New Issue
Block a user