Skip to content

Commit

Permalink
add validate
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Mar 7, 2024
1 parent b228a28 commit f285fe4
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions x/crosschain/types/cctx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/zeta-chain/zetacore/common"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
)

Expand Down Expand Up @@ -46,6 +48,81 @@ func GetAllAuthzZetaclientTxTypes() []string {
}
}

func (m CrossChainTx) Validate() error {
if m.InboundTxParams == nil {
return fmt.Errorf("inbound tx params cannot be nil")
}
if m.OutboundTxParams == nil {
return fmt.Errorf("outbound tx params cannot be nil")
}
if m.CctxStatus == nil {
return fmt.Errorf("cctx status cannot be nil")
}
if len(m.OutboundTxParams) > 2 {
return fmt.Errorf("outbound tx params cannot be more than 2")
}
if len(m.Index) != 66 {
return ErrInvalidCCTXIndex
}

return nil
}

func (m InboundTxParams) Validate() error {
if m.Sender == "" {
return fmt.Errorf("sender cannot be empty")
}
if m.InboundTxObservedHash == "" {
return fmt.Errorf("inbound tx observed hash cannot be empty")
}
if len(m.InboundTxBallotIndex) != 66 {
return fmt.Errorf("inbound tx ballot index must be 66 characters")
}
if common.IsEthereumChain(m.SenderChainId) {
if !ethcommon.IsHexAddress(m.Sender) {
return fmt.Errorf("sender a valid ethereum address")
}
}
if m.Amount.IsNil() {
return fmt.Errorf("amount cannot be nil")
}
if common.IsBitcoinChain(m.SenderChainId) {
//if _, err := common.BitcoinAddressToPubKeyHash(m.Sender); err != nil {
// return fmt.Errorf("sender must be a valid bitcoin address")
//}
}
return nil
}

func (m OutboundTxParams) Validate() error {
if m.Receiver == "" {
return fmt.Errorf("receiver cannot be empty")
}
if m.Amount.IsNil() {
return fmt.Errorf("amount cannot be nil")
}
if m.OutboundTxGasPrice == "" {
return fmt.Errorf("outbound tx gas price cannot be empty")
}
if m.GasLimit == 0 {
return fmt.Errorf("gas limit cannot be 0")
}
if m.ReceiverChainId == 0 {
return fmt.Errorf("receiver chain id cannot be 0")
}
if common.IsEthereumChain(m.ReceiverChainId) {
if !ethcommon.IsHexAddress(m.Receiver) {
return fmt.Errorf("receiver must be a valid ethereum address")
}
}
if common.IsBitcoinChain(m.ReceiverChainId) {
//if _, err := common.BitcoinAddressToPubKeyHash(m.Receiver); err != nil {
// return fmt.Errorf("receiver must be a valid bitcoin address")
//}
}
return nil
}

// GetGasPrice returns the gas price of the outbound tx
func (m OutboundTxParams) GetGasPrice() (uint64, error) {
gasPrice, err := strconv.ParseUint(m.OutboundTxGasPrice, 10, 64)
Expand Down

0 comments on commit f285fe4

Please sign in to comment.