From c9a9f660d0cfa092dcacd39f7f3a7c8af0eb93d9 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Tue, 3 Dec 2024 21:42:18 +0100 Subject: [PATCH] chore(wallet)_: community deployment related types moved to wallet requests package `DeploymentParameters` and `DeploymentDetails` types moved from `communitytokens` package to `requests` of the wallet service. --- services/communitytokens/api.go | 122 +++++------------- services/communitytokens/api_test.go | 65 ---------- services/communitytokens/estimations.go | 5 +- services/communitytokens/manager.go | 5 +- services/communitytokens/service.go | 3 +- .../requests/router_input_community_params.go | 83 ++++++++++++ .../router_input_community_params_test.go | 72 +++++++++++ 7 files changed, 192 insertions(+), 163 deletions(-) create mode 100644 services/wallet/requests/router_input_community_params.go create mode 100644 services/wallet/requests/router_input_community_params_test.go diff --git a/services/communitytokens/api.go b/services/communitytokens/api.go index 43e6165aa95..9cdb0a986e6 100644 --- a/services/communitytokens/api.go +++ b/services/communitytokens/api.go @@ -5,7 +5,6 @@ import ( "fmt" "math/big" - "github.com/pkg/errors" "go.uber.org/zap" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -18,13 +17,13 @@ import ( communityownertokenregistry "github.com/status-im/status-go/contracts/community-tokens/registry" "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/types" - "github.com/status-im/status-go/images" "github.com/status-im/status-go/logutils" "github.com/status-im/status-go/protocol/communities/token" "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/services/utils" "github.com/status-im/status-go/services/wallet/bigint" wcommon "github.com/status-im/status-go/services/wallet/common" + "github.com/status-im/status-go/services/wallet/requests" "github.com/status-im/status-go/services/wallet/wallettypes" "github.com/status-im/status-go/transactions" ) @@ -39,80 +38,17 @@ type API struct { s *Service } -type DeploymentDetails struct { - ContractAddress string `json:"contractAddress"` - TransactionHash string `json:"transactionHash"` - CommunityToken *token.CommunityToken `json:"communityToken"` - OwnerToken *token.CommunityToken `json:"ownerToken"` - MasterToken *token.CommunityToken `json:"masterToken"` -} - -const maxSupply = 999999999 - -type DeploymentParameters struct { - Name string `json:"name"` - Symbol string `json:"symbol"` - Supply *bigint.BigInt `json:"supply"` - InfiniteSupply bool `json:"infiniteSupply"` - Transferable bool `json:"transferable"` - RemoteSelfDestruct bool `json:"remoteSelfDestruct"` - TokenURI string `json:"tokenUri"` - OwnerTokenAddress string `json:"ownerTokenAddress"` - MasterTokenAddress string `json:"masterTokenAddress"` - CommunityID string `json:"communityId"` - Description string `json:"description"` - CroppedImage *images.CroppedImage `json:"croppedImage,omitempty"` // for community tokens - Base64Image string `json:"base64image"` // for owner & master tokens - Decimals int `json:"decimals"` -} - -func (d *DeploymentParameters) GetSupply() *big.Int { - if d.InfiniteSupply { - return d.GetInfiniteSupply() - } - return d.Supply.Int -} - -// infinite supply for ERC721 is 2^256-1 -func (d *DeploymentParameters) GetInfiniteSupply() *big.Int { - return GetInfiniteSupply() -} - -func GetInfiniteSupply() *big.Int { - max := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil) - max.Sub(max, big.NewInt(1)) - return max -} - -func (d *DeploymentParameters) Validate(isAsset bool) error { - if len(d.Name) <= 0 { - return errors.New("empty collectible name") - } - if len(d.Symbol) <= 0 { - return errors.New("empty collectible symbol") - } - var maxForType = big.NewInt(maxSupply) - if isAsset { - assetMultiplier, _ := big.NewInt(0).SetString("1000000000000000000", 10) - maxForType = maxForType.Mul(maxForType, assetMultiplier) - } - if !d.InfiniteSupply && (d.Supply.Cmp(big.NewInt(0)) < 0 || d.Supply.Cmp(maxForType) > 0) { - return fmt.Errorf("wrong supply value: %v", d.Supply) - } - return nil -} - -func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deploymentParameters DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) { +func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deploymentParameters requests.DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) { err := deploymentParameters.Validate(false) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password)) ethClient, err := api.s.manager.rpcClient.EthClient(chainID) if err != nil { logutils.ZapLogger().Error(err.Error()) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } address, tx, _, err := collectibles.DeployCollectibles(transactOpts, ethClient, deploymentParameters.Name, deploymentParameters.Symbol, deploymentParameters.GetSupply(), @@ -121,7 +57,7 @@ func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deployme common.HexToAddress(deploymentParameters.MasterTokenAddress)) if err != nil { logutils.ZapLogger().Error(err.Error()) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } err = api.s.pendingTracker.TrackPendingTransaction( @@ -135,16 +71,16 @@ func (api *API) DeployCollectibles(ctx context.Context, chainID uint64, deployme ) if err != nil { logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err)) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } savedCommunityToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), deploymentParameters, txArgs.From.Hex(), address.Hex(), protobuf.CommunityTokenType_ERC721, token.CommunityLevel, tx.Hash().Hex()) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } - return DeploymentDetails{ + return requests.DeploymentDetails{ ContractAddress: address.Hex(), TransactionHash: tx.Hash().Hex(), CommunityToken: savedCommunityToken}, nil @@ -180,27 +116,27 @@ func prepareDeploymentSignatureStruct(signature string, communityID string, addr } func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64, - ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters, - signerPubKey string, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) { + ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters, + signerPubKey string, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) { err := ownerTokenParameters.Validate(false) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } if len(signerPubKey) <= 0 { - return DeploymentDetails{}, fmt.Errorf("signerPubKey is empty") + return requests.DeploymentDetails{}, fmt.Errorf("signerPubKey is empty") } err = masterTokenParameters.Validate(false) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password)) deployerContractInst, err := api.NewCommunityTokenDeployerInstance(chainID) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } ownerTokenConfig := communitytokendeployer.CommunityTokenDeployerTokenConfig{ @@ -217,12 +153,12 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64, signature, err := api.s.Messenger.CreateCommunityTokenDeploymentSignature(context.Background(), chainID, txArgs.From.Hex(), ownerTokenParameters.CommunityID) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } communitySignature, err := prepareDeploymentSignatureStruct(types.HexBytes(signature).String(), ownerTokenParameters.CommunityID, common.Address(txArgs.From)) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } logutils.ZapLogger().Debug("Prepare deployment", zap.Any("signature", communitySignature)) @@ -231,7 +167,7 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64, if err != nil { logutils.ZapLogger().Error(err.Error()) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } logutils.ZapLogger().Debug("Contract deployed", zap.Stringer("hash", tx.Hash())) @@ -247,21 +183,21 @@ func (api *API) DeployOwnerToken(ctx context.Context, chainID uint64, ) if err != nil { logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err)) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } savedOwnerToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), ownerTokenParameters, txArgs.From.Hex(), api.s.TemporaryOwnerContractAddress(tx.Hash().Hex()), protobuf.CommunityTokenType_ERC721, token.OwnerLevel, tx.Hash().Hex()) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } savedMasterToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), masterTokenParameters, txArgs.From.Hex(), api.s.TemporaryMasterContractAddress(tx.Hash().Hex()), protobuf.CommunityTokenType_ERC721, token.MasterLevel, tx.Hash().Hex()) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } - return DeploymentDetails{ + return requests.DeploymentDetails{ ContractAddress: "", TransactionHash: tx.Hash().Hex(), OwnerToken: savedOwnerToken, @@ -273,11 +209,11 @@ func (api *API) ReTrackOwnerTokenDeploymentTransaction(ctx context.Context, chai return api.s.ReTrackOwnerTokenDeploymentTransaction(ctx, chainID, contractAddress) } -func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentParameters DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (DeploymentDetails, error) { +func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentParameters requests.DeploymentParameters, txArgs wallettypes.SendTxArgs, password string) (requests.DeploymentDetails, error) { err := deploymentParameters.Validate(true) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } transactOpts := txArgs.ToTransactOpts(utils.VerifyPasswordAndGetSigner(chainID, api.s.accountsManager, api.s.config.KeyStoreDir, txArgs.From, password)) @@ -285,7 +221,7 @@ func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentPara ethClient, err := api.s.manager.rpcClient.EthClient(chainID) if err != nil { logutils.ZapLogger().Error(err.Error()) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } const decimals = 18 @@ -296,7 +232,7 @@ func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentPara common.HexToAddress(deploymentParameters.MasterTokenAddress)) if err != nil { logutils.ZapLogger().Error(err.Error()) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } err = api.s.pendingTracker.TrackPendingTransaction( @@ -310,16 +246,16 @@ func (api *API) DeployAssets(ctx context.Context, chainID uint64, deploymentPara ) if err != nil { logutils.ZapLogger().Error("TrackPendingTransaction error", zap.Error(err)) - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } savedCommunityToken, err := api.s.CreateCommunityTokenAndSave(int(chainID), deploymentParameters, txArgs.From.Hex(), address.Hex(), protobuf.CommunityTokenType_ERC20, token.CommunityLevel, tx.Hash().Hex()) if err != nil { - return DeploymentDetails{}, err + return requests.DeploymentDetails{}, err } - return DeploymentDetails{ + return requests.DeploymentDetails{ ContractAddress: address.Hex(), TransactionHash: tx.Hash().Hex(), CommunityToken: savedCommunityToken}, nil @@ -334,7 +270,7 @@ func (api *API) DeployAssetsEstimate(ctx context.Context, chainID uint64, fromAd } func (api *API) DeployOwnerTokenEstimate(ctx context.Context, chainID uint64, fromAddress string, - ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters, + ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters, communityID string, signerPubKey string) (*CommunityTokenFees, error) { return api.s.deployOwnerTokenEstimate(ctx, chainID, fromAddress, ownerTokenParameters, masterTokenParameters, communityID, signerPubKey) } diff --git a/services/communitytokens/api_test.go b/services/communitytokens/api_test.go index 07669d76092..0d4705095aa 100644 --- a/services/communitytokens/api_test.go +++ b/services/communitytokens/api_test.go @@ -1,78 +1,13 @@ package communitytokens import ( - "math/big" "testing" "github.com/stretchr/testify/require" "github.com/ethereum/go-ethereum/common" - - "github.com/status-im/status-go/services/wallet/bigint" ) -func TestDeploymentParameters(t *testing.T) { - var testCases = []struct { - name string - parameters DeploymentParameters - isError bool - }{ - { - name: "emptyName", - parameters: DeploymentParameters{"", "SYMBOL", &bigint.BigInt{Int: big.NewInt(int64(123))}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: true, - }, - { - name: "emptySymbol", - parameters: DeploymentParameters{"NAME", "", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: true, - }, - { - name: "negativeSupply", - parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: true, - }, - { - name: "zeroSupply", - parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(0)}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: false, - }, - { - name: "negativeSupplyAndInfinite", - parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, true, false, false, "", "", "", "", "", nil, "", 0}, - isError: false, - }, - { - name: "supplyGreaterThanMax", - parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply + 1)}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: true, - }, - { - name: "supplyIsMax", - parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply)}, false, false, false, "", "", "", "", "", nil, "", 0}, - isError: false, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - err := tc.parameters.Validate(false) - if tc.isError { - require.Error(t, err) - } else { - require.NoError(t, err) - } - }) - } - - notInfiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0} - requiredSupply := big.NewInt(123) - require.Equal(t, notInfiniteSupplyParams.GetSupply(), requiredSupply) - infiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, true, false, false, "", "", "", "", "", nil, "", 0} - requiredSupply = infiniteSupplyParams.GetInfiniteSupply() - require.Equal(t, infiniteSupplyParams.GetSupply(), requiredSupply) -} - func TestTypedDataHash(t *testing.T) { sigHash := common.Hex2Bytes("dd91c30357aafeb2792b5f0facbd83995943c1ea113a906ebbeb58bfeb27dfc2") domainSep := common.Hex2Bytes("4a672b5a08e88d37f7239165a0c9e03a01196587d52c638c0c99cbee5ba527c8") diff --git a/services/communitytokens/estimations.go b/services/communitytokens/estimations.go index 808d9acbd6a..8e257f6416a 100644 --- a/services/communitytokens/estimations.go +++ b/services/communitytokens/estimations.go @@ -20,6 +20,7 @@ import ( "github.com/status-im/status-go/logutils" "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/services/wallet/bigint" + "github.com/status-im/status-go/services/wallet/requests" "github.com/status-im/status-go/services/wallet/router/fees" "github.com/status-im/status-go/services/wallet/wallettypes" ) @@ -45,7 +46,7 @@ func gweiToWei(val *big.Float) *big.Int { } func (s *Service) deployOwnerTokenEstimate(ctx context.Context, chainID uint64, fromAddress string, - ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters, + ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters, communityID string, signerPubKey string) (*CommunityTokenFees, error) { gasUnits, err := s.deployOwnerTokenGasUnits(ctx, chainID, fromAddress, ownerTokenParameters, masterTokenParameters, @@ -157,7 +158,7 @@ func (s *Service) remoteBurnGasUnits(ctx context.Context, chainID uint64, contra } func (s *Service) deployOwnerTokenGasUnits(ctx context.Context, chainID uint64, fromAddress string, - ownerTokenParameters DeploymentParameters, masterTokenParameters DeploymentParameters, + ownerTokenParameters requests.DeploymentParameters, masterTokenParameters requests.DeploymentParameters, communityID string, signerPubKey string) (uint64, error) { ethClient, err := s.manager.rpcClient.EthClient(chainID) if err != nil { diff --git a/services/communitytokens/manager.go b/services/communitytokens/manager.go index 02e75429a68..70826ab5a36 100644 --- a/services/communitytokens/manager.go +++ b/services/communitytokens/manager.go @@ -16,6 +16,7 @@ import ( "github.com/status-im/status-go/protocol/communities" "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/services/wallet/bigint" + "github.com/status-im/status-go/services/wallet/requests" ) type Manager struct { @@ -96,7 +97,7 @@ func (m *Manager) GetCollectibleContractData(chainID uint64, contractAddress str TotalSupply: &bigint.BigInt{Int: totalSupply}, Transferable: transferable, RemoteBurnable: remoteBurnable, - InfiniteSupply: GetInfiniteSupply().Cmp(totalSupply) == 0, + InfiniteSupply: requests.GetInfiniteSupply().Cmp(totalSupply) == 0, }, nil } @@ -113,7 +114,7 @@ func (m *Manager) GetAssetContractData(chainID uint64, contractAddress string) ( return &communities.AssetContractData{ TotalSupply: &bigint.BigInt{Int: totalSupply}, - InfiniteSupply: GetInfiniteSupply().Cmp(totalSupply) == 0, + InfiniteSupply: requests.GetInfiniteSupply().Cmp(totalSupply) == 0, }, nil } diff --git a/services/communitytokens/service.go b/services/communitytokens/service.go index bacc4313744..e7863d08c2a 100644 --- a/services/communitytokens/service.go +++ b/services/communitytokens/service.go @@ -33,6 +33,7 @@ import ( "github.com/status-im/status-go/services/utils" "github.com/status-im/status-go/services/wallet/bigint" wcommon "github.com/status-im/status-go/services/wallet/common" + "github.com/status-im/status-go/services/wallet/requests" "github.com/status-im/status-go/services/wallet/router/fees" "github.com/status-im/status-go/services/wallet/walletevent" "github.com/status-im/status-go/services/wallet/wallettypes" @@ -571,7 +572,7 @@ func (s *Service) maxSupply(ctx context.Context, chainID uint64, contractAddress } } -func (s *Service) CreateCommunityTokenAndSave(chainID int, deploymentParameters DeploymentParameters, +func (s *Service) CreateCommunityTokenAndSave(chainID int, deploymentParameters requests.DeploymentParameters, deployerAddress string, contractAddress string, tokenType protobuf.CommunityTokenType, privilegesLevel token.PrivilegesLevel, transactionHash string) (*token.CommunityToken, error) { contractVersion := "" diff --git a/services/wallet/requests/router_input_community_params.go b/services/wallet/requests/router_input_community_params.go new file mode 100644 index 00000000000..8aa9b21ef9c --- /dev/null +++ b/services/wallet/requests/router_input_community_params.go @@ -0,0 +1,83 @@ +package requests + +import ( + "fmt" + "math/big" + + "github.com/status-im/status-go/errors" + "github.com/status-im/status-go/images" + "github.com/status-im/status-go/protocol/communities/token" + "github.com/status-im/status-go/services/wallet/bigint" +) + +const maxSupply = 999999999 + +var ( + ErrEmptyCollectibleName = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-001"), Details: "empty collectible name"} + ErrEmptyCollectibleSymbol = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-002"), Details: "empty collectible symbol"} + ErrWrongSupplyValue = &errors.ErrorResponse{Code: errors.ErrorCode("WRRC-003"), Details: "wrong supply value: %v"} +) + +type DeploymentDetails struct { + ContractAddress string `json:"contractAddress"` + TransactionHash string `json:"transactionHash"` + CommunityToken *token.CommunityToken `json:"communityToken"` + OwnerToken *token.CommunityToken `json:"ownerToken"` + MasterToken *token.CommunityToken `json:"masterToken"` +} + +type DeploymentParameters struct { + Name string `json:"name"` + Symbol string `json:"symbol"` + Supply *bigint.BigInt `json:"supply"` + InfiniteSupply bool `json:"infiniteSupply"` + Transferable bool `json:"transferable"` + RemoteSelfDestruct bool `json:"remoteSelfDestruct"` + TokenURI string `json:"tokenUri"` + OwnerTokenAddress string `json:"ownerTokenAddress"` + MasterTokenAddress string `json:"masterTokenAddress"` + CommunityID string `json:"communityId"` + Description string `json:"description"` + CroppedImage *images.CroppedImage `json:"croppedImage,omitempty"` // for community tokens + Base64Image string `json:"base64image"` // for owner & master tokens + Decimals int `json:"decimals"` +} + +func (d *DeploymentParameters) GetSupply() *big.Int { + if d.InfiniteSupply { + return d.GetInfiniteSupply() + } + return d.Supply.Int +} + +// infinite supply for ERC721 is 2^256-1 +func (d *DeploymentParameters) GetInfiniteSupply() *big.Int { + return GetInfiniteSupply() +} + +func GetInfiniteSupply() *big.Int { + max := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil) + max.Sub(max, big.NewInt(1)) + return max +} + +func (d *DeploymentParameters) Validate(isAsset bool) error { + if len(d.Name) <= 0 { + return ErrEmptyCollectibleName + } + if len(d.Symbol) <= 0 { + return ErrEmptyCollectibleSymbol + } + var maxForType = big.NewInt(maxSupply) + if isAsset { + assetMultiplier, _ := big.NewInt(0).SetString("1000000000000000000", 10) + maxForType = maxForType.Mul(maxForType, assetMultiplier) + } + if !d.InfiniteSupply && (d.Supply.Cmp(big.NewInt(0)) < 0 || d.Supply.Cmp(maxForType) > 0) { + return &errors.ErrorResponse{ + Code: ErrWrongSupplyValue.Code, + Details: fmt.Sprintf(ErrWrongSupplyValue.Details, d.Supply), + } + } + return nil +} diff --git a/services/wallet/requests/router_input_community_params_test.go b/services/wallet/requests/router_input_community_params_test.go new file mode 100644 index 00000000000..d847a9b0d51 --- /dev/null +++ b/services/wallet/requests/router_input_community_params_test.go @@ -0,0 +1,72 @@ +package requests + +import ( + "math/big" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/status-im/status-go/services/wallet/bigint" +) + +func TestDeploymentParameters(t *testing.T) { + var testCases = []struct { + name string + parameters DeploymentParameters + isError bool + }{ + { + name: "emptyName", + parameters: DeploymentParameters{"", "SYMBOL", &bigint.BigInt{Int: big.NewInt(int64(123))}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: true, + }, + { + name: "emptySymbol", + parameters: DeploymentParameters{"NAME", "", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: true, + }, + { + name: "negativeSupply", + parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: true, + }, + { + name: "zeroSupply", + parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(0)}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: false, + }, + { + name: "negativeSupplyAndInfinite", + parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(-123)}, true, false, false, "", "", "", "", "", nil, "", 0}, + isError: false, + }, + { + name: "supplyGreaterThanMax", + parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply + 1)}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: true, + }, + { + name: "supplyIsMax", + parameters: DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(maxSupply)}, false, false, false, "", "", "", "", "", nil, "", 0}, + isError: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := tc.parameters.Validate(false) + if tc.isError { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } + + notInfiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, false, false, false, "", "", "", "", "", nil, "", 0} + requiredSupply := big.NewInt(123) + require.Equal(t, notInfiniteSupplyParams.GetSupply(), requiredSupply) + infiniteSupplyParams := DeploymentParameters{"NAME", "SYM", &bigint.BigInt{Int: big.NewInt(123)}, true, false, false, "", "", "", "", "", nil, "", 0} + requiredSupply = infiniteSupplyParams.GetInfiniteSupply() + require.Equal(t, infiniteSupplyParams.GetSupply(), requiredSupply) +}