Skip to content

createSharedState

Usage

connect() must be called and WalletConnect session must be initiated.

The createSharedState() function creates a shared state account within the user’s wallet and returns the new account’s seed and address.

See Shared State for an overview.

import { createSharedState } from '@puzzlehq/sdk-core';
import { useState } from 'react';
export const Page = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>();
const [sharedStateAccount, setSharedStateAccount] = useState<{ seed: string, address: string} | undefined>();
const onClick = async () => {
setLoading(true);
setError(undefined);
try {
const response: CreateSharedStateResponse = await createSharedState();
if (response.data) setSharedStateAccount(response.data);
else if (response.error) setError(response.error);
} catch (e) {
setError((e as Error).message);
} finally {
setLoading(false);
}
}
return (
<div>
<button
onClick={ onClick }
disabled={ loading }
>
create shared state
</button>
{ sharedStateAccount && (
<div>
<p>{'seed: ' + sharedStateAccount.seed}</p>
<p>{'address: ' + sharedStateAccount.address}</p>
</div>
)}
{ error && <p>error creating shared state: {error}</p> }
</div>
)
}

Types

type CreateSharedStateResponse = {
data?: {
seed: string;
address: string;
};
error?: string;
};