hydraex/exchange

dev-master 2019-07-23 10:06 UTC

This package is not auto-updated.

Last update: 2024-09-25 10:53:19 UTC


README

匹配器允许通过改变交易者的余额和订单簿中每个订单的量来交换任何资产。

安装

通过 Composer

php composer require hydraex/exchange

基本使用案例

要调用匹配器,您需要从左右订单簿(按价格排序)获取前两个订单,并将它们转换为此存储库中的订单实体。

//...
use Hydra\Exchange\Entities\{Pair, BuyOrder, SellOrder, SellerBalance, BuyerBalance, Asset};
use Hydra\Exchange\Libs\{Matcher, Logger};
//...

//creation of pair
$pair = new Pair(
    new Asset("BTC", "Bitcoin"), //primary asset
    new Asset("ETH", "Ether") //secondary asset
);

//setting of user balances
$buyersBalance = new BuyerBalance(1001, 89);
$sellerBalance = new SellerBalance(99, 11);

//two counter orders:
//one wants to buy 100 units of the ETH for the price of 10
$buyOrder = new BuyOrder($pair, 100, 10, $buyersBalance, 1);
//another wants to sell 10 units of the ETH for the price of 9
$sellOrder = new SellOrder($pair, 10, 9, $sellerBalance, 2);
//(the last parameter (1 and 2) is order number among all orders)

//deal with it:
$matcher = new Matcher($buyOrder, $sellOrder);
$deal = $matcher->matching();

echo "Deal price is " . $deal->getPrice() . "\n";
echo "Deal quantity is " . $deal->getQuantity() . "\n";
echo "Buyer BTC balance is " . $buyersBalance->getPrimary() . "\n";
echo "Buyer ETH balance is " . $buyersBalance->getSecondary() . "\n";
echo "Seller BTC balance is " . $sellerBalance->getPrimary() . "\n";
echo "Seller ETH balance is " . $sellerBalance->getSecondary() . "\n";
echo "Buy order now contains only " . $buyOrder->getQuantityRemain() . " ETH \n";
echo "Sell order now contains " . $buyOrder->getQuantityRemain() . " ETH and his status is " . $sellOrder->getStatus() . "\n";
echo "Log: ";
var_dump(Logger::list());