Skip to content

Core Concepts

The LuminaDex SDK is built around several key concepts that are important to understand before diving into implementation details.

State Machines

At the heart of the SDK are two main state machines powered by XState:

Wallet Machine

The Wallet machine handles all interactions with the Mina blockchain wallet:

  • Detecting wallet extension presence
  • Connecting to the wallet
  • Switching networks
  • Fetching account balances
  • Handling network and account changes
INITCONNECTINGFETCHING_BALANCEREADYSWITCHING_NETWORK Connect SetAccount success RequestNetworkChange FetchBalance setWalletNetwork

DEX Machine

The DEX machine handles all operations related to the Lumina DEX:

  • Compiling and loading smart contracts
  • Swapping tokens
  • Adding/removing liquidity
  • Deploying pools and tokens
  • Minting tokens
  • Claiming from faucet (on test networks)
Contract SystemLOADING_CONTRACTSCOMPILE_FUNGIBLE_TOKENCOMPILE_POOLCOMPILE_POOL_TOKEN_HOLDERCOMPILE_FUNGIBLE_TOKEN_ADMINCOMPILE_POOL_FACTORYCOMPILE_FAUCETCONTRACTS_READY loadContracts compileContract compileContract compileContract compileContract compileContract compileContract
CalculatingReadyCALCULATING_SWAP_AMOUNTCALCULATING_ADD_LIQUIDITY_AMOUNTCALCULATING_REMOVE_LIQUIDITY_AMOUNTSWAPPINGADDING_LIQUIDITYREMOVING_LIQUIDITY
OperationsReadyDEPLOYING_TOKENDEPLOYING_POOLCLAIMING_FROM_FAUCETMINTING

Network Types

The SDK supports several network configurations:

ts
type NetworkLayer = "mina" | "zeko"
type ChainNetwork = "mainnet" | "devnet" | "testnet"
type NetworkUri =
	| "mina:mainnet"
	| "mina:devnet"
	| "zeko:testnet"
	| "zeko:mainnet"

These network identifiers are used throughout the SDK to specify which blockchain network to interact with.

Actor System

The SDK uses an actor-based approach where state machines are instantiated as actors that can:

  1. Receive events through the send() method
  2. Maintain internal state
  3. Execute side effects
  4. Communicate with other actors
ApplicationSDKBlockchainUser InterfaceAuroWalletWalletDexMina WalletMina NodeStateContextStateContext provides Events / Subscribe Connect,Fetch Balance Transactions,Queries
ts
// Create actors
const Wallet = createWallet()
const Dex = createDex({ input: { wallet: Wallet, ... } })

// Send events to actors
Wallet.send({ type: "Connect" })
Dex.send({ type: "ChangeSwapSettings", settings: { ... } })

// Subscribe to actor state changes
Wallet.subscribe(state => {
  console.log("New wallet state:", state.value)
})

//Read the actor state
console.log(Wallet.getSnapshot())

Web Workers

For computationally intensive operations like cryptographic calculations, the SDK utilizes Web Workers to free-up the main thread. The DEX machine spawns a worker that handles:

  • Contract compilation with a remote cache
  • Cryptographic operations
  • Transaction preparation

Additionally, o1js is being used, which includes other web workers based optimizations.

Main ThreadWorker ThreadAppSDKLuminaDex WorkerDexWalletContract Instanceso1js, proofsTransaction Builder Comlink

This architecture ensures the main thread remains responsive during heavy computations, such as contract compilation or zk proof generations.

GraphQL for Blockchain Data

The SDK uses GraphQL to fetch blockchain data from Mina nodes and archive services. It provides:

  • Pre-configured GraphQL clients with urql
  • Type-safe queries using gql.tada
  • GraphQL schemas for Mina, Archive, and Zeko services
  • Helper functions for common data requests
SDKBlockchainLumina CDNGraphQL ClientsqueriesHelper FunctionsMina NodeArchive NodeZeko NodeToken ListsMina ClientArchive ClientZeko ClientGetBalance QueryGetPool QueryGetEvents Query...Fetch Pool Tokens from CDNFetch Pool Events from BlockchainFetch Pool Tokens from Blockchain executes uses HTTP HTTP HTTP HTTP

Released under the MIT License.