From db24b9ba2e0315f1a58d762b1dc0c6c4b2891311 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 14 Sep 2024 12:45:28 +0300 Subject: [PATCH] Fix: token savings --- internal/storage/postgres/transaction.go | 9 +++------ internal/storage/postgres/transaction_test.go | 2 ++ pkg/indexer/store/store.go | 5 ++--- pkg/indexer/store/sub_models.go | 6 +----- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/internal/storage/postgres/transaction.go b/internal/storage/postgres/transaction.go index 4528343..8885b91 100644 --- a/internal/storage/postgres/transaction.go +++ b/internal/storage/postgres/transaction.go @@ -59,13 +59,10 @@ func (t Transaction) SaveTokens(ctx context.Context, tokens ...*models.Token) er if len(tokens) == 0 { return nil } - - values := t.Tx().NewValues(&tokens).Column("first_height", "contract_id", "token_id", "type") - _, err := t.Tx().NewInsert().Column("first_height", "contract_id", "token_id", "type"). - With("_data", values). - Model((*models.Token)(nil)). + _, err := t.Tx(). + NewInsert(). + Model(&tokens). On("CONFLICT ON CONSTRAINT token_unique_id DO NOTHING"). - TableExpr("_data"). Exec(ctx) return err } diff --git a/internal/storage/postgres/transaction_test.go b/internal/storage/postgres/transaction_test.go index accde59..3908a5b 100644 --- a/internal/storage/postgres/transaction_test.go +++ b/internal/storage/postgres/transaction_test.go @@ -182,6 +182,8 @@ func (s *TransactionTest) TestTokens() { err = tx.SaveTokens(ctx, tokens...) s.Require().NoError(err) + s.Require().Greater(tokens[0].ID, uint64(0)) + s.Require().Greater(tokens[1].ID, uint64(0)) err = tx.Flush(ctx) s.Require().NoError(err) diff --git a/pkg/indexer/store/store.go b/pkg/indexer/store/store.go index 1b1a355..873862d 100644 --- a/pkg/indexer/store/store.go +++ b/pkg/indexer/store/store.go @@ -6,7 +6,6 @@ import ( models "github.com/dipdup-io/starknet-indexer/internal/storage" "github.com/dipdup-io/starknet-indexer/internal/storage/postgres" "github.com/dipdup-io/starknet-indexer/pkg/indexer/cache" - "github.com/dipdup-io/starknet-indexer/pkg/indexer/parser/data" parserData "github.com/dipdup-io/starknet-indexer/pkg/indexer/parser/data" "github.com/dipdup-net/go-lib/database" "github.com/dipdup-net/indexer-sdk/pkg/storage" @@ -104,7 +103,7 @@ func (store *Store) Save( } if result.Block.TxCount > 0 { - sm := newSubModels(store.internals, store.transfers, store.events) + sm := newSubModels() if err := store.saveDeclare(ctx, tx, result, sm); err != nil { return tx.HandleError(ctx, err) @@ -189,7 +188,7 @@ func (store *Store) saveInternals( return nil } -func (store *Store) saveProxies(ctx context.Context, tx postgres.Transaction, proxies data.ProxyMap[*models.ProxyUpgrade]) error { +func (store *Store) saveProxies(ctx context.Context, tx postgres.Transaction, proxies parserData.ProxyMap[*models.ProxyUpgrade]) error { return proxies.Range(func(_ parserData.ProxyKey, value *models.ProxyUpgrade) (bool, error) { store.cache.SetProxy(value.Hash, value.Selector, value.ToProxy()) if err := tx.Add(ctx, value); err != nil { diff --git a/pkg/indexer/store/sub_models.go b/pkg/indexer/store/sub_models.go index d65f64b..8b41ac9 100644 --- a/pkg/indexer/store/sub_models.go +++ b/pkg/indexer/store/sub_models.go @@ -19,11 +19,7 @@ type subModels struct { Internals []models.Internal } -func newSubModels( - internalsStorage models.IInternal, - transfersStorage models.ITransfer, - eventsStorage models.IEvent, -) *subModels { +func newSubModels() *subModels { return &subModels{ Transfers: make([]models.Transfer, 0), Events: make([]models.Event, 0),