- React
- React Native
- JavaScript SDK
- Flutter
- Unity
Here’s an example of using the
getBalance method to get the balance of the connected user’s walletCopy
Ask AI
import { useEffect, useState } from 'react';
import {
useDynamicContext,
DynamicContextProvider,
UserProfile,
useIsLoggedIn,
} from '@dynamic-labs/sdk-react-core';
const Home = () => {
const { user, handleLogOut, setShowAuthFlow, primaryWallet } =
useDynamicContext();
const isLoggedIn = useIsLoggedIn();
const [balance, setBalance] = useState<string | null>(null);
useEffect(() => {
primaryWallet?.getBalance().then((balance) => {
if (balance) {
setBalance(balance.toString());
}
});
}, [primaryWallet]);
if (isLoggedIn) {
return (
<div>
<p>User is logged in</p>
{ primaryWallet && <>}
<p>Address: {primaryWallet.address}</p>
<p>Balance: {balance}</p>
</>}
<button type='button' onClick={handleLogOut}>
Log Out
</button>
</div>
);
}
return (
<div>
<button type='button' onClick={() => setShowAuthFlow(true)}>
Connect With My Wallet
</button>
</div>
);
};
const App = () => (
<DynamicContextProvider
settings={{
appLogoUrl:
'https://upload.wikimedia.org/wikipedia/commons/3/34/Examplelogo.svg',
appName: 'Example App',
environmentId: 'REPLACE_WITH_YOUR_ENV_ID',
onAuthSuccess: ({
user,
}: {
user: UserProfile;
}) => {
console.log(
`Welcome ${user.email}`,
);
window.location.assign('/success');
},
}}
>
<Home />
</DynamicContextProvider>
);
export default App;
React Native
Copy
Ask AI
// Using the RN client directly
import { dynamicClient } from '<path-to-your-dynamicClient>';
export const getPrimaryBalance = async () => {
const wallet = dynamicClient.wallets.primary;
if (!wallet) return null;
const balance = await wallet.getBalance();
return balance?.toString() ?? null;
}
Use the client helper to fetch the native balance for a specific wallet account.
Copy
Ask AI
import { getBalance } from '@dynamic-labs-sdk/client';
export async function fetchBalance(walletAccount) {
const { balance } = await getBalance({ walletAccount });
return balance?.toString() ?? null;
}
Copy
Ask AI
DynamicSDK.instance.wallets.getBalance(wallet: DynamicSDK.instance.wallets.primary);
Copy
Ask AI
DynamicSDKManager.Instance.GetBalance();