Skip to content

Commit

Permalink
feat: get onchain asset average cost (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
phucledien authored May 31, 2024
1 parent 4a3203e commit 8f39eb2
Show file tree
Hide file tree
Showing 23 changed files with 1,981 additions and 1 deletion.
33 changes: 33 additions & 0 deletions cmd/update-user-onchain-asset-average-cost/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"time"

"github.com/defipod/mochi/pkg/config"
"github.com/defipod/mochi/pkg/entities"
"github.com/defipod/mochi/pkg/job"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/service"
)

func main() {
cfg := config.LoadConfig(config.DefaultConfigLoaders())
log := logger.NewLogrusLogger()
// *** entities ***
err := entities.Init(cfg, log)
if err != nil {
log.Fatal(err, "failed to init entities")
}

service, err := service.NewService(cfg, log)
if err != nil {
log.Fatal(err, "failed to init service")
}
defer service.Sentry.Flush(2 * time.Second)

if err := job.NewUpdateUserOnchainAssetAvgCostJob(entities.Get()).Run(); err != nil {
log.Fatal(err, "failed to run job")
}

log.Info("done")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS onchain_asset_avg_costs (
wallet_address TEXT NOT NULL,
token_address TEXT NOT NULL,
symbol TEXT NOT NULL,
blockchain TEXT NOT NULL,
average_cost FLOAT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE UNIQUE INDEX "uidx_wallet_address_token_address_chain" ON onchain_asset_avg_costs (wallet_address, token_address, blockchain);

-- +migrate Down
DELETE TABLE IF EXISTS onchain_asset_avg_costs;
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ type Config struct {

SentryDsn string
TonConfigURL string

DuneApiKey string
}

type MarketplaceBaseUrl struct {
Expand Down Expand Up @@ -383,6 +385,8 @@ func generateConfigFromViper(v *viper.Viper) Config {

SentryDsn: v.GetString("SENTRY_DSN"),
TonConfigURL: v.GetString("TON_CONFIG_URL"),

DuneApiKey: v.GetString("DUNE_API_KEY"),
}
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/entities/onchain_asset_avg_cost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package entities

import (
"github.com/defipod/mochi/pkg/model"
)

func (e *Entity) UpsertManyOnchainAssetAvgCost(assets []model.OnchainAssetAvgCost) error {
err := e.repo.OnchainAssetAverageCost.UpsertMany(assets)
if err != nil {
e.log.Error(err, "[entities.UpsertManyOnchainAssetAvgCost] - repo.OnchainAssetAverageCost.UpsertMany failed")
return err
}
return nil
}

func (e *Entity) GetOnchainAssetAvgCostByWalletAddress(walletAddress string) ([]model.OnchainAssetAvgCost, error) {
assets, err := e.repo.OnchainAssetAverageCost.GetByWalletAddr(walletAddress)
if err != nil {
e.log.Error(err, "[entities.GetOnchainAssetAvgCostByWalletAddress] - repo.OnchainAssetAverageCost.GetByWalletAddress failed")
return nil, err
}
return assets, nil
}
11 changes: 11 additions & 0 deletions pkg/entities/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,14 @@ func (e *Entity) Swap(req request.SwapRequest) (interface{}, error) {
}
return nil, nil
}

func (e *Entity) GetOnchainAssetAvgCost(req request.GetOnchainAssetAvgCost) ([]model.OnchainAssetAvgCost, error) {
// get all onchain asset avg cost
assets, err := e.repo.OnchainAssetAverageCost.GetByWalletAddr(req.WalletAddress)
if err != nil {
e.log.Fields(logger.Fields{"req": req}).Error(err, "[entity.GetOnchainAssetAvgCost] - repo.OnchainAssetAverageCost.GetByWalletAddr() failed")
return nil, err
}

return assets, nil
}
1 change: 1 addition & 0 deletions pkg/handler/swap/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type IHandler interface {
GetSwapRoutes(c *gin.Context)
ExecuteSwapRoutes(c *gin.Context)
OnchainData(c *gin.Context)
GetOnchainAssetAvgCost(c *gin.Context)
}
18 changes: 18 additions & 0 deletions pkg/handler/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,21 @@ func (h *Handler) OnchainData(c *gin.Context) {

c.JSON(http.StatusOK, response.CreateResponse[any](data, nil, nil, nil))
}

func (h *Handler) GetOnchainAssetAvgCost(c *gin.Context) {
var req request.GetOnchainAssetAvgCost
if err := c.ShouldBindQuery(&req); err != nil {
h.log.Fields(logger.Fields{"req": req}).Error(err, "[handler.GetOnchainAssetAvgCost] - failed to read JSON")
c.JSON(http.StatusBadRequest, response.CreateResponse[any](nil, nil, err, nil))
return
}

data, err := h.entities.GetOnchainAssetAvgCost(req)
if err != nil {
h.log.Fields(logger.Fields{"wallet_address": req.WalletAddress}).Error(err, "[handler.GetOnchainAssetAvgCost] - entity.GetOnchainAssetAvgCost() failed")
c.JSON(baseerrs.GetStatusCode(err), response.CreateResponse[any](nil, nil, err, nil))
return
}

c.JSON(http.StatusOK, response.CreateResponse[any](data, nil, nil, nil))
}
Loading

0 comments on commit 8f39eb2

Please sign in to comment.