add function

This commit is contained in:
Callum Waters
2022-10-17 16:57:28 +03:00
parent 3b4c1b903a
commit 43ce473402
2 changed files with 36 additions and 11 deletions
+26 -10
View File
@@ -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{}
}
+10 -1
View File
@@ -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
}