diff --git a/internal/storage/generic.go b/internal/storage/generic.go index c2f748d..0d402d4 100644 --- a/internal/storage/generic.go +++ b/internal/storage/generic.go @@ -90,6 +90,7 @@ type Transaction interface { GetRollup(ctx context.Context, rollupId []byte) (Rollup, error) Validators(ctx context.Context) ([]Validator, error) GetBridgeIdByAddressId(ctx context.Context, id uint64) (uint64, error) + GetAddressId(ctx context.Context, hash string) (uint64, error) } type SearchResult struct { diff --git a/internal/storage/mock/generic.go b/internal/storage/mock/generic.go index f5612bc..bc1d2bd 100644 --- a/internal/storage/mock/generic.go +++ b/internal/storage/mock/generic.go @@ -281,6 +281,45 @@ func (c *MockTransactionFlushCall) DoAndReturn(f func(context.Context) error) *M return c } +// GetAddressId mocks base method. +func (m *MockTransaction) GetAddressId(ctx context.Context, hash string) (uint64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAddressId", ctx, hash) + ret0, _ := ret[0].(uint64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetAddressId indicates an expected call of GetAddressId. +func (mr *MockTransactionMockRecorder) GetAddressId(ctx, hash any) *MockTransactionGetAddressIdCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAddressId", reflect.TypeOf((*MockTransaction)(nil).GetAddressId), ctx, hash) + return &MockTransactionGetAddressIdCall{Call: call} +} + +// MockTransactionGetAddressIdCall wrap *gomock.Call +type MockTransactionGetAddressIdCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *MockTransactionGetAddressIdCall) Return(arg0 uint64, arg1 error) *MockTransactionGetAddressIdCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *MockTransactionGetAddressIdCall) Do(f func(context.Context, string) (uint64, error)) *MockTransactionGetAddressIdCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *MockTransactionGetAddressIdCall) DoAndReturn(f func(context.Context, string) (uint64, error)) *MockTransactionGetAddressIdCall { + c.Call = c.Call.DoAndReturn(f) + return c +} + // GetBridgeIdByAddressId mocks base method. func (m *MockTransaction) GetBridgeIdByAddressId(ctx context.Context, id uint64) (uint64, error) { m.ctrl.T.Helper() diff --git a/internal/storage/postgres/transaction.go b/internal/storage/postgres/transaction.go index 844228a..d7f2e54 100644 --- a/internal/storage/postgres/transaction.go +++ b/internal/storage/postgres/transaction.go @@ -500,3 +500,12 @@ func (tx Transaction) GetBridgeIdByAddressId(ctx context.Context, id uint64) (br Scan(ctx, &bridgeId) return } + +func (tx Transaction) GetAddressId(ctx context.Context, hash string) (addrId uint64, err error) { + err = tx.Tx().NewSelect(). + Column("id"). + Model((*models.Address)(nil)). + Where("hash = ?", hash). + Scan(ctx, &addrId) + return +} diff --git a/pkg/indexer/decode/ibc.go b/pkg/indexer/decode/ibc.go index df6bcfc..8d995e6 100644 --- a/pkg/indexer/decode/ibc.go +++ b/pkg/indexer/decode/ibc.go @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2024 PK Lab AG +// SPDX-License-Identifier: MIT + package decode import "github.com/shopspring/decimal" diff --git a/pkg/indexer/storage/action.go b/pkg/indexer/storage/action.go index a45893b..9a97e2a 100644 --- a/pkg/indexer/storage/action.go +++ b/pkg/indexer/storage/action.go @@ -57,7 +57,7 @@ func saveAction( if payerId, ok := addrToId[actions[i].Fee.Payer.Hash]; ok { actions[i].Fee.PayerId = payerId } else { - return errors.Errorf("unknown payer id") + return errors.Errorf("unknown payer id: %s", actions[i].Fee.Payer.Hash) } fees = append(fees, actions[i].Fee) } @@ -66,15 +66,20 @@ func saveAction( actions[i].Deposit.ActionId = actions[i].Id actions[i].Deposit.TxId = actions[i].TxId - if addrId, ok := addrToId[actions[i].Deposit.Bridge.Address.Hash]; ok { - bridgeId, err := tx.GetBridgeIdByAddressId(ctx, addrId) + addrId, ok := addrToId[actions[i].Deposit.Bridge.Address.Hash] + if !ok { + id, err := tx.GetAddressId(ctx, actions[i].Deposit.Bridge.Address.Hash) if err != nil { - return errors.Wrap(err, "receiving deposit bridge id") + return errors.Wrapf(err, "receiving deposit bridge address: %s", actions[i].Deposit.Bridge.Address.Hash) } - actions[i].Deposit.BridgeId = bridgeId - } else { - return errors.Errorf("unknown payer id") + addrId = id + } + + bridgeId, err := tx.GetBridgeIdByAddressId(ctx, addrId) + if err != nil { + return errors.Wrap(err, "receiving deposit bridge id") } + actions[i].Deposit.BridgeId = bridgeId rollup, err := tx.GetRollup(ctx, actions[i].Deposit.Rollup.AstriaId) if err != nil {