Rust SDK

GitHub Code: orderbook-contract

Installation

To install the Spark Market SDK, add the following to your Cargo.toml:

[dependencies]
spark-market-sdk = { git = "https://github.com/compolabs/orderbook-contract.git", package = "spark-market-sdk" }

SDK Methods

Deploying the Market Contract

pub async fn deploy(
    base_asset: AssetId,
    base_decimals: u32,
    quote_asset: AssetId,
    quote_decimals: u32,
    price_decimals: u32,
    owner: WalletUnlocked,
    fuel_asset: AssetId,
) -> anyhow::Result<Self>

Deploys the market contract with the provided configurations.

  • base_asset: AssetId - The asset identifier for the base token.

  • base_decimals: u32 - The decimal precision for the base asset.

  • quote_asset: AssetId - The asset identifier for the quote token.

  • quote_decimals: u32 - The decimal precision for the quote asset.

  • price_decimals: u32 - The decimal precision for the price.

  • owner: WalletUnlocked - The wallet of the contract owner.

  • fuel_asset: AssetId - The asset identifier for the fuel token.

Creating a New Market Contract Instance

pub async fn new(contract_id: ContractId, wallet: WalletUnlocked) -> Self

Creates a new instance of the market contract.

  • contract_id: ContractId - The ID of the deployed contract.

  • wallet: WalletUnlocked - The wallet associated with the contract.

Associating an Account with the Contract

pub async fn with_account(&self, account: &WalletUnlocked) -> anyhow::Result<Self>

Associates a new account with the contract instance.

  • account: &WalletUnlocked - The account to be associated with the contract.

Retrieving Contract ID

pub fn id(&self) -> Bytes32

Retrieves the contract ID.

Retrieving Bech32 Contract ID

pub fn contract_id(&self) -> &Bech32ContractId

Retrieves the Bech32 formatted contract ID.

Depositing Assets

pub async fn deposit(&self, amount: u64, asset: AssetId) -> anyhow::Result<CallResponse<()>>

Deposits a specified amount of assets into the contract.

  • amount: u64 - The amount to be deposited.

  • asset: AssetId - The asset identifier.

Withdrawing Assets

pub async fn withdraw(&self, amount: u64, asset_type: AssetType) -> anyhow::Result<CallResponse<()>>

Withdraws a specified amount of assets from the contract.

  • amount: u64 - The amount to be withdrawn.

  • asset_type: AssetType - The type of asset to withdraw.

Opening an Order

pub async fn open_order(
    &self,
    amount: u64,
    asset_type: AssetType,
    order_type: OrderType,
    price: u64,
) -> anyhow::Result<CallResponse<Bits256>>

Opens a new order in the market.

  • amount: u64 - The amount of the order.

  • asset_type: AssetType - The type of asset for the order.

  • order_type: OrderType - The type of the order (buy/sell).

  • price: u64 - The price for the order.

Opening an Order with Matcher Fee

pub async fn open_order_with_matcher_fee(
    &self,
    amount: u64,
    asset_type: AssetType,
    order_type: OrderType,
    price: u64,
    matcher_fee: u32,
) -> anyhow::Result<CallResponse<Bits256>>

Opens a new order in the market with a specified matcher fee.

  • amount: u64 - The amount of the order.

  • asset_type: AssetType - The type of asset for the order.

  • order_type: OrderType - The type of the order (buy/sell).

  • price: u64 - The price for the order.

  • matcher_fee: u32 - The matcher fee.

Canceling an Order

pub async fn cancel_order(&self, order_id: Bits256) -> anyhow::Result<CallResponse<()>>

Cancels an existing order.

  • order_id: Bits256 - The identifier of the order to be canceled.

Matching an Order Pair

pub async fn match_order_pair(
    &self,
    order_id0: Bits256,
    order_id1: Bits256,
) -> anyhow::Result<CallResponse<()>>

Matches a pair of orders.

  • order_id0: Bits256 - The first order ID.

  • order_id1: Bits256 - The second order ID.

