# Quickstart

## 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.

```bash title="Pure JS"
npm install @megaeth-labs/wallet-sdk
```

```bash title="React"
npm install @megaeth-labs/wallet-sdk-react @tanstack/react-query
```

```bash title="Wagmi"
npm install @megaeth-labs/wallet-wagmi-connector wagmi viem @tanstack/react-query
```

## Initialise
Once installed, the wallet must be initialised before calling the SDK functionality. This differs depending on the
SDK being used.

```ts title="Pure JS"
import { mega } from '@megaeth-labs/wallet-sdk';

const status = await mega.initialise({
    network: 'testnet'
});
```

```tsx title="React"
import { MegaProvider } from '@megaeth-labs/wallet-sdk-react';

export default function App() {
    return (
        <MegaProvider config={{ network: 'testnet' }}>
            {/* ...your app */}
        </MegaProvider>
    );
}

```

```tsx title="Wagmi"
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>
    );
}
```

> [!NOTE]
> `MegaProvider` sets up TanStack Query for you, so you don't need your own `QueryClientProvider`. To use your own
> `QueryClient`, put its provider inside `MegaProvider`. See
> [wallet-sdk-react](https://moss.megaeth.com/developers/packages/wallet-sdk-react#megaprovider).

[Learn more](https://moss.megaeth.com/developers/methods/connecting/initialise)

## 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`.

```ts title="Pure JS"
import { mega } from '@megaeth-labs/wallet-sdk';

const { status, address, network } = await mega.status();
```

```tsx title="React"
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>
    );
}
```

```tsx title="Wagmi"
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>
    );
}
```

[Learn more](https://moss.megaeth.com/developers/methods/connecting/status)

## Connect
Trigger the connect flow for a user to create a new wallet or restore an existing one.

```ts title="Pure JS"
import { mega } from '@megaeth-labs/wallet-sdk';

const { status, address } = await mega.connect();

if (status === 'connected') {
    console.log('Wallet connected:', address);
}
```

```tsx title="React"
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>
    );
}
```

```tsx title="Wagmi"
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>
    );
}
```
