Swap APIs
The Swap APIs allow users to perform swaps between supported assets using the Kaleidoswap taker-maker model. This section details the endpoints for retrieving node information, subscribing to price updates, initiating swaps, and executing them.Get Node Information
Endpoint
GET /api/v1/swaps/nodeinfo
Description
Retrieve information about the RGB Lightning Node, such as its identifier, alias, and current state.Response Structure
node_id: The unique identifier of the node.alias: The human-readable name of the node.color: The color code associated with the node.num_peers: Number of peers connected to the node.num_channels: Number of active channels.block_height: Current blockchain height the node is synced to.synced_to_chain: Boolean indicating if the node is fully synced to the blockchain.
Example Response
Real-time Price Updates
WebSocket Endpoint
Testnet WebSocket (not-active):wss://testnet.api.kaleidoswap.com/ws/{client_id}
Mainnet WebSocket (not-active): wss://api.kaleidoswap.com/ws/{client_id}
Description
Establish a WebSocket connection to subscribe to real-time price updates for specific trading pairs.Connection
- Replace
{client_id}with a unique identifier for your client.
Subscription Format
To subscribe to a trading pair, send a JSON message with the following format:Response Format
Price updates are streamed as JSON objects:action: The action performed (e.g.,priceUpdate).data: The details of the price update.
Example Response
Field Descriptions
rfq_id: Therequest_for_quotation_id, a unique identifier generated by the maker. This ID is crucial for initiating swaps and must be passed in theinitendpoint.price_buy: The buy price for the trading pair.price_sell: The sell price for the trading pair.price_precision: The number of decimal places to which the price is quoted, relevant to the quote asset. For example, ifpricePrecisionis6for the BTC/USDT pair, it indicates that the USDT is quoted to six decimal places.base_precision: The number of decimal places to which the base asset is quoted. For example, ifbasePrecisionis8for the BTC/USDT pair, it indicates that BTC is quoted to eight decimal places.pair: The trading pair, such as BTC/USDT.timestamp: The timestamp when the price update was generated in unix time.expires_at: The timestamp when therfqIdwill expire in unix time.
Price Precision Note
- Prices provided in the response do not reflect the precision of the base asset. For instance, in a BTC/USDT pair, the base asset (BTC) is always denominated in satoshis, and the quote asset (USDT) price precision is found in the update. For example, a
buyPriceof59507000000unit for a size of100000000sats (1 BTC) corresponds to59507USDT, considering thepricePrecisionof8.
Additional Notes:
- The server validates the requested pair against the list of available pairs. If an invalid pair is requested, the subscription will be ignored.
- When BTC is in a pair, it is always denominated in satoshis (sats) with a precision of
0. - Clients can subscribe to multiple pairs by sending multiple subscription messages.
- The WebSocket connection will remain open until the client disconnects or a network error occurs.
Initiate Swap
Endpoint
POST /api/v1/swaps/init
Description
Initiate a swap based on the price update received via WebSocket. This endpoint locks the price and prepares the swap for execution.Request Body
| Field | Type | Description |
|---|---|---|
rfq_id | String | The ID of the price update received via WebSocket. |
from_asset | String | The RGB asset ID to swap from. |
from_amount | Integer | The amount of the asset to swap from.* |
to_asset | String | The RGB asset ID to swap to. |
to_amount | Integer | The amount of the asset to swap to.* |
Example Request
Response Structure
swapstring: A string representation of the swap to be executed.payment_hash: The payment hash associated with the swap.
Example Response
Additional Notes
- The precision for an RGB asset can be obtained using the
/assetsAPI on the maker’s node. If the client has the same asset, the precision can also be retrieved via the node API. - The swapstring returned by this endpoint needs to be whitelisted using the
/takerAPI of the RGB Lightning Node (RLN) of the client before executing the swap. - In this example, the user is selling 1,000 sats for 0.5877 USDT (using the RGB asset
rgb:2NZGjyz-pJePUgegh-RLHbpx1Hy-iZMagWiZZ-qY4AxGymW-yCEYwwB).
Execute Swap
Endpoint
POST /api/v1/swaps/execute
Description
Execute the swap after it has been initiated and validated.Request Body
| Field | Type | Description |
|---|---|---|
swapstring | String | The swap string generated by init endpoint. |
taker_pubkey | String | The public key of the taker. |
payment_hash | String | The payment hash associated with the swap. |
Example Request
Response Structure
status: Status code of the swap execution.message: Additional details about the execution.
Example Response
Get Swap Status
Endpoint
GET /api/v1/swaps/status
Description
Retrieve the current status of a swap using the associatedpayment_hash.
Query Parameters
payment_hash(string, required): The payment hash of the swap whose status is to be retrieved.
Example Request
Response
swap: An object containing detailed information about the swap.
Example Response
Swap Object Schema
qty_from(integer): The quantity of the asset being swapped from. Example:30qty_to(integer): The quantity of the asset being swapped to. Example:10from_asset(string): The RGB asset ID being swapped from. Example:rgb:2dkSTbr-jFhznbPmo-TQafzswCN-av4gTsJjX-ttx6CNou5-M98k8Zdto_asset(string): The RGB asset ID being swapped to. Example:rgb:2eVw8uw-8G88LQ2tQ-kexM12SoD-nCX8DmQrw-yLMu6JDfK-xx1SCfcpayment_hash(string): The unique payment hash associated with the swap. Example:7c2c95b9c2aa0a7d140495b664de7973b76561de833f0dd84def3efa08941664status(SwapStatus): The current status of the swap. Possible values:WaitingPendingSucceededExpiredFailed
requested_at(integer): Unix timestamp when the swap was requested. Example:1691160765initiated_at(integer): Unix timestamp when the swap was initiated. Example:1691168512expires_at(integer): Unix timestamp when the swap expires. Example:1691172703completed_at(integer): Unix timestamp when the swap was completed. Example:1691171075
Additional Notes
- The
statusfield provides real-time updates on the swap’s progress. - Ensure that the
payment_hashprovided is accurate to retrieve the correct swap status. - Time-related fields are in Unix timestamp format.
- The
SwapStatusfield in theSwapobject can have one of the following values:Waiting: The swap is awaiting initiation.Pending: The swap has been initiated and is currently in progress.Succeeded: The swap has been successfully completed.Expired: The swap was not completed within the required timeframe.Failed: The swap encountered an error and did not complete successfully.
For error details, refer to Error Handling.