Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: balance not updating on metamask button #553

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion packages/sdk-react-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@
"@metamask/sdk-react>@metamask/sdk-communication-layer>utf-8-validate": false,
"@metamask/sdk-react>@metamask/sdk>@metamask/sdk-communication-layer>bufferutil": false,
"@metamask/sdk-react>@metamask/sdk>@metamask/sdk-communication-layer>eciesjs>secp256k1": false,
"@metamask/sdk-react>@metamask/sdk>@metamask/sdk-communication-layer>utf-8-validate": false
"@metamask/sdk-react>@metamask/sdk>@metamask/sdk-communication-layer>utf-8-validate": false,
"@metamask/sdk>@metamask/sdk-communication-layer>bufferutil": false,
"@metamask/sdk>@metamask/sdk-communication-layer>eciesjs>secp256k1": false,
"@metamask/sdk>@metamask/sdk-communication-layer>utf-8-validate": false,
"@metamask/sdk>eciesjs>secp256k1": false
}
}
}
29 changes: 23 additions & 6 deletions packages/sdk-react-ui/src/MetaMaskButton/Balance.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import React from 'react';
import { useSDK } from '@metamask/sdk-react';
import React, { useMemo } from 'react';
import {
useNetwork,
useAccount,
useBalance,
useNetwork,
} from '../hooks/MetaMaskWagmiHooks';
import { getBalance } from './utils';

const Balance = ({ theme }: { theme: string }) => {
const { address, isConnected } = useAccount();
const { chain } = useNetwork();
const { data, isError, isLoading } = useBalance({
const { isError, isLoading } = useBalance({
address: address,
chainId: chain?.id,
enabled: isConnected,
});
const { balance } = useSDK();
const decimals = 2;

const formattedBalance = useMemo(() => {
if (!balance) {
return `0.${'0'.repeat(decimals)}`;
}
// Convert the hexadecimal balance to a decimal number
const balanceInWei = parseInt(balance, 16);

// Assuming the balance is in Wei (for Ethereum), convert it to Ether.
// 1 Ether = 1e18 Wei
const balanceInEther = balanceInWei / 1e18;

// Format the number
return balanceInEther.toFixed(decimals);
}, [balance, decimals]);

const balance = getBalance({ data, isError, isLoading });
// const balance = getBalance({ data, isError, isLoading });

if (!balance || isLoading || isError) return null;

Expand All @@ -29,7 +46,7 @@ const Balance = ({ theme }: { theme: string }) => {
theme === 'light' ? 'tw-bg-neutral-200' : 'tw-bg-neutral-400'
} tw-p-1.5 tw-rounded`}
>
{balance}
{formattedBalance}
</span>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-react-ui/src/MetaMaskUIProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const WagmiWrapper = ({
if (debug) {
console.debug(`[MetamaskProvider] validConnectors`, { ready, sdk });
}
if (ready && sdk) {
if (ready && sdk?.isInitialized()) {
return [
new MetaMaskConnector({
chains: networks,
Expand Down