Skip to content

Commit

Permalink
Merge pull request ethereum#503 from ngtuna/cancel-order-1
Browse files Browse the repository at this point in the history
Cancel order 1
  • Loading branch information
ngtuna authored May 6, 2019
2 parents 8940560 + 4e6f5f9 commit d7f4633
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 142 deletions.
2 changes: 1 addition & 1 deletion tomox/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (api *PublicTomoXAPI) CancelOrder(ctx context.Context, req NewMessage) (boo
return false, err
}
//set cancel signature to the order payload
payload.Type = "CANCEL"
payload.Status = "CANCELLED"
//then encode it again
params.Payload, err = json.Marshal(payload)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tomox/batchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewBatchDatabaseWithEncode(datadir string, cacheLimit, maxPending int, enco
cacheItems, _ := lru.New(defaultCacheLimit)

batchDB := &BatchDatabase{
db: db,
db: db,
//EncodeToBytes: encode,
//DecodeBytes: decode,
itemCacheLimit: itemCacheLimit,
Expand Down
64 changes: 32 additions & 32 deletions tomox/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func DecodeBytesNodeItem(bytes []byte, item *Item) error {

// Order item
func EncodeBytesOrderItem(item *OrderItem) ([]byte, error) {
start := 2 * common.HashLength //Quantity, Price
start := 2 * common.HashLength //Quantity, Price
totalLength := start + 4*common.AddressLength //ExchangeAddress,UserAddress,BaseToken,QuoteToken
totalLength += 15 // maxlength item.Status = 15
totalLength += 4 // maxlength item.Side = 4
totalLength += 6 // maxlength item.Type = 6
totalLength += common.HashLength //hash
totalLength += 1 //Signature.V = 1 byte
totalLength += 2*common.HashLength //Signature.R, Signature.S
totalLength += 4*common.HashLength //FilledAmount, Nonce, MakeFee, TakeFee
totalLength += len(item.PairName) //PairName
totalLength += 15 // maxlength item.Status = 15
totalLength += 4 // maxlength item.Side = 4
totalLength += 6 // maxlength item.Type = 6
totalLength += common.HashLength //hash
totalLength += 1 //Signature.V = 1 byte
totalLength += 2 * common.HashLength //Signature.R, Signature.S
totalLength += 4 * common.HashLength //FilledAmount, Nonce, MakeFee, TakeFee
totalLength += len(item.PairName) //PairName
// uint64 and int64 are 8 byte
totalLength += 3*8 // CreatedAt, UpdatedAt, OrderID
totalLength += 3*common.HashLength // next, prev, orderlist
totalLength += 3 * 8 // CreatedAt, UpdatedAt, OrderID
totalLength += 3 * common.HashLength // next, prev, orderlist

returnBytes := make([]byte, totalLength)

Expand All @@ -93,13 +93,13 @@ func EncodeBytesOrderItem(item *OrderItem) ([]byte, error) {
copy(returnBytes[start:start+common.AddressLength], item.QuoteToken[:])
start += common.AddressLength

copy(returnBytes[start:start+15],[]byte(item.Status))
copy(returnBytes[start:start+15], []byte(item.Status))
start += 15

copy(returnBytes[start:start+4],[]byte(item.Side))
copy(returnBytes[start:start+4], []byte(item.Side))
start += 4

copy(returnBytes[start:start+6],[]byte(item.Type))
copy(returnBytes[start:start+6], []byte(item.Type))
start += 6

copy(returnBytes[start:start+common.HashLength], item.Hash.Bytes())
Expand Down Expand Up @@ -160,61 +160,61 @@ func DecodeBytesOrderItem(bytes []byte, item *OrderItem) error {
item.Price.SetBytes(bytes[start : start+common.HashLength])
start += common.HashLength

item.ExchangeAddress = common.BytesToAddress(bytes[start:start+common.AddressLength])
item.ExchangeAddress = common.BytesToAddress(bytes[start : start+common.AddressLength])
start += common.AddressLength
item.UserAddress = common.BytesToAddress(bytes[start:start+common.AddressLength])
item.UserAddress = common.BytesToAddress(bytes[start : start+common.AddressLength])
start += common.AddressLength
item.BaseToken = common.BytesToAddress(bytes[start:start+common.AddressLength])
item.BaseToken = common.BytesToAddress(bytes[start : start+common.AddressLength])
start += common.AddressLength
item.QuoteToken = common.BytesToAddress(bytes[start:start+common.AddressLength])
item.QuoteToken = common.BytesToAddress(bytes[start : start+common.AddressLength])
start += common.AddressLength

item.Status = string(bytes[start:start+15])
item.Status = string(bytes[start : start+15])
start += 15
item.Side = string(bytes[start:start+4])
item.Side = string(bytes[start : start+4])
start += 4
item.Type = string(bytes[start:start+6])
item.Type = string(bytes[start : start+6])
start += 6

item.Hash = common.BytesToHash(bytes[start:start+common.HashLength])
item.Hash = common.BytesToHash(bytes[start : start+common.HashLength])
start += common.HashLength

if item.Signature == nil {
item.Signature = &Signature{}
}
item.Signature.V = bytes[start]
start += 1
item.Signature.R = common.BytesToHash(bytes[start:start+common.HashLength])
item.Signature.R = common.BytesToHash(bytes[start : start+common.HashLength])
start += common.HashLength
item.Signature.S = common.BytesToHash(bytes[start:start+common.HashLength])
item.Signature.S = common.BytesToHash(bytes[start : start+common.HashLength])
start += common.HashLength

item.FilledAmount = new(big.Int)
item.FilledAmount.SetBytes(bytes[start:start+common.HashLength])
item.FilledAmount.SetBytes(bytes[start : start+common.HashLength])
start += common.HashLength

item.Nonce = new(big.Int)
item.Nonce.SetBytes(bytes[start:start+common.HashLength])
item.Nonce.SetBytes(bytes[start : start+common.HashLength])
start += common.HashLength

item.MakeFee = new(big.Int)
item.MakeFee.SetBytes(bytes[start:start+common.HashLength])
item.MakeFee.SetBytes(bytes[start : start+common.HashLength])
start += common.HashLength

item.TakeFee = new(big.Int)
item.TakeFee.SetBytes(bytes[start:start+common.HashLength])
item.TakeFee.SetBytes(bytes[start : start+common.HashLength])
start += common.HashLength

item.PairName = string(bytes[start:start+86])
item.PairName = string(bytes[start : start+86])
start += 86

item.CreatedAt = uint64(binary.BigEndian.Uint64(bytes[start:start+8]))
item.CreatedAt = uint64(binary.BigEndian.Uint64(bytes[start : start+8]))
start += 8

item.UpdatedAt = uint64(binary.BigEndian.Uint64(bytes[start:start+8]))
item.UpdatedAt = uint64(binary.BigEndian.Uint64(bytes[start : start+8]))
start += 8

item.OrderID = uint64(binary.BigEndian.Uint64(bytes[start:start+8]))
item.OrderID = uint64(binary.BigEndian.Uint64(bytes[start : start+8]))
start += 8

// pointers
Expand Down
99 changes: 6 additions & 93 deletions tomox/mongodb_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,6 @@ import (
"github.com/tomochain/tomodex/utils/math"
)

func EncodeNodeItem(item *Item) (interface{}, error) {
n := ItemBSON{
Keys: &KeyMetaBSON{
Left: common.Bytes2Hex(item.Keys.Left),
Right: common.Bytes2Hex(item.Keys.Right),
Parent: common.Bytes2Hex(item.Keys.Parent),
},
Value: common.Bytes2Hex(item.Value),
Color: item.Color,
}

return n, nil
}

func EncodeOrderItem(o *OrderItem) (interface{}, error) {
or := OrderItemBSON{
PairName: o.PairName,
ExchangeAddress: o.ExchangeAddress.Hex(),
UserAddress: o.UserAddress.Hex(),
BaseToken: o.BaseToken.Hex(),
QuoteToken: o.QuoteToken.Hex(),
Status: o.Status,
Side: o.Side,
Type: o.Type,
Hash: o.Hash.Hex(),
Quantity: o.Quantity.String(),
Price: o.Price.String(),
Nonce: o.Nonce.String(),
MakeFee: o.MakeFee.String(),
TakeFee: o.TakeFee.String(),
CreatedAt: strconv.FormatUint(o.CreatedAt, 10),
UpdatedAt: strconv.FormatUint(o.UpdatedAt, 10),
}

if o.FilledAmount != nil {
or.FilledAmount = o.FilledAmount.String()
}

if o.Signature != nil {
or.Signature = &SignatureRecord{
V: o.Signature.V,
R: o.Signature.R.Hex(),
S: o.Signature.S.Hex(),
}
}

return or, nil
}

func EncodeOrderListItem(item *OrderListItem) (interface{}, error) {

return nil, nil
}

func EncodeOrderTreeItem(oti *OrderTreeItem) (interface{}, error) {
otib := OrderTreeItemBSON{
Volume: oti.Volume.String(),
NumOrders: strconv.FormatUint(oti.NumOrders, 10),
PriceTreeKey: common.Bytes2Hex(oti.PriceTreeKey),
PriceTreeSize: strconv.FormatUint(oti.PriceTreeSize, 10),
}

return otib, nil
}

func EncodeOrderBookItem(item *OrderBookItem) (interface{}, error) {

return nil, nil
}

func EncodeItem(val interface{}) (interface{}, error) {
switch val.(type) {
case *Item:
return EncodeNodeItem(val.(*Item))
case *OrderItem:
return EncodeOrderItem(val.(*OrderItem))
case *OrderListItem:
return EncodeOrderListItem(val.(*OrderListItem))
case *OrderTreeItem:
return EncodeOrderTreeItem(val.(*OrderTreeItem))
case *OrderBookItem:
return EncodeOrderBookItem(val.(*OrderBookItem))
default:
return nil, nil
}
}

func (nir *ItemRecord) GetBSON() (interface{}, error) {
irb := ItemRecordBSON{
Key: nir.Key,
Expand Down Expand Up @@ -191,8 +104,8 @@ func (o *OrderItem) SetBSON(raw bson.Raw) error {
Side string `json:"side" bson:"side"`
Type string `json:"type" bson:"type"`
Hash string `json:"hash" bson:"hash"`
PricePoint string `json:"pricepoint" bson:"pricepoint"`
Amount string `json:"amount" bson:"amount"`
Price string `json:"price" bson:"price"`
Quantity string `json:"quantity" bson:"quantity"`
FilledAmount string `json:"filledAmount" bson:"filledAmount"`
Nonce string `json:"nonce" bson:"nonce"`
MakeFee string `json:"makeFee" bson:"makeFee"`
Expand Down Expand Up @@ -226,16 +139,16 @@ func (o *OrderItem) SetBSON(raw bson.Raw) error {
o.Type = decoded.Type
o.Hash = common.HexToHash(decoded.Hash)

if decoded.Amount != "" {
o.Quantity = math.ToBigInt(decoded.Amount)
if decoded.Quantity != "" {
o.Quantity = math.ToBigInt(decoded.Quantity)
}

if decoded.FilledAmount != "" {
o.FilledAmount = math.ToBigInt(decoded.FilledAmount)
}

if decoded.PricePoint != "" {
o.Price = math.ToBigInt(decoded.PricePoint)
if decoded.Price != "" {
o.Price = math.ToBigInt(decoded.Price)
}

if decoded.Signature != nil {
Expand Down
33 changes: 21 additions & 12 deletions tomox/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
Bid = "BUY"
Market = "market"
Limit = "limit"
Cancel = "CANCEL"
Cancel = "CANCELLED"

// we use a big number as segment for storing order, order list from order tree slot.
// as sequential id
Expand Down Expand Up @@ -343,24 +343,33 @@ func (orderBook *OrderBook) processOrderList(side string, orderList *OrderList,

// 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 {
func (orderBook *OrderBook) CancelOrder(order *OrderItem) error {
orderBook.UpdateTime()
key := GetKeyFromBig(big.NewInt(int64(orderID)))
key := GetKeyFromBig(big.NewInt(int64(order.OrderID)))
var err error
if side == Bid {
order := orderBook.Bids.GetOrder(key, price)
if order != nil {
_, err = orderBook.Bids.RemoveOrder(order)
if order.Side == Bid {
orderInDB := orderBook.Bids.GetOrder(key, order.Price)
if orderInDB == nil || orderInDB.Item.Hash != order.Hash {
return fmt.Errorf("Can't cancel order as it doesn't exist - order: %v", order)
}
orderInDB.Item.Status = Cancel
_, err = orderBook.Bids.RemoveOrder(orderInDB)
if err != nil {
return err
}
} else {

order := orderBook.Asks.GetOrder(key, price)
if order != nil {
_, err = orderBook.Asks.RemoveOrder(order)
orderInDB := orderBook.Asks.GetOrder(key, order.Price)
if orderInDB == nil || orderInDB.Item.Hash != order.Hash {
return fmt.Errorf("Can't cancel order as it doesn't exist - order: %v", order)
}
orderInDB.Item.Status = Cancel
_, err = orderBook.Asks.RemoveOrder(orderInDB)
if err != nil {
return err
}
}

return err
return nil
}

func (orderBook *OrderBook) UpdateOrder(order *OrderItem) error {
Expand Down
4 changes: 3 additions & 1 deletion tomox/orderlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ func (orderList *OrderList) RemoveOrder(order *Order) error {
return nil
}

err := orderList.DeleteOrder(order)
//err := orderList.DeleteOrder(order)
//FIXME: instead of delete, we save order with CANCEL state
err := orderList.SaveOrder(order)

if err != nil {
// stop other operations
Expand Down
4 changes: 2 additions & 2 deletions tomox/tomox.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (tomox *TomoX) postEvent(envelope *Envelope, isP2P bool) error {
return err
}

if order.Type == Cancel {
if order.Status == Cancel {
err := tomox.CancelOrder(order)
if err != nil {
log.Error("Can't cancel order", "err", err)
Expand Down Expand Up @@ -643,7 +643,7 @@ func (tomox *TomoX) ProcessOrder(order *OrderItem) ([]map[string]string, *OrderI
func (tomox *TomoX) CancelOrder(order *OrderItem) error {
ob, err := tomox.getAndCreateIfNotExisted(order.PairName)
if ob != nil && err == nil {
return ob.CancelOrder(order.Side, order.OrderID, order.Price)
return ob.CancelOrder(order)
}

return err
Expand Down

0 comments on commit d7f4633

Please sign in to comment.