Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Enhance wallet connection handling in Create Invoice Form and V…
…iew Requests

- Added wallet connection and disconnection handling in Create Invoice Form using `watchAccount`.
- Improved state management for account and cipher provider in View Requests.
- Refactored wallet change logic for better clarity and functionality.
  • Loading branch information
rodrigopavezi committed Dec 18, 2024
commit 006d3006804bb0173c9affbb9a952bff6f9c5c6f
41 changes: 34 additions & 7 deletions packages/create-invoice-form/src/lib/create-invoice-form.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<svelte:options customElement="create-invoice-form" />

<script lang="ts">
import { getAccount } from "@wagmi/core";
import { getAccount, watchAccount, WatchAccountReturnType } from "@wagmi/core";
import { Config as WagmiConfig } from "wagmi";
// Types
import { type GetAccountReturnType } from "@wagmi/core";
Expand All @@ -24,6 +24,7 @@
import Status from "@requestnetwork/shared-components/status.svelte";
import Modal from "@requestnetwork/shared-components/modal.svelte";
import { EncryptionTypes, CipherProviderTypes } from "@requestnetwork/types";
import { onDestroy, onMount, tick } from "svelte";

interface CipherProvider extends CipherProviderTypes.ICipherProvider {
disconnectWallet: () => void;
Expand All @@ -35,7 +36,7 @@
export let currencies: CurrencyTypes.CurrencyInput[] = [];
let cipherProvider: CipherProvider | undefined;

let account: GetAccountReturnType;
let account: GetAccountReturnType | undefined = wagmiConfig && getAccount(wagmiConfig);
let isTimeout = false;
let activeConfig = config ? config : defaultConfig;
let mainColor = activeConfig.colors.main;
Expand Down Expand Up @@ -121,12 +122,38 @@

$: cipherProvider = requestNetwork?.getCipherProvider() as CipherProvider;

$: {
if (wagmiConfig) {
account = getAccount(wagmiConfig);
cipherProvider?.disconnectWallet();
const handleWalletConnection = async () => {
account = getAccount(wagmiConfig);
};

const handleWalletDisconnection = () => {
cipherProvider?.disconnectWallet();
};

const handleWalletChange = (data: any) => {
if (data?.address) {
handleWalletConnection();
} else {
handleWalletDisconnection();
}
}
};

onMount(() => {
unwatchAccount = watchAccount(wagmiConfig, {
onChange(data) {
tick().then(() => {
console.log("Wallet changed");
handleWalletChange(data);
});
},
});
});

let unwatchAccount: WatchAccountReturnType | undefined;

onDestroy(() => {
if (typeof unwatchAccount === "function") unwatchAccount();
});

$: {
formData.creatorId = (account?.address ?? "") as string;
Expand Down
6 changes: 3 additions & 3 deletions packages/invoice-dashboard/src/lib/view-requests.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@
};

const handleWalletDisconnection = () => {
cipherProvider?.disconnectWallet();
account = undefined;
requests = [];
activeRequest = undefined;
cipherProvider?.disconnectWallet();
cipherProvider = undefined;
account = undefined;
};

const handleWalletChange = (data: any) => {
Expand Down Expand Up @@ -384,7 +384,7 @@
};

const loadRequests = async (sliderValue: string, currentAccount: GetAccountReturnType, currentRequestNetwork: RequestNetwork | undefined | null) => {
if (!currentAccount?.address || !currentRequestNetwork && !cipherProvider) return;
if (!currentAccount?.address || !currentRequestNetwork || !cipherProvider) return;

loading = true;
if (sliderValue === "on") {
Expand Down