mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-18 21:26:20 +00:00
add function
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package orderbook
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
dbm "github.com/tendermint/tm-db"
|
||||
|
||||
@@ -19,7 +21,6 @@ type StateMachine struct {
|
||||
|
||||
// in-memory state
|
||||
accounts map[uint64]*Account
|
||||
commodities map[string]*Commodity
|
||||
|
||||
// ephemeral state (not used for the app hash) but for
|
||||
// convienience
|
||||
@@ -38,13 +39,6 @@ func (sm *StateMachine) Info(req types.RequestInfo) types.ResponseInfo {
|
||||
return types.ResponseInfo{}
|
||||
}
|
||||
|
||||
func (sm *StateMachine) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx {
|
||||
|
||||
// execute the trade i.e. update everyone's accounts
|
||||
|
||||
return types.ResponseDeliverTx{Code: 0}
|
||||
}
|
||||
|
||||
// CheckTx to be stateless
|
||||
func (sm *StateMachine) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx {
|
||||
var msg = new(Msg)
|
||||
@@ -92,7 +86,8 @@ func (sm *StateMachine) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx
|
||||
}
|
||||
|
||||
// check if account exists
|
||||
if _, ok := sm.accounts[m.MsgAsk.AskOrder.OwnerId]; !ok {
|
||||
account, ok := sm.accounts[m.MsgAsk.AskOrder.OwnerId]
|
||||
if !ok {
|
||||
return types.ResponseCheckTx{Code: 4}
|
||||
}
|
||||
|
||||
@@ -108,7 +103,16 @@ func (sm *StateMachine) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx
|
||||
}
|
||||
|
||||
// check the account has a enough quantity
|
||||
if m.MsgAsk.AskOrder.Quantity > sm.commodities[m.MsgAsk.Pair.SellersDenomination].Quantity {
|
||||
found := false
|
||||
for _, commodity := range account.Commodities {
|
||||
if commodity.Denom == m.MsgAsk.Pair.SellersDenomination {
|
||||
if m.MsgAsk.AskOrder.Quantity > commodity.Quantity {
|
||||
return types.ResponseCheckTx{Code: 4}
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return types.ResponseCheckTx{Code: 4}
|
||||
}
|
||||
|
||||
@@ -135,6 +139,18 @@ func (sm *StateMachine) BeginBlock(req types.RequestBeginBlock) types.ResponseBe
|
||||
return types.ResponseBeginBlock{}
|
||||
}
|
||||
|
||||
func (sm *StateMachine) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx {
|
||||
tradeSet := new(TradeSet)
|
||||
if err := proto.Unmarshal(req.Tx, tradeSet); err != nil {
|
||||
panic(fmt.Sprintf("unmarshalling tx: %w", err))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return types.ResponseDeliverTx{Code: 0}
|
||||
}
|
||||
|
||||
func (sm *StateMachine) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
|
||||
return types.ResponseEndBlock{}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package orderbook
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
)
|
||||
|
||||
@@ -111,5 +111,14 @@ func (o *OrderAsk) ValidateBasic() error {
|
||||
return errors.New("min price must be greater than 0")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Account) FindCommidity(denom string) *Commodity {
|
||||
for _, c := range a.Commodities {
|
||||
if c.Denom == denom {
|
||||
return c
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user