From e4d41cc7a44b937be555528f2ed0d9c3baddac38 Mon Sep 17 00:00:00 2001 From: Gaze Date: Sun, 9 Jun 2024 17:00:01 +0700 Subject: [PATCH] feat: skip non-brc20 transfers --- .../brc20/database/postgresql/queries/data.sql | 8 ++++---- modules/brc20/internal/datagateway/brc20.go | 2 +- modules/brc20/internal/entity/event_deploy.go | 2 +- .../internal/entity/event_inscribe_transfer.go | 2 +- modules/brc20/internal/entity/event_mint.go | 2 +- .../internal/entity/event_transfer_transfer.go | 2 +- .../brc20/internal/repository/postgres/brc20.go | 4 ++-- .../internal/repository/postgres/gen/data.sql.go | 16 ++++++++-------- .../brc20/internal/repository/postgres/mapper.go | 8 ++++---- modules/brc20/processor_brc20.go | 8 ++++++++ 10 files changed, 31 insertions(+), 23 deletions(-) diff --git a/modules/brc20/database/postgresql/queries/data.sql b/modules/brc20/database/postgresql/queries/data.sql index 14ca824..fa2664b 100644 --- a/modules/brc20/database/postgresql/queries/data.sql +++ b/modules/brc20/database/postgresql/queries/data.sql @@ -55,10 +55,10 @@ WITH "latest_deploy_id" AS ( SELECT "id" FROM "brc20_event_transfer_transfers" ORDER BY "id" DESC LIMIT 1 ) SELECT - (SELECT "id" FROM "latest_deploy_id") AS "event_deploy_id", - (SELECT "id" FROM "latest_mint_id") AS "event_mint_id", - (SELECT "id" FROM "latest_inscribe_transfer_id") AS "event_inscribe_transfer_id", - (SELECT "id" FROM "latest_transfer_transfer_id") AS "event_transfer_transfer_id"; + COALESCE((SELECT "id" FROM "latest_deploy_id"), -1) AS "event_deploy_id", + COALESCE((SELECT "id" FROM "latest_mint_id"), -1) AS "event_mint_id", + COALESCE((SELECT "id" FROM "latest_inscribe_transfer_id"), -1) AS "event_inscribe_transfer_id", + COALESCE((SELECT "id" FROM "latest_transfer_transfer_id"), -1) AS "event_transfer_transfer_id"; -- name: GetBalancesBatchAtHeight :many SELECT DISTINCT ON ("brc20_balances"."pkscript", "brc20_balances"."tick") "brc20_balances".* FROM "brc20_balances" diff --git a/modules/brc20/internal/datagateway/brc20.go b/modules/brc20/internal/datagateway/brc20.go index 9d0d429..ef1fb98 100644 --- a/modules/brc20/internal/datagateway/brc20.go +++ b/modules/brc20/internal/datagateway/brc20.go @@ -33,7 +33,7 @@ type BRC20ReaderDataGateway interface { GetBalancesBatchAtHeight(ctx context.Context, blockHeight uint64, queries []GetBalancesBatchAtHeightQuery) (map[string]map[string]*entity.Balance, error) GetTickEntriesByTicks(ctx context.Context, ticks []string) (map[string]*entity.TickEntry, error) GetEventInscribeTransfersByInscriptionIds(ctx context.Context, ids []ordinals.InscriptionId) (map[ordinals.InscriptionId]*entity.EventInscribeTransfer, error) - GetLatestEventId(ctx context.Context) (uint64, error) + GetLatestEventId(ctx context.Context) (int64, error) } type BRC20WriterDataGateway interface { diff --git a/modules/brc20/internal/entity/event_deploy.go b/modules/brc20/internal/entity/event_deploy.go index 55277d5..02a59fa 100644 --- a/modules/brc20/internal/entity/event_deploy.go +++ b/modules/brc20/internal/entity/event_deploy.go @@ -9,7 +9,7 @@ import ( ) type EventDeploy struct { - Id uint64 + Id int64 InscriptionId ordinals.InscriptionId InscriptionNumber int64 Tick string diff --git a/modules/brc20/internal/entity/event_inscribe_transfer.go b/modules/brc20/internal/entity/event_inscribe_transfer.go index 34293cd..37ed8f3 100644 --- a/modules/brc20/internal/entity/event_inscribe_transfer.go +++ b/modules/brc20/internal/entity/event_inscribe_transfer.go @@ -9,7 +9,7 @@ import ( ) type EventInscribeTransfer struct { - Id uint64 + Id int64 InscriptionId ordinals.InscriptionId InscriptionNumber int64 Tick string diff --git a/modules/brc20/internal/entity/event_mint.go b/modules/brc20/internal/entity/event_mint.go index d358ed1..2978f28 100644 --- a/modules/brc20/internal/entity/event_mint.go +++ b/modules/brc20/internal/entity/event_mint.go @@ -9,7 +9,7 @@ import ( ) type EventMint struct { - Id uint64 + Id int64 InscriptionId ordinals.InscriptionId InscriptionNumber int64 Tick string diff --git a/modules/brc20/internal/entity/event_transfer_transfer.go b/modules/brc20/internal/entity/event_transfer_transfer.go index 2058184..bfb62e6 100644 --- a/modules/brc20/internal/entity/event_transfer_transfer.go +++ b/modules/brc20/internal/entity/event_transfer_transfer.go @@ -9,7 +9,7 @@ import ( ) type EventTransferTransfer struct { - Id uint64 + Id int64 InscriptionId ordinals.InscriptionId InscriptionNumber int64 Tick string diff --git a/modules/brc20/internal/repository/postgres/brc20.go b/modules/brc20/internal/repository/postgres/brc20.go index 21e3d53..bb9914f 100644 --- a/modules/brc20/internal/repository/postgres/brc20.go +++ b/modules/brc20/internal/repository/postgres/brc20.go @@ -158,12 +158,12 @@ func (r *Repository) GetInscriptionParentsByIds(ctx context.Context, ids []ordin return result, nil } -func (r *Repository) GetLatestEventId(ctx context.Context) (uint64, error) { +func (r *Repository) GetLatestEventId(ctx context.Context) (int64, error) { row, err := r.queries.GetLatestEventIds(ctx) if err != nil { return 0, errors.WithStack(err) } - return uint64(max(row.EventDeployID, row.EventMintID, row.EventInscribeTransferID, row.EventTransferTransferID)), nil + return max(row.EventDeployID.(int64), row.EventMintID.(int64), row.EventInscribeTransferID.(int64), row.EventTransferTransferID.(int64)), nil } func (r *Repository) GetBalancesBatchAtHeight(ctx context.Context, blockHeight uint64, queries []datagateway.GetBalancesBatchAtHeightQuery) (map[string]map[string]*entity.Balance, error) { diff --git a/modules/brc20/internal/repository/postgres/gen/data.sql.go b/modules/brc20/internal/repository/postgres/gen/data.sql.go index f71bf41..ade188c 100644 --- a/modules/brc20/internal/repository/postgres/gen/data.sql.go +++ b/modules/brc20/internal/repository/postgres/gen/data.sql.go @@ -471,17 +471,17 @@ WITH "latest_deploy_id" AS ( SELECT "id" FROM "brc20_event_transfer_transfers" ORDER BY "id" DESC LIMIT 1 ) SELECT - (SELECT "id" FROM "latest_deploy_id") AS "event_deploy_id", - (SELECT "id" FROM "latest_mint_id") AS "event_mint_id", - (SELECT "id" FROM "latest_inscribe_transfer_id") AS "event_inscribe_transfer_id", - (SELECT "id" FROM "latest_transfer_transfer_id") AS "event_transfer_transfer_id" + COALESCE((SELECT "id" FROM "latest_deploy_id"), -1) AS "event_deploy_id", + COALESCE((SELECT "id" FROM "latest_mint_id"), -1) AS "event_mint_id", + COALESCE((SELECT "id" FROM "latest_inscribe_transfer_id"), -1) AS "event_inscribe_transfer_id", + COALESCE((SELECT "id" FROM "latest_transfer_transfer_id"), -1) AS "event_transfer_transfer_id" ` type GetLatestEventIdsRow struct { - EventDeployID int64 - EventMintID int64 - EventInscribeTransferID int64 - EventTransferTransferID int64 + EventDeployID interface{} + EventMintID interface{} + EventInscribeTransferID interface{} + EventTransferTransferID interface{} } func (q *Queries) GetLatestEventIds(ctx context.Context) (GetLatestEventIdsRow, error) { diff --git a/modules/brc20/internal/repository/postgres/mapper.go b/modules/brc20/internal/repository/postgres/mapper.go index d44844d..1feb2a7 100644 --- a/modules/brc20/internal/repository/postgres/mapper.go +++ b/modules/brc20/internal/repository/postgres/mapper.go @@ -353,7 +353,7 @@ func mapEventDeployModelToType(src gen.Brc20EventDeploy) (entity.EventDeploy, er return entity.EventDeploy{}, errors.Wrap(err, "cannot parse satpoint") } return entity.EventDeploy{ - Id: uint64(src.Id), + Id: src.Id, InscriptionId: inscriptionId, InscriptionNumber: src.InscriptionNumber, Tick: src.Tick, @@ -420,7 +420,7 @@ func mapEventMintModelToType(src gen.Brc20EventMint) (entity.EventMint, error) { parentId = &parentIdValue } return entity.EventMint{ - Id: uint64(src.Id), + Id: src.Id, InscriptionId: inscriptionId, InscriptionNumber: src.InscriptionNumber, Tick: src.Tick, @@ -479,7 +479,7 @@ func mapEventInscribeTransferModelToType(src gen.Brc20EventInscribeTransfer) (en return entity.EventInscribeTransfer{}, errors.Wrap(err, "cannot parse satPoint") } return entity.EventInscribeTransfer{ - Id: uint64(src.Id), + Id: src.Id, InscriptionId: inscriptionId, InscriptionNumber: src.InscriptionNumber, Tick: src.Tick, @@ -544,7 +544,7 @@ func mapEventTransferTransferModelToType(src gen.Brc20EventTransferTransfer) (en return entity.EventTransferTransfer{}, errors.Wrap(err, "cannot parse toSatPoint") } return entity.EventTransferTransfer{ - Id: uint64(src.Id), + Id: src.Id, InscriptionId: inscriptionId, InscriptionNumber: src.InscriptionNumber, Tick: src.Tick, diff --git a/modules/brc20/processor_brc20.go b/modules/brc20/processor_brc20.go index ddd0323..a5a9941 100644 --- a/modules/brc20/processor_brc20.go +++ b/modules/brc20/processor_brc20.go @@ -20,6 +20,10 @@ func (p *Processor) processBRC20States(ctx context.Context, transfers []*entity. payloads := make([]*brc20.Payload, 0) ticks := make(map[string]struct{}) for _, transfer := range transfers { + if transfer.Content == nil { + // skip empty content + continue + } payload, err := brc20.ParsePayload(transfer) if err != nil { return errors.Wrap(err, "failed to parse payload") @@ -27,6 +31,10 @@ func (p *Processor) processBRC20States(ctx context.Context, transfers []*entity. payloads = append(payloads, payload) ticks[payload.Tick] = struct{}{} } + if len(payloads) == 0 { + // skip if no valid payloads + return nil + } // TODO: concurrently fetch from db to optimize speed tickEntries, err := p.brc20Dg.GetTickEntriesByTicks(ctx, lo.Keys(ticks)) if err != nil {