diff --git a/internal/storage/postgres/scopes.go b/internal/storage/postgres/scopes.go index be886b8..9a358bf 100644 --- a/internal/storage/postgres/scopes.go +++ b/internal/storage/postgres/scopes.go @@ -39,6 +39,7 @@ func addressListFilter(query *bun.SelectQuery, fltrs storage.AddressListFilter) func txFilter(query *bun.SelectQuery, fltrs storage.TxFilter) *bun.SelectQuery { query = limitScope(query, fltrs.Limit) query = sortScope(query, "tx.id", fltrs.Sort) + query = offsetScope(query, fltrs.Offset) if !fltrs.ActionTypes.Empty() { query = query.Where("action_types & ? > 0", fltrs.ActionTypes.Bits) diff --git a/internal/storage/postgres/tx.go b/internal/storage/postgres/tx.go index 305e24e..0200637 100644 --- a/internal/storage/postgres/tx.go +++ b/internal/storage/postgres/tx.go @@ -39,21 +39,25 @@ func (tx *Tx) ByHash(ctx context.Context, hash []byte) (transaction storage.Tx, } func (tx *Tx) ByHeight(ctx context.Context, height types.Level, limit, offset int) (txs []storage.Tx, err error) { - query := tx.DB().NewSelect().Model(&txs). - Where("tx.height = ?", height). - Relation("Signer") + query := tx.DB().NewSelect().Model((*storage.Tx)(nil)). + Where("tx.height = ?", height) query = limitScope(query, limit) if offset > 0 { query = query.Offset(offset) } - err = query.Scan(ctx) + err = tx.DB().NewSelect(). + TableExpr("(?) as tx", query). + ColumnExpr("tx.*"). + ColumnExpr("address.hash as signer__hash"). + Join("left join address on address.id = tx.signer_id"). + Scan(ctx, &txs) return } func (tx *Tx) Filter(ctx context.Context, fltrs storage.TxFilter) (txs []storage.Tx, err error) { - query := tx.DB().NewSelect().Model(&txs).Offset(fltrs.Offset).Relation("Signer") + query := tx.DB().NewSelect().Model(&txs).Relation("Signer") query = txFilter(query, fltrs) err = query.Scan(ctx) @@ -64,8 +68,7 @@ func (tx *Tx) ByAddress(ctx context.Context, addressId uint64, fltrs storage.TxF query := tx.DB().NewSelect(). Model(&txs). Where("signer_id = ?", addressId). - Relation("Signer"). - Offset(fltrs.Offset) + Relation("Signer") query = txFilter(query, fltrs) diff --git a/internal/storage/postgres/tx_test.go b/internal/storage/postgres/tx_test.go index 3d039b6..effb330 100644 --- a/internal/storage/postgres/tx_test.go +++ b/internal/storage/postgres/tx_test.go @@ -57,6 +57,7 @@ func (s *StorageTestSuite) TestTxByHeight() { s.Require().EqualValues(hash, tx.Hash) s.Require().NotNil(tx.Signer) + s.Require().Equal("3fff1c39b9d163bfb9bcbf9dfea78675f1b4bc2c", hex.EncodeToString(tx.Signer.Hash)) } func (s *StorageTestSuite) TestTxFilter() { @@ -87,6 +88,8 @@ func (s *StorageTestSuite) TestTxFilter() { s.Require().EqualValues(hash, tx.Hash) s.Require().NotNil(tx.Signer) + s.Require().Equal("3fff1c39b9d163bfb9bcbf9dfea78675f1b4bc2c", hex.EncodeToString(tx.Signer.Hash)) + s.Require().Len(tx.Actions, 1) }