Only available from v4.37.0 onwards.
Summary
The useWalletDelegation
hook provides methods to trigger and manage the wallet delegation flow for Dynamic embedded wallets.
For more information on how wallet delegation works, see the delegated access documentation.
The hook needs to be initialized within a child of DynamicContextProvider.
Return Value
The hook returns an object with the following properties:
{
initDelegationProcess: (options?: { wallets?: Wallet[] }) => Promise<void>;
requiresDelegation: boolean | undefined;
shouldPromptWalletDelegation: () => boolean;
}
Functions
initDelegationProcess
Initiates the wallet delegation flow by opening the Dynamic modal and displaying the delegation prompt to the user.
Parameters:
options
(optional): Configuration object
wallets
(optional): Array of specific wallets to delegate. If not provided, delegates all eligible MPC wallets.
Returns: Promise<void>
- Resolves when delegation completes successfully, rejects on failure.
Errors:
- Throws
"No primary wallet"
if the user doesn’t have a primary wallet connected
- Throws
"user_not_logged_in"
if the user is not authenticated
Example:
const { initDelegationProcess } = useWalletDelegation();
try {
await initDelegationProcess();
console.log('Delegation completed successfully');
} catch (error) {
console.error('Delegation failed:', error);
}
shouldPromptWalletDelegation
Determines whether the user should be prompted for wallet delegation based on:
- Delegated access settings in project configuration
- User’s previous delegation decisions (denied or completed)
- Existence of undelegated MPC wallets
Returns: boolean
- true
if the user should see the delegation prompt, false
otherwise.
Example:
const { shouldPromptWalletDelegation } = useWalletDelegation();
if (shouldPromptWalletDelegation()) {
// Show custom UI or automatically trigger delegation
}
Properties
requiresDelegation
A boolean value from your project settings that indicates whether wallet delegation is required for your application.
Type: boolean | undefined
Example:
const { requiresDelegation } = useWalletDelegation();
if (requiresDelegation) {
// Delegation is mandatory for this application
// You might want to block certain actions until delegation is complete
}
Usage
import { useWalletDelegation } from '@dynamic-labs/sdk-react-core';
const MyComponent = () => {
const {
initDelegationProcess,
shouldPromptWalletDelegation,
requiresDelegation
} = useWalletDelegation();
const handleDelegateWallet = async () => {
try {
await initDelegationProcess();
console.log('Wallet delegation successful!');
} catch (error) {
console.error('Failed to delegate wallet:', error);
}
};
// Automatically prompt on mount if needed
useEffect(() => {
if (shouldPromptWalletDelegation()) {
initDelegationProcess();
}
}, []);
return (
<div>
{requiresDelegation && (
<p>This app requires wallet delegation to function</p>
)}
<button onClick={handleDelegateWallet}>
Delegate Wallet Access
</button>
</div>
);
};
Advanced Usage
Delegate specific wallets:
import { useWalletDelegation } from '@dynamic-labs/sdk-react-core';
import { useUserWallets } from '@dynamic-labs/sdk-react-core';
const MyComponent = () => {
const { initDelegationProcess } = useWalletDelegation();
const { userWallets } = useUserWallets();
const delegateSpecificWallet = async (walletId: string) => {
const wallet = userWallets.find(w => w.id === walletId);
if (wallet) {
try {
await initDelegationProcess({ wallets: [wallet] });
console.log('Specific wallet delegated successfully');
} catch (error) {
console.error('Delegation failed:', error);
}
}
};
return (
<div>
{userWallets.map(wallet => (
<button
key={wallet.id}
onClick={() => delegateSpecificWallet(wallet.id)}
>
Delegate {wallet.address}
</button>
))}
</div>
);
};
Important Notes
- The user must be authenticated and have a primary wallet for delegation to work
- The delegation modal is part of the Dynamic UI and handles all user interactions
- User delegation preferences are persisted across sessions using local storage
Learn More