Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Nov 21, 2024
1 parent bb24380 commit 3e87de4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 50 deletions.
39 changes: 36 additions & 3 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
Expand Down Expand Up @@ -239,9 +240,41 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return err
}

// Handle unbonding process
if err := s.handleUnbondingProcess(ctx, unbondedEarlyEvent, delegation); err != nil {
return err
unbondingStartHeight, parseErr := strconv.ParseUint(unbondedEarlyEvent.StartHeight, 10, 32)
if parseErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse start height: %w", parseErr),
)
}

// Save timelock expire
unbondingExpireHeight := uint32(unbondingStartHeight) + delegation.UnbondingTime
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
unbondingExpireHeight,
types.EarlyUnbondingTxType.String(),
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to save timelock expire: %w", err),
)
}

// Update delegation state
if err := s.db.UpdateBTCDelegationState(
ctx,
unbondedEarlyEvent.StakingTxHash,
types.StateUnbonding,
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", err),
)
}

// Register unbonding spend notification
Expand Down
47 changes: 0 additions & 47 deletions internal/services/delegation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,15 @@ import (
"encoding/hex"
"fmt"
"net/http"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
bbn "github.com/babylonlabs-io/babylon/types"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
)

func (s *Service) handleUnbondingProcess(
ctx context.Context,
event *bbntypes.EventBTCDelgationUnbondedEarly,
delegation *model.BTCDelegationDetails,
) *types.Error {
unbondingStartHeight, parseErr := strconv.ParseUint(event.StartHeight, 10, 32)
if parseErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse start height: %w", parseErr),
)
}

// Save timelock expire
unbondingExpireHeight := uint32(unbondingStartHeight) + delegation.UnbondingTime
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
unbondingExpireHeight,
types.EarlyUnbondingTxType.String(),
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to save timelock expire: %w", err),
)
}

// Update delegation state
if err := s.db.UpdateBTCDelegationState(
ctx,
event.StakingTxHash,
types.StateUnbonding,
); err != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to update BTC delegation state: %w", err),
)
}

return nil
}

func (s *Service) registerUnbondingSpendNotification(
ctx context.Context,
delegation *model.BTCDelegationDetails,
Expand Down

0 comments on commit 3e87de4

Please sign in to comment.