Skip to content

Commit

Permalink
Improve airdrop getting handler, add notice comment
Browse files Browse the repository at this point in the history
  • Loading branch information
violog committed May 13, 2024
1 parent c7d8ea6 commit 9e93032
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
Expand Down
7 changes: 4 additions & 3 deletions internal/config/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ func (b *broadcasterer) Broadcaster() Broadcaster {
panic(fmt.Errorf("broadcaster: invalid airdrop amount: %w", err))
}

tlsConfig := &tls.Config{
// this hack is required to dial gRPC, please test it with remote RPC if you change this code
withInsecure := grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
}
cosmosRPC, err := grpc.Dial(cfg.CosmosRPC, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithKeepaliveParams(keepalive.ClientParameters{
}))
cosmosRPC, err := grpc.Dial(cfg.CosmosRPC, withInsecure, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 10 * time.Second, // wait time before ping if no activity
Timeout: 20 * time.Second, // ping timeout
}))
Expand Down
27 changes: 15 additions & 12 deletions internal/service/handlers/get_airdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@ package handlers
import (
"net/http"

"github.com/go-chi/chi"
validation "github.com/go-ozzo/ozzo-validation/v4"
data "github.com/rarimo/airdrop-svc/internal/data"
"github.com/rarimo/airdrop-svc/internal/service/requests"
"github.com/rarimo/airdrop-svc/resources"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
)

func GetAirdrop(w http.ResponseWriter, r *http.Request) {
var (
nullifier = chi.URLParam(r, "nullifier")
err = validation.Errors{"{nullifier}": validation.Validate(nullifier, validation.Required)}.Filter()
)
nullifier, err := requests.NewGetAirdrop(r)
if err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

airdrop, err := AirdropsQ(r).
airdrops, err := AirdropsQ(r).
FilterByNullifier(nullifier).
FilterByStatus(data.TxStatusCompleted).
Get()
Select()
if err != nil {
Log(r).WithError(err).Error("Failed to get airdrop by ID")
Log(r).WithError(err).Error("Failed to select airdrops by nullifier")
ape.RenderErr(w, problems.InternalError())
return
}
if airdrop == nil {
if len(airdrops) == 0 {
ape.RenderErr(w, problems.NotFound())
return
}

ape.Render(w, toAirdropResponse(*airdrop))
airdrop := airdrops[0]
for _, a := range airdrops[1:] {
if a.Status == data.TxStatusCompleted {
airdrop = a
break
}
}

ape.Render(w, toAirdropResponse(airdrop))
}

func toAirdropResponse(tx data.Airdrop) resources.AirdropResponse {
Expand Down
19 changes: 19 additions & 0 deletions internal/service/requests/get_airdrop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package requests

import (
"net/http"

"github.com/go-chi/chi"
val "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)

func NewGetAirdrop(r *http.Request) (nullifier string, err error) {
nullifier = chi.URLParam(r, "nullifier")

err = val.Errors{
"{nullifier}": val.Validate(nullifier, val.Required, is.Digit),
}.Filter()

return
}

0 comments on commit 9e93032

Please sign in to comment.