test(router_validation)_: integrated the new errors into test loigc

This commit is contained in:
Samuel Hawksby-Robinson 2024-07-02 11:25:30 +01:00
parent e4420533d6
commit 59853fdbe2
3 changed files with 59 additions and 73 deletions

View File

@ -1,47 +1,30 @@
package router
import (
"errors"
sErrors "github.com/status-im/status-go/errors"
"github.com/status-im/status-go/errors"
)
// Abbreviation `WR` for the error code stands for Wallet Router
var (
ErrUsernameAndPubKeyRequiredForENSRegister = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-001"), Details: "username and public key are required for ENSRegister"}
ErrOnlySTTSupportedForENSRegisterOnTestnet = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-002"), Details: "only STT is supported for ENSRegister on testnet"}
ErrOnlySTTSupportedForENSReleaseOnTestnet = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-003"), Details: "only STT is supported for ENSRelease on testnet"}
ErrUsernameRequiredForENSRelease = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-004"), Details: "username is required for ENSRelease"}
ErrUsernameAndPubKeyRequiredForENSSetPubKey = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-005"), Details: "username and public key are required for ENSSetPubKey"}
ErrPackIDRequiredForStickersBuy = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-006"), Details: "packID is required for StickersBuy"}
ErrToTokenIDRequiredForSwap = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-007"), Details: "toTokenID is required for Swap"}
ErrTokenIDAndToTokenIDDifferent = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-008"), Details: "tokenID and toTokenID must be different"}
ErrOnlyOneOfAmountInOrOutSet = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-009"), Details: "only one of amountIn or amountOut can be set"}
ErrAmountInMustBePositive = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-010"), Details: "amountIn must be positive"}
ErrAmountOutMustBePositive = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-011"), Details: "amountOut must be positive"}
ErrLockedAmountNotSupportedForNetwork = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-012"), Details: "locked amount is not supported for the selected network"}
ErrLockedAmountMustBePositive = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-013"), Details: "locked amount must be positive"}
ErrLockedAmountExceedsTotalSendAmount = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-014"), Details: "locked amount exceeds the total amount to send"}
ErrLockedAmountLessThanSendAmountAllNetworks = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-015"), Details: "locked amount is less than the total amount to send, but all networks are locked"}
ErrNotEnoughTokenBalance = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-016"), Details: "not enough token balance"}
ErrNotEnoughNativeBalance = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-017"), Details: "not enough native balance"}
ErrNativeTokenNotFound = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-018"), Details: "native token not found"}
ErrDisabledChainFoundAmongLockedNetworks = &sErrors.ErrorResponse{Code: sErrors.ErrorCode("WR-019"), Details: "disabled chain found among locked networks"}
)
var (
ErrorENSRegisterRequires = errors.New("username and public key are required for ENSRegister")
ErrorENSRegisterTestNetSTTOnly = errors.New("only STT is supported for ENSRegister on testnet")
ErrorENSRegisterSNTOnly = errors.New("only SNT is supported for ENSRegister")
ErrorENSReleaseRequires = errors.New("username is required for ENSRelease")
ErrorENSSetPubKeyRequires = errors.New("username and public key are required for ENSSetPubKey")
ErrorStickersBuyRequires = errors.New("packID is required for StickersBuy")
ErrorSwapRequires = errors.New("toTokenID is required for Swap")
ErrorSwapTokenIDMustBeDifferent = errors.New("tokenID and toTokenID must be different")
ErrorSwapAmountInAmountOutMustBeExclusive = errors.New("only one of amountIn or amountOut can be set")
ErrorSwapAmountInMustBePositive = errors.New("amountIn must be positive")
ErrorSwapAmountOutMustBePositive = errors.New("amountOut must be positive")
ErrorLockedAmountNotSupportedNetwork = errors.New("locked amount is not supported for the selected network")
ErrorLockedAmountNotNegative = errors.New("locked amount must not be negative")
ErrorLockedAmountExcludesAllSupported = errors.New("all supported chains are excluded, routing impossible")
ErrENSRegisterRequiresUsernameAndPubKey = &errors.ErrorResponse{Code: errors.ErrorCode("WR-001"), Details: "username and public key are required for ENSRegister"}
ErrENSRegisterTestnetSTTOnly = &errors.ErrorResponse{Code: errors.ErrorCode("WR-002"), Details: "only STT is supported for ENSRegister on testnet"}
ErrENSRegisterMainnetSNTOnly = &errors.ErrorResponse{Code: errors.ErrorCode("WR-003"), Details: "only SNT is supported for ENSRegister on mainnet"}
ErrENSReleaseRequiresUsername = &errors.ErrorResponse{Code: errors.ErrorCode("WR-004"), Details: "username is required for ENSRelease"}
ErrENSSetPubKeyRequiresUsernameAndPubKey = &errors.ErrorResponse{Code: errors.ErrorCode("WR-005"), Details: "username and public key are required for ENSSetPubKey"}
ErrStickersBuyRequiresPackID = &errors.ErrorResponse{Code: errors.ErrorCode("WR-006"), Details: "packID is required for StickersBuy"}
ErrSwapRequiresToTokenID = &errors.ErrorResponse{Code: errors.ErrorCode("WR-007"), Details: "toTokenID is required for Swap"}
ErrSwapTokenIDMustBeDifferent = &errors.ErrorResponse{Code: errors.ErrorCode("WR-008"), Details: "tokenID and toTokenID must be different"}
ErrSwapAmountInAmountOutMustBeExclusive = &errors.ErrorResponse{Code: errors.ErrorCode("WR-009"), Details: "only one of amountIn or amountOut can be set"}
ErrSwapAmountInMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WR-010"), Details: "amountIn must be positive"}
ErrSwapAmountOutMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WR-011"), Details: "amountOut must be positive"}
ErrLockedAmountNotSupportedForNetwork = &errors.ErrorResponse{Code: errors.ErrorCode("WR-012"), Details: "locked amount is not supported for the selected network"}
ErrLockedAmountNotNegative = &errors.ErrorResponse{Code: errors.ErrorCode("WR-013"), Details: "locked amount must not be negative"}
ErrLockedAmountExceedsTotalSendAmount = &errors.ErrorResponse{Code: errors.ErrorCode("WR-014"), Details: "locked amount exceeds the total amount to send"}
ErrLockedAmountLessThanSendAmountAllNetworks = &errors.ErrorResponse{Code: errors.ErrorCode("WR-015"), Details: "locked amount is less than the total amount to send, but all networks are locked"}
ErrNotEnoughTokenBalance = &errors.ErrorResponse{Code: errors.ErrorCode("WR-016"), Details: "not enough token balance"}
ErrNotEnoughNativeBalance = &errors.ErrorResponse{Code: errors.ErrorCode("WR-017"), Details: "not enough native balance"}
ErrNativeTokenNotFound = &errors.ErrorResponse{Code: errors.ErrorCode("WR-018"), Details: "native token not found"}
ErrDisabledChainFoundAmongLockedNetworks = &errors.ErrorResponse{Code: errors.ErrorCode("WR-019"), Details: "disabled chain found among locked networks"}
ErrENSSetPubKeyInvalidUsername = &errors.ErrorResponse{Code: errors.ErrorCode("WR-020"), Details: "a valid username, ending in '.eth', is required for ENSSetPubKey"}
ErrLockedAmountExcludesAllSupported = &errors.ErrorResponse{Code: errors.ErrorCode("WR-021"), Details: "all supported chains are excluded, routing impossible"}
)

