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

Add utils.ParseUint32(...) function #130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 14 additions & 16 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package model

import (
"fmt"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
Expand Down Expand Up @@ -59,22 +57,22 @@ func FromEventBTCDelegationCreated(
bbnBlockHeight,
bbnBlockTime int64,
) (*BTCDelegationDetails, error) {
stakingOutputIdx, err := strconv.ParseUint(event.StakingOutputIndex, 10, 32)
stakingOutputIdx, err := utils.ParseUint32(event.StakingOutputIndex)
if err != nil {
return nil, fmt.Errorf("failed to parse staking output index: %w", err)
}

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

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

unbondingTime, err := strconv.ParseUint(event.UnbondingTime, 10, 32)
unbondingTime, err := utils.ParseUint32(event.UnbondingTime)
if err != nil {
return nil, fmt.Errorf("failed to parse unbonding time: %w", err)
}
Expand All @@ -89,17 +87,17 @@ func FromEventBTCDelegationCreated(
return &BTCDelegationDetails{
StakingTxHashHex: stakingTx.TxHash().String(),
StakingTxHex: event.StakingTxHex,
StakingTime: uint32(stakingTime),
StakingTime: stakingTime,
StakingAmount: uint64(stakingValue),
StakingOutputIdx: uint32(stakingOutputIdx),
StakingOutputIdx: stakingOutputIdx,
StakerBtcPkHex: event.StakerBtcPkHex,
FinalityProviderBtcPksHex: event.FinalityProviderBtcPksHex,
ParamsVersion: uint32(paramsVersion),
UnbondingTime: uint32(unbondingTime),
ParamsVersion: paramsVersion,
UnbondingTime: unbondingTime,
UnbondingTx: event.UnbondingTx,
State: types.StatePending, // initial state will always be PENDING
StartHeight: uint32(0), // it should be set when the inclusion proof is received
EndHeight: uint32(0), // it should be set when the inclusion proof is received
StartHeight: 0, // it should be set when the inclusion proof is received
EndHeight: 0, // it should be set when the inclusion proof is received
CovenantUnbondingSignatures: []CovenantSignature{},
BTCDelegationCreatedBlock: BTCDelegationCreatedBbnBlock{
Height: bbnBlockHeight,
Expand All @@ -117,11 +115,11 @@ func FromEventBTCDelegationCreated(
func FromEventBTCDelegationInclusionProofReceived(
event *bbntypes.EventBTCDelegationInclusionProofReceived,
) *BTCDelegationDetails {
startHeight, _ := strconv.ParseUint(event.StartHeight, 10, 32)
endHeight, _ := strconv.ParseUint(event.EndHeight, 10, 32)
startHeight, _ := utils.ParseUint32(event.StartHeight)
endHeight, _ := utils.ParseUint32(event.EndHeight)
return &BTCDelegationDetails{
StartHeight: uint32(startHeight),
EndHeight: uint32(endHeight),
StartHeight: startHeight,
EndHeight: endHeight,
State: types.DelegationState(event.NewState),
}
}
Expand Down
11 changes: 5 additions & 6 deletions internal/services/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package services
import (
"context"
"fmt"
"strconv"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
ftypes "github.com/babylonlabs-io/babylon/x/finality/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/rs/zerolog/log"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
)

const (
Expand Down Expand Up @@ -196,7 +195,7 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
}
newState := types.DelegationState(inclusionProofEvent.NewState)
if newState == types.StateActive {
stakingStartHeight, _ := strconv.ParseUint(inclusionProofEvent.StartHeight, 10, 32)
stakingStartHeight, _ := utils.ParseUint32(inclusionProofEvent.StartHeight)

log.Debug().
Str("staking_tx", inclusionProofEvent.StakingTxHash).
Expand All @@ -216,7 +215,7 @@ func (s *Service) processBTCDelegationInclusionProofReceivedEvent(
delegation.StakingTxHashHex,
delegation.StakingTxHex,
delegation.StakingOutputIdx,
uint32(stakingStartHeight),
stakingStartHeight,
); err != nil {
return err
}
Expand Down Expand Up @@ -269,15 +268,15 @@ func (s *Service) processBTCDelegationUnbondedEarlyEvent(
return err
}

unbondingStartHeight, parseErr := strconv.ParseUint(unbondedEarlyEvent.StartHeight, 10, 32)
unbondingStartHeight, parseErr := utils.ParseUint32(unbondedEarlyEvent.StartHeight)
if parseErr != nil {
return fmt.Errorf("failed to parse start height: %w", parseErr)
}

subState := types.SubStateEarlyUnbonding

// Save timelock expire
unbondingExpireHeight := uint32(unbondingStartHeight) + delegation.UnbondingTime
unbondingExpireHeight := unbondingStartHeight + delegation.UnbondingTime
if err := s.db.SaveNewTimeLockExpire(
ctx,
delegation.StakingTxHashHex,
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/strconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package utils

import "strconv"

func ParseUint32(s string) (uint32, error) {
v, err := strconv.ParseUint(s, 10, 32)
return uint32(v), err
}
Loading