github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/client/rest/rest.go (about)

     1  package rest
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	ethcmn "github.com/ethereum/go-ethereum/common"
     9  	"github.com/gorilla/mux"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/query"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    15  	comm "github.com/fibonacci-chain/fbc/x/common"
    16  	"github.com/fibonacci-chain/fbc/x/feesplit/types"
    17  	govRest "github.com/fibonacci-chain/fbc/x/gov/client/rest"
    18  )
    19  
    20  func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
    21  	r.HandleFunc("/feesplit/contract/{contract}", contractHandlerFn(cliCtx)).Methods("GET")
    22  	r.HandleFunc("/feesplit/deployer/{deployer}", deployerHandlerFn(cliCtx)).Methods("GET")
    23  	r.HandleFunc("/feesplit/withdrawer/{withdrawer}", withdrawerHandlerFn(cliCtx)).Methods("GET")
    24  	r.HandleFunc("/feesplit/parameters", queryParamsHandlerFn(cliCtx)).Methods("GET")
    25  }
    26  
    27  func RegisterRoutesV2(cliCtx context.CLIContext, r *mux.Router) {
    28  	r.HandleFunc("/feesplit/contract/{contract}", contractHandlerFnV2(cliCtx)).Methods("GET")
    29  	r.HandleFunc("/feesplit/deployer/{deployer}", deployerHandlerFnV2(cliCtx)).Methods("GET")
    30  	r.HandleFunc("/feesplit/parameters", queryParamsHandlerFnV2(cliCtx)).Methods("GET")
    31  }
    32  
    33  // FeeSplitSharesProposalRESTHandler defines feesplit proposal handler
    34  func FeeSplitSharesProposalRESTHandler(context.CLIContext) govRest.ProposalRESTHandler {
    35  	return govRest.ProposalRESTHandler{}
    36  }
    37  
    38  func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    39  	return func(w http.ResponseWriter, r *http.Request) {
    40  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    41  		if !ok {
    42  			return
    43  		}
    44  
    45  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s",
    46  			types.RouterKey, types.QueryParameters), nil)
    47  		if err != nil {
    48  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    49  			return
    50  		}
    51  
    52  		cliCtx = cliCtx.WithHeight(height)
    53  		rest.PostProcessResponse(w, cliCtx, res)
    54  	}
    55  }
    56  
    57  func contractHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    58  	return func(w http.ResponseWriter, r *http.Request) {
    59  		contract := mux.Vars(r)["contract"]
    60  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    61  		if !ok {
    62  			return
    63  		}
    64  
    65  		req := &types.QueryFeeSplitRequest{ContractAddress: contract}
    66  		data, err := cliCtx.Codec.MarshalJSON(req)
    67  		if err != nil {
    68  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
    69  			return
    70  		}
    71  
    72  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.RouterKey, types.QueryFeeSplit), data)
    73  		if err != nil {
    74  			sdkErr := comm.ParseSDKError(err.Error())
    75  			comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
    76  			return
    77  		}
    78  
    79  		var result types.QueryFeeSplitResponse
    80  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
    81  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
    82  			return
    83  		}
    84  
    85  		cliCtx = cliCtx.WithHeight(height)
    86  		rest.PostProcessResponse(w, cliCtx, result)
    87  	}
    88  }
    89  
    90  func deployerHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    91  	return func(w http.ResponseWriter, r *http.Request) {
    92  		addr := mux.Vars(r)["deployer"]
    93  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    94  		if !ok {
    95  			return
    96  		}
    97  
    98  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
    99  		if err != nil {
   100  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
   101  			return
   102  		}
   103  
   104  		req := &types.QueryDeployerFeeSplitsRequest{
   105  			DeployerAddress: addr,
   106  			Pagination:      query.NewPaginateFromPageLimit(page, limit),
   107  		}
   108  		data, err := cliCtx.Codec.MarshalJSON(req)
   109  		if err != nil {
   110  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
   111  			return
   112  		}
   113  
   114  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s",
   115  			types.RouterKey, types.QueryDeployerFeeSplits), data)
   116  		if err != nil {
   117  			sdkErr := comm.ParseSDKError(err.Error())
   118  			comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   119  			return
   120  		}
   121  
   122  		var result types.QueryDeployerFeeSplitsResponse
   123  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   124  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
   125  			return
   126  		}
   127  
   128  		cliCtx = cliCtx.WithHeight(height)
   129  		rest.PostProcessResponse(w, cliCtx, res)
   130  	}
   131  }
   132  
   133  func withdrawerHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   134  	return func(w http.ResponseWriter, r *http.Request) {
   135  		addr := mux.Vars(r)["withdrawer"]
   136  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   137  		if !ok {
   138  			return
   139  		}
   140  
   141  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
   142  		if err != nil {
   143  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
   144  			return
   145  		}
   146  
   147  		req := &types.QueryWithdrawerFeeSplitsRequest{
   148  			WithdrawerAddress: addr,
   149  			Pagination:        query.NewPaginateFromPageLimit(page, limit),
   150  		}
   151  		data, err := cliCtx.Codec.MarshalJSON(req)
   152  		if err != nil {
   153  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
   154  			return
   155  		}
   156  
   157  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s",
   158  			types.RouterKey, types.QueryWithdrawerFeeSplits), data)
   159  		if err != nil {
   160  			sdkErr := comm.ParseSDKError(err.Error())
   161  			comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   162  			return
   163  		}
   164  
   165  		var result types.QueryWithdrawerFeeSplitsResponse
   166  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   167  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
   168  			return
   169  		}
   170  
   171  		cliCtx = cliCtx.WithHeight(height)
   172  		rest.PostProcessResponse(w, cliCtx, res)
   173  	}
   174  }
   175  
   176  func contractHandlerFnV2(cliCtx context.CLIContext) http.HandlerFunc {
   177  	return func(w http.ResponseWriter, r *http.Request) {
   178  		contract := mux.Vars(r)["contract"]
   179  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   180  		if !ok {
   181  			return
   182  		}
   183  
   184  		req := &types.QueryFeeSplitRequest{ContractAddress: contract}
   185  		data, err := cliCtx.Codec.MarshalJSON(req)
   186  		if err != nil {
   187  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
   188  			return
   189  		}
   190  
   191  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.RouterKey, types.QueryFeeSplit), data)
   192  		if err != nil {
   193  			sdkErr := comm.ParseSDKError(err.Error())
   194  			comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   195  			return
   196  		}
   197  
   198  		var result types.QueryFeeSplitResponse
   199  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   200  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
   201  			return
   202  		}
   203  		// convert ex to 0x
   204  		if addr, err := sdk.AccAddressFromBech32(result.FeeSplit.DeployerAddress); err == nil {
   205  			result.FeeSplit.DeployerAddress = ethcmn.BytesToAddress(addr.Bytes()).String()
   206  		}
   207  		if addr, err := sdk.AccAddressFromBech32(result.FeeSplit.WithdrawerAddress); err == nil {
   208  			result.FeeSplit.WithdrawerAddress = ethcmn.BytesToAddress(addr.Bytes()).String()
   209  		}
   210  
   211  		resultJson, err := json.Marshal(comm.GetBaseResponse(result.FeeSplit))
   212  		if err != nil {
   213  			comm.HandleErrorMsg(w, cliCtx, comm.CodeMarshalJSONFailed, err.Error())
   214  			return
   215  		}
   216  
   217  		cliCtx = cliCtx.WithHeight(height)
   218  		rest.PostProcessResponse(w, cliCtx, resultJson)
   219  	}
   220  }
   221  
   222  func deployerHandlerFnV2(cliCtx context.CLIContext) http.HandlerFunc {
   223  	return func(w http.ResponseWriter, r *http.Request) {
   224  		addr := mux.Vars(r)["deployer"]
   225  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   226  		if !ok {
   227  			return
   228  		}
   229  
   230  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
   231  		if err != nil {
   232  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
   233  			return
   234  		}
   235  
   236  		req := &types.QueryDeployerFeeSplitsRequest{
   237  			DeployerAddress: addr,
   238  			Pagination:      query.NewPaginateFromPageLimit(page, limit),
   239  		}
   240  		data, err := cliCtx.Codec.MarshalJSON(req)
   241  		if err != nil {
   242  			rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
   243  			return
   244  		}
   245  
   246  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s",
   247  			types.RouterKey, types.QueryDeployerFeeSplitsDetail), data)
   248  		if err != nil {
   249  			sdkErr := comm.ParseSDKError(err.Error())
   250  			comm.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   251  			return
   252  		}
   253  
   254  		var result types.QueryDeployerFeeSplitsResponseV2
   255  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   256  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
   257  			return
   258  		}
   259  		// convert ex to 0x
   260  		for i, fs := range result.FeeSplits {
   261  			if addr, err := sdk.AccAddressFromBech32(fs.DeployerAddress); err == nil {
   262  				result.FeeSplits[i].DeployerAddress = ethcmn.BytesToAddress(addr.Bytes()).String()
   263  			}
   264  			if addr, err := sdk.AccAddressFromBech32(fs.WithdrawerAddress); err == nil {
   265  				result.FeeSplits[i].WithdrawerAddress = ethcmn.BytesToAddress(addr.Bytes()).String()
   266  			}
   267  		}
   268  
   269  		resultJson, err := json.Marshal(comm.GetBaseResponse(result))
   270  		if err != nil {
   271  			comm.HandleErrorMsg(w, cliCtx, comm.CodeMarshalJSONFailed, err.Error())
   272  			return
   273  		}
   274  
   275  		cliCtx = cliCtx.WithHeight(height)
   276  		rest.PostProcessResponse(w, cliCtx, resultJson)
   277  	}
   278  }
   279  
   280  func queryParamsHandlerFnV2(cliCtx context.CLIContext) http.HandlerFunc {
   281  	return func(w http.ResponseWriter, r *http.Request) {
   282  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   283  		if !ok {
   284  			return
   285  		}
   286  
   287  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s",
   288  			types.RouterKey, types.QueryParameters), nil)
   289  		if err != nil {
   290  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   291  			return
   292  		}
   293  
   294  		var result types.QueryParamsResponse
   295  		if err := cliCtx.Codec.UnmarshalJSON(res, &result); err != nil {
   296  			comm.HandleErrorMsg(w, cliCtx, comm.CodeUnMarshalJSONFailed, err.Error())
   297  			return
   298  		}
   299  
   300  		resultJson, err := json.Marshal(comm.GetBaseResponse(result))
   301  		if err != nil {
   302  			comm.HandleErrorMsg(w, cliCtx, comm.CodeMarshalJSONFailed, err.Error())
   303  			return
   304  		}
   305  
   306  		cliCtx = cliCtx.WithHeight(height)
   307  		rest.PostProcessResponse(w, cliCtx, resultJson)
   308  	}
   309  }