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

     1  package rest
     2  
     3  import (
     4  	"net/http"
     5  	"strconv"
     6  
     7  	"github.com/fibonacci-chain/fbc/x/wasm/ioutils"
     8  	"github.com/gorilla/mux"
     9  
    10  	clientCtx "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    13  	wasmUtils "github.com/fibonacci-chain/fbc/x/wasm/client/utils"
    14  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    15  )
    16  
    17  func registerTxRoutes(cliCtx clientCtx.CLIContext, r *mux.Router) {
    18  	r.HandleFunc("/wasm/code", storeCodeHandlerFn(cliCtx)).Methods("POST")
    19  	r.HandleFunc("/wasm/code/{codeId}", instantiateContractHandlerFn(cliCtx)).Methods("POST")
    20  	r.HandleFunc("/wasm/contract/{contractAddr}", executeContractHandlerFn(cliCtx)).Methods("POST")
    21  }
    22  
    23  type storeCodeReq struct {
    24  	BaseReq   rest.BaseReq `json:"base_req" yaml:"base_req"`
    25  	WasmBytes []byte       `json:"wasm_bytes"`
    26  }
    27  
    28  type instantiateContractReq struct {
    29  	BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
    30  	Label   string       `json:"label" yaml:"label"`
    31  	Deposit sdk.Coins    `json:"deposit" yaml:"deposit"`
    32  	Admin   string       `json:"admin,omitempty" yaml:"admin"`
    33  	Msg     []byte       `json:"msg" yaml:"msg"`
    34  }
    35  
    36  type executeContractReq struct {
    37  	BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
    38  	ExecMsg []byte       `json:"exec_msg" yaml:"exec_msg"`
    39  	Amount  sdk.Coins    `json:"coins" yaml:"coins"`
    40  }
    41  
    42  func storeCodeHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
    43  	return func(w http.ResponseWriter, r *http.Request) {
    44  		var req storeCodeReq
    45  		if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
    46  			return
    47  		}
    48  
    49  		req.BaseReq = req.BaseReq.Sanitize()
    50  		if !req.BaseReq.ValidateBasic(w) {
    51  			return
    52  		}
    53  
    54  		var err error
    55  		wasm := req.WasmBytes
    56  
    57  		// gzip the wasm file
    58  		if ioutils.IsWasm(wasm) {
    59  			wasm, err = ioutils.GzipIt(wasm)
    60  			if err != nil {
    61  				rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    62  				return
    63  			}
    64  		} else if !ioutils.IsGzip(wasm) {
    65  			rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid input file, use wasm binary or zip")
    66  			return
    67  		}
    68  
    69  		// build and sign the transaction, then broadcast to Tendermint
    70  		msg := types.MsgStoreCode{
    71  			Sender:       req.BaseReq.From,
    72  			WASMByteCode: wasm,
    73  		}
    74  
    75  		if err := msg.ValidateBasic(); err != nil {
    76  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    77  			return
    78  		}
    79  
    80  		wasmUtils.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, &msg)
    81  	}
    82  }
    83  
    84  func instantiateContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
    85  	return func(w http.ResponseWriter, r *http.Request) {
    86  		var req instantiateContractReq
    87  		if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
    88  			return
    89  		}
    90  		vars := mux.Vars(r)
    91  
    92  		req.BaseReq = req.BaseReq.Sanitize()
    93  		if !req.BaseReq.ValidateBasic(w) {
    94  			return
    95  		}
    96  
    97  		// get the id of the code to instantiate
    98  		codeID, err := strconv.ParseUint(vars["codeId"], 10, 64)
    99  		if err != nil {
   100  			return
   101  		}
   102  
   103  		msg := types.MsgInstantiateContract{
   104  			Sender: req.BaseReq.From,
   105  			CodeID: codeID,
   106  			Label:  req.Label,
   107  			Funds:  sdk.CoinsToCoinAdapters(req.Deposit),
   108  			Msg:    req.Msg,
   109  			Admin:  req.Admin,
   110  		}
   111  
   112  		if err := msg.ValidateBasic(); err != nil {
   113  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
   114  			return
   115  		}
   116  
   117  		wasmUtils.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, &msg)
   118  	}
   119  }
   120  
   121  func executeContractHandlerFn(cliCtx clientCtx.CLIContext) http.HandlerFunc {
   122  	return func(w http.ResponseWriter, r *http.Request) {
   123  		var req executeContractReq
   124  		if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
   125  			return
   126  		}
   127  		vars := mux.Vars(r)
   128  		contractAddr := vars["contractAddr"]
   129  
   130  		req.BaseReq = req.BaseReq.Sanitize()
   131  		if !req.BaseReq.ValidateBasic(w) {
   132  			return
   133  		}
   134  
   135  		msg := types.MsgExecuteContract{
   136  			Sender:   req.BaseReq.From,
   137  			Contract: contractAddr,
   138  			Msg:      req.ExecMsg,
   139  			Funds:    sdk.CoinsToCoinAdapters(req.Amount),
   140  		}
   141  
   142  		if err := msg.ValidateBasic(); err != nil {
   143  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
   144  			return
   145  		}
   146  
   147  		wasmUtils.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, &msg)
   148  	}
   149  }