-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MarkDelegationAsTransitioned method (#163)
- Loading branch information
1 parent
b3311ec
commit 918d24a
Showing
16 changed files
with
143 additions
and
4 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
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
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,55 @@ | ||
package v2dbclient | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/shared/db" | ||
dbmodel "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func (v2dbclient *V2Database) MarkV1DelegationAsTransitioned(ctx context.Context, stakingTxHashHex string) error { | ||
session, err := v2dbclient.Client.StartSession() | ||
if err != nil { | ||
return err | ||
} | ||
defer session.EndSession(ctx) | ||
|
||
transactionWork := func(sessCtx mongo.SessionContext) (interface{}, error) { | ||
client := v2dbclient.Client.Database(v2dbclient.DbName).Collection(dbmodel.V1DelegationCollection) | ||
filter := bson.M{"_id": stakingTxHashHex} | ||
|
||
var delegation interface{} | ||
err := client.FindOne(sessCtx, filter).Decode(&delegation) | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
return nil, &db.NotFoundError{ | ||
Key: stakingTxHashHex, | ||
Message: "Delegation not found", | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
update := bson.M{"$set": bson.M{"is_transitioned": true}} | ||
result, err := client.UpdateOne(sessCtx, filter, update) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.MatchedCount == 0 { | ||
return nil, &db.NotFoundError{ | ||
Key: stakingTxHashHex, | ||
Message: "Delegation not found", | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
_, err = session.WithTransaction(ctx, transactionWork) | ||
return err | ||
} | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.