View File

@ -336,15 +336,15 @@ func findBestV2(routes [][]*PathV2, tokenPrice float64, nativeChainTokenPrice fl
func validateInputData(input *RouteInputParams) error {
if input.SendType == ENSRegister {
if input.Username == "" || input.PublicKey == "" {
return ErrUsernameAndPubKeyRequiredForENSRegister
return ErrENSRegisterRequiresUsernameAndPubKey
}
if input.testnetMode {
if input.TokenID != pathprocessor.SttSymbol {
return ErrOnlySTTSupportedForENSRegisterOnTestnet
return ErrENSRegisterTestnetSTTOnly
}
} else {
if input.TokenID != pathprocessor.SntSymbol {
return ErrOnlySTTSupportedForENSReleaseOnTestnet
return ErrENSRegisterMainnetSNTOnly
}
}
return nil
@ -352,43 +352,47 @@ func validateInputData(input *RouteInputParams) error {
if input.SendType == ENSRelease {
if input.Username == "" {
return ErrUsernameRequiredForENSRelease
return ErrENSReleaseRequiresUsername
}
}
if input.SendType == ENSSetPubKey {
if input.Username == "" || input.PublicKey == "" || ens.ValidateENSUsername(input.Username) != nil {
return ErrUsernameAndPubKeyRequiredForENSSetPubKey
if input.Username == "" || input.PublicKey == "" {
return ErrENSSetPubKeyRequiresUsernameAndPubKey
}
if ens.ValidateENSUsername(input.Username) != nil {
return ErrENSSetPubKeyInvalidUsername
}
}
if input.SendType == StickersBuy {
if input.PackID == nil {
return ErrPackIDRequiredForStickersBuy
return ErrStickersBuyRequiresPackID
}
}
if input.SendType == Swap {
if input.ToTokenID == "" {
return ErrToTokenIDRequiredForSwap
return ErrSwapRequiresToTokenID
}
if input.TokenID == input.ToTokenID {
return ErrTokenIDAndToTokenIDDifferent
return ErrSwapTokenIDMustBeDifferent
}
// we can do this check, cause AmountIn is required in `RouteInputParams`
if input.AmountIn.ToInt().Cmp(pathprocessor.ZeroBigIntValue) > 0 &&
input.AmountOut != nil &&
input.AmountOut.ToInt().Cmp(pathprocessor.ZeroBigIntValue) > 0 {
return ErrOnlyOneOfAmountInOrOutSet
return ErrSwapAmountInAmountOutMustBeExclusive
}
if input.AmountIn.ToInt().Sign() < 0 {
return ErrAmountInMustBePositive
return ErrSwapAmountInMustBePositive
}
if input.AmountOut != nil && input.AmountOut.ToInt().Sign() < 0 {
return ErrAmountOutMustBePositive
return ErrSwapAmountOutMustBePositive
}
}
@ -419,7 +423,7 @@ func validateInputData(input *RouteInputParams) error {
totalLockedAmount = new(big.Int).Add(totalLockedAmount, amount.ToInt())
if amount == nil || amount.ToInt().Sign() < 0 {
return ErrLockedAmountMustBePositive
return ErrLockedAmountNotNegative
}
}

View File

@ -3,7 +3,6 @@ package router
import (
"context"
"database/sql"
"errors"
"math/big"
"testing"
@ -2636,7 +2635,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: pathprocessor.SttSymbol,
testnetMode: true,
},
expectedError: errors.New("username and public key are required for ENSRegister"),
expectedError: ErrENSRegisterRequiresUsernameAndPubKey,
},
{
name: "ENSRegister missing public key",
@ -2646,7 +2645,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: pathprocessor.SttSymbol,
testnetMode: true,
},
expectedError: errors.New("username and public key are required for ENSRegister"),
expectedError: ErrENSRegisterRequiresUsernameAndPubKey,
},
{
name: "ENSRegister invalid token on testnet",
@ -2657,7 +2656,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: "invalidtoken",
testnetMode: true,
},
expectedError: errors.New("only STT is supported for ENSRegister on testnet"),
expectedError: ErrENSRegisterTestnetSTTOnly,
},
{
name: "ENSRegister invalid token on mainnet",
@ -2667,7 +2666,7 @@ func TestValidateInputData(t *testing.T) {
PublicKey: "validpublickey",
TokenID: "invalidtoken",
},
expectedError: errors.New("only SNT is supported for ENSRegister"),
expectedError: ErrENSRegisterMainnetSNTOnly,
},
{
name: "ENSRelease valid data",
@ -2682,7 +2681,7 @@ func TestValidateInputData(t *testing.T) {
input: &RouteInputParams{
SendType: ENSRelease,
},
expectedError: errors.New("username is required for ENSRelease"),
expectedError: ErrENSReleaseRequiresUsername,
},
{
name: "ENSSetPubKey valid data",
@ -2699,7 +2698,7 @@ func TestValidateInputData(t *testing.T) {
SendType: ENSSetPubKey,
PublicKey: "validpublickey",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrENSSetPubKeyRequiresUsernameAndPubKey,
},
{
name: "ENSSetPubKey missing public key",
@ -2707,7 +2706,7 @@ func TestValidateInputData(t *testing.T) {
SendType: ENSSetPubKey,
Username: "validusername",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrENSSetPubKeyRequiresUsernameAndPubKey,
},
{
name: "ENSSetPubKey invalid ENS username",
@ -2716,21 +2715,21 @@ func TestValidateInputData(t *testing.T) {
Username: "invalidusername",
PublicKey: "validpublickey",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrENSSetPubKeyInvalidUsername,
},
{
name: "StickersBuy missing packID",
input: &RouteInputParams{
SendType: StickersBuy,
},
expectedError: errors.New("packID is required for StickersBuy"),
expectedError: ErrStickersBuyRequiresPackID,
},
{
name: "Swap missing toTokenID",
input: &RouteInputParams{
SendType: Swap,
},
expectedError: errors.New("toTokenID is required for Swap"),
expectedError: ErrSwapRequiresToTokenID,
},
{
name: "Swap tokenID equal to toTokenID",
@ -2739,7 +2738,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: "token",
ToTokenID: "token",
},
expectedError: errors.New("tokenID and toTokenID must be different"),
expectedError: ErrSwapTokenIDMustBeDifferent,
},
{
name: "Swap both amountIn and amountOut set",
@ -2750,7 +2749,7 @@ func TestValidateInputData(t *testing.T) {
AmountIn: (*hexutil.Big)(big.NewInt(100)),
AmountOut: (*hexutil.Big)(big.NewInt(100)),
},
expectedError: errors.New("only one of amountIn or amountOut can be set"),
expectedError: ErrSwapAmountInAmountOutMustBeExclusive,
},
{
name: "Swap negative amountIn",
@ -2760,7 +2759,7 @@ func TestValidateInputData(t *testing.T) {
ToTokenID: "token2",
AmountIn: (*hexutil.Big)(big.NewInt(-100)),
},
expectedError: errors.New("amountIn must be positive"),
expectedError: ErrSwapAmountInMustBePositive,
},
{
name: "Swap negative amountOut",
@ -2770,7 +2769,7 @@ func TestValidateInputData(t *testing.T) {
ToTokenID: "token2",
AmountOut: (*hexutil.Big)(big.NewInt(-100)),
},
expectedError: errors.New("amountOut must be positive"),
expectedError: ErrSwapAmountOutMustBePositive,
},
{
name: "fromLockedAmount with supported network on testnet",
@ -2799,7 +2798,7 @@ func TestValidateInputData(t *testing.T) {
},
testnetMode: true,
},
expectedError: errors.New("locked amount is not supported for the selected network"),
expectedError: ErrLockedAmountNotSupportedForNetwork,
},
{
name: "fromLockedAmount with unsupported network on testnet",
@ -2809,7 +2808,7 @@ func TestValidateInputData(t *testing.T) {
},
testnetMode: true,
},
expectedError: errors.New("locked amount is not supported for the selected network"),
expectedError: ErrLockedAmountNotSupportedForNetwork,
},
{
name: "fromLockedAmount with unsupported network on mainnet",
@ -2818,7 +2817,7 @@ func TestValidateInputData(t *testing.T) {
999: (*hexutil.Big)(big.NewInt(10)),
},
},
expectedError: errors.New("locked amount is not supported for the selected network"),
expectedError: ErrLockedAmountNotSupportedForNetwork,
},
{
name: "fromLockedAmount with negative amount",
@ -2827,7 +2826,7 @@ func TestValidateInputData(t *testing.T) {
walletCommon.EthereumMainnet: (*hexutil.Big)(big.NewInt(-10)),
},
},
expectedError: errors.New("locked amount must not be negative"),
expectedError: ErrLockedAmountNotNegative,
},
{
name: "fromLockedAmount with zero amount",
@ -2857,7 +2856,7 @@ func TestValidateInputData(t *testing.T) {
walletCommon.ArbitrumMainnet: (*hexutil.Big)(big.NewInt(0)),
},
},
expectedError: errors.New("all supported chains are excluded, routing impossible"),
expectedError: ErrLockedAmountExcludesAllSupported,
},
{
name: "fromLockedAmount with all supported test networks with zero amount",
@ -2869,7 +2868,7 @@ func TestValidateInputData(t *testing.T) {
},
testnetMode: true,
},
expectedError: errors.New("all supported chains are excluded, routing impossible"),
expectedError: ErrLockedAmountExcludesAllSupported,
},
}