From 9fa2b34a5d5bccbf8599e54e8281fbc4ee4c79c7 Mon Sep 17 00:00:00 2001 From: Filipe Soccol Date: Tue, 1 Apr 2025 12:04:24 -0300 Subject: [PATCH] Update all to useUSer composabe. Still some bugs to resolve. --- package.json | 4 +- src/App.vue | 4 + src/blockchain/__tests__/addresses.spec.ts | 30 ++-- src/blockchain/addresses.ts | 18 +- src/blockchain/buyerMethods.ts | 160 +++++------------- src/blockchain/events.ts | 26 ++- src/blockchain/provider.ts | 19 ++- src/blockchain/sellerMethods.ts | 44 ++--- src/blockchain/wallet.ts | 65 +++---- .../BuyConfirmedComponent.vue | 20 +-- .../ListingComponent/ListingComponent.vue | 17 +- .../__tests__/ListingComponent.spec.ts | 4 +- src/components/SearchComponent.vue | 9 +- .../SellerSteps/SellerComponent.vue | 9 +- .../SellerSteps/SellerSearchComponent.vue | 11 +- src/components/ToasterComponent.vue | 106 ++++++++++++ src/components/TopBar/TopBar.vue | 28 +-- .../TopBar/__tests__/TopBar.spec.ts | 6 +- src/composables/useUser.ts | 103 +++++++++++ src/main.ts | 2 - src/store/viem.ts | 65 ------- src/utils/imagesPath.ts | 17 +- src/views/HomeView.vue | 20 +-- src/views/ManageBidsView.vue | 11 +- src/views/SellerView.vue | 16 +- yarn.lock | 135 ++++++++------- 26 files changed, 495 insertions(+), 454 deletions(-) create mode 100644 src/components/ToasterComponent.vue create mode 100644 src/composables/useUser.ts delete mode 100644 src/store/viem.ts diff --git a/package.json b/package.json index 29fbb6d..54388ec 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,8 @@ "crc": "^3.8.0", "ethers": "^6.13.4", "marked": "^4.2.12", - "pinia": "^2.0.23", "qrcode": "^1.5.1", - "viem": "2.x", + "viem": "^2.24.3", "vite-svg-loader": "^5.1.0", "vue": "^3.2.41", "vue-markdown": "^2.2.4", @@ -36,7 +35,6 @@ "devDependencies": { "@babel/preset-env": "^7.20.2", "@babel/preset-typescript": "^7.18.6", - "@pinia/testing": "^0.0.14", "@rushstack/eslint-patch": "^1.1.4", "@types/crc": "^3.8.0", "@types/jest": "^27.0.0", diff --git a/src/App.vue b/src/App.vue index 02ab545..b9816b9 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,13 +2,16 @@ import { useRoute } from "vue-router"; import TopBar from "@/components/TopBar/TopBar.vue"; import SpinnerComponent from "@/components/SpinnerComponent.vue"; +import ToasterComponent from "@/components/ToasterComponent.vue"; import { init, useOnboard } from "@web3-onboard/vue"; import injectedModule from "@web3-onboard/injected-wallets"; import { Networks } from "./model/Networks"; import { NetworkEnum } from "./model/NetworkEnum"; +import { ref } from "vue"; const route = useRoute(); const injected = injectedModule(); +const targetNetwork = ref(NetworkEnum.sepolia); const web3Onboard = init({ wallets: [injected], @@ -58,5 +61,6 @@ if (!connectedWallet) { + diff --git a/src/blockchain/__tests__/addresses.spec.ts b/src/blockchain/__tests__/addresses.spec.ts index a8bd566..8ea10ec 100644 --- a/src/blockchain/__tests__/addresses.spec.ts +++ b/src/blockchain/__tests__/addresses.spec.ts @@ -8,7 +8,7 @@ import { import { setActivePinia, createPinia } from "pinia"; import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; describe("addresses.ts types", () => { it("My addresses.ts types work properly", () => { @@ -25,16 +25,16 @@ describe("addresses.ts functions", () => { }); it("getTokenAddress Ethereum", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.sepolia); + const user = useUser(); + user.setNetworkId(NetworkEnum.sepolia); expect(getTokenAddress(TokenEnum.BRZ)).toBe( "0x4A2886EAEc931e04297ed336Cc55c4eb7C75BA00" ); }); it("getTokenAddress Rootstock", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.rootstock); + const user = useUser(); + user.setNetworkId(NetworkEnum.rootstock); expect(getTokenAddress(TokenEnum.BRZ)).toBe( "0xfE841c74250e57640390f46d914C88d22C51e82e" ); @@ -47,16 +47,16 @@ describe("addresses.ts functions", () => { }); it("getP2PixAddress Ethereum", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.sepolia); + const user = useUser(); + user.setNetworkId(NetworkEnum.sepolia); expect(getP2PixAddress()).toBe( "0x2414817FF64A114d91eCFA16a834d3fCf69103d4" ); }); it("getP2PixAddress Rootstock", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.rootstock); + const user = useUser(); + user.setNetworkId(NetworkEnum.rootstock); expect(getP2PixAddress()).toBe( "0x98ba35eb14b38D6Aa709338283af3e922476dE34" ); @@ -69,14 +69,14 @@ describe("addresses.ts functions", () => { }); it("getProviderUrl Ethereum", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.sepolia); + const user = useUser(); + user.setNetworkId(NetworkEnum.sepolia); expect(getProviderUrl()).toBe(import.meta.env.VITE_GOERLI_API_URL); }); it("getProviderUrl Rootstock", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.rootstock); + const user = useUser(); + user.setNetworkId(NetworkEnum.rootstock); expect(getProviderUrl()).toBe(import.meta.env.VITE_ROOTSTOCK_API_URL); }); @@ -85,8 +85,8 @@ describe("addresses.ts functions", () => { }); it("isPossibleNetwork Returns", () => { - const etherStore = useViemStore(); - etherStore.setNetworkId(NetworkEnum.sepolia); + const user = useUser(); + user.setNetworkId(NetworkEnum.sepolia); expect(isPossibleNetwork(0x5 as NetworkEnum)).toBe(true); expect(isPossibleNetwork(5 as NetworkEnum)).toBe(true); expect(isPossibleNetwork(0x13881 as NetworkEnum)).toBe(true); diff --git a/src/blockchain/addresses.ts b/src/blockchain/addresses.ts index 8059b8f..c812e9f 100644 --- a/src/blockchain/addresses.ts +++ b/src/blockchain/addresses.ts @@ -1,4 +1,4 @@ -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { NetworkEnum, TokenEnum } from "@/model/NetworkEnum"; import { createPublicClient, http } from "viem"; import { sepolia, rootstock } from "viem/chains"; @@ -15,8 +15,8 @@ const Tokens: { [key in NetworkEnum]: { [key in TokenEnum]: string } } = { }; export const getTokenByAddress = (address: string) => { - const viemStore = useViemStore(); - const networksTokens = Tokens[viemStore.networkName]; + const user = useUser(); + const networksTokens = Tokens[user.networkName.value]; for (const [token, tokenAddress] of Object.entries(networksTokens)) { if (tokenAddress.toLowerCase() === address.toLowerCase()) { return token; @@ -29,28 +29,28 @@ export const getTokenAddress = ( token: TokenEnum, network?: NetworkEnum ): string => { - const viemStore = useViemStore(); - return Tokens[network ? network : viemStore.networkName][token]; + const user = useUser(); + return Tokens[network ? network : user.networkName.value][token]; }; export const getP2PixAddress = (network?: NetworkEnum): string => { - const viemStore = useViemStore(); + const user = useUser(); const possibleP2PixAddresses: { [key in NetworkEnum]: string } = { [NetworkEnum.sepolia]: "0x2414817FF64A114d91eCFA16a834d3fCf69103d4", [NetworkEnum.rootstock]: "0x98ba35eb14b38D6Aa709338283af3e922476dE34", }; - return possibleP2PixAddresses[network ? network : viemStore.networkName]; + return possibleP2PixAddresses[network ? network : user.networkName.value]; }; export const getProviderUrl = (network?: NetworkEnum): string => { - const viemStore = useViemStore(); + const user = useUser(); const possibleProvidersUrls: { [key in NetworkEnum]: string } = { [NetworkEnum.sepolia]: import.meta.env.VITE_SEPOLIA_API_URL, [NetworkEnum.rootstock]: import.meta.env.VITE_RSK_API_URL, }; - return possibleProvidersUrls[network || viemStore.networkName]; + return possibleProvidersUrls[network || user.networkName.value]; }; export const getProviderByNetwork = (network: NetworkEnum) => { diff --git a/src/blockchain/buyerMethods.ts b/src/blockchain/buyerMethods.ts index e0f2f8b..991fcaa 100644 --- a/src/blockchain/buyerMethods.ts +++ b/src/blockchain/buyerMethods.ts @@ -1,141 +1,61 @@ -import { getContract, getWalletClient } from "./provider"; +import { getContract } from "./provider"; import { getTokenAddress } from "./addresses"; -import { parseEther, stringToHex, toHex } from "viem"; - +import { parseEther } from "viem"; import type { TokenEnum } from "@/model/NetworkEnum"; -import { createSolicitation } from "../utils/bbPay"; -import type { Offer } from "../utils/bbPay"; -const addLock = async ( - sellerId: string, - token: string, +export const addLock = async ( + sellerAddress: string, + tokenAddress: string, amount: number ): Promise => { const { address, abi, client } = await getContract(); - const walletClient = getWalletClient(); - - if (!walletClient) { - throw new Error("Wallet client not initialized"); - } - - const [account] = await walletClient.getAddresses(); - - const hash = await walletClient.writeContract({ + + const parsedAmount = parseEther(amount.toString()); + + const { request } = await client.simulateContract({ address, abi, - functionName: 'lock', - args: [ - sellerId, - token, - parseEther(String(amount)), - [], - [] - ], - account + functionName: "addLock", + args: [sellerAddress, tokenAddress, parsedAmount], }); - + + const hash = await client.writeContract(request); const receipt = await client.waitForTransactionReceipt({ hash }); - const logs = receipt.logs; - - // Extract the lockID from transaction logs - // This is a simplified approach - in production you'll want more robust log parsing - const lockId = logs[0].topics[2]; // Simplified - adjust based on actual event structure - - const offer: Offer = { - amount, - lockId: String(lockId), - sellerId: sellerId, - }; - - await createSolicitation(offer); - - return String(lockId); + + return receipt.status ? receipt.logs[0].topics[2] : ""; }; -const releaseLock = async ( - pixKey: string, - amount: number, - e2eId: string, - lockId: string -): Promise => { - const { address, abi, client } = await getContract(); - const walletClient = getWalletClient(); - - if (!walletClient) { - throw new Error("Wallet client not initialized"); - } - - const [account] = await walletClient.getAddresses(); - - // In a real implementation, you would get this signature from your backend - // This is just a placeholder for the mock implementation - const signature = "0x1234567890"; - - const hash = await walletClient.writeContract({ - address, - abi, - functionName: 'release', - args: [ - BigInt(lockId), - toHex(e2eId, { size: 32 }), - signature - ], - account - }); - - const receipt = await client.waitForTransactionReceipt({ hash }); - return receipt; -}; - -const cancelDeposit = async (depositId: bigint): Promise => { - const { address, abi, client } = await getContract(); - const walletClient = getWalletClient(); - - if (!walletClient) { - throw new Error("Wallet client not initialized"); - } - - const [account] = await walletClient.getAddresses(); - - const hash = await walletClient.writeContract({ - address, - abi, - functionName: 'cancelDeposit', - args: [depositId], - account - }); - - const receipt = await client.waitForTransactionReceipt({ hash }); - return receipt; -}; - -const withdrawDeposit = async ( +export const withdrawDeposit = async ( amount: string, token: TokenEnum -): Promise => { +): Promise => { const { address, abi, client } = await getContract(); - const walletClient = getWalletClient(); - - if (!walletClient) { - throw new Error("Wallet client not initialized"); - } - - const [account] = await walletClient.getAddresses(); - - const hash = await walletClient.writeContract({ + + const tokenAddress = getTokenAddress(token); + + const { request } = await client.simulateContract({ address, abi, - functionName: 'withdraw', - args: [ - getTokenAddress(token), - parseEther(String(amount)), - [] - ], - account + functionName: "withdrawDeposit", + args: [tokenAddress, parseEther(amount)], }); - + + const hash = await client.writeContract(request); const receipt = await client.waitForTransactionReceipt({ hash }); - return receipt; + + return receipt.status; }; -export { cancelDeposit, withdrawDeposit, addLock, releaseLock }; +export const releaseLock = async (solicitation: any): Promise => { + const { address, abi, client } = await getContract(); + + const { request } = await client.simulateContract({ + address, + abi, + functionName: "releaseLock", + args: [solicitation.lockId, solicitation.e2eId], + }); + + const hash = await client.writeContract(request); + return client.waitForTransactionReceipt({ hash }); +}; diff --git a/src/blockchain/events.ts b/src/blockchain/events.ts index 481d3ee..51d5270 100644 --- a/src/blockchain/events.ts +++ b/src/blockchain/events.ts @@ -1,4 +1,4 @@ -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { formatEther, decodeEventLog, parseAbi, toHex, type PublicClient, type Address } from "viem"; import p2pix from "@/utils/smart_contract_files/P2PIX.json"; @@ -14,8 +14,8 @@ import type { UnreleasedLock } from "@/model/UnreleasedLock"; import type { Pix } from "@/model/Pix"; const getNetworksLiquidity = async (): Promise => { - const viemStore = useViemStore(); - viemStore.setLoadingNetworkLiquidity(true); + const user = useUser(); + user.setLoadingNetworkLiquidity(true); const depositLists: ValidDeposit[][] = []; @@ -23,22 +23,16 @@ const getNetworksLiquidity = async (): Promise => { (v) => !isNaN(Number(v)) )) { console.log("getNetworksLiquidity", network); - - // Get public client for this network - const client = getProviderByNetwork(network as NetworkEnum); - const address = getP2PixAddress(network as NetworkEnum); - - depositLists.push( - await getValidDeposits( - getTokenAddress(viemStore.selectedToken, network as NetworkEnum), - network as NetworkEnum, - { client, address } - ) + const deposits = await getValidDeposits( + getTokenAddress(user.selectedToken.value), + Number(network) ); + if (deposits) depositLists.push(deposits); } - viemStore.setDepositsValidList(depositLists.flat()); - viemStore.setLoadingNetworkLiquidity(false); + const allDeposits = depositLists.flat(); + user.setDepositsValidList(allDeposits); + user.setLoadingNetworkLiquidity(false); }; const getPixKey = async (seller: string, token: string): Promise => { diff --git a/src/blockchain/provider.ts b/src/blockchain/provider.ts index 85a62d9..3ae1380 100644 --- a/src/blockchain/provider.ts +++ b/src/blockchain/provider.ts @@ -3,17 +3,17 @@ import { updateWalletStatus } from "./wallet"; import { getProviderUrl, getP2PixAddress } from "./addresses"; import { createPublicClient, createWalletClient, custom, http } from "viem"; import { sepolia, rootstock } from "viem/chains"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; let publicClient = null; let walletClient = null; const getPublicClient = (onlyRpcProvider = false) => { if (onlyRpcProvider) { - const viemStore = useViemStore(); + const user = useUser(); const rpcUrl = getProviderUrl(); return createPublicClient({ - chain: viemStore.networkName === sepolia.id ? sepolia : rootstock, + chain: Number(user.networkName.value) === sepolia.id ? sepolia : rootstock, transport: http(rpcUrl) }); } @@ -28,24 +28,25 @@ const getContract = async (onlyRpcProvider = false) => { const client = getPublicClient(onlyRpcProvider); const address = getP2PixAddress(); const abi = p2pix.abi; - + return { address, abi, client }; }; const connectProvider = async (p: any): Promise => { - const viemStore = useViemStore(); - const chain = viemStore.networkName === sepolia.id ? sepolia : rootstock; - + console.log("Connecting to provider..."); + const user = useUser(); + const chain = Number(user.networkName.value) === sepolia.id ? sepolia : rootstock; + publicClient = createPublicClient({ chain, transport: custom(p) }); - + walletClient = createWalletClient({ chain, transport: custom(p) }); - + await updateWalletStatus(); }; diff --git a/src/blockchain/sellerMethods.ts b/src/blockchain/sellerMethods.ts index 7027773..3b6d714 100644 --- a/src/blockchain/sellerMethods.ts +++ b/src/blockchain/sellerMethods.ts @@ -3,25 +3,25 @@ import { getTokenAddress, getP2PixAddress } from "./addresses"; import { parseEther, toHex } from "viem"; import mockToken from "../utils/smart_contract_files/MockToken.json"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { createParticipant } from "@/utils/bbPay"; import type { Participant } from "@/utils/bbPay"; const approveTokens = async (participant: Participant): Promise => { - const viemStore = useViemStore(); + const user = useUser(); const publicClient = getPublicClient(); const walletClient = getWalletClient(); - + if (!publicClient || !walletClient) { throw new Error("Clients not initialized"); } - - viemStore.setSeller(participant); + + user.setSeller(participant); const [account] = await walletClient.getAddresses(); - + // Get token address - const tokenAddress = getTokenAddress(viemStore.selectedToken); - + const tokenAddress = getTokenAddress(user.selectedToken.value); + // Check if the token is already approved const allowance = await publicClient.readContract({ address: tokenAddress, @@ -29,17 +29,17 @@ const approveTokens = async (participant: Participant): Promise => { functionName: 'allowance', args: [account, getP2PixAddress()] }); - - if (allowance < parseEther(participant.offer)) { + + if (allowance < parseEther(participant.offer.toString())) { // Approve tokens const hash = await walletClient.writeContract({ address: tokenAddress, abi: mockToken.abi, functionName: 'approve', - args: [getP2PixAddress(), parseEther(participant.offer)], + args: [getP2PixAddress(), parseEther(participant.offer.toString())], account }); - + await publicClient.waitForTransactionReceipt({ hash }); return true; } @@ -49,17 +49,17 @@ const approveTokens = async (participant: Participant): Promise => { const addDeposit = async (): Promise => { const { address, abi, client } = await getContract(); const walletClient = getWalletClient(); - const viemStore = useViemStore(); - + const user = useUser(); + if (!walletClient) { throw new Error("Wallet client not initialized"); } - + const [account] = await walletClient.getAddresses(); - - const sellerId = await createParticipant(viemStore.seller); - viemStore.setSellerId(sellerId.id); - + + const sellerId = await createParticipant(user.seller.value); + user.setSellerId(sellerId.id); + const hash = await walletClient.writeContract({ address, abi, @@ -67,13 +67,13 @@ const addDeposit = async (): Promise => { args: [ sellerId.id, toHex("", { size: 32 }), - getTokenAddress(viemStore.selectedToken), - parseEther(viemStore.seller.offer), + getTokenAddress(user.selectedToken.value), + parseEther(user.seller.value.offer), true ], account }); - + const receipt = await client.waitForTransactionReceipt({ hash }); return receipt; }; diff --git a/src/blockchain/wallet.ts b/src/blockchain/wallet.ts index 988096b..f30ceb5 100644 --- a/src/blockchain/wallet.ts +++ b/src/blockchain/wallet.ts @@ -5,7 +5,7 @@ import { type Log, parseAbi, } from "viem"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { getPublicClient, getWalletClient, getContract } from "./provider"; import { getTokenAddress, isPossibleNetwork } from "./addresses"; @@ -21,46 +21,31 @@ import type { UnreleasedLock } from "@/model/UnreleasedLock"; import type { Pix } from "@/model/Pix"; export const updateWalletStatus = async (): Promise => { - const viemStore = useViemStore(); + const user = useUser(); const publicClient = getPublicClient(); const walletClient = getWalletClient(); - + if (!publicClient || !walletClient) { console.error("Client not initialized"); return; } - - const chainId = await publicClient.getChainId(); - if (!isPossibleNetwork(Number(chainId))) { - window.alert("Invalid chain!:" + chainId); - return; - } - viemStore.setNetworkId(Number(chainId)); - // Get account address - const [address] = await walletClient.getAddresses(); - - // Get token balance - const tokenAddress = getTokenAddress(viemStore.selectedToken); - const balanceResult = await publicClient.readContract({ - address: tokenAddress, - abi: mockToken.abi, - functionName: 'balanceOf', - args: [address] - }); + // Get balance + const [account] = await walletClient.getAddresses(); + const balance = await publicClient.getBalance({ address: account }); - viemStore.setBalance(formatEther(balanceResult)); - viemStore.setWalletAddress(getAddress(address)); + user.setWalletAddress(account); + user.setBalance(formatEther(balance)); }; export const listValidDepositTransactionsByWalletAddress = async ( walletAddress: string ): Promise => { - const viemStore = useViemStore(); + const user = useUser(); const walletDeposits = await getValidDeposits( - getTokenAddress(viemStore.selectedToken), - viemStore.networkName + getTokenAddress(user.selectedToken.value), + user.networkName.value ); if (walletDeposits) { return walletDeposits @@ -96,12 +81,12 @@ const filterLockStatus = async ( data: transaction.data, topics: transaction.topics, }); - + if (!decoded || !decoded.args) continue; - + // Type assertion to handle the args safely const args = decoded.args as Record; - + const tx: WalletTransaction = { token: args.token ? String(args.token) : "", blockNumber: Number(transaction.blockNumber), @@ -244,7 +229,7 @@ const listLockTransactionByWalletAddress = async ( }); return lockLogs - .sort((a:Log, b:Log) => { + .sort((a: Log, b: Log) => { return Number(b.blockNumber) - Number(a.blockNumber); }) .map((log: Log) => { @@ -259,7 +244,7 @@ const listLockTransactionByWalletAddress = async ( return null; } }) - .filter((decoded:any) => decoded !== null); + .filter((decoded: any) => decoded !== null); }; const listLockTransactionBySellerAddress = async ( @@ -267,7 +252,7 @@ const listLockTransactionBySellerAddress = async ( ) => { const { address, abi, client } = await getContract(true); console.log("Will get locks as seller", sellerAddress); - + const lockLogs = await client.getLogs({ address, event: parseAbi(['event LockAdded(address indexed buyer, uint256 indexed lockID, address seller, address token, uint256 amount)'])[0], @@ -290,8 +275,8 @@ const listLockTransactionBySellerAddress = async ( }) .filter((decoded: any) => decoded !== null) .filter( - (decoded:any) => decoded.args && decoded.args.seller && - decoded.args.seller.toLowerCase() === sellerAddress.toLowerCase() + (decoded: any) => decoded.args && decoded.args.seller && + decoded.args.seller.toLowerCase() === sellerAddress.toLowerCase() ); }; @@ -304,25 +289,25 @@ export const checkUnreleasedLock = async ( }; const addedLocks = await listLockTransactionByWalletAddress(walletAddress); - + if (!addedLocks.length) return undefined; - + const lockIds = addedLocks.map((lock: any) => lock.args.lockID); - + const lockStatus = await client.readContract({ address, abi, functionName: 'getLocksStatus', args: [lockIds] }); - + const unreleasedLockId = lockStatus[1].findIndex( (status: number) => status == 1 ); if (unreleasedLockId !== -1) { const lockID = lockStatus[0][unreleasedLockId]; - + const lock = await client.readContract({ address, abi, @@ -351,7 +336,7 @@ export const getActiveLockAmount = async ( if (!lockSeller.length) return 0; const lockIds = lockSeller.map((lock: any) => lock.args.lockID); - + const lockStatus = await client.readContract({ address, abi, diff --git a/src/components/BuyConfirmedComponent/BuyConfirmedComponent.vue b/src/components/BuyConfirmedComponent/BuyConfirmedComponent.vue index 4f22afc..c9cbc3b 100644 --- a/src/components/BuyConfirmedComponent/BuyConfirmedComponent.vue +++ b/src/components/BuyConfirmedComponent/BuyConfirmedComponent.vue @@ -8,7 +8,7 @@ import { import CustomButton from "@/components/CustomButton/CustomButton.vue"; import type { ValidDeposit } from "@/model/ValidDeposit"; import type { WalletTransaction } from "@/model/WalletTransaction"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { NetworkEnum } from "@/model/NetworkEnum"; import { onMounted, ref, watch } from "vue"; import ListingComponent from "../ListingComponent/ListingComponent.vue"; @@ -19,8 +19,8 @@ const props = defineProps<{ isCurrentStep: boolean; }>(); -const viemStore = useViemStore(); -const { walletAddress } = storeToRefs(viemStore); +const user = useUser(); +const { walletAddress } = useUser(); const lastWalletTransactions = ref([]); const depositList = ref([]); @@ -29,7 +29,7 @@ const activeLockAmount = ref(0); // methods const getWalletTransactions = async () => { - viemStore.setLoadingWalletTransactions(true); + user.setLoadingWalletTransactions(true); if (walletAddress.value) { const walletDeposits = await listValidDepositTransactionsByWalletAddress( walletAddress.value @@ -48,20 +48,20 @@ const getWalletTransactions = async () => { lastWalletTransactions.value = allUserTransactions; } } - viemStore.setLoadingWalletTransactions(false); + user.setLoadingWalletTransactions(false); }; const callWithdraw = async (amount: string) => { if (amount) { - viemStore.setLoadingWalletTransactions(true); - const withdraw = await withdrawDeposit(amount, viemStore.selectedToken); + user.setLoadingWalletTransactions(true); + const withdraw = await withdrawDeposit(amount, user.selectedToken.value); if (withdraw) { console.log("Saque realizado!"); await getWalletTransactions(); } else { console.log("Não foi possível realizar o saque!"); } - viemStore.setLoadingWalletTransactions(false); + user.setLoadingWalletTransactions(false); } }; @@ -93,14 +93,14 @@ onMounted(async () => {

Tokens recebidos

- {{ props.tokenAmount }} {{ viemStore.selectedToken }} + {{ props.tokenAmount }} {{ user.selectedToken }}

Não encontrou os tokens?
Clique no botão abaixo para
- cadastrar o {{ viemStore.selectedToken }} em sua carteira. + cadastrar o {{ user.selectedToken }} em sua carteira.

diff --git a/src/components/ListingComponent/ListingComponent.vue b/src/components/ListingComponent/ListingComponent.vue index 876cb63..b07cdee 100644 --- a/src/components/ListingComponent/ListingComponent.vue +++ b/src/components/ListingComponent/ListingComponent.vue @@ -3,8 +3,7 @@ import { withdrawDeposit } from "@/blockchain/buyerMethods"; import { NetworkEnum } from "@/model/NetworkEnum"; import type { ValidDeposit } from "@/model/ValidDeposit"; import type { WalletTransaction } from "@/model/WalletTransaction"; -import { useViemStore } from "@/store/viem"; -import { storeToRefs } from "pinia"; +import { useUser } from "@/composables/useUser"; import { ref, watch, onMounted } from "vue"; import SpinnerComponent from "../SpinnerComponent.vue"; import { decimalCount } from "@/utils/decimalCount"; @@ -12,7 +11,7 @@ import { debounce } from "@/utils/debounce"; import { getTokenByAddress } from "@/blockchain/addresses"; import { useFloating, arrow, offset, flip, shift } from "@floating-ui/vue"; -const etherStore = useViemStore(); +const user = useUser(); // props const props = defineProps<{ @@ -23,8 +22,9 @@ const props = defineProps<{ const emit = defineEmits(["depositWithdrawn"]); -const { loadingWalletTransactions } = storeToRefs(etherStore); -const remaining = ref(0.0); +const { loadingWalletTransactions } = user; + +const remaining = ref(0); const itemsToShow = ref([]); const withdrawAmount = ref(""); const withdrawButtonOpacity = ref(0.6); @@ -85,8 +85,7 @@ watch(withdrawAmount, (): void => { }); const getRemaining = (): number => { - if (props.validDeposits instanceof Array) { - // Here we are getting only the first element of the list because + if (props.validDeposits.length > 0) { // in this release only the BRL token is being used. const deposit = props.validDeposits[0]; remaining.value = deposit ? deposit.remaining : 0; @@ -96,7 +95,7 @@ const getRemaining = (): number => { }; const getExplorer = (): string => { - return etherStore.networkName == NetworkEnum.sepolia + return user.networkName.value == NetworkEnum.sepolia ? "Etherscan" : "Polygonscan"; }; @@ -107,7 +106,7 @@ const showInitialItems = (): void => { const openEtherscanUrl = (transactionHash: string): void => { const networkUrl = - etherStore.networkName == NetworkEnum.sepolia + user.networkName.value == NetworkEnum.sepolia ? "sepolia.etherscan.io" : "mumbai.polygonscan.com"; const url = `https://${networkUrl}/tx/${transactionHash}`; diff --git a/src/components/ListingComponent/__tests__/ListingComponent.spec.ts b/src/components/ListingComponent/__tests__/ListingComponent.spec.ts index 7de5266..fba7a33 100644 --- a/src/components/ListingComponent/__tests__/ListingComponent.spec.ts +++ b/src/components/ListingComponent/__tests__/ListingComponent.spec.ts @@ -2,13 +2,13 @@ import { describe, it, expect, vi, beforeEach } from "vitest"; import { mount } from "@vue/test-utils"; import ListingComponent from "../ListingComponent.vue"; import SpinnerComponent from "../../SpinnerComponent.vue"; -import { useViemStore } from "@/store/viem"; +import { useUser } from "@/composables/useUser"; import { MockValidDeposits } from "@/model/mock/ValidDepositMock"; import { MockWalletTransactions } from "@/model/mock/WalletTransactionMock"; describe("ListingComponent.vue", () => { beforeEach(() => { - useViemStore().setLoadingWalletTransactions(false); + useUser().setLoadingWalletTransactions(false); }); test("Test Message when an empty array is received", () => { diff --git a/src/components/SearchComponent.vue b/src/components/SearchComponent.vue index 270902f..b7ad3d2 100644 --- a/src/components/SearchComponent.vue +++ b/src/components/SearchComponent.vue @@ -1,7 +1,6 @@ diff --git a/src/components/SellerSteps/SellerSearchComponent.vue b/src/components/SellerSteps/SellerSearchComponent.vue index 9fffac3..5be8f5d 100644 --- a/src/components/SellerSteps/SellerSearchComponent.vue +++ b/src/components/SellerSteps/SellerSearchComponent.vue @@ -1,7 +1,6 @@ + + + + diff --git a/src/components/TopBar/TopBar.vue b/src/components/TopBar/TopBar.vue index ce98d80..61c3937 100644 --- a/src/components/TopBar/TopBar.vue +++ b/src/components/TopBar/TopBar.vue @@ -1,8 +1,7 @@