Skip to Content

stardust-stellar

Every export of the main entry, with defineConfig, the amount helpers, StardustError and the core API.

stardust-stellar is the entry stardust.config.ts imports. It also exports the amount helpers, StardustError, the core API that stardust-stellar/test is built on, and the types of wallets, prompts and outcomes.

import { defineConfig, StardustError, units, xlm } from 'stardust-stellar';
import type { TransactionPrompt } from 'stardust-stellar';

Test files import test and expect from stardust-stellar/test. They import from here only the types stardust-stellar/test doesn't export, such as TransactionPrompt.

Config

defineConfig

export function defineConfig(config: StardustConfig): StardustConfig;

Types the default export of stardust.config.ts. It returns the config unchanged.

stardust.config.ts
import { defineConfig } from 'stardust-stellar';

export default defineConfig({
  network: 'testnet',
  app: { url: 'http://localhost:3000', start: 'pnpm dev' },
  tests: './e2e',
});

StardustConfig

export interface StardustConfig {
  app: {
    url: string;
    start?: string;
  };
  network:
    | 'testnet'
    | {
        rpcUrl: string;
      };
  tests?: string;
}

The settings in stardust.config.ts. See Config.

FieldTypeDefaultDescription
network'testnet' | { rpcUrl: string }requiredThe network the tests run on: 'testnet', or { rpcUrl } for another RPC endpoint. Stardust reads the passphrase and Friendbot URL from the network, and refuses mainnet
app.urlstringrequiredWhere the app runs. page.goto('/swap') opens this URL plus /swap
app.startstringnoneThe command that starts the app, such as 'pnpm dev'. Stardust runs it only when nothing answers at app.url, and stops it after the run
testsstring'./e2e'The folder with the test files, relative to the project folder

Node loads the config with its built-in TypeScript support, so the file can't use enum or namespace.

Amount helpers

stardust-stellar/test exports the same two functions.

xlm

export function xlm(amount: string): bigint;

Converts an XLM amount, written as a decimal string, to stroops. xlm('10') is 100_000_000n. Throws a RangeError when amount isn't a decimal amount or has more than 7 decimal places.

units

export function units(amount: string, decimals: number): bigint;

Converts a token amount, written as a decimal string, to the token's smallest units. units('9.8', 7) is 98_000_000n. Throws a RangeError when amount isn't a decimal amount or has more decimal places than decimals, and when decimals isn't a whole number of 0 or more.

Errors

StardustError

export class StardustError extends Error {
  constructor(code: StardustErrorCode, message: string, options?: ErrorOptions);
  readonly code: StardustErrorCode;
  readonly retryable: boolean;
}

The error Stardust throws when the test or its setup is wrong. The message says what happened and what to do next. Wallet errors the app receives are never StardustErrors.

MemberDescription
codeWhat went wrong, a StardustErrorCode
retryableWhether the same step can succeed if run again, such as after a network problem. true for NETWORK_UNREACHABLE, FUNDING_FAILED and OUTCOME_UNKNOWN

The error's name is StardustError followed by the code, so output reads StardustError NO_PROMPT: ....

StardustErrorCode

export type StardustErrorCode =
  | 'CONFIG_NOT_FOUND'
  | 'CONFIG_INVALID'
  | 'NETWORK_NOT_SUPPORTED'
  | 'NETWORK_UNREACHABLE'
  | 'MAINNET_REFUSED'
  | 'APP_NOT_REACHABLE'
  | 'FUNDING_FAILED'
  | 'NO_PROMPT'
  | 'ALREADY_ANSWERED'
  | 'PROMPT_BLOCKED'
  | 'WALLET_LOCKED'
  | 'OUTCOME_UNKNOWN'
  | 'UNKNOWN_ACCOUNT'
  | 'UNSUPPORTED'
  | 'PROTOCOL_VIOLATION'
  | 'PROMPTS_PENDING';

Errors lists when each code happens and what to do.

Core API

stardust-stellar/test builds its users from these. Use them directly only for a custom setup that doesn't use stardust-stellar/test.

import { createStardust } from 'stardust-stellar';
import type { PageHost } from 'stardust-stellar';

const stardust = await createStardust({ network: 'testnet' });
const account = await stardust.accounts.create({ fund: '50' });
const wallet = stardust.wallet({ accounts: [account], policy: 'manual' });

// context is a BrowserContext.
const host: PageHost = {
  exposeBinding: (name, handler) =>
    context.exposeBinding(name, ({ page, frame }, payload) => {
      if (frame !== page.mainFrame()) throw new Error('top frame only');
      return handler(new URL(frame.url()).origin, payload);
    }),
  addInitScript: (source) => context.addInitScript(source),
};
await wallet.attach(host, { wallet: 'varial' });
const page = await context.newPage();

