Skip to content

Commit

Permalink
Feat/ethers export (#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored Feb 13, 2024
1 parent 5ae6766 commit 1dcdaf3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions packages/ethers/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions packages/ethers/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions packages/ethers5/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions packages/ethers5/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion packages/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1dcdaf3

Please sign in to comment.