From 1dcdaf3e7b5fa01b8839f5c6fbf2b051b9948b51 Mon Sep 17 00:00:00 2001 From: Sven <38101365+svenvoskamp@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:02:24 +0100 Subject: [PATCH] Feat/ethers export (#1913) --- packages/core/src/utils/CoreHelperUtil.ts | 2 +- packages/ethers/exports/react.tsx | 10 ++++++++++ packages/ethers/exports/vue.ts | 10 ++++++++++ packages/ethers/src/client.ts | 3 ++- packages/ethers5/exports/react.tsx | 10 ++++++++++ packages/ethers5/exports/vue.ts | 10 ++++++++++ packages/ethers5/src/client.ts | 2 +- 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/core/src/utils/CoreHelperUtil.ts b/packages/core/src/utils/CoreHelperUtil.ts index 4fcfb50945..2955891b14 100644 --- a/packages/core/src/utils/CoreHelperUtil.ts +++ b/packages/core/src/utils/CoreHelperUtil.ts @@ -138,7 +138,7 @@ export const CoreHelperUtil = { } } - return formattedBalance ? `${formattedBalance} ${symbol ?? ''}` : '0.000' + return formattedBalance ? `${formattedBalance} ${symbol ?? ''}` : `0.000 ${symbol ?? ''}` }, formatBalance2(balance: string | undefined, symbol: string | undefined) { diff --git a/packages/ethers/exports/react.tsx b/packages/ethers/exports/react.tsx index 77738a96fb..58ae46920d 100644 --- a/packages/ethers/exports/react.tsx +++ b/packages/ethers/exports/react.tsx @@ -49,6 +49,16 @@ export function useDisconnect() { } } +export function useSwitchNetwork() { + async function switchNetwork(chainId: number) { + await modal?.switchNetwork(chainId) + } + + return { + switchNetwork + } +} + export function useWeb3ModalAccount() { const { address, isConnected, chainId } = useSnapshot(EthersStoreUtil.state) diff --git a/packages/ethers/exports/vue.ts b/packages/ethers/exports/vue.ts index 34fb06bc89..a009cfdffd 100644 --- a/packages/ethers/exports/vue.ts +++ b/packages/ethers/exports/vue.ts @@ -56,6 +56,16 @@ export function useDisconnect() { } } +export function useSwitchNetwork() { + async function switchNetwork(chainId: number) { + await modal?.switchNetwork(chainId) + } + + return { + switchNetwork + } +} + export function useWeb3ModalAccount() { if (!modal) { throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalAccount" composition') diff --git a/packages/ethers/src/client.ts b/packages/ethers/src/client.ts index 86ef8e5df8..23bfd85620 100644 --- a/packages/ethers/src/client.ts +++ b/packages/ethers/src/client.ts @@ -860,13 +860,14 @@ export class Web3Modal extends Web3ModalScaffold { if (jsonRpcProvider) { const balance = await jsonRpcProvider.getBalance(address) const formattedBalance = formatEther(balance) + this.setBalance(formattedBalance, chain.currency) } } } } - private async switchNetwork(chainId: number) { + public async switchNetwork(chainId: number) { const provider = EthersStoreUtil.state.provider const providerType = EthersStoreUtil.state.providerType if (this.chains) { diff --git a/packages/ethers5/exports/react.tsx b/packages/ethers5/exports/react.tsx index b50a8b00d6..599b508e8c 100644 --- a/packages/ethers5/exports/react.tsx +++ b/packages/ethers5/exports/react.tsx @@ -49,6 +49,16 @@ export function useDisconnect() { } } +export function useSwitchNetwork() { + async function switchNetwork(chainId: number) { + await modal?.switchNetwork(chainId) + } + + return { + switchNetwork + } +} + export function useWeb3ModalAccount() { const { address, isConnected, chainId } = useSnapshot(EthersStoreUtil.state) diff --git a/packages/ethers5/exports/vue.ts b/packages/ethers5/exports/vue.ts index 6cdf0ebe72..1ab5816ff7 100644 --- a/packages/ethers5/exports/vue.ts +++ b/packages/ethers5/exports/vue.ts @@ -58,6 +58,16 @@ export function useDisconnect() { } } +export function useSwitchNetwork() { + async function switchNetwork(chainId: number) { + await modal?.switchNetwork(chainId) + } + + return { + switchNetwork + } +} + export function useWeb3ModalAccount() { if (!modal) { throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalAccount" composition') diff --git a/packages/ethers5/src/client.ts b/packages/ethers5/src/client.ts index dc693b0648..d0f3372f24 100644 --- a/packages/ethers5/src/client.ts +++ b/packages/ethers5/src/client.ts @@ -799,7 +799,7 @@ export class Web3Modal extends Web3ModalScaffold { } } - private async switchNetwork(chainId: number) { + public async switchNetwork(chainId: number) { const provider = EthersStoreUtil.state.provider const providerType = EthersStoreUtil.state.providerType if (this.chains) {