> For the complete documentation index, see [llms.txt](https://docs.megaeth.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.megaeth.com/moss-docs/core-sdk/lifecycle.md).

# Connection Lifecycle

The lifecycle methods control how your app checks wallet state, prompts the user, disconnects, or brings the wallet UI forward.

## Status Shape

```ts
type ConnectionStatus = {
  status: 'connected' | 'disconnected' | 'cancelled';
  address?: `0x${string}`;
  network: 'mainnet' | 'testnet';
};
```

## Status()

Read the current wallet connection state.

```ts
const status = await mega.status();
```

Use it after initialisation, on route transitions, or when restoring app state after a refresh.

## Connect()

Prompt the user to connect MOSS and return the resulting connection state.

```ts
const result = await mega.connect();

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

## Disconnect()

Terminate the current wallet session and return the resulting disconnected state.

```ts
const result = await mega.disconnect();
console.log(result.status);
```

## Open()

Bring the wallet UI forward without starting a specific signing or transaction flow.

```ts
await mega.open();
```

This is useful when your product includes an explicit “Open wallet” affordance for account management or session inspection.

## Recommended UX Pattern

1. Call `initialise()` once.
2. Read `status()` to render initial UI.
3. Trigger `connect()` from a deliberate user action.
4. Subscribe to `onStatusChange` for session-driven UI updates.
5. Use `disconnect()` only when the product clearly intends to terminate the session.

Treat `cancelled` as a normal user choice, not an error state. The cleanest partner integrations distinguish cancellation from transport or contract failures.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.megaeth.com/moss-docs/core-sdk/lifecycle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
