-
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.
* docs: get balance request * update: dependencies * update: generated docs * add: get user balance endpoint
- Loading branch information
Showing
12 changed files
with
200 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,16 @@ | ||
allOf: | ||
- $ref: '#/components/schemas/TokenKey' | ||
- type: object | ||
required: | ||
- attributes | ||
properties: | ||
attributes: | ||
type: object | ||
required: | ||
- amount | ||
properties: | ||
amount: | ||
type: integer | ||
format: "*big.Int" | ||
description: Amount of the tokens on the user address | ||
example: "123" |
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,10 @@ | ||
type: object | ||
required: | ||
- id | ||
- type | ||
properties: | ||
id: | ||
type: string | ||
type: | ||
type: string | ||
enum: [ token ] |
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,28 @@ | ||
get: | ||
tags: | ||
- Tokens | ||
summary: Get Balance | ||
description: Get token's balance by the given user address | ||
operationId: GetBalance | ||
parameters: | ||
- in: query | ||
name: address | ||
required: true | ||
schema: | ||
type: string | ||
format: common.Address | ||
responses: | ||
200: | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
type: object | ||
required: | ||
- data | ||
properties: | ||
data: | ||
$ref: '#/components/schemas/Balance' | ||
400: | ||
$ref: '#/components/responses/invalidParameter' | ||
500: | ||
$ref: '#/components/responses/internalError' |
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
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
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
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,43 @@ | ||
/* | ||
* GENERATED. Do not modify. Your changes might be overwritten! | ||
*/ | ||
|
||
package resources | ||
|
||
import "encoding/json" | ||
|
||
type Balance struct { | ||
Key | ||
Attributes BalanceAttributes `json:"attributes"` | ||
} | ||
type BalanceResponse struct { | ||
Data Balance `json:"data"` | ||
Included Included `json:"included"` | ||
} | ||
|
||
type BalanceListResponse struct { | ||
Data []Balance `json:"data"` | ||
Included Included `json:"included"` | ||
Links *Links `json:"links"` | ||
Meta json.RawMessage `json:"meta,omitempty"` | ||
} | ||
|
||
func (r *BalanceListResponse) PutMeta(v interface{}) (err error) { | ||
r.Meta, err = json.Marshal(v) | ||
return err | ||
} | ||
|
||
func (r *BalanceListResponse) GetMeta(out interface{}) error { | ||
return json.Unmarshal(r.Meta, out) | ||
} | ||
|
||
// MustBalance - returns Balance from include collection. | ||
// if entry with specified key does not exist - returns nil | ||
// if entry with specified key exists but type or ID mismatches - panics | ||
func (c *Included) MustBalance(key Key) *Balance { | ||
var balance Balance | ||
if c.tryFindEntry(key, &balance) { | ||
return &balance | ||
} | ||
return nil | ||
} |
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,12 @@ | ||
/* | ||
* GENERATED. Do not modify. Your changes might be overwritten! | ||
*/ | ||
|
||
package resources | ||
|
||
import "math/big" | ||
|
||
type BalanceAttributes struct { | ||
// Amount of the tokens on the user address | ||
Amount *big.Int `json:"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