REE Exchange Rust SDK
The Rust SDK for building native Bitcoin dApps on REE(Runes Exchange Environment).
Unlike Ethereum and other smart contract platforms, Bitcoin's scripting language is not Turing complete, making it extremely challenging—if not impossible—to develop complex applications like AMM protocols directly on the Bitcoin network using BTC scripts and the UTXO model.
REE overcomes this limitation by leveraging the powerful Chain Key technology of the Internet Computer Protocol (ICP) and Bitcoin's Partially Signed Bitcoin Transactions (PSBT) to extend the programmability of Bitcoin's Rune assets.
Basic procedures of REE exchange
Constructing the PSBT: The REE exchange client application (e.g., a wallet or interface) gathers the necessary information from the REE exchange and constructs a PSBT based on the user’s input. The user then signs the PSBT to authorize the transaction.
Submitting the PSBT to REE: The client composes the signed PSBT and essential information retrieved in the previous step and submit to REE Orchestrator. REE will validate the PSBT(including the UTXOs and their RUNE information) and analysis the input-output relations. If all check pass, Orchestrator will forward the request to RichSwap.
Exchange's Validation and Signing: The exchange verifies the transaction details from REE Orchestrator and, if everything is valid, signs the pool’s UTXO using the ICP Chain Key. This step transforms the PSBT into a fully valid Bitcoin transaction.
Broadcasting the Transaction: The finalized transaction is returned to the REE, which broadcasts it to the Bitcoin network for execution.
Quick start
If you are familier with IC canister development, you could easily create an empty rust crate and paste the code into the lib.rs.
use ;
use ;
use ;
use ;
use Serialize;
REE exchange client
To complete an REE transaction, the exchange client should call REE Orchestrator invoke rather than making request into exchanges directly.
The invoke function of Orchestrator takes InvokeArgs as a parameter, which includes the following fields:
Where IntentionSet is defined as:
The invoke function returns a Result<String, String>, where:
- The
Okvalue is thetxidof the final Bitcoin transaction, which will be formed and broadcasted. - The
Errvalue is an error message if the execution ofinvokefails.
The invoke function will call the execute_tx function of the exchange canister(s) based on the provided IntentionSet. If all intentions are successfully executed, the function broadcasts the final Bitcoin transaction and returns the txid.
Before invoking the exchange canisters, the Orchestrator performs necessary validations on the IntentionSet to ensure it aligns with the provided PSBT data.
Intention Details
Each IntentionSet can contain multiple Intention objects, reflecting the user's intentions. The Intention struct consists of the following fields:
exchange_id: The identifier of a registered exchange responsible for executing the intention. The Orchestrator will validate this field.action: The specific action to be executed by the exchange. The Orchestrator will NOT validate this field.action_params: Parameters for the action, specific to the exchange. The Orchestrator will NOT validate this field.pool_address: The address of the exchange pool where the intention will be executed. The Orchestrator will validate this field.nonce: A nonce representing the pool state in the exchange. The Orchestrator will NOT validate this field.pool_utxo_spent: The UTXO(s) owned by the pool that will be spent in the intention. The clients of REE can leave this field empty as the Orchestrator will fill it in with the UTXO(s) that the pool will spend in the final Bitcoin transaction.pool_utxo_received: The UTXO(s) that the pool will receive as part of the intention. These UTXOs should correspond to the outputs of the final Bitcoin transaction. The clients of REE can leave this field empty as the Orchestrator will fill it in with the UTXO(s) that the pool will receive in the final Bitcoin transaction.input_coins: The coins that will be spent in the intention. These should appear as inputs in the final Bitcoin transaction.output_coins: The coins that will be received in the intention. These should appear as outputs in the final Bitcoin transaction.