9.1Spark限价单智能合约 (Spark Limit Orders Smart Contract)

https://github.com/compolabs/spark/tree/master/contracts

版本警告 (Version warning)

The master branch contains the latest work-in-progress version of the Spark smart contract. It hasn't been audited and may contain severe security issues or may not work at all.

主分支( master branch)包含Spark智能合约的最新工作进程版本。它没有经过审计,可能包含严重的安全问题,也可能根本无法工作。

关于 (About)

This repository contains a smart contract for Fuel blockchain which allows users to place limit orders orderbook, that later could be filled and matched on-chain. Limit order itself is a data structure created off-chain.

这个存储库包含了Fuel区块链的智能合约,它允许用户在订单簿上部署限价单,以后可以在链上执行和匹配。限价单本身是一个在链外创建的数据结构。

文档 (Docs)

合约存储 (Contract Storage)

storage {
   orders: StorageMap<u64, Order> = StorageMap {},
   orders_amount: u64 = 0,
   deposits: StorageMap<Address, u64> = StorageMap {},
   trades: StorageVec<Trade> = StorageVec {},
}

Abi

abi LimitOrders {
   #[storage(read)]
   fn get_deposit_by_address(address: Address) -> u64;

   #[payable]
   #[storage(read, write)]
   fn deposit();

   #[storage(read, write)]
   fn withdraw(amount: u64);

   #[storage(read)]
   fn orders_amount() -> u64;

   #[storage(read)]
   fn trades(offset: u64) -> (Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>, Option<Trade>);

   #[storage(read)]
   fn orders(offset: u64) -> (Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>);

   #[storage(read)]
   fn orders_by_id(ids: [u64; 10]) -> (Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>, Option<Order>);

   #[storage(read)]
   fn order_by_id(id: u64) -> Order;

   #[payable]
   #[storage(read, write)]
   fn create_order(asset1: ContractId, amount1: u64, matcher_fee: u64) -> u64;

   #[storage(read, write)]
   fn cancel_order(id: u64);

   #[payable]
   #[storage(read, write)]
   fn fulfill_order(id: u64);

   #[storage(read, write)]
   fn match_orders(order_id_a: u64, order_id_b: u64);
}

结构 (Structs)

订单 Order

pub struct Order {
   asset0: ContractId,
   amount0: u64,
   asset1: ContractId,
   amount1: u64,
   status: Status,
   fulfilled0: u64,
   fulfilled1: u64,
   owner: Address,
   id: u64,
   timestamp: u64,
   matcher_fee: u64,
   matcher_fee_used: u64,
}

交易 Trade

struct Trade {
   order_id: u64,
   asset0: ContractId,
   amount0: u64,
   asset1: ContractId,
   amount1: u64,
   timestamp: u64,
}

穷举 (Enums)

状态 Status

enum Status {
    Active: (),
    Canceled: (),
    Completed: (),
}

使用Fuel Rust SDK设置和运行测试 (Setting up and running the tests using Fuel Rust SDK)

依赖性 (Dependencies)

forc is Sway equivalent of Rust's cargo. fuel-core is a Fuel full node implementation.

Sway的forc相当于Rust的cargo。 fuel-core是一个Fuel全节点的实现。

克隆 repo (Clone repo)

git clone https://github.com/sway-gang/spark.git 
cd spark

构建合约 (Build contract)

cd contract
forc build

运行测试 (Run tests)

cargo test

Last updated