diff --git a/abci/example/orderbook/app.go b/abci/example/orderbook/app.go index 495c8540b..9b521f30a 100644 --- a/abci/example/orderbook/app.go +++ b/abci/example/orderbook/app.go @@ -96,7 +96,7 @@ func (sm *StateMachine) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx } // check if account exists - if _, ok := sm.accounts[m.MsgAsk.AskOrder.Owner]; !ok { + if _, ok := sm.accounts[m.MsgAsk.AskOrder.OwnerId]; !ok { return types.ResponseCheckTx{Code: 4} } @@ -112,8 +112,8 @@ func (sm *StateMachine) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx } // check the account has a enough quantity - if err := m.MsgAsk.AskOrder.Quantity >= sm.commodities[m.MsgAsk.Pair.SellersDenomination].Quantity; !ok { - return types.ResponseCheckTx{Code: 4, Log: err.Error()} + if m.MsgAsk.AskOrder.Quantity > sm.commodities[m.MsgAsk.Pair.SellersDenomination].Quantity { + return types.ResponseCheckTx{Code: 4} } default: @@ -162,21 +162,21 @@ func (sm *StateMachine) ApplySnapshotChunk(req types.RequestApplySnapshotChunk) func (sm *StateMachine) PrepareProposal(req types.RequestPrepareProposal) types.ResponsePrepareProposal { // fetch and match all the bids and asks for each market - for _, market := range sm.markets { - tradeSet, err := market.Match() + // for _, market := range sm.markets { + // tradeSet, err := market.Match() - for _, matchedOrder := range tradeSet.MatchedOrders { + // for _, matchedOrder := range tradeSet.MatchedOrders { - // validate the trade: - // does the buyer and seller have sufficient funds + // // validate the trade: + // // does the buyer and seller have sufficient funds - // add it to the set of txs + // // add it to the set of txs - } + // } - txs = append(txs, trades) - } + // txs = append(txs, trades) + // } // loop through the transactions provided by tendermint and look out for register pair and create account. // those should still be added. @@ -259,6 +259,8 @@ func (c *Commodity) ValidateBasic() error { if c.Quantity <= 0 { return errors.New("quantity must be greater than zero") } + + return nil } func (p *Pair) ValidateBasic() error { diff --git a/abci/example/orderbook/market.go b/abci/example/orderbook/market.go index ec768275c..c1571122e 100644 --- a/abci/example/orderbook/market.go +++ b/abci/example/orderbook/market.go @@ -16,17 +16,17 @@ func NewMarket(p Pair) *Market { return &Market{pair: p} } -func (m *Market) AddBid(b MsgBid) { +func (m *Market) AddBid(b OrderBid) { heap.Push(m.bidOrders, b) - if b.BidOrder.MaxPrice > m.highestBid { - m.highestBid = b.BidOrder.MaxPrice + if b.MaxPrice > m.highestBid { + m.highestBid = b.MaxPrice } } -func (m *Market) AddAsk(a MsgAsk) { +func (m *Market) AddAsk(a OrderAsk) { heap.Push(m.askOrders, a) - if a.AskOrder.AskPrice < m.lowestAsk { - m.lowestAsk = a.AskOrder.AskPrice + if a.AskPrice < m.lowestAsk { + m.lowestAsk = a.AskPrice } } diff --git a/abci/example/orderbook/market_test.go b/abci/example/orderbook/market_test.go new file mode 100644 index 000000000..498abf589 --- /dev/null +++ b/abci/example/orderbook/market_test.go @@ -0,0 +1,56 @@ +package orderbook_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/abci/example/orderbook" +) + +var testPair = orderbook.Pair{BuyersDenomination: "ATOM", SellersDenomination: "USD"} + +func TestTrackLowestAndHighestPrices(t *testing.T) { + market := orderbook.NewMarket(testPair) + require.Zero(t, market.LowestAsk()) + require.Zero(t, market.HighestBid()) + + market.AddBid(orderbook.OrderBid{MaxPrice: 100}) + require.Equal(t, 100, market.HighestBid()) + + market.AddAsk(orderbook.OrderAsk{AskPrice: 50}) + require.Equal(t, 50, market.LowestAsk()) + + market.AddAsk(orderbook.OrderAsk{AskPrice: 30}) + require.Equal(t, 30, market.LowestAsk()) + + market.AddAsk(orderbook.OrderAsk{AskPrice: 40}) + require.Equal(t, 30, market.LowestAsk()) +} + +func TestSimpleOrderMatching(t *testing.T) { + testcases := []struct { + bid orderbook.OrderBid + ask orderbook.OrderAsk + match bool + }{ + { + bid: orderbook.OrderBid{ + MaxPrice: 50, + MaxQuantity: 10, + }, + ask: orderbook.OrderAsk{ + AskPrice: 50, + Quantity: 10, + }, + match: true, + }, + } + + for idx, tc := range testcases { + market := orderbook.NewMarket(testPair) + market.AddAsk(tc.ask) + market.AddBid(tc.bid) + resp := market.Match() + require.Equal(t, tc.match, len(resp.MatchedOrders) == 1, idx) + } +} \ No newline at end of file