2022-05-14 10:15:45 +02:00
|
|
|
package public
|
|
|
|
|
|
|
|
import (
|
2022-05-19 08:52:34 +02:00
|
|
|
"cic-dw/pkg/address"
|
2022-05-14 10:15:45 +02:00
|
|
|
"context"
|
2022-05-19 08:52:34 +02:00
|
|
|
"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"
|
2022-05-19 08:52:34 +02:00
|
|
|
"github.com/lmittmann/w3"
|
2022-05-14 10:15:45 +02:00
|
|
|
)
|
|
|
|
|
2022-05-19 08:52:34 +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 (
|
2022-05-19 08:52:34 +02:00
|
|
|
api = c.Get("api").(*api)
|
|
|
|
data []dbRes
|
|
|
|
tokenAddresses []common.Address
|
|
|
|
res []balanceRes
|
2022-05-14 10:15:45 +02:00
|
|
|
)
|
|
|
|
|
2022-05-19 08:52:34 +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
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:52:34 +02:00
|
|
|
for _, rowData := range data {
|
|
|
|
tokenAddresses = append(tokenAddresses, w3.A(address.Checksum(rowData.TokenAddress)))
|
|
|
|
}
|
|
|
|
|
2022-05-23 14:41:24 +02:00
|
|
|
balances, err := api.bb.TokensBalance(context.Background(), w3.A(address.Checksum(qAddress)), tokenAddresses)
|
2022-05-19 08:52:34 +02:00
|
|
|
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
|
|
|
}
|