Skip to content

Commit

Permalink
add: get user balance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrynenko committed Jun 12, 2024
1 parent f0c4955 commit e1abe37
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
30 changes: 30 additions & 0 deletions internal/service/api/handlers/get_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package handlers

import (
"net/http"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/rarimo/evm-airdrop-svc/internal/service/api"
"github.com/rarimo/evm-airdrop-svc/internal/service/api/models"
"github.com/rarimo/evm-airdrop-svc/internal/service/api/requests"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
)

func GetBalance(w http.ResponseWriter, r *http.Request) {
req, err := requests.NewGetBalanceRequest(r)
if err != nil {
api.Log(r).WithError(err).Error("failed to parse request")
ape.RenderErr(w, problems.BadRequest(err)...)
return
}

balance, err := api.ERC20Permit(r).BalanceOf(&bind.CallOpts{}, req.Address)
if err != nil {
api.Log(r).WithError(err).Error("failed to get user balance")
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, models.NewBalanceResponse(req.Address.String(), balance))
}
21 changes: 21 additions & 0 deletions internal/service/api/models/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package models

import (
"math/big"

"github.com/rarimo/evm-airdrop-svc/resources"
)

func NewBalanceResponse(addr string, amount *big.Int) resources.BalanceResponse {
return resources.BalanceResponse{
Data: resources.Balance{
Key: resources.Key{
ID: addr,
Type: resources.TOKEN,
},
Attributes: resources.BalanceAttributes{
Amount: amount,
},
},
}
}
32 changes: 32 additions & 0 deletions internal/service/api/requests/get_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"net/http"

"github.com/ethereum/go-ethereum/common"
validation "github.com/go-ozzo/ozzo-validation/v4"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/distributed_lab/urlval"
)

type GetUserBalanceRequest struct {
Address common.Address `url:"address"`
}

func NewGetBalanceRequest(r *http.Request) (GetUserBalanceRequest, error) {
var req GetUserBalanceRequest

if err := urlval.Decode(r.URL.Query(), &req); err != nil {
return req, validation.Errors{
"query": errors.Wrap(err, "failed to decode url"),
}.Filter()
}

return req, req.validate()
}

func (r *GetUserBalanceRequest) validate() error {
return validation.Errors{
"/address": validation.Validate(r.Address, validation.Required),
}.Filter()
}
4 changes: 4 additions & 0 deletions internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func Run(ctx context.Context, cfg *config.Config) {

r.Post("/permit-hash", handlers.BuildPermitHash)
})

r.Route("/token", func(r chi.Router) {
r.Get("/balance", handlers.GetBalance)
})
})

cfg.Log().Info("Service started")
Expand Down

0 comments on commit e1abe37

Please sign in to comment.