Skip to content

Commit

Permalink
Tvl save bulk;
Browse files Browse the repository at this point in the history
  • Loading branch information
mismirnov committed Dec 20, 2024
1 parent d90d757 commit e713682
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
12 changes: 10 additions & 2 deletions internal/storage/postgres/tvl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ func NewTvl(db *database.Bun) *Tvl {
}
}

func (p *Tvl) Save(ctx context.Context, tvl *storage.Tvl) error {
func (t *Tvl) Save(ctx context.Context, tvl *storage.Tvl) error {
if tvl == nil {
return nil
}
_, err := p.db.DB().NewInsert().Model(tvl).Exec(ctx)
_, err := t.db.DB().NewInsert().Model(tvl).Exec(ctx)
return err
}

func (t *Tvl) SaveBulk(ctx context.Context, tvls ...*storage.Tvl) error {
if len(tvls) == 0 {
return nil
}
_, err := t.db.DB().NewInsert().Model(&tvls).Exec(ctx)
return err
}
1 change: 1 addition & 0 deletions internal/storage/tvl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
type ITvl interface {
Save(ctx context.Context, rollupTvl *Tvl) error
SaveBulk(ctx context.Context, tvls ...*Tvl) error
}

// Tvl -
Expand Down
27 changes: 17 additions & 10 deletions pkg/tvl/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,26 @@ func (m *Module) getTvl(ctx context.Context, timeframe storage.TvlTimeframe) {
}

rollupProject := url[lastIndex+1:]
tvl, err := m.rollupTvlFromL2Beat(ctx, rollupProject, storage.TvlTimeframeMax)
tvl, err := m.rollupTvlFromL2Beat(ctx, rollupProject, timeframe)
if err != nil {
m.Log.Err(err).Msg("receiving TVL from L2Beat")
}

for _, t := range tvl[0].Result.Data.Json {
tvlResponse := tvl[0].Result.Data.Json
tvlModels := make([]*storage.Tvl, 0, len(tvlResponse))
for _, t := range tvlResponse {
rollupTvl := t[1].(float64) + t[2].(float64) + t[3].(float64)
tsTvl := int64(t[0].(float64))
if err := m.tvl.Save(ctx, &storage.Tvl{
tvlModels = append(tvlModels, &storage.Tvl{
Value: rollupTvl,
Time: time.Unix(tsTvl, 0),
Rollup: rollups[i],
RollupId: rollups[i].Id,
}); err != nil {
m.Log.Err(err).Msg("saving tvl")
}
})
}

if err := m.tvl.SaveBulk(ctx, tvlModels...); err != nil {
m.Log.Err(err).Msg("saving tvls")
}

continue
Expand All @@ -95,15 +99,18 @@ func (m *Module) getTvl(ctx context.Context, timeframe storage.TvlTimeframe) {
m.Log.Err(err).Msg("receiving TVL from DeFi Lama")
}

tvlModels := make([]*storage.Tvl, 0, len(tvl))
for _, t := range tvl {
if err := m.tvl.Save(ctx, &storage.Tvl{
tvlModels = append(tvlModels, &storage.Tvl{
Value: t.TVL,
Time: time.Unix(t.Date, 0),
Rollup: rollups[i],
RollupId: rollups[i].Id,
}); err != nil {
m.Log.Err(err).Msg("saving tvl")
}
})
}

if err := m.tvl.SaveBulk(ctx, tvlModels...); err != nil {
m.Log.Err(err).Msg("saving tvls")
}

continue
Expand Down

0 comments on commit e713682

Please sign in to comment.