code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_remove_network.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package api
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/libs/jsonrpc"
    23  
    24  	"github.com/mitchellh/mapstructure"
    25  )
    26  
    27  type AdminRemoveNetworkParams struct {
    28  	Name string `json:"name"`
    29  }
    30  
    31  type AdminRemoveNetwork struct {
    32  	networkStore NetworkStore
    33  }
    34  
    35  // Handle removes a wallet from the computer.
    36  func (h *AdminRemoveNetwork) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    37  	params, err := validateRemoveNetworkParams(rawParams)
    38  	if err != nil {
    39  		return nil, InvalidParams(err)
    40  	}
    41  
    42  	if exist, err := h.networkStore.NetworkExists(params.Name); err != nil {
    43  		return nil, InternalError(fmt.Errorf("could not verify the network existence: %w", err))
    44  	} else if !exist {
    45  		return nil, InvalidParams(ErrNetworkDoesNotExist)
    46  	}
    47  
    48  	if err := h.networkStore.DeleteNetwork(params.Name); err != nil {
    49  		return nil, InternalError(fmt.Errorf("could not remove the wallet: %w", err))
    50  	}
    51  
    52  	return nil, nil
    53  }
    54  
    55  func validateRemoveNetworkParams(rawParams jsonrpc.Params) (AdminRemoveNetworkParams, error) {
    56  	if rawParams == nil {
    57  		return AdminRemoveNetworkParams{}, ErrParamsRequired
    58  	}
    59  
    60  	params := AdminRemoveNetworkParams{}
    61  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    62  		return AdminRemoveNetworkParams{}, ErrParamsDoNotMatch
    63  	}
    64  
    65  	if params.Name == "" {
    66  		return AdminRemoveNetworkParams{}, ErrNetworkNameIsRequired
    67  	}
    68  
    69  	return params, nil
    70  }
    71  
    72  func NewAdminRemoveNetwork(
    73  	networkStore NetworkStore,
    74  ) *AdminRemoveNetwork {
    75  	return &AdminRemoveNetwork{
    76  		networkStore: networkStore,
    77  	}
    78  }