For the complete documentation index, see llms.txt. This page is also available as Markdown.

Elixir Library (rujira_ex)

rujira_ex is an Elixir library for querying Rujira and THORChain data, resolving assets and amounts, and parsing blockchain events.

You can use it directly within an Elixir application or as the data layer of a service consumed by applications written in other languages. For example, an Elixir service using rujira_ex can supply market data and parsed events to a trading bot written in Python, TypeScript, or Rust through an API, message queue, or database.

This lets builders use rujira_ex for data collection and processing while choosing their preferred language for trading strategies, user interfaces, and transaction execution.

View the repository · View the Hex package

What can you build?

Use rujira_ex to build applications that connect users and trading strategies to Rujira’s markets:

  • Trading bots: use market data, order books, and order queries to inform automated trading strategies. Combine these reads with your application’s transaction signing and submission to place and manage orders on RUJI Trade.

  • Market-making tools: monitor market conditions, existing orders, and liquidity ranges to support strategies that quote prices and provide liquidity.

  • Strategy monitoring and analytics: process trade events and query protocol state to track fills, analyse strategy activity, and identify when your application should act.

  • Market dashboards: help traders explore available pairs, inspect order book depth, and make informed trading decisions.

  • Trading interfaces and execution services: build backend services that supply market data and track orders for user-facing trading applications.

rujira_ex provides the data-querying and event-parsing components for these applications. Your application supplies the strategy logic, wallet integration, transaction signing and submission, and operational controls.

For applications that need access to Rujira’s hosted GraphQL or REST APIs, see Developer Endpoints.

Main components

Component
Purpose

Rujira.Fin

Query RUJI Trade markets, order books, orders, and ranges.

Rujira.Events

Parse blockchain events into protocol-specific structs.

Rujira.Assets

Resolve asset identifiers and chain denominations.

Rujira.Amount and Rujira.Coin

Work with amounts and asset–amount pairs.

Rujira.Contracts

Query CosmWasm smart contracts.

Rujira.Deployments

Resolve deployed contracts from THORChain’s on-chain registry.

Rujira.Node

Connect the library to your application’s gRPC node client.

Quick start

1. Install the library

You need an Elixir project using Elixir 1.18 or a compatible later 1.x release.

Add the dependency to the existing dependency list in mix.exs:

Fetch dependencies and start an interactive session:

2. Work with amounts

THORChain and Rujira represent asset amounts as integers using eight decimal places. One whole unit is 100_000_000, so an amount of 150_000_000 represents 1.5 units.

Use Rujira.Amount.new/1 to parse an amount supplied as a string, then format/1 to produce a human-readable value. This example does not require a node connection:

new/1 parses the supplied base-unit amount without changing its precision. format/1 converts the eight-decimal integer into a display string.

If your application also reads raw amounts directly from external chains, use Rujira.Amount.normalize/2 to convert their native precision into eight decimals. For example, a six-decimal amount of 1_500_000 becomes 150_000_000; both represent 1.5 units. Only apply this conversion when the input is still expressed in the external chain’s native precision.

3. Configure chain queries

To read chain data, provide a module implementing the Rujira.Node behaviour and configure it in your application:

MyApp.Node represents a module you implement. Its query/3 function connects queries to your gRPC client. The implementation must forward query options and preserve the returned response metadata, which the library uses to verify queries at a specific block height.

See the Rujira.Node implementation contract and Developer Endpoints for connection options.

4. Discover RUJI Trade markets

Once your node implementation is configured, your application can advance the library to a confirmed block height and query the available pairs:

This returns {:ok, pairs} on success or an error tuple if the operation fails.

Listing pairs is the starting point for a trading integration. From there, use Rujira.Fin.get_pair/2 and Rujira.Fin.load_pair/3 to inspect a market and its order book, then query orders or liquidity ranges relevant to your application.

A trading bot can use these reads to evaluate market conditions, submit transactions through its execution layer, and monitor subsequent blocks to check the outcome.

5. Keep your application up to date

Keep calling Rujira.Node.advance/1 as new blocks arrive. The library uses block events to update its cache.

Queries without an explicit height: use the library’s current head—the latest block height it has applied. Before the first successful advance, these cached queries return {:error, :no_head}.

Pass the same explicit height: to related queries when your application needs a consistent view of a market at one block. Queries at historical heights depend on the connected node retaining the requested data.

See the caching guide for the full model.

Working with events

Rujira.Events converts chain events into structs your application can inspect through Elixir pattern matching. For example, a recognised FIN trade is represented by a Rujira.Fin.Events.Event envelope containing Rujira.Fin.Events.Trade data.

For trading applications, events help connect a submitted action to what happened on-chain. Your application can use recognised trade events to record fills, update its view of market activity, or trigger a strategy review. Combine event processing with order queries to reconcile the state your application tracks.

You can handle specific actions, such as trades, or broader groups of protocol events. Your application remains responsible for receiving the events, handling parsing errors, and storing or processing the results.

Follow the event pipeline walkthrough for a complete example.

Further reading

Last updated