From 3aa935355c5eaac1aef0921a4260ee0143cf4301 Mon Sep 17 00:00:00 2001 From: fabtagliaferro Date: Wed, 31 Aug 2022 15:52:19 +0200 Subject: [PATCH] add `give` get com REST method --- ignite/pkg/cosmosfaucet/http.go | 4 ++ ignite/pkg/cosmosfaucet/http_com_faucet.go | 51 +++++++++++++++++++ .../pkg/cosmosfaucet/openapi/openapi.yml.tmpl | 28 ++++++++++ 3 files changed, 83 insertions(+) create mode 100644 ignite/pkg/cosmosfaucet/http_com_faucet.go diff --git a/ignite/pkg/cosmosfaucet/http.go b/ignite/pkg/cosmosfaucet/http.go index 541ef8c0d5..efadd2ceda 100644 --- a/ignite/pkg/cosmosfaucet/http.go +++ b/ignite/pkg/cosmosfaucet/http.go @@ -17,6 +17,10 @@ func (f Faucet) ServeHTTP(w http.ResponseWriter, r *http.Request) { router.Handle("/", cors.Default().Handler(http.HandlerFunc(f.faucetHandler))). Methods(http.MethodPost) + router.Handle("/give", cors.Default().Handler(http.HandlerFunc(f.comFaucetHandler))). + Queries("addr", "{addr}", "amount", "{amount}"). + Methods(http.MethodGet) + router.Handle("/info", cors.Default().Handler(http.HandlerFunc(f.faucetInfoHandler))). Methods(http.MethodGet) diff --git a/ignite/pkg/cosmosfaucet/http_com_faucet.go b/ignite/pkg/cosmosfaucet/http_com_faucet.go new file mode 100644 index 0000000000..03075bfadb --- /dev/null +++ b/ignite/pkg/cosmosfaucet/http_com_faucet.go @@ -0,0 +1,51 @@ +package cosmosfaucet + +import ( + "context" + "net/http" + + "github.com/gorilla/mux" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// package initialization for correct validation of commercionetwork addresses +func init() { + configTestPrefixes() +} + +func configTestPrefixes() { + AccountAddressPrefix := "did:com:" + AccountPubKeyPrefix := AccountAddressPrefix + "pub" + config := sdk.GetConfig() + config.SetBech32PrefixForAccount(AccountAddressPrefix, AccountPubKeyPrefix) + config.Seal() +} + +func (f Faucet) comFaucetHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + address, err := sdk.AccAddressFromBech32(vars["addr"]) + if err != nil { + responseError(w, http.StatusBadRequest, err) + return + } + + var coins []sdk.Coin + coin, err := sdk.ParseCoinNormalized(vars["amount"] + "ucommercio") + if err != nil { + responseError(w, http.StatusBadRequest, err) + return + } + coins = append(coins, coin) + + // try performing the transfer + if err := f.Transfer(r.Context(), address.String(), coins); err != nil { + if err == context.Canceled { + return + } + responseError(w, http.StatusInternalServerError, err) + } else { + responseSuccess(w) + } +} diff --git a/ignite/pkg/cosmosfaucet/openapi/openapi.yml.tmpl b/ignite/pkg/cosmosfaucet/openapi/openapi.yml.tmpl index bb81778930..b9aa6e8cc6 100644 --- a/ignite/pkg/cosmosfaucet/openapi/openapi.yml.tmpl +++ b/ignite/pkg/cosmosfaucet/openapi/openapi.yml.tmpl @@ -33,6 +33,34 @@ paths: schema: $ref: "#/definitions/SendResponse" + /give: + get: + summary: "Send com tokens to addr account" + produces: + - "application/json" + parameters: + - in: query + name: addr + description: address that should receive the tokens + required: true + schema: + type: string + - in: query + name: amount + description: the amount of com tokens that should be sent by the faucet + required: true + schema: + type: int + responses: + "400": + description: "Bad request" + "500": + description: "Internal error" + "200": + description: "All coins are successfully sent\n\nAfter making a sample execution, visit the following link to see the difference in sample account's balance: {{ .APIAddress }}/bank/balances/cosmos1uzv4v9g9xln2qx2vtqhz99yxum33calja5vruz" + schema: + $ref: "#/definitions/SendResponse" + definitions: SendRequest: type: "object"