Skip to main content

SDK Client

The Client class offers a high-level interface for interacting with the Mojito wallet extension, or with any other account provider. It covers the connection lifecycle, account queries, and one method per transaction type (see Transactions).

Creating a client

Use the async Client.create factory, which initializes the client and is the recommended entry point:

import { Client } from '@mintlayer/sdk';

const client = await Client.create({
network: 'testnet',
autoRestore: true,
// accountProvider?: AccountProvider, defaults to MojitoAccountProvider
// apiProvider?: ApiProvider, defaults to MintlayerApiProvider
});
OptionTypeDefaultDescription
network'mainnet' | 'testnet''mainnet'Chain the client operates on
autoRestorebooleantrueTry to restore a previous wallet session on creation
accountProviderAccountProviderMojitoAccountProviderProvider that supplies addresses and signing
apiProviderApiProviderMintlayerApiProviderChain-data backend for UTXOs, fees, and queries

You can also construct the client directly with new Client(options) and initialize it yourself; Client.create is almost always what you want.

Connection lifecycle

const addresses = await client.connect();   // prompt the user; returns connected addresses
const ok = await client.restore(); // re-attach to an existing session
const connected = client.isConnected(); // boolean
await client.disconnect(); // clear connected addresses
  • connect() asks the wallet for access and stores the returned addresses. With Mojito this opens the extension's approval prompt.
  • restore() resumes a previously approved session without prompting the user again. When autoRestore is enabled this happens during Client.create, so returning users are connected automatically.
  • disconnect() clears the local session state.

Connected addresses are split into receiving and change chains:

const { receiving, change } = client.getAddresses();

Network

client.getNetwork();      // 'mainnet' | 'testnet'
client.setNetwork('mainnet');

The network must match the network the wallet is configured for; transactions built for the wrong network are rejected on signing.

Events

client.on('accountsChanged', (data) => {
// re-read client.getAddresses() or force a reconnect
});

on(eventName, callback) registers a listener for wallet events forwarded by the account provider, such as account or network changes in the extension. See Mojito Inject for the underlying event model.

Account queries

Once connected, the client exposes read helpers for the connected addresses. They resolve chain data through the configured ApiProvider.

MethodReturns
getBalance()Base-coin balance of the connected addresses
getBalances(){ coin, token: Record<tokenId, number> }, coin and per-token balances
getDelegations()Delegation details for the connected addresses
getDelegationsTotal()Total delegated amount
getTokensOwned()Token IDs owned by the connected addresses
getAccountOrders()Trading orders created by this account
getAvailableOrders()Trading orders available on the network
getXPub()Extended public key from the wallet
warning

getXPub() exposes the extended public key, from which all addresses can be derived. Only request it when you really need it, and never ask for it without telling the user why.

Staking-specific helpers are covered in Staking, token operations in Tokens.

Chain data

The default MintlayerApiProvider points at the public Mintlayer API and implements the ApiProvider interface: chain tip, address lookups, delegations, tokens, orders, transactions, UTXO fetching, and broadcasting. To run against your own indexer, construct it with your base URLs:

import { Client, MintlayerApiProvider } from '@mintlayer/sdk';

const apiProvider = new MintlayerApiProvider('https://api.example.com', 'https://batch.example.com');

const client = await Client.create({ network: 'testnet', apiProvider });

The endpoints mirror the public API reference, so self-hosting the API web server gives you a fully local stack.

Signing without a wallet

Everything the Client does through the account provider can also be done with explicit keys via the Signer class; see Transactions. For bots and scanners that need balances derived from chain data, see Wallet state.