Skip to content

Commit

Permalink
Merge pull request #346 from baking-bad/refactoring/tzip
Browse files Browse the repository at this point in the history
Refactoring/tzip
  • Loading branch information
aopoltorzhicky authored Oct 26, 2020
2 parents 09a4379 + 6355a03 commit 68bf9f5
Show file tree
Hide file tree
Showing 92 changed files with 1,704 additions and 2,809 deletions.
2 changes: 0 additions & 2 deletions cmd/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ RUN swag init --parseDependency
RUN go build -a -installsuffix cgo -o /go/bin/api .

WORKDIR $GOPATH/src/github.com/baking-bad/bcdhub/scripts/
RUN cd aliases && go build -a -installsuffix cgo -o /go/bin/aliases .
RUN cd esctl && go build -a -installsuffix cgo -o /go/bin/esctl .
RUN cd migration && go build -a -installsuffix cgo -o /go/bin/migration .

Expand All @@ -44,7 +43,6 @@ COPY --from=builder /go/bin/api /go/bin/api
COPY /cmd/api/*.yml /app/api/
COPY /cmd/api/data/errors.json /app/api/data/errors.json

COPY --from=builder /go/bin/aliases /go/bin/aliases
COPY --from=builder /go/bin/esctl /go/bin/esctl
COPY --from=builder /go/bin/migration /go/bin/migration

Expand Down
6 changes: 3 additions & 3 deletions cmd/api/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func (ctx *Context) getAccountBalances(network, address string) ([]TokenBalance,
result := make([]TokenBalance, 0)
for key, b := range tokenBalances {
tb := TokenBalance{
Contract: key.Address,
TokenID: key.TokenID,
Balance: b,
Balance: b,
}
token, err := ctx.ES.GetTokenMetadata(elastic.GetTokenMetadataContext{
TokenID: key.TokenID,
Expand All @@ -84,6 +82,8 @@ func (ctx *Context) getAccountBalances(network, address string) ([]TokenBalance,
tb.Name = token[0].Name
tb.Symbol = token[0].Symbol
}
tb.Contract = key.Address
tb.TokenID = key.TokenID

result = append(result, tb)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (ctx *Context) GetBySlug(c *gin.Context) {
return
}

a, err := ctx.DB.GetBySlug(req.Slug)
a, err := ctx.ES.GetBySlug(req.Slug)
if gorm.IsRecordNotFoundError(err) {
handleError(c, err, http.StatusBadRequest)
return
Expand Down
22 changes: 15 additions & 7 deletions cmd/api/handlers/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handlers
import (
"github.com/baking-bad/bcdhub/cmd/api/oauth"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/database"
"github.com/baking-bad/bcdhub/internal/elastic"
"github.com/gin-gonic/gin"
)

Expand All @@ -17,7 +17,7 @@ type tokenKey struct {
type Context struct {
*config.Context
OAUTH oauth.Config
Tokens map[tokenKey]database.Token
Tokens map[tokenKey]TokenMetadata
}

// NewContext -
Expand All @@ -43,18 +43,26 @@ func NewContext(cfg config.Config) (*Context, error) {
config.WithRabbit(cfg.RabbitMQ, cfg.API.ProjectName, cfg.API.Queues),
)

tokens, err := ctx.DB.GetTokens()
tokens, err := ctx.ES.GetTokenMetadata(elastic.GetTokenMetadataContext{
TokenID: -1,
})
if err != nil {
return nil, err
}

mapTokens := make(map[tokenKey]database.Token)
mapTokens := make(map[tokenKey]TokenMetadata)
for i := range tokens {
mapTokens[tokenKey{
Network: tokens[i].Network,
Contract: tokens[i].Contract,
Contract: tokens[i].Address,
TokenID: int64(tokens[i].TokenID),
}] = tokens[i]
}] = TokenMetadata{
Contract: tokens[i].Address,
TokenID: tokens[i].TokenID,
Symbol: tokens[i].Symbol,
Name: tokens[i].Name,
Decimals: tokens[i].Decimals,
}
}

return &Context{
Expand All @@ -76,7 +84,7 @@ func CurrentUserID(c *gin.Context) uint {
}

// FindToken -
func (ctx *Context) FindToken(network, address string, tokenID int64) (database.Token, bool) {
func (ctx *Context) FindToken(network, address string, tokenID int64) (TokenMetadata, bool) {
token, ok := ctx.Tokens[tokenKey{
Network: network,
Contract: address,
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (ctx *Context) contractPostprocessing(contract models.Contract, c *gin.Cont
return res, err
}

if alias, err := ctx.DB.GetAlias(contract.Address, contract.Network); err == nil {
if alias, err := ctx.ES.GetAlias(contract.Network, contract.Address); err == nil {
res.Slug = alias.Slug
} else {
return res, err
Expand Down
23 changes: 10 additions & 13 deletions cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import (
"net/http"

"github.com/baking-bad/bcdhub/internal/contractparser/consts"
"github.com/baking-bad/bcdhub/internal/database"
"github.com/baking-bad/bcdhub/internal/elastic"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/tzip"
"github.com/gin-gonic/gin"
)

// GetDAppList -
func (ctx *Context) GetDAppList(c *gin.Context) {
dapps, err := ctx.DB.GetDApps()
dapps, err := ctx.ES.GetDApps()
if handleError(c, err, 0) {
return
}

results := make([]DApp, len(dapps))
for i := range dapps {
result, err := ctx.appendDAppInfo(dapps[i], false)
result, err := ctx.appendDAppInfo(&dapps[i], false)
if handleError(c, err, 0) {
return
}
Expand All @@ -36,7 +35,7 @@ func (ctx *Context) GetDApp(c *gin.Context) {
return
}

dapp, err := ctx.DB.GetDAppBySlug(req.Slug)
dapp, err := ctx.ES.GetDAppBySlug(req.Slug)
if handleError(c, err, 0) {
return
}
Expand All @@ -49,13 +48,11 @@ func (ctx *Context) GetDApp(c *gin.Context) {
c.JSON(http.StatusOK, response)
}

func (ctx *Context) appendDAppInfo(dapp database.DApp, withDetails bool) (DApp, error) {
func (ctx *Context) appendDAppInfo(dapp *tzip.DApp, withDetails bool) (DApp, error) {
result := DApp{
Name: dapp.Name,
ShortDescription: dapp.ShortDescription,
FullDescription: dapp.FullDescription,
Version: dapp.Version,
License: dapp.License,
WebSite: dapp.WebSite,
Slug: dapp.Slug,
AgoraReviewPostID: dapp.AgoraReviewPostID,
Expand Down Expand Up @@ -88,7 +85,7 @@ func (ctx *Context) appendDAppInfo(dapp database.DApp, withDetails bool) (DApp,

if withDetails {
if len(dapp.DexTokens) > 0 {
result.DexTokens = make([]models.TokenMetadata, 0)
result.DexTokens = make([]TokenMetadata, 0)
for _, token := range dapp.DexTokens {
tokenMetadata, err := ctx.ES.GetTokenMetadata(elastic.GetTokenMetadataContext{
Contract: token.Contract,
Expand All @@ -101,7 +98,10 @@ func (ctx *Context) appendDAppInfo(dapp database.DApp, withDetails bool) (DApp,
}
return result, err
}
result.DexTokens = append(result.DexTokens, tokenMetadata...)
for i := range tokenMetadata {
tm := TokenMetadataFromElasticModel(tokenMetadata[i])
result.DexTokens = append(result.DexTokens, tm)
}
}
}

Expand All @@ -125,9 +125,6 @@ func (ctx *Context) appendDAppInfo(dapp database.DApp, withDetails bool) (DApp,

tokens, err := ctx.getTokens(consts.Mainnet, address)
if err != nil {
if elastic.IsRecordNotFound(err) {
continue
}
return result, err
}
result.Tokens = append(result.Tokens, tokens...)
Expand Down
40 changes: 26 additions & 14 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/baking-bad/bcdhub/internal/contractparser/cerrors"
"github.com/baking-bad/bcdhub/internal/contractparser/docstring"
"github.com/baking-bad/bcdhub/internal/contractparser/formatter"
"github.com/baking-bad/bcdhub/internal/database"
"github.com/baking-bad/bcdhub/internal/elastic"
"github.com/baking-bad/bcdhub/internal/jsonschema"
"github.com/baking-bad/bcdhub/internal/models"
Expand Down Expand Up @@ -447,8 +446,8 @@ type Alias struct {
}

// FromModel -
func (a *Alias) FromModel(alias database.Alias) {
a.Alias = alias.Alias
func (a *Alias) FromModel(alias *models.TZIP) {
a.Alias = alias.Name
a.Address = alias.Address
a.Network = alias.Network
a.Slug = alias.Slug
Expand Down Expand Up @@ -573,7 +572,7 @@ type BigMapHistoryItem struct {
// Transfer -
type Transfer struct {
*models.Transfer
Token *database.Token `json:"token,omitempty"`
Token *TokenMetadata `json:"token,omitempty"`
}

// TransferResponse -
Expand All @@ -595,12 +594,10 @@ type DApp struct {
Name string `json:"name"`
ShortDescription string `json:"short_description"`
FullDescription string `json:"full_description"`
Version string `json:"version"`
License string `json:"license"`
WebSite string `json:"website"`
Slug string `json:"slug,omitempty"`
AgoraReviewPostID uint `json:"agora_review_post_id,omitempty"`
AgoraQAPostID uint `json:"agora_qa_post_id,omitempty"`
AgoraReviewPostID int64 `json:"agora_review_post_id,omitempty"`
AgoraQAPostID int64 `json:"agora_qa_post_id,omitempty"`
Authors []string `json:"authors"`
SocialLinks []string `json:"social_links"`
Interfaces []string `json:"interfaces"`
Expand All @@ -609,10 +606,10 @@ type DApp struct {
Logo string `json:"logo"`
Cover string `json:"cover,omitempty"`

Screenshots []Screenshot `json:"screenshots,omitempty"`
Contracts []DAppContract `json:"contracts,omitempty"`
DexTokens []models.TokenMetadata `json:"dex_tokens,omitempty"`
Tokens []Token `json:"tokens,omitempty"`
Screenshots []Screenshot `json:"screenshots,omitempty"`
Contracts []DAppContract `json:"contracts,omitempty"`
DexTokens []TokenMetadata `json:"dex_tokens,omitempty"`
Tokens []Token `json:"tokens,omitempty"`
}

// DAppContract -
Expand All @@ -631,7 +628,7 @@ type Screenshot struct {

// Token -
type Token struct {
models.TokenMetadata
TokenMetadata
elastic.TokenSupply
}

Expand All @@ -648,10 +645,25 @@ type AccountInfo struct {

// TokenBalance -
type TokenBalance struct {
TokenMetadata
Balance int64 `json:"balance"`
}

// TokenMetadata -
type TokenMetadata struct {
Contract string `json:"contract"`
TokenID int64 `json:"token_id"`
Balance int64 `json:"balance"`
Symbol string `json:"symbol,omitempty"`
Name string `json:"name,omitempty"`
Decimals int64 `json:"decimals"`
}

// TokenMetadataFromElasticModel -
func TokenMetadataFromElasticModel(model elastic.TokenMetadata) (tm TokenMetadata) {
tm.TokenID = model.TokenID
tm.Symbol = model.Symbol
tm.Name = model.Name
tm.Decimals = model.Decimals
tm.Contract = model.Address
return
}
12 changes: 4 additions & 8 deletions cmd/api/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,7 @@ func (ctx *Context) GetContractTokens(c *gin.Context) {
return
}
tokens, err := ctx.getTokens(req.Network, req.Address)
if err != nil {
if !elastic.IsRecordNotFound(err) {
handleError(c, err, 0)
} else {
c.JSON(http.StatusOK, []interface{}{})
}
if handleError(c, err, 0) {
return
}
c.JSON(http.StatusOK, tokens)
Expand All @@ -302,7 +297,7 @@ func (ctx *Context) getTokens(network, address string) ([]Token, error) {
})
if err != nil {
if elastic.IsRecordNotFound(err) {
return nil, nil
return []Token{}, nil
}
return nil, err
}
Expand All @@ -312,8 +307,9 @@ func (ctx *Context) getTokens(network, address string) ([]Token, error) {
if err != nil {
return nil, err
}
tokenMetadata := TokenMetadataFromElasticModel(token)
tokens = append(tokens, Token{
token, supply,
tokenMetadata, supply,
})
}
return tokens, nil
Expand Down
39 changes: 39 additions & 0 deletions cmd/api/handlers/tzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package handlers

import (
"net/http"

"github.com/baking-bad/bcdhub/internal/elastic"
"github.com/gin-gonic/gin"
)

// GetMetadata godoc
// @Summary Get metadata for account
// @Description Returns full metadata for account
// @Tags account
// @ID get-account-tzip
// @Param network path string true "Network"
// @Param address path string true "KT or tz address" minlength(36) maxlength(36)
// @Accept json
// @Produce json
// @Success 200 {object} models.TZIP
// @Success 204 {object} gin.H
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /account/{network}/{address}/tzip [get]
func (ctx *Context) GetMetadata(c *gin.Context) {
var req getContractRequest
if err := c.BindUri(&req); handleError(c, err, http.StatusBadRequest) {
return
}
tzip, err := ctx.ES.GetTZIP(req.Network, req.Address)
if err != nil {
if elastic.IsRecordNotFound(err) {
c.JSON(http.StatusOK, gin.H{})
} else {
handleError(c, err, 0)
}
return
}
c.JSON(http.StatusOK, tzip)
}
2 changes: 2 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func main() {
contract.GET("operations", ctx.GetContractOperations)
contract.GET("migrations", ctx.GetContractMigrations)
contract.GET("transfers", ctx.GetContractTransfers)

tokens := contract.Group("tokens")
{
tokens.GET("", ctx.GetContractTokens)
Expand Down Expand Up @@ -211,6 +212,7 @@ func main() {
account := v1.Group("account/:network/:address")
{
account.GET("", ctx.GetInfo)
account.GET("metadata", ctx.GetMetadata)
}

fa12 := v1.Group("tokens/:network")
Expand Down
Loading

0 comments on commit 68bf9f5

Please sign in to comment.