Skip to content

Commit

Permalink
re-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchon committed Dec 27, 2021
1 parent 7af3dff commit 6a96b6e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
30 changes: 19 additions & 11 deletions chain/exchange/types/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"math/big"
"reflect"
"regexp"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -85,29 +87,35 @@ func StringInSlice(a string, list *[]string) bool {
}

func IsValidSubaccountID(subaccountID string) (*common.Address, bool) {
if len(subaccountID) != 66 {
if !IsHexHash(subaccountID) {
return nil, false
}
subaccountIdBytes := common.FromHex(subaccountID)

if len(subaccountIdBytes) != common.HashLength {
return nil, false
}
addressBytes := subaccountIdBytes[:common.AddressLength]
if !common.IsHexAddress(common.Bytes2Hex(addressBytes)) {
return nil, false
}
address := common.BytesToAddress(addressBytes)
return &address, true
}

func IsValidOrderHash(orderHash string) bool {
if len(orderHash) != 66 {
return IsHexHash(orderHash)
}

// IsHexHash verifies whether a string can represent a valid hex-encoded hash or not.
func IsHexHash(s string) bool {
if !isHexString(s) {
return false
}

orderHashBytes := common.FromHex(orderHash)
return len(orderHashBytes) == common.HashLength
if strings.HasPrefix(s, "0x") {
return len(s) == 2*common.HashLength+2
}

return len(s) == 2*common.HashLength
}

func isHexString(str string) bool {
isMatched, _ := regexp.MatchString("^(0x)?[0-9a-fA-F]+$", str)
return isMatched
}

func BreachesMinimumTickSize(value sdk.Dec, minTickSize sdk.Dec) bool {
Expand Down
23 changes: 17 additions & 6 deletions chain/exchange/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
)

func (o *SpotOrder) ValidateBasic(senderAddr sdk.AccAddress) error {
if o.MarketId == "" {
if !IsHexHash(o.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId)
}
switch o.OrderType {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (o *OrderInfo) ValidateBasic(senderAddr sdk.AccAddress) error {
}

func (o *DerivativeOrder) ValidateBasic(senderAddr sdk.AccAddress) error {
if o.MarketId == "" {
if !IsHexHash(o.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId)
}
switch o.OrderType {
Expand All @@ -101,7 +101,7 @@ func (o *DerivativeOrder) ValidateBasic(senderAddr sdk.AccAddress) error {
}

func (o *OrderData) ValidateBasic(senderAddr sdk.AccAddress) error {
if o.MarketId == "" {
if !IsHexHash(o.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, o.MarketId)
}

Expand Down Expand Up @@ -878,9 +878,10 @@ func (msg *MsgIncreasePositionMargin) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender)
}

if msg.MarketId == "" {
if !IsHexHash(msg.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, msg.MarketId)
}

if !msg.Amount.IsPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
Expand Down Expand Up @@ -928,7 +929,7 @@ func (msg *MsgLiquidatePosition) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender)
}

if msg.MarketId == "" {
if !IsHexHash(msg.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, msg.MarketId)
}

Expand Down Expand Up @@ -975,7 +976,11 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error {
hasSubaccountIdForCancelAll := msg.SubaccountId != ""

if hasCancelAllMarketId && !hasSubaccountIdForCancelAll {
return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains cancel all market id but no subaccount id")
return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains cancel all marketIDs but no subaccountID")
}

if hasSubaccountIdForCancelAll && !hasCancelAllMarketId {
return sdkerrors.Wrap(ErrInvalidBatchMsgUpdate, "msg contains subaccountID but no cancel all marketIDs")
}

if hasSubaccountIdForCancelAll {
Expand Down Expand Up @@ -1011,6 +1016,9 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error {
if len(msg.SpotMarketIdsToCancelAll) > 0 && len(msg.SpotOrdersToCancel) > 0 {
seen := make(map[common.Hash]struct{})
for _, marketID := range msg.SpotMarketIdsToCancelAll {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
seen[common.HexToHash(marketID)] = struct{}{}
}

Expand All @@ -1024,6 +1032,9 @@ func (msg MsgBatchUpdateOrders) ValidateBasic() error {
if len(msg.DerivativeMarketIdsToCancelAll) > 0 && len(msg.DerivativeOrdersToCancel) > 0 {
seen := make(map[common.Hash]struct{})
for _, marketID := range msg.DerivativeMarketIdsToCancelAll {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
seen[common.HexToHash(marketID)] = struct{}{}
}

Expand Down
28 changes: 26 additions & 2 deletions chain/exchange/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (p *SpotMarketParamUpdateProposal) ProposalType() string {

// ValidateBasic returns ValidateBasic result of this proposal.
func (p *SpotMarketParamUpdateProposal) ValidateBasic() error {
if p.MarketId == "" {
if !IsHexHash(p.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, p.MarketId)
}
if p.MakerFeeRate == nil && p.TakerFeeRate == nil && p.RelayerFeeShareRate == nil && p.MinPriceTickSize == nil && p.MinQuantityTickSize == nil && p.Status == MarketStatus_Unspecified {
Expand Down Expand Up @@ -403,7 +403,7 @@ func (p *DerivativeMarketParamUpdateProposal) ProposalType() string {

// ValidateBasic returns ValidateBasic result of this proposal.
func (p *DerivativeMarketParamUpdateProposal) ValidateBasic() error {
if p.MarketId == "" {
if !IsHexHash(p.MarketId) {
return sdkerrors.Wrap(ErrMarketInvalid, p.MarketId)
}
if p.MakerFeeRate == nil &&
Expand Down Expand Up @@ -916,6 +916,18 @@ func (t *TradingRewardCampaignBoostInfo) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "boosted spot market ids is not matching spot market multipliers")
}

for _, marketID := range t.BoostedSpotMarketIds {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
}

for _, marketID := range t.BoostedDerivativeMarketIds {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
}

if len(t.BoostedDerivativeMarketIds) != len(t.DerivativeMarketMultipliers) {
return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "boosted derivative market ids is not matching derivative market multipliers")
}
Expand Down Expand Up @@ -971,6 +983,12 @@ func (c *TradingRewardCampaignInfo) ValidateBasic() error {
}
}

for _, marketID := range c.DisqualifiedMarketIds {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
}

hasDuplicatesInDisqualifiedMarkets := c != nil && HasDuplicates(c.DisqualifiedMarketIds)
if hasDuplicatesInDisqualifiedMarkets {
return sdkerrors.Wrap(ErrInvalidTradingRewardCampaign, "campaign contains duplicate disqualified market ids")
Expand Down Expand Up @@ -1061,6 +1079,12 @@ func (p *FeeDiscountProposal) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidFeeDiscountSchedule, "new fee discount schedule cannot have duplicate quote denoms")
}

for _, marketID := range p.Schedule.DisqualifiedMarketIds {
if !IsHexHash(marketID) {
return sdkerrors.Wrap(ErrMarketInvalid, marketID)
}
}

if HasDuplicates(p.Schedule.DisqualifiedMarketIds) {
return sdkerrors.Wrap(ErrInvalidFeeDiscountSchedule, "new fee discount schedule cannot have duplicate disqualified market ids")
}
Expand Down

0 comments on commit 6a96b6e

Please sign in to comment.