Skip to content

Commit

Permalink
pr comment: utils for stateHistoryStrs
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Jan 16, 2025
1 parent 2183da9 commit ed704fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
8 changes: 8 additions & 0 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,11 @@ func (d *BTCDelegationDetails) HasInclusionProof() bool {
// Ref: https://github.com/babylonlabs-io/babylon/blob/b1a4b483f60458fcf506adf1d80aaa6c8c10f8a4/x/btcstaking/types/btc_delegation.go#L47
return d.StartHeight > 0 && d.EndHeight > 0
}

func ToStateStrings(stateHistory []StateRecord) []string {
states := make([]string, len(stateHistory))
for i, record := range stateHistory {
states[i] = record.State.String()
}
return states
}
32 changes: 8 additions & 24 deletions internal/services/consumer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ func (s *Service) emitActiveDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
stateHistory := make([]string, len(delegation.StateHistory))
for i, record := range delegation.StateHistory {
stateHistory[i] = record.State.String()
}

stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
stakingEvent := queuecli.NewActiveStakingEvent(
delegation.StakingTxHashHex,
delegation.StakerBtcPkHex,
delegation.FinalityProviderBtcPksHex,
delegation.StakingAmount,
stateHistory,
stateHistoryStrs,
)

if err := s.queueManager.PushActiveStakingEvent(&stakingEvent); err != nil {
Expand All @@ -36,17 +32,13 @@ func (s *Service) emitUnbondingDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
stateHistory := make([]string, len(delegation.StateHistory))
for i, record := range delegation.StateHistory {
stateHistory[i] = record.State.String()
}

stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewUnbondingStakingEvent(
delegation.StakingTxHashHex,
delegation.StakerBtcPkHex,
delegation.FinalityProviderBtcPksHex,
delegation.StakingAmount,
stateHistory,
stateHistoryStrs,
)
if err := s.queueManager.PushUnbondingStakingEvent(&ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the unbonding event to the queue: %w", err))
Expand All @@ -58,17 +50,13 @@ func (s *Service) emitWithdrawableDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
stateHistory := make([]string, len(delegation.StateHistory))
for i, record := range delegation.StateHistory {
stateHistory[i] = record.State.String()
}

stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewWithdrawableStakingEvent(
delegation.StakingTxHashHex,
delegation.StakerBtcPkHex,
delegation.FinalityProviderBtcPksHex,
delegation.StakingAmount,
stateHistory,
stateHistoryStrs,
)
if err := s.queueManager.PushWithdrawableStakingEvent(&ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the withdrawable event to the queue: %w", err))
Expand All @@ -80,17 +68,13 @@ func (s *Service) emitWithdrawnDelegationEvent(
ctx context.Context,
delegation *model.BTCDelegationDetails,
) *types.Error {
stateHistory := make([]string, len(delegation.StateHistory))
for i, record := range delegation.StateHistory {
stateHistory[i] = record.State.String()
}

stateHistoryStrs := model.ToStateStrings(delegation.StateHistory)
ev := queuecli.NewWithdrawnStakingEvent(
delegation.StakingTxHashHex,
delegation.StakerBtcPkHex,
delegation.FinalityProviderBtcPksHex,
delegation.StakingAmount,
stateHistory,
stateHistoryStrs,
)
if err := s.queueManager.PushWithdrawnStakingEvent(&ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to push the withdrawn event to the queue: %w", err))
Expand Down

0 comments on commit ed704fa

Please sign in to comment.