Skip to content
Permalink
Browse files

Done all cases

  • Loading branch information...
tubackkhoa committed Jan 11, 2019
1 parent aea1e95 commit 6e49f5b7a46e70e7ac7a3a4e9b45ae151ee586e0
@@ -77,8 +77,8 @@ func initPrompt() {
{
Name: "getOrder",
Arguments: []terminal.Argument{
{Name: "pairName", Value: "TOMO/WETH"},
{Name: "orderID", Value: "1"},
{Name: "pair_name", Value: "TOMO/WETH"},
{Name: "order_id", Value: "1"},
},
Description: "Get the order from the orderbook storgae",
},
@@ -132,14 +132,14 @@ func initPrompt() {
{
Name: "getBestAskList",
Arguments: []terminal.Argument{
{Name: "pairName", Value: "TOMO/WETH"},
{Name: "pair_name", Value: "TOMO/WETH"},
},
Description: "Get best ask list",
},
{
Name: "getBestBidList",
Arguments: []terminal.Argument{
{Name: "pairName", Value: "TOMO/WETH"},
{Name: "pair_name", Value: "TOMO/WETH"},
},
Description: "Get best bid list",
},
@@ -241,9 +241,9 @@ func Start() error {
demo.LogInfo("-> Update", "wsPort", results["wsPort"])
wsPort = results["wsPort"]
case "getOrder":
demo.LogInfo("-> Get orders", "pairName", results["pairName"], "orderID", results["orderID"])
demo.LogInfo("-> Get orders", "pair_name", results["pair_name"], "order_id", results["order_id"])
// put message on channel
callRPC(result, "orderbook_getOrder", results["pairName"], results["orderID"])
callRPC(result, "orderbook_getOrder", results["pair_name"], results["order_id"])
// case "updateOrder":
// level, _ := strconv.ParseUint(results["level"], 10, 8)
// time, _ := strconv.ParseUint(results["time"], 10, 64)
@@ -253,10 +253,10 @@ func Start() error {
// logResult(result, err)
case "getBestAskList":
demo.LogInfo("-> Best ask list:")
callRPC(result, "orderbook_getBestAskList", results["pairName"])
callRPC(result, "orderbook_getBestAskList", results["pair_name"])
case "getBestBidList":
demo.LogInfo("-> Best bid list:")
callRPC(result, "orderbook_getBestBidList", results["pairName"])
callRPC(result, "orderbook_getBestBidList", results["pair_name"])
default:
demo.LogInfo(fmt.Sprintf("-> Unknown command: %s\n", command.Name))
}
@@ -61,9 +61,15 @@ func initPrompt(privateKeyName string) {
nodeaddr = "enode://655b231711df566a1bbf8f62dd0abaad71a1baa2c4bc865cae1691431bff2d9185fb66c99b982e20fd0fd562ced2c1ced96bd3e1daba0235870dfce0663a3483@127.0.0.1:30100?discport=0"
}

cancelOrderArguments := []terminal.Argument{
{Name: "order_id", Value: "1"},
{Name: "pair_name", Value: "TOMO/WETH"},
{Name: "side", Value: orderbook.Ask},
{Name: "price", Value: "100"},
}

orderArguments := []terminal.Argument{
{Name: "id", Value: "1"},
{Name: "pairName", Value: "TOMO/WETH"},
{Name: "pair_name", Value: "TOMO/WETH"},
{Name: "type", Value: "limit"},
{Name: "side", Value: orderbook.Ask},
{Name: "quantity", Value: "10"},
@@ -77,24 +83,27 @@ func initPrompt(privateKeyName string) {
{Name: "trade_id", Value: "1"},
}

updateOrderArguments := append([]terminal.Argument{
{Name: "order_id", Value: "1"},
}, orderArguments...)

// init prompt commands
commands = []terminal.Command{
{
Name: "processOrder",
Name: "addOrder",
Arguments: orderArguments,
Description: "Process order and store on swarm network",
Description: "Add order",
},
{
Name: "updateOrder",
Arguments: updateOrderArguments,
Description: "Update order, order_id must greater than 0",
},
{
Name: "cancelOrder",
Arguments: cancelOrderArguments,
Description: "Cancel order, order_id must greater than 0",
},
// {
// Name: "publicKey",
// Description: "Get public key",
// },
// {
// Name: "addNode",
// Arguments: []terminal.Argument{
// {Name: "nodeaddr", Value: nodeaddr},
// },
// Description: "Add node to seed",
// },
{
Name: "nodeAddr",
Description: "Get Node address",
@@ -195,15 +204,19 @@ func Start(p2pPort int, httpPort int, wsPort int, name string, privateKey string

// process command
switch command.Name {
case "processOrder":
case "addOrder":
demo.LogInfo("-> Add order", "payload", results)
// put message on channel
results["order_id"] = "0"
go processOrder(results)

// case "addNode":
// nodeaddr := results["nodeaddr"]
// demo.LogInfo(fmt.Sprintf("-> Add node: %s\n", nodeaddr))
// addNode(nodeaddr)
case "updateOrder":
demo.LogInfo("-> Update order", "payload", results)
// put message on channel
go processOrder(results)
case "cancelOrder":
demo.LogInfo("-> Cancel order", "payload", results)
// put message on channel
go cancelOrder(results)

case "nodeAddr":
demo.LogInfo(fmt.Sprintf("-> Node Address: %s\n", nodeAddr()))
@@ -261,7 +274,25 @@ func processOrder(payload map[string]string) error {

}

return nil
return err
}

func cancelOrder(payload map[string]string) error {
// add order at this current node first
// get timestamp in milliseconds
payload["timestamp"] = strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
msg, err := protocol.NewOrderbookMsg(payload)
if err == nil {
// try to store into model, if success then process at local and broad cast
err := orderbookEngine.CancelOrder(payload)
demo.LogInfo("Orderbook cancel result", "err", err, "msg", msg)

// broad cast message
msgC <- msg

}

return err
}

func updateEthService() {
@@ -3,7 +3,10 @@ package orderbook
import (
"fmt"
"math/big"
"strconv"
"strings"

demo "github.com/tomochain/orderbook/common"
)

// Engine : singleton orderbook for testing
@@ -80,15 +83,47 @@ func (engine *Engine) GetOrder(pairName, orderID string) *Order {

func (engine *Engine) ProcessOrder(quote map[string]string) ([]map[string]string, map[string]string) {

ob, _ := engine.getAndCreateIfNotExisted(quote["pairName"])
ob, _ := engine.getAndCreateIfNotExisted(quote["pair_name"])
var trades []map[string]string
var orderInBook map[string]string

if ob != nil {
// get map as general input, we can set format later to make sure there is no problem
trades, orderInBook = ob.ProcessOrder(quote, true)
orderID, err := strconv.ParseUint(quote["order_id"], 10, 64)
if err == nil {
// insert
if orderID == 0 {
demo.LogInfo("Process order")
trades, orderInBook = ob.ProcessOrder(quote, true)
} else {
demo.LogInfo("Update order")
err = ob.UpdateOrder(quote)
if err != nil {
demo.LogInfo("Update order failed", "quote", quote, "err", err)
}
}
}

}

return trades, orderInBook

}

func (engine *Engine) CancelOrder(quote map[string]string) error {
ob, err := engine.getAndCreateIfNotExisted(quote["pair_name"])
if ob != nil {
orderID, err := strconv.ParseUint(quote["order_id"], 10, 64)
if err == nil {

price, ok := new(big.Int).SetString(quote["price"], 10)
if !ok {
return fmt.Errorf("Price is not correct :%s", quote["price"])
}

return ob.CancelOrder(quote["side"], orderID, price)
}
}

return err
}
@@ -92,7 +92,7 @@ func (order *Order) UpdateQuantity(orderList *OrderList, newQuantity *big.Int, n
orderList.Item.Volume = Sub(orderList.Item.Volume, Sub(order.Item.Quantity, newQuantity))
order.Item.Timestamp = newTimestamp
order.Item.Quantity = CloneBigInt(newQuantity)
// fmt.Println("QUANTITY", order.Item.Quantity.String())
fmt.Println("QUANTITY", order.Item.Quantity.String())
orderList.SaveOrder(order)
orderList.Save()
}
@@ -389,15 +389,16 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,
return quantityToTrade, trades
}

// CancelOrder : cancel the order
func (orderBook *OrderBook) CancelOrder(side string, orderID int, price *big.Int) {
// CancelOrder : cancel the order, just need ID, side and price, of course order must belong
// to a price point as well
func (orderBook *OrderBook) CancelOrder(side string, orderID uint64, price *big.Int) error {
orderBook.UpdateTime()
key := GetKeyFromBig(big.NewInt(int64(orderID)))

var err error
if side == Bid {
order := orderBook.Bids.GetOrder(key, price)
if order != nil {
orderBook.Bids.RemoveOrder(order)
_, err = orderBook.Bids.RemoveOrder(order)
}
// if orderBook.Bids.OrderExist(key, price) {
// orderBook.Bids.RemoveOrder(order)
@@ -406,37 +407,54 @@ func (orderBook *OrderBook) CancelOrder(side string, orderID int, price *big.Int

order := orderBook.Asks.GetOrder(key, price)
if order != nil {
orderBook.Asks.RemoveOrder(order)
_, err = orderBook.Asks.RemoveOrder(order)
}

// if orderBook.Asks.OrderExist(key) {
// orderBook.Asks.RemoveOrder(order)
// }
}

return err
}

func (orderBook *OrderBook) UpdateOrder(quoteUpdate map[string]string) error {
orderID, err := strconv.ParseUint(quoteUpdate["order_id"], 10, 64)
if err == nil {
price, ok := new(big.Int).SetString(quoteUpdate["price"], 10)
if !ok {
return fmt.Errorf("Price is not correct :%s", quoteUpdate["price"])
}

return orderBook.ModifyOrder(quoteUpdate, orderID, price)
}
return err
}

// ModifyOrder : modify the order
func (orderBook *OrderBook) ModifyOrder(quoteUpdate map[string]string, orderID int, price *big.Int) {
func (orderBook *OrderBook) ModifyOrder(quoteUpdate map[string]string, orderID uint64, price *big.Int) error {
orderBook.UpdateTime()

side := quoteUpdate["side"]
quoteUpdate["order_id"] = strconv.Itoa(orderID)
quoteUpdate["order_id"] = strconv.FormatUint(orderID, 10)
quoteUpdate["timestamp"] = strconv.FormatUint(orderBook.Item.Timestamp, 10)
key := GetKeyFromBig(ToBigInt(quoteUpdate["order_id"]))
if side == Bid {

if orderBook.Bids.OrderExist(key, price) {
orderBook.Bids.UpdateOrder(quoteUpdate)
return orderBook.Bids.UpdateOrder(quoteUpdate)
}
// if orderBook.Bids.OrderExist(key) {
// orderBook.Bids.UpdateOrder(quoteUpdate)
// }
} else {

if orderBook.Asks.OrderExist(key, price) {
orderBook.Asks.UpdateOrder(quoteUpdate)
return orderBook.Asks.UpdateOrder(quoteUpdate)
}
}

return nil
}

// VolumeAtPrice : get volume at the current price
@@ -343,7 +343,7 @@ func (orderTree *OrderTree) InsertOrder(quote map[string]string) error {
}

// UpdateOrder : update an order
func (orderTree *OrderTree) UpdateOrder(quote map[string]string) {
func (orderTree *OrderTree) UpdateOrder(quote map[string]string) error {
// order := orderTree.OrderMap[quote["order_id"]]

price := ToBigInt(quote["price"])
@@ -382,7 +382,7 @@ func (orderTree *OrderTree) UpdateOrder(quote map[string]string) {
orderTree.Item.Volume = Add(orderTree.Item.Volume, Sub(order.Item.Quantity, originalQuantity))

// should use batch to optimize the performance
orderTree.Save()
return orderTree.Save()
}

func (orderTree *OrderTree) RemoveOrderFromOrderList(order *Order, orderList *OrderList) error {
Oops, something went wrong.

0 comments on commit 6e49f5b

Please sign in to comment.
You can’t perform that action at this time.