-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters