-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
524 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.21-alpine AS builder | ||
FROM golang:1.22.3-alpine AS builder | ||
|
||
ARG VERSION="HEAD" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model" | ||
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func (db *Database) SaveNewBTCDelegation( | ||
ctx context.Context, delegationDoc *model.BTCDelegationDetails, | ||
) error { | ||
_, err := db.client.Database(db.dbName). | ||
Collection(model.BTCDelegationDetailsCollection). | ||
InsertOne(ctx, delegationDoc) | ||
if err != nil { | ||
var writeErr mongo.WriteException | ||
if errors.As(err, &writeErr) { | ||
for _, e := range writeErr.WriteErrors { | ||
if mongo.IsDuplicateKeyError(e) { | ||
return &DuplicateKeyError{ | ||
Key: delegationDoc.StakingTxHashHex, | ||
Message: "delegation already exists", | ||
} | ||
} | ||
} | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (db *Database) UpdateBTCDelegationState( | ||
ctx context.Context, stakingTxHash string, newState types.DelegationState, | ||
) error { | ||
filter := map[string]interface{}{"_id": stakingTxHash} | ||
update := map[string]interface{}{"$set": map[string]string{"state": newState.String()}} | ||
|
||
res := db.client.Database(db.dbName). | ||
Collection(model.BTCDelegationDetailsCollection). | ||
FindOneAndUpdate(ctx, filter, update) | ||
|
||
if res.Err() != nil { | ||
if errors.Is(res.Err(), mongo.ErrNoDocuments) { | ||
return &NotFoundError{ | ||
Key: stakingTxHash, | ||
Message: "BTC delegation not found when updating state", | ||
} | ||
} | ||
return res.Err() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (db *Database) GetBTCDelegationByStakingTxHash( | ||
ctx context.Context, stakingTxHash string, | ||
) (*model.BTCDelegationDetails, error) { | ||
filter := map[string]interface{}{"_id": stakingTxHash} | ||
res := db.client.Database(db.dbName). | ||
Collection(model.BTCDelegationDetailsCollection). | ||
FindOne(ctx, filter) | ||
|
||
var delegationDoc model.BTCDelegationDetails | ||
err := res.Decode(&delegationDoc) | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
return nil, &NotFoundError{ | ||
Key: stakingTxHash, | ||
Message: "BTC delegation not found when getting by staking tx hash", | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
return &delegationDoc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.