Matching Multiple Orders

pub async fn match_order_many(&self, orders: Vec<Bits256>) -> anyhow::Result<CallResponse<()>>

Matches multiple orders.

  • orders: Vec<Bits256> - A vector of order IDs to be matched.

Fulfilling Multiple Orders

pub async fn fulfill_many(
    &self,
    amount: u64,
    asset_type: AssetType,
    order_type: OrderType,
    limit_type: LimitType,
    price: u64,
    slippage: u64,
    orders: Vec<Bits256>,
) -> anyhow::Result<CallResponse<Bits256>>

Fulfills multiple orders with the specified parameters.

  • amount: u64 - The amount to fulfill.

  • asset_type: AssetType - The type of asset.

  • order_type: OrderType - The type of the order (buy/sell).

  • limit_type: LimitType - The type of limit.

  • price: u64 - The price of the order.

  • slippage: u64 - The slippage tolerance.

  • orders: Vec<Bits256> - A vector of order IDs to be fulfilled.

Setting the Protocol Fee

pub async fn set_protocol_fee(&self, amount: u32) -> anyhow::Result<CallResponse<()>>

Sets the protocol fee amount.

  • amount: u32 - The protocol fee amount.

Setting the Matcher Fee

pub async fn set_matcher_fee(&self, amount: u32) -> anyhow::Result<CallResponse<()>>

Sets the matcher fee amount.

  • amount: u32 - The matcher fee amount.

Withdrawing Protocol Fee

pub async fn withdraw_protocol_fee(&self, to: Identity) -> anyhow::Result<CallResponse<()>>

Withdraws the protocol fee to a specified identity.

  • to: Identity - The identity to receive the protocol fee.

Retrieving Account Information

pub async fn account(&self, user: Identity) -> anyhow::Result<CallResponse<Option<Account>>>

Retrieves the account information for a specified user.

  • user: Identity - The identity of the user.

Retrieving Protocol Fee

pub async fn protocol_fee(&self) -> anyhow::Result<CallResponse<u32>>

Retrieves the current protocol fee.

Retrieving Total Protocol Fee

pub async fn total_protocol_fee(&self) -> anyhow::Result<CallResponse<u64>>

Retrieves the total accumulated protocol fee.

Calculating Protocol Fee Amount

pub async fn protocol_fee_amount(
    &self,
    amount: u64,
    asset_type: AssetType,
) -> anyhow::Result<CallResponse<u64>>

Calculates the protocol fee amount for a given order.

  • amount: u64 - The amount of the order.

  • asset_type: AssetType - The type of asset.

Retrieving Matcher Fee

pub async fn matcher_fee(&self) -> anyhow::Result<CallResponse<u32>>

Retrieves the current matcher fee.

Retrieving Order Information

pub async fn order(&self, order: Bits256) -> anyhow::Result<CallResponse<Option<Order>>>

Retrieves information about a specific order.

  • order: Bits256 - The order ID.

Retrieving User Orders

pub async fn user_orders(&self, user: Identity) -> anyhow::Result<CallResponse<Vec<Bits256>>>

Retrieves all orders associated with a specific user.

  • user: Identity - The identity of the user.

Retrieving Order Change Information

pub async fn order_change_info(
    &self,
    order_id: Bits256,
) -> anyhow::Result<CallResponse<Vec<OrderChangeInfo>>>

Retrieves the change information for a specific order.

  • order_id: Bits256 - The order ID.

Retrieving Contract Configuration

pub async fn config(
    &self,
) -> anyhow::Result<CallResponse<(Address, AssetId, u32, AssetId, u32, u32, AssetId)>>

Retrieves the contract configuration details.

Calculating Order ID

pub async fn order_id(
    &self,
    asset_type: AssetType,
    order_type: OrderType,
    owner: Identity,
    price: u64,
    block_height: u32,
) -> anyhow::Result<CallResponse<Bits256>>

Calculates the order ID based on the provided parameters.

Last updated