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

feat: handle withdrawn status #43

Merged
merged 14 commits into from
Nov 14, 2024
10 changes: 10 additions & 0 deletions internal/db/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func (db *Database) UpdateBTCDelegationState(
return nil
}

func (db *Database) GetBTCDelegationState(
ctx context.Context, stakingTxHash string,
) (*types.DelegationState, error) {
delegation, err := db.GetBTCDelegationByStakingTxHash(ctx, stakingTxHash)
if err != nil {
return nil, err
}
return &delegation.State, nil
}

func (db *Database) UpdateBTCDelegationDetails(
ctx context.Context,
stakingTxHash string,
Expand Down
14 changes: 14 additions & 0 deletions internal/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ type DbInterface interface {
SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error
/**
* GetStakingParams retrieves the staking parameters by the version.
* @param ctx The context
* @param version The version of the staking parameters
* @return The staking parameters or an error
*/
GetStakingParams(ctx context.Context, version uint32) (*bbnclient.StakingParams, error)
/**
* SaveCheckpointParams saves the checkpoint parameters to the database.
* @param ctx The context
Expand Down Expand Up @@ -93,6 +100,13 @@ type DbInterface interface {
UpdateBTCDelegationState(
ctx context.Context, stakingTxHash string, newState types.DelegationState,
) error
/**
* GetBTCDelegationState retrieves the BTC delegation state.
* @param ctx The context
* @param stakingTxHash The staking tx hash
* @return The BTC delegation state or an error
*/
GetBTCDelegationState(ctx context.Context, stakingTxHash string) (*types.DelegationState, error)
/**
* UpdateBTCDelegationDetails updates the BTC delegation details.
* @param ctx The context
Expand Down
71 changes: 59 additions & 12 deletions internal/db/model/delegation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

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

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
Expand All @@ -9,14 +11,14 @@ import (

type BTCDelegationDetails struct {
StakingTxHashHex string `bson:"_id"` // Primary key
ParamsVersion string `bson:"params_version"`
ParamsVersion uint32 `bson:"params_version"`
FinalityProviderBtcPksHex []string `bson:"finality_provider_btc_pks_hex"`
StakerBtcPkHex string `bson:"staker_btc_pk_hex"`
StakingTime string `bson:"staking_time"`
StakingAmount string `bson:"staking_amount"`
StakingTime uint32 `bson:"staking_time"`
StakingAmount uint64 `bson:"staking_amount"`
StakingOutputPkScript string `bson:"staking_output_pk_script"`
StakingOutputIndex string `bson:"staking_output_index"`
UnbondingTime string `bson:"unbonding_time"`
StakingOutputIdx uint32 `bson:"staking_output_idx"`
UnbondingTime uint32 `bson:"unbonding_time"`
UnbondingTx string `bson:"unbonding_tx"`
State types.DelegationState `bson:"state"`
StartHeight uint32 `bson:"start_height"`
Expand All @@ -25,22 +27,67 @@ type BTCDelegationDetails struct {

func FromEventBTCDelegationCreated(
event *bbntypes.EventBTCDelegationCreated,
) *BTCDelegationDetails {
) (*BTCDelegationDetails, *types.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),
)
}

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),
)
}

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),
)
}

stakingAmount, err := strconv.ParseUint(event.StakingAmount, 10, 64)
if err != nil {
return nil, types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to parse staking amount: %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 &BTCDelegationDetails{
StakingTxHashHex: event.StakingTxHash, // babylon returns a hex string
ParamsVersion: event.ParamsVersion,
ParamsVersion: uint32(paramsVersion),
FinalityProviderBtcPksHex: event.FinalityProviderBtcPksHex,
StakerBtcPkHex: event.StakerBtcPkHex,
StakingTime: event.StakingTime,
StakingAmount: event.StakingAmount,
StakingTime: uint32(stakingTime),
StakingAmount: stakingAmount,
StakingOutputPkScript: event.StakingOutputPkScript,
StakingOutputIndex: event.StakingOutputIndex,
UnbondingTime: event.UnbondingTime,
StakingOutputIdx: uint32(stakingOutputIdx),
UnbondingTime: uint32(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
}
}, nil
}

func FromEventBTCDelegationInclusionProofReceived(
Expand Down
18 changes: 18 additions & 0 deletions internal/db/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ func (db *Database) SaveCheckpointParams(

return nil
}

func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*bbnclient.StakingParams, error) {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}

var params model.GlobalParamsDocument
err := collection.FindOne(ctx, filter).Decode(&params)
if err != nil {
return nil, fmt.Errorf("failed to get staking params: %w", err)
}

return params.Params.(*bbnclient.StakingParams), nil
}
9 changes: 3 additions & 6 deletions internal/services/consumer_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
Expand Down Expand Up @@ -37,16 +36,14 @@ func (s *Service) emitConsumerEvent(
}

func (s *Service) sendActiveDelegationEvent(ctx context.Context, delegation *model.BTCDelegationDetails) *types.Error {
stakingTime, _ := strconv.ParseUint(delegation.StakingTime, 10, 64)
stakingAmount, _ := strconv.ParseUint(delegation.StakingAmount, 10, 64)
ev := queueclient.NewActiveStakingEvent(
delegation.StakingTxHashHex,
delegation.StakerBtcPkHex,
delegation.FinalityProviderBtcPksHex,
stakingAmount,
delegation.StakingAmount,
uint64(delegation.StartHeight),
time.Now().Unix(),
stakingTime,
uint64(delegation.StakingTime),
0,
"",
false,
Expand Down Expand Up @@ -81,7 +78,7 @@ func (s *Service) sendUnbondingDelegationEvent(ctx context.Context, delegation *
uint64(delegation.StartHeight),
uint64(delegation.EndHeight),
delegation.UnbondingTx,
delegation.UnbondingTime,
"",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remind me the value of this unbondingTime? is it the timelock or the final height in which the unbonding tx timelock will expire?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unbondingTime is basically the unbonding timelock (if the staker does early unbonding)

the indexer is not pushing correct data to the api queues (there is some empty strings or dummy stuff) this needs fixing once schema is finalized

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's good you reminded, logged an issue about the queue schema
#45

)
if err := s.queueManager.SendUnbondingStakingEvent(ctx, &ev); err != nil {
return types.NewInternalServiceError(fmt.Errorf("failed to send unbonding staking event: %w", err))
Expand Down
Loading
Loading