Generate
The provider comes with a generate
action that creates a connection URI and deeplink for custom wallet buttons and actions.
// Some where within your codebase...
const { actions } = useProvider();
const response = await actions.generate()
Input (Optional)
This action does not require any inputs (similar to the `connect` action), but allows for a `walletId` parameter which will automatically generate a custom URI and deeplink for a targeted wallet.
{
walletId?: string;
chain?: string;
walletId?: string;
pairing?: { topic: string };
openModal?: boolean;
}
Return Type
The method will resolve if a connection is re-established by the user.
Promise<{
uri: string;
deeplink: string;
}>
Example usage:
// File: ./src/app/component.tsx
'use client';
import * as React from 'react';
import { useProvider } from './page';
import core from '@joey-wallet/wc-client/core';
const wallets = core.constants.wallets;
export default function Component() {
const { actions, session, accounts } = useProvider();
const handleGenerate = () => {
const generate = await actions.generate({
walletId: wallets.joey.projectId,
});
// Get raw uri from generate function and manufacture deeplink for mobile
const uri = generate.data?.uri;
const deeplink = generate.data?.deeplink;
if (isMobile) {
const openDeeplink = (link: string) => (window.location.href = link);
if (deeplink) openDeeplink(deeplink);
} else {
openModal && triggerModal(true);
}
return new Promise((resolve, reject) => {
// Listen for connect event to capture session and URI
const onConnect = () => {
resolve(true);
// Clean up listener
provider.off('connect', onConnect);
};
// Handle provider errors
const onError = () => {
triggerModal(false); // Close modal on error
reject(new Error(`Failed to connect`));
provider.off('error', onError);
};
// Register event listeners
provider.on('connect', onConnect);
provider.on('error', onError);
});
};
return (
<div>
<button type="button" onClick={handleGenerate}>
Generate
</button>
{session && (
<div>
<span>Successfully connected!</span>
{accounts && accounts.map((account,index) => (
<div key={index}>{account}</div>
)
)}
</div>
)}
</div>
);
}
Last updated