Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace types.error with error 2 (#122) #126

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package model

import (
"fmt"
"net/http"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
Expand Down Expand Up @@ -59,50 +58,30 @@ func FromEventBTCDelegationCreated(
event *bbntypes.EventBTCDelegationCreated,
bbnBlockHeight,
bbnBlockTime int64,
) (*BTCDelegationDetails, *types.Error) {
) (*BTCDelegationDetails, error) {
stakingOutputIdx, err := strconv.ParseUint(event.StakingOutputIndex, 10, 32)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse staking output index: %w", err),
)
return nil, fmt.Errorf("failed to parse staking output index: %w", err)
}

paramsVersion, err := strconv.ParseUint(event.ParamsVersion, 10, 32)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse params version: %w", err),
)
return nil, fmt.Errorf("failed to parse params version: %w", err)
}

stakingTime, err := strconv.ParseUint(event.StakingTime, 10, 32)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse staking time: %w", err),
)
return nil, fmt.Errorf("failed to parse staking time: %w", err)
}

unbondingTime, err := strconv.ParseUint(event.UnbondingTime, 10, 32)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse unbonding time: %w", err),
)
return nil, fmt.Errorf("failed to parse unbonding time: %w", err)
}

stakingTx, err := utils.DeserializeBtcTransactionFromHex(event.StakingTxHex)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to deserialize staking tx: %w", err),
)
return nil, fmt.Errorf("failed to deserialize staking tx: %w", err)
}

stakingValue := btcutil.Amount(stakingTx.TxOut[stakingOutputIdx].Value)
Expand Down
11 changes: 2 additions & 9 deletions internal/services/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package services
import (
"context"
"fmt"
"net/http"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -86,15 +83,11 @@ func (s *Service) processBlocksSequentially(ctx context.Context) error {
// /block_result endpoint of the BBN blockchain.
func (s *Service) getEventsFromBlock(
ctx context.Context, blockHeight int64,
) ([]BbnEvent, *types.Error) {
) ([]BbnEvent, error) {
events := make([]BbnEvent, 0)
blockResult, err := s.bbn.GetBlockResults(ctx, &blockHeight)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.ClientRequestError,
fmt.Errorf("failed to get block results: %w", err),
)
return nil, fmt.Errorf("failed to get block results: %w", err)
}
// Append transaction-level events
for _, txResult := range blockResult.TxsResults {
Expand Down
17 changes: 8 additions & 9 deletions internal/services/consumer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
queuecli "github.com/babylonlabs-io/staking-queue-client/client"
)

func (s *Service) emitActiveDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
) error {
stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
stakingEvent := queuecli.NewActiveStakingEvent(
delegation.StakingTxHashHex,
Expand All @@ -23,15 +22,15 @@ func (s *Service) emitActiveDelegationEvent(
)

if err := s.queueManager.PushActiveStakingEvent(ctx, &stakingEvent); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the staking event to the queue: %w", err))
return fmt.Errorf("failed to push the staking event to the queue: %w", err)
}
return nil
}

func (s *Service) emitUnbondingDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
) error {
stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewUnbondingStakingEvent(
delegation.StakingTxHashHex,
Expand All @@ -41,15 +40,15 @@ func (s *Service) emitUnbondingDelegationEvent(
stateHistoryStrs,
)
if err := s.queueManager.PushUnbondingStakingEvent(ctx, &ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the unbonding event to the queue: %w", err))
return fmt.Errorf("failed to push the unbonding event to the queue: %w", err)
}
return nil
}

func (s *Service) emitWithdrawableDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
) error {
stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewWithdrawableStakingEvent(
delegation.StakingTxHashHex,
Expand All @@ -59,15 +58,15 @@ func (s *Service) emitWithdrawableDelegationEvent(
stateHistoryStrs,
)
if err := s.queueManager.PushWithdrawableStakingEvent(ctx, &ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the withdrawable event to the queue: %w", err))
return fmt.Errorf("failed to push the withdrawable event to the queue: %w", err)
}
return nil
}

func (s *Service) emitWithdrawnDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
) error {
stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewWithdrawnStakingEvent(
delegation.StakingTxHashHex,
Expand All @@ -77,7 +76,7 @@ func (s *Service) emitWithdrawnDelegationEvent(
stateHistoryStrs,
)
if err := s.queueManager.PushWithdrawnStakingEvent(ctx, &ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the withdrawn event to the queue: %w", err))
return fmt.Errorf("failed to push the withdrawn event to the queue: %w", err)
}
return nil
}
Loading
Loading