test(router_validation)_: Added Error consts for all tested paths

This commit is contained in:
Samuel Hawksby-Robinson 2024-06-18 10:35:14 +01:00
parent 79d0d08cfd
commit cdaca322d0
3 changed files with 49 additions and 37 deletions

View File

@ -1,28 +1,41 @@
package router
import (
"github.com/status-im/status-go/errors"
"errors"
sErrors "github.com/status-im/status-go/errors"
)
// Abbreviation `WR` for the error code stands for Wallet Router
var (
ErrUsernameAndPubKeyRequiredForENSRegister = &errors.ErrorResponse{Code: errors.ErrorCode("WR-001"), Details: "username and public key are required for ENSRegister"}
ErrOnlySTTSupportedForENSRegisterOnTestnet = &errors.ErrorResponse{Code: errors.ErrorCode("WR-002"), Details: "only STT is supported for ENSRegister on testnet"}
ErrOnlySTTSupportedForENSReleaseOnTestnet = &errors.ErrorResponse{Code: errors.ErrorCode("WR-003"), Details: "only STT is supported for ENSRelease on testnet"}
ErrUsernameRequiredForENSRelease = &errors.ErrorResponse{Code: errors.ErrorCode("WR-004"), Details: "username is required for ENSRelease"}
ErrUsernameAndPubKeyRequiredForENSSetPubKey = &errors.ErrorResponse{Code: errors.ErrorCode("WR-005"), Details: "username and public key are required for ENSSetPubKey"}
ErrPackIDRequiredForStickersBuy = &errors.ErrorResponse{Code: errors.ErrorCode("WR-006"), Details: "packID is required for StickersBuy"}
ErrToTokenIDRequiredForSwap = &errors.ErrorResponse{Code: errors.ErrorCode("WR-007"), Details: "toTokenID is required for Swap"}
ErrTokenIDAndToTokenIDDifferent = &errors.ErrorResponse{Code: errors.ErrorCode("WR-008"), Details: "tokenID and toTokenID must be different"}
ErrOnlyOneOfAmountInOrOutSet = &errors.ErrorResponse{Code: errors.ErrorCode("WR-009"), Details: "only one of amountIn or amountOut can be set"}
ErrAmountInMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WR-010"), Details: "amountIn must be positive"}
ErrAmountOutMustBePositive = &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"}
ErrLockedAmountMustBePositive = &errors.ErrorResponse{Code: errors.ErrorCode("WR-013"), Details: "locked amount must be positive"}
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"}
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")
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")
)

View File

@ -2,7 +2,6 @@ package router
import (
"context"
"errors"
"fmt"
"math"
"math/big"
@ -444,17 +443,17 @@ func validateFromLockedAmount(fromLockedAmount map[uint64]*hexutil.Big, isTestne
for chainID, amount := range fromLockedAmount {
if isTestnetMode {
if !supportedTestNetworks[chainID] {
return errors.New("locked amount is not supported for the selected network")
return ErrorLockedAmountNotSupportedNetwork
}
} else {
if !supportedNetworks[chainID] {
return errors.New("locked amount is not supported for the selected network")
return ErrorLockedAmountNotSupportedNetwork
}
}
// Check locked amount is not negative
if amount == nil || amount.ToInt().Sign() < 0 {
return errors.New("locked amount must not be negative")
return ErrorLockedAmountNotNegative
}
// Check if locked chain ID is a duplicate
@ -469,7 +468,7 @@ func validateFromLockedAmount(fromLockedAmount map[uint64]*hexutil.Big, isTestne
}
if (!isTestnetMode && excludedChainCount == len(supportedNetworks)) ||
(isTestnetMode && excludedChainCount == len(supportedTestNetworks)) {
return errors.New("all supported chains are excluded, routing impossible")
return ErrorLockedAmountExcludesAllSupported
}
return nil
}

View File

@ -2636,7 +2636,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: pathprocessor.SttSymbol,
testnetMode: true,
},
expectedError: errors.New("username and public key are required for ENSRegister"),
expectedError: ErrorENSRegisterRequires,
},
{
name: "ENSRegister missing public key",
@ -2646,7 +2646,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: pathprocessor.SttSymbol,
testnetMode: true,
},
expectedError: errors.New("username and public key are required for ENSRegister"),
expectedError: ErrorENSRegisterRequires,
},
{
name: "ENSRegister invalid token on testnet",
@ -2657,7 +2657,7 @@ func TestValidateInputData(t *testing.T) {
TokenID: "invalidtoken",
testnetMode: true,
},
expectedError: errors.New("only STT is supported for ENSRegister on testnet"),
expectedError: ErrorENSRegisterTestNetSTTOnly,
},
{
name: "ENSRegister invalid token on mainnet",
@ -2667,7 +2667,7 @@ func TestValidateInputData(t *testing.T) {
PublicKey: "validpublickey",
TokenID: "invalidtoken",
},
expectedError: errors.New("only SNT is supported for ENSRegister"),
expectedError: ErrorENSRegisterSNTOnly,
},
{
name: "ENSRelease valid data",
@ -2682,7 +2682,7 @@ func TestValidateInputData(t *testing.T) {
input: &RouteInputParams{
SendType: ENSRelease,
},
expectedError: errors.New("username is required for ENSRelease"),
expectedError: ErrorENSReleaseRequires,
},
{
name: "ENSSetPubKey valid data",
@ -2699,7 +2699,7 @@ func TestValidateInputData(t *testing.T) {
SendType: ENSSetPubKey,
PublicKey: "validpublickey",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrorENSSetPubKeyRequires,
},
{
name: "ENSSetPubKey missing public key",
@ -2707,7 +2707,7 @@ func TestValidateInputData(t *testing.T) {
SendType: ENSSetPubKey,
Username: "validusername",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrorENSSetPubKeyRequires,
},
{
name: "ENSSetPubKey invalid ENS username",
@ -2716,7 +2716,7 @@ func TestValidateInputData(t *testing.T) {
Username: "invalidusername",
PublicKey: "validpublickey",
},
expectedError: errors.New("username and public key are required for ENSSetPubKey"),
expectedError: ErrorENSSetPubKeyRequires,
},
{
name: "fromLockedAmount with supported network on testnet",
@ -2764,7 +2764,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: ErrorLockedAmountNotSupportedNetwork,
},
{
name: "fromLockedAmount with negative amount",
@ -2773,7 +2773,7 @@ func TestValidateInputData(t *testing.T) {
walletCommon.EthereumMainnet: (*hexutil.Big)(big.NewInt(-10)),
},
},
expectedError: errors.New("locked amount must not be negative"),
expectedError: ErrorLockedAmountNotNegative,
},
{
name: "fromLockedAmount with zero amount",
@ -2803,7 +2803,7 @@ func TestValidateInputData(t *testing.T) {
walletCommon.ArbitrumMainnet: (*hexutil.Big)(big.NewInt(0)),
},
},
expectedError: errors.New("all supported chains are excluded, routing impossible"),
expectedError: ErrorLockedAmountExcludesAllSupported,
},
{
name: "fromLockedAmount with all supported test networks with zero amount",
@ -2815,7 +2815,7 @@ func TestValidateInputData(t *testing.T) {
},
testnetMode: true,
},
expectedError: errors.New("all supported chains are excluded, routing impossible"),
expectedError: ErrorLockedAmountExcludesAllSupported,
},
}