The end-of-test checks for unanswered prompts and strict mode run only in stardust-stellar/test. In a custom setup, read wallet.prompts() and wallet.violations() yourself.

createStardust

export function createStardust(input: CoreConfig): Promise<Stardust>;

Checks the network and returns Stardust's core: accounts and wallets. It waits up to 60 s for the RPC to report healthy. Throws NETWORK_UNREACHABLE when the RPC isn't healthy and MAINNET_REFUSED on mainnet.

CoreConfig

export interface CoreConfig {
  fetch?: typeof fetch;
  network:
    | 'testnet'
    | {
        rpcUrl: string;
      };
}

The settings for createStardust.

FieldTypeDefaultDescription
network'testnet' | { rpcUrl: string }requiredThe network. 'testnet' uses https://soroban-testnet.stellar.org. Mainnet is refused
fetchtypeof fetchglobalThis.fetchThe fetch used for RPC and Friendbot requests

Stardust

export interface Stardust {
  readonly accounts: {
    create(options?: AccountOptions): Promise<Account>;
    createMany(options: readonly AccountOptions[]): Promise<Account[]>;
  };
  readonly network: NetworkInfo;
  wallet(options: WalletOptions): Wallet;
}

Stardust's core, as createStardust returns it.

MemberDescription
networkThe network, as checked by createStardust, a NetworkInfo
accounts.create(options?)Creates a throwaway account, funded from the machine's testnet funder. Takes AccountOptions
accounts.createMany(options)Creates several accounts in one transaction
wallet(options)Creates an emulated Wallet. Call attach on it to put it in a page

accounts.create and accounts.createMany throw CONFIG_INVALID for wrong options and FUNDING_FAILED when the funder can't fund the accounts.

WalletOptions

export interface WalletOptions {
  accounts: readonly Account[];
  allowHttp?: boolean;
  connected?: boolean;
  policy?: Policy;
  strict?: boolean;
}

The settings for one emulated wallet. In tests, UserOptions sets them.

FieldTypeDefaultDescription
accountsreadonly Account[]requiredThe accounts in the wallet. The first is active. Empty throws CONFIG_INVALID
connectedbooleanfalseStart as if the user had already connected the site, for every account
policyPolicy'approve'How the wallet answers prompts
strictbooleantrueFail the test at its end when the app broke wallet rules
allowHttpbooleantrueWhether the wallet signs on http:// pages

In stardust-stellar/test, connected: true also saves the Stellar Wallets Kit session in the user's browser. The core API doesn't.

PageHost

export interface PageHost {
  addInitScript(source: string): Promise<void>;
  exposeBinding(
    name: string,
    handler: (origin: string, payload: unknown) => Promise<unknown>,
  ): Promise<void>;
}

Where a wallet lives: a browser page, or a stand-in for one. stardust-stellar/test builds one on each user's BrowserContext.

MemberDescription
exposeBinding(name, handler)Makes handler callable from the page as window[name]
addInitScript(source)Runs source in every page before the page's own scripts

Pass handler the origin of the calling frame, taken from the browser. Never read it from the page's message. The wallet uses it to decide which site is connected.

Types shared with stardust-stellar/test

These types describe accounts, wallets, prompts and outcomes. Tests reach them through users, so they're documented in stardust-stellar/test. stardust-stellar exports them by name.

ExportDescription
AccountA throwaway testnet account Stardust created for one test
AccountOptionsHow an account is created
AddTokenPromptThe app asks the wallet to add a token
Asset'native', 'CODE:ISSUER' or a token contract, C...
AuthEntryPromptThe app asks for one contract call to be authorized
BalanceChangeHow much one address's balance of one asset changed
BlockedReasonWhy the real wallet would refuse a prompt
ConnectPromptThe app asks for the user's address
ContractErrorAn error a contract returned
ContractEventAn event a contract emitted
DecisionHow a policy function answers a prompt
DecodedAuthEntryAn authorization entry attached to a transaction, decoded
DecodedAuthPreimageAn authorization entry preimage the app asked the wallet to sign, decoded
DecodedOperationOne operation of a transaction
DecodedTransactionA transaction the app asked the wallet to sign, decoded
InvocationA contract call
LandedA transaction's outcome once it's in a ledger
MessagePromptThe app asks for a message to be signed
NetworkInfoA network, as Stardust read it from its RPC
PolicyHow the wallet answers prompts
PolicyMode'approve', 'reject' or 'manual'
PromptAny of the five prompt types
PromptKindWhat a prompt asks for, such as 'signTransaction'
PromptOfThe prompt type for one kind
PromptStatus'pending' until a prompt is answered, then the answer
TransactionPromptThe app asks for a whole transaction to be signed
ViolationA wallet rule the app broke
WaitOptionsHow long to wait
WalletAn emulated wallet, with the accounts of one user

BasePrompt, Failure and ViolationKind are exported too.

On this page