Skip to content

Commit

Permalink
fix params
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Nov 22, 2024
1 parent 5486858 commit 9a507e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
2 changes: 2 additions & 0 deletions internal/clients/bbnclient/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type StakingParams struct {
UnbondingFeeSat int64 `bson:"unbonding_fee_sat"`
MinCommissionRate string `bson:"min_commission_rate"`
DelegationCreationBaseGasFee uint64 `bson:"delegation_creation_base_gas_fee"`
AllowListExpirationHeight uint64 `bson:"allow_list_expiration_height"`
}

type CheckpointParams struct {
Expand All @@ -46,6 +47,7 @@ func FromBbnStakingParams(params stakingtypes.Params) *StakingParams {
UnbondingFeeSat: params.UnbondingFeeSat,
MinCommissionRate: params.MinCommissionRate.String(),
DelegationCreationBaseGasFee: params.DelegationCreationBaseGasFee,
AllowListExpirationHeight: params.AllowListExpirationHeight,
}
}

Expand Down
22 changes: 18 additions & 4 deletions internal/db/model/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package model

type GlobalParamsDocument struct {
Type string `bson:"type"`
Version uint32 `bson:"version"`
Params interface{} `bson:"params"`
import "github.com/babylonlabs-io/babylon-staking-indexer/internal/clients/bbnclient"

// Base document for common fields
type BaseParamsDocument struct {
Type string `bson:"type"`
Version uint32 `bson:"version"`
}

// Specific document for staking params
type StakingParamsDocument struct {
BaseParamsDocument `bson:",inline"`
Params *bbnclient.StakingParams `bson:"params"`
}

// Specific document for checkpoint params
type CheckpointParamsDocument struct {
BaseParamsDocument `bson:",inline"`
Params *bbnclient.CheckpointParams `bson:"params"`
}
55 changes: 24 additions & 31 deletions internal/db/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,47 @@ const (
func (db *Database) SaveStakingParams(
ctx context.Context, version uint32, params *bbnclient.StakingParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)

update := bson.M{
"$setOnInsert": &model.GlobalParamsDocument{
doc := &model.StakingParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Type: STAKING_PARAMS_TYPE,
Version: version,
Params: params,
},
Params: params,
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("failed to save staking params: %w", err)
filter := bson.M{
"type": STAKING_PARAMS_TYPE,
"version": version,
}
return nil
update := bson.M{"$setOnInsert": doc}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
return err
}

func (db *Database) SaveCheckpointParams(
ctx context.Context, params *bbnclient.CheckpointParams,
) error {
collection := db.client.Database(db.dbName).
Collection(model.GlobalParamsCollection)

filter := bson.M{
"type": CHECKPOINT_PARAMS_TYPE,
"version": CHECKPOINT_PARAMS_VERSION,
}
collection := db.client.Database(db.dbName).Collection(model.GlobalParamsCollection)

update := bson.M{
"$setOnInsert": &model.GlobalParamsDocument{
doc := &model.CheckpointParamsDocument{
BaseParamsDocument: model.BaseParamsDocument{
Type: CHECKPOINT_PARAMS_TYPE,
Version: CHECKPOINT_PARAMS_VERSION,
Params: params,
Version: CHECKPOINT_PARAMS_VERSION, // hardcoded as 0
},
Params: params,
}

_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
if err != nil {
return fmt.Errorf("failed to save checkpoint params: %w", err)
filter := bson.M{
"type": CHECKPOINT_PARAMS_TYPE,
"version": CHECKPOINT_PARAMS_VERSION, // hardcoded as 0
}
update := bson.M{"$setOnInsert": doc}

return nil
_, err := collection.UpdateOne(ctx, filter, update, options.Update().SetUpsert(true))
return err
}

func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*bbnclient.StakingParams, error) {
Expand All @@ -82,11 +75,11 @@ func (db *Database) GetStakingParams(ctx context.Context, version uint32) (*bbnc
"version": version,
}

var params model.GlobalParamsDocument
var params model.StakingParamsDocument
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
return params.Params, nil
}

0 comments on commit 9a507e0

Please sign in to comment.