Skip to content

Commit

Permalink
Fix: content type for ticket balance
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 28, 2023
1 parent e84cee4 commit d14e96d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
14 changes: 6 additions & 8 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,15 @@ func NewSmartRollup(rollup smartrollup.SmartRollup) SmartRollup {
}

type TicketBalance struct {
Ticketer string `json:"ticketer"`
Amount string `json:"amount"`
ContentType stdJSON.RawMessage `json:"content_type"`
Content stdJSON.RawMessage `json:"content"`
Ticketer string `json:"ticketer"`
Amount string `json:"amount"`
ContentType []ast.Typedef `json:"content_type"`
Content *ast.MiguelNode `json:"content,omitempty"`
}

func NewTicketBalance(balance ticket.Balance) TicketBalance {
return TicketBalance{
Ticketer: balance.Ticket.Ticketer.Address,
ContentType: balance.Ticket.ContentType,
Content: balance.Ticket.Content,
Amount: balance.Amount.String(),
Ticketer: balance.Ticket.Ticketer.Address,
Amount: balance.Amount.String(),
}
}
36 changes: 33 additions & 3 deletions cmd/api/handlers/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func GetTicketBalancesForAccount() gin.HandlerFunc {
if handleError(c, ctx.Storage, err, 0) {
return
}
response := make([]TicketBalance, len(balances))
for i := range balances {
response[i] = NewTicketBalance(balances[i])
response, err := prepareTicketBalances(balances)
if handleError(c, ctx.Storage, err, 0) {
return
}
c.SecureJSON(http.StatusOK, response)
}
Expand Down Expand Up @@ -150,3 +150,33 @@ func prepareTicketUpdates(c context.Context, ctx *config.Context, updates []tick

return response, nil
}

func prepareTicketBalances(balances []ticket.Balance) ([]TicketBalance, error) {
response := make([]TicketBalance, len(balances))
for i := range balances {
balance := NewTicketBalance(balances[i])

content, err := ast.NewTypedAstFromBytes(balances[i].Ticket.ContentType)
if err != nil {
return nil, err
}
docs, err := content.Docs("")
if err != nil {
return nil, err
}
balance.ContentType = docs

if err := content.SettleFromBytes(balances[i].Ticket.Content); err != nil {
return nil, err
}
contentMiguel, err := content.ToMiguel()
if err != nil {
return nil, err
}
if len(contentMiguel) > 0 {
balance.Content = contentMiguel[0]
}
response = append(response, balance)
}
return response, nil
}

0 comments on commit d14e96d

Please sign in to comment.