Quickstart
@megaeth-labs/wallet-sdk@megaeth-labs/wallet-sdk-react
Install#
MOSS provides different SDKs depending on your environment: Pure JS, React and a Wagmi connector. Each SDK is available via NPM and support all functionality.
npm install @megaeth-labs/wallet-sdknpm install @megaeth-labs/wallet-sdk-react @tanstack/react-querynpm install @megaeth-labs/wallet-wagmi-connector wagmi viem @tanstack/react-queryInitialise#
Once installed, the wallet must be initialised before calling the SDK functionality. This differs depending on the SDK being used.
import { mega } from '@megaeth-labs/wallet-sdk';
const status = await mega.initialise({
network: 'testnet'
});import { MegaProvider } from '@megaeth-labs/wallet-sdk-react';
export default function App() {
return (
<MegaProvider config={{ network: 'testnet' }}>
{/* ...your app */}
</MegaProvider>
);
}
import { mossWallet } from '@megaeth-labs/wallet-wagmi-connector';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { megaethTestnet } from 'viem/chains';
import { createConfig, http, WagmiProvider } from 'wagmi';
const config = createConfig({
chains: [megaethTestnet],
connectors: [mossWallet({ network: 'testnet' })],
transports: {
[megaethTestnet.id]: http(),
},
});
const queryClient = new QueryClient();
export default function App() {
return (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
{/* ...your app */}
</QueryClientProvider>
</WagmiProvider>
);
}Status#
Check the current connection status on loading your app either from the response of initialise, a call to the
status method or the React hook useStatus.
import { mega } from '@megaeth-labs/wallet-sdk';
const { status, address, network } = await mega.status();import { useStatus } from '@megaeth-labs/wallet-sdk-react';
export default function Component() {
const { initialised, status, address, network, error } = useStatus();
if (error) return <p>Couldn't load the wallet: {error.message}</p>;
if (!initialised) return <p>Loading…</p>;
return (
<ul>
<li>Status: {status}</li>
<li>Network: {network}</li>
{address && <li>Address: {address}</li>}
</ul>
);
}import { useAccount } from 'wagmi';
export default function Component() {
const { status, address, chain } = useAccount();
if (status === 'connecting' || status === 'reconnecting') return <p>Loading…</p>;
return (
<ul>
<li>Status: {status}</li>
<li>Network: {chain?.name}</li>
{address && <li>Address: {address}</li>}
</ul>
);
}Runs on testnet with your wallet
Connect#
Trigger the connect flow for a user to create a new wallet or restore an existing one.
import { mega } from '@megaeth-labs/wallet-sdk';
const { status, address } = await mega.connect();
if (status === 'connected') {
console.log('Wallet connected:', address);
}import { useConnect } from '@megaeth-labs/wallet-sdk-react';
export default function ConnectButton() {
const { mutate: connect, isPending } = useConnect({
onSuccess: ({ status, address }) => {
if (status === 'connected') {
console.log('Wallet connected:', address);
}
},
});
return (
<button onClick={() => connect()} disabled={isPending}>
{isPending ? 'Connecting…' : 'Connect wallet'}
</button>
);
}import { useConnect } from 'wagmi';
export default function ConnectButton() {
const { connect, connectors, isPending } = useConnect({
mutation: {
onSuccess: ({ accounts }) => {
console.log('Wallet connected:', accounts[0]);
},
},
});
const connector = connectors.find((c) => c.id === 'mossWallet');
return (
<button onClick={() => connector && connect({ connector })} disabled={!connector || isPending}>
{isPending ? 'Connecting…' : 'Connect wallet'}
</button>
);
}Runs on testnet with your wallet