# Swap

Trigger the MOSS swaps flow and know when a user has completed a swap.

[Read more](https://moss.megaeth.com/developers/concepts/swaps)

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

// Both fields are optional; the user picks the rest in the wallet.
const result = await mega.swap({
    fromToken: 'native',
    toToken: '0x2715b6d04b6a0AB227631019899282a585Be410F',
});
```

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

export default function SwapButton() {
    const { mutate: swap, isPending } = useSwap();

    return (
        <button onClick={() => swap({ fromToken: 'native', toToken: '0x2715b6d04b6a0AB227631019899282a585Be410F' })} disabled={isPending}>
            Swap
        </button>
    );
}
```

```tsx title="Wagmi"
import type { MegaWalletProvider } from '@megaeth-labs/wallet-wagmi-connector';
import { useAccount } from 'wagmi';

export default function SwapButton() {
    const { connector } = useAccount();

    async function swap() {
        const provider = (await connector?.getProvider()) as MegaWalletProvider;

        // Both fields are optional; the user picks the rest in the wallet.
        await provider.swap({
            fromToken: 'native',
            toToken: '0x2715b6d04b6a0AB227631019899282a585Be410F',
        });
    }

    return (
        <button onClick={swap} disabled={!connector}>
            Swap
        </button>
    );
}
```

```ts
interface SwapRequest {
    fromToken?: `0x${string}` | 'native';
    toToken?: `0x${string}` | 'native';
}

// Resolves to a TransactionResult
// { status: 'approved' | 'cancelled' | 'error', receipt?, error? }
```
