cic-dw/internal/public/balances.go

67 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-05-14 10:15:45 +02:00
package public
import (
"cic-dw/pkg/address"
2022-05-14 10:15:45 +02:00
"context"
"net/http"
"github.com/ethereum/go-ethereum/common"
2022-05-14 10:15:45 +02:00
"github.com/georgysavva/scany/pgxscan"
"github.com/labstack/echo/v4"
"github.com/lmittmann/w3"
2022-05-14 10:15:45 +02:00
)
type balanceRes struct {
TokenSymbol string `json:"symbol"`
Balance int64 `json:"balance"`
}
2022-05-14 10:15:45 +02:00
type dbRes struct {
TokenSymbol string `db:"token_symbol"`
TokenAddress string `db:"token_address"`
}
func handleBalancesQuery(c echo.Context) error {
var (
api = c.Get("api").(*api)
data []dbRes
tokenAddresses []common.Address
res []balanceRes
2022-05-14 10:15:45 +02:00
)
// TODO: return 400
qAddress, err := address.SarafuAddress(c.Param("address"))
if err != nil {
return err
}
rows, err := api.db.Query(context.Background(), api.q["all-known-tokens"], qAddress)
2022-05-14 10:15:45 +02:00
if err != nil {
return err
}
if err := pgxscan.ScanAll(&data, rows); err != nil {
return err
}
for _, rowData := range data {
tokenAddresses = append(tokenAddresses, w3.A(address.Checksum(rowData.TokenAddress)))
}
balances, err := api.bb.TokensBalance(context.Background(), w3.A(address.Checksum(qAddress)), tokenAddresses)
if err != nil {
return err
}
for i, balance := range balances {
var d balanceRes
d.Balance = balance.Int64()
d.TokenSymbol = data[i].TokenSymbol
res = append(res, d)
}
return c.JSON(http.StatusOK, res)
2022-05-14 10:15:45 +02:00
}