Skip to content

Commit

Permalink
add give get com REST method
Browse files Browse the repository at this point in the history
  • Loading branch information
fabtagliaferro committed Aug 31, 2022
1 parent 9bc61b8 commit 3aa9353
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ignite/pkg/cosmosfaucet/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
51 changes: 51 additions & 0 deletions ignite/pkg/cosmosfaucet/http_com_faucet.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 28 additions & 0 deletions ignite/pkg/cosmosfaucet/openapi/openapi.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3aa9353

Please sign in to comment.