Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Crescent and Osmosis LPs tokens prices #152

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions api/cached/api.go

This file was deleted.

5 changes: 2 additions & 3 deletions api/chains/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (
"testing"
"time"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/emerishq/demeris-api-server/api/chains"
utils "github.com/emerishq/demeris-api-server/api/test_utils"
"github.com/emerishq/demeris-api-server/lib/stringcache"
"github.com/emerishq/emeris-utils/exported/sdktypes"
"github.com/emerishq/demeris-backend-models/cns"
"github.com/emerishq/emeris-utils/logging"
"github.com/gin-gonic/gin"

"github.com/emerishq/demeris-backend-models/cns"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion api/chains/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"time"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/emerishq/demeris-backend-models/cns"
"github.com/emerishq/emeris-utils/exported/sdktypes"
)

//go:generate mockgen -package chains_test -source ports.go -destination ports_mocks_test.go
Expand Down
6 changes: 3 additions & 3 deletions api/chains/ports_mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions api/database/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,27 @@ func (d *Database) ChainsOnlineStatuses(ctx context.Context) ([]ChainOnlineStatu

return rows, nil
}

func (d *Database) Denoms(ctx context.Context) ([]cns.Denom, error) {
defer sentry.StartSpan(ctx, "db.DenomTicker").Finish()

q := `
SELECT denoms
FROM cns.chains
WHERE enabled
`

var rows []struct {
Denoms cns.DenomList `db:"denoms"`
}
if err := d.dbi.DB.SelectContext(ctx, &rows, q); err != nil {
return nil, err
}

var ret []cns.Denom
for _, r := range rows {
ret = append(ret, r.Denoms...)
}

return ret, nil
}
6 changes: 6 additions & 0 deletions api/database/chains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,9 @@ func (s *TestSuite) TestChainsOnlineStatuses() {
})
}
}

func (s *TestSuite) TestDenoms() {
denoms, err := s.ctx.Router.DB.Denoms(context.Background())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this setup, you can expect one denom :

	err := s.ctx.CnsDB.AddChain(utils.ChainWithoutPublicEndpoints)
	require.NoError(t, err)
  

s.Require().NoError(err)
s.Require().Greater(len(denoms), 0)
}
11 changes: 11 additions & 0 deletions api/prices/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package prices

import (
"context"

"github.com/emerishq/demeris-api-server/usecase"
)

type App interface {
PoolPrices(ctx context.Context) usecase.PoolPricesResult
}
55 changes: 55 additions & 0 deletions api/prices/prices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package prices

import (
"net/http"

"github.com/emerishq/demeris-api-server/lib/ginutils"
"github.com/emerishq/demeris-api-server/usecase"
"github.com/emerishq/emeris-utils/logging"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

type PricesAPI struct {
app App
}

func New(app App) *PricesAPI {
return &PricesAPI{
app: app,
}
}

func Register(router *gin.Engine, app App) {
api := New(app)

router.Group("/prices").
GET("/pools", api.GetPoolsPrices)
}

type GetPoolsPricesResponse struct {
PoolResults usecase.PoolPricesResult `json:"pool_results"`
}

// GetPoolsPrices returns the list of available pools and their associated token's price.
// @Summary Get pools' tokens prices
// @Tags Price
// @ID get-pools-prices
// @Produce json
// @Success 200 {object} GetPoolsPricesResponse
// @Failure 500 {object} apierrors.UserFacingError
// @Router /prices/pools [get]
func (api *PricesAPI) GetPoolsPrices(c *gin.Context) {
prices := api.app.PoolPrices(c.Request.Context())

logger := ginutils.GetValue[*zap.SugaredLogger](c, logging.LoggerKey)
for _, opt := range prices {
if opt.Err != nil {
logger.Errorw("fetching pool price", "err", opt.Err)
}
}

c.JSON(http.StatusOK, GetPoolsPricesResponse{
PoolResults: prices,
})
}
8 changes: 4 additions & 4 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"

"github.com/emerishq/demeris-api-server/api/block"
"github.com/emerishq/demeris-api-server/api/cached"
"github.com/emerishq/demeris-api-server/api/liquidity"
"github.com/emerishq/demeris-api-server/api/prices"
"github.com/emerishq/demeris-api-server/lib/apierrors"
"github.com/emerishq/demeris-api-server/lib/stringcache"
"github.com/emerishq/demeris-api-server/sdkservice"
Expand Down Expand Up @@ -162,7 +162,7 @@ func registerRoutes(engine *gin.Engine, db *database.Database, s *store.Store,
// @tag.description pool-related endpoints
liquidity.Register(engine, db, s)

// @tag.name cached
// @tag.description cached data endpoints
cached.Register(engine, db, s)
// @tag.name prices
// @tag.description prices related endpoints
prices.Register(engine, app)
}
13 changes: 12 additions & 1 deletion cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/emerishq/demeris-api-server/api/database"
"github.com/emerishq/demeris-api-server/api/router"
"github.com/emerishq/demeris-api-server/lib/fflag"
"github.com/emerishq/demeris-api-server/lib/poclient"
"github.com/emerishq/demeris-api-server/sdkservice"
"github.com/emerishq/demeris-api-server/usecase"
"github.com/emerishq/emeris-utils/k8s"
Expand Down Expand Up @@ -108,7 +109,17 @@ func main() {
l.Panicw("cannot initialize sdk-service clients", "error", err)
}

app := usecase.NewApp(sdkServiceClients)
osmosisClient := usecase.NewOsmosisGrpcClient("osmosis:9090")

crescentClient := usecase.NewCrescentGrpcClient("crescent:9090")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO these GRPC clients shouldn't be located in the usecase package, because they don't contain business code. Moreover they can't be tested efficiently. I suggest to move them in a specific package like poclient, for instance grpcclient ?

In addition, it's weird to have both OsmosisClient interface and implementation in the same package


priceOracle := poclient.NewPOClient("http://price-oracle:8000")

denomer := usecase.NewDatabaseDenomer(dbi)

denomPricer := usecase.NewPriceOracleDenomPricer(denomer, dbi, priceOracle)

app := usecase.NewApp(sdkServiceClients, osmosisClient, crescentClient, denomPricer)

r := router.New(
dbi,
Expand Down
1 change: 0 additions & 1 deletion docs/swagger_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
_ "github.com/emerishq/demeris-backend-models/tracelistener"
_ "github.com/emerishq/emeris-utils/exported/sdktypes"
_ "github.com/emerishq/emeris-utils/store"
_ "github.com/gravity-devs/liquidity/x/liquidity/types"
_ "github.com/swaggo/swag"
_ "github.com/tendermint/tendermint/proto/tendermint/version"
_ "github.com/tendermint/tendermint/rpc/core/types"
Expand Down
Loading