<!--
Sitemap:
- [MBGA · Reactive Primitives for Spark](/index)
- [Acknowledgments](/acknowledgments)
- [API Reference](/api/)
- [Connectors](/connectors/)
- [Examples](/examples/)
- [Flashnet](/flashnet/)
- [Getting Started](/getting-started)
- [Installation](/installation)
- [Kit](/kit/)
- [Configuration](/api/configuration)
- [Core Actions](/api/core-actions)
- [React Hooks](/api/hooks)
- [Types & Errors](/api/types)
- [Custom Connectors](/connectors/custom)
- [Sats Connect (Xverse)](/connectors/sats-connect)
- [Spark SDK](/connectors/spark-sdk)
- [Wallet Standard](/connectors/wallet-standard)
- [Flashnet Authentication](/flashnet/authentication)
- [Next.js](/frameworks/nextjs)
- [Vite](/frameworks/vite)
- [AccountModal](/kit/account-modal)
- [ConnectButton](/kit/connect-button)
- [ConnectModal](/kit/connect-modal)
-->

# Core Actions

Core actions are framework-agnostic functions exported from `@mbga/core`. They take a `Config` as the first argument. The React hooks wrap these actions with TanStack Query.

```ts
import { connect, getBalance, sendPayment } from '@mbga/core'
```

## `connect`

```ts
function connect(
  config: Config,
  parameters: ConnectParameters,
): Promise<ConnectReturnType>
```

```ts
type ConnectParameters = { connector: Connector }
type ConnectReturnType = { accounts: readonly string[] }
```

Throws `ConnectorAlreadyConnectedError` if already connected.

## `reconnect`

```ts
function reconnect(
  config: Config,
  parameters?: ReconnectParameters,
): Promise<void>
```

```ts
type ReconnectParameters = { connectors?: Config['connectors'] }
```

Looks up the most recently used connector ID from storage and attempts to reconnect. Sets status to `'disconnected'` if no stored connector is found or authorization has expired.

## `getConnection`

```ts
function getConnection(config: Config): GetConnectionReturnType
```

```ts
type GetConnectionReturnType = {
  accounts: readonly string[]
  connector: Connector
  isConnected: boolean
  isConnecting: boolean
  isDisconnected: boolean
  isReconnecting: boolean
  status: 'connected' | 'connecting' | 'disconnected' | 'reconnecting'
}
```

Synchronous. Returns the current connection state derived from `config.state`.

## `getConnectors`

```ts
function getConnectors(config: Config): GetConnectorsReturnType
```

```ts
type GetConnectorsReturnType = readonly Connector[]
```

Returns all registered connectors.

## `createInvoice`

```ts
function createInvoice(
  config: Config,
  parameters: CreateInvoiceParameters,
): Promise<CreateInvoiceReturnType>
```

```ts
type CreateInvoiceParameters = { amount: bigint; memo?: string }
type CreateInvoiceReturnType = { invoice: string; id?: string }
```

## `signMessage`

```ts
function signMessage(
  config: Config,
  parameters: SignMessageParameters,
): Promise<SignMessageReturnType>
```

```ts
type SignMessageParameters = { message: string; address?: string }
type SignMessageReturnType = { signature: string }
```

## `waitForPayment`

```ts
function waitForPayment(
  config: Config,
  parameters?: WaitForPaymentParameters,
): Promise<WaitForPaymentReturnType>
```

```ts
type WaitForPaymentParameters = {
  invoice?: string
  invoiceId?: string
  timeout?: number // default: 300000 (5 minutes)
}
type WaitForPaymentReturnType = { id: string; amount: bigint }
```

## `watchConnections`

Subscribes to changes in the connections map.

```ts
function watchConnections(
  config: Config,
  parameters: WatchConnectionsParameters,
): () => void
```

```ts
type WatchConnectionsParameters = {
  onChange(
    connections: GetConnectionsReturnType,
    prevConnections: GetConnectionsReturnType,
  ): void
}
```
