Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
happycoder9345 committed Sep 23, 2024
1 parent 14bd6b4 commit a711a7b
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 249 deletions.
5 changes: 4 additions & 1 deletion proto/lorenzo/btcstaking/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ option go_package = "github.com/Lorenzo-Protocol/lorenzo/v3/x/btcstaking/types";
message EventBTCStakingCreated { BTCStakingRecord record = 1; }

// BTCB staking creation event
message EventBTCBStakingCreated { BTCBStakingRecord record = 1; }
message EventBTCBStakingCreated { XBTCStakingRecord record = 1; }

// xBTC staking creation event
message EventxBTCStakingCreated { XBTCStakingRecord record = 1; }

message EventBurnCreated {
string signer = 1;
Expand Down
10 changes: 5 additions & 5 deletions proto/lorenzo/btcstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ service Query {
option (google.api.http).get = "/lorenzo/btcstaking/v1/stakingRecord";
}

rpc BTCBStakingRecord(QueryBTCBStakingRecordRequest)
returns (QueryBTCBStakingRecordResponse) {
option (google.api.http).get = "/lorenzo/btcstaking/v1/btcb-staking-record";
rpc XBTCStakingRecord(QueryxBTCStakingRecordRequest)
returns (QueryxBTCStakingRecordResponse) {
option (google.api.http).get = "/lorenzo/btcstaking/v1/xbtc-staking-record";
}
}
message QueryParamsRequest {}
Expand All @@ -38,9 +38,9 @@ message StakingRecordDisplay {
message QueryStakingRecordRequest { bytes txHash = 1; }
message QueryStakingRecordResponse { BTCStakingRecord record = 1; }

message QueryBTCBStakingRecordRequest {
message QueryxBTCStakingRecordRequest {
uint32 chain_id = 1;
string contract = 2;
uint64 staking_idx = 3;
}
message QueryBTCBStakingRecordResponse { BTCBStakingRecord record = 1; }
message QueryxBTCStakingRecordResponse { XBTCStakingRecord record = 1; }
6 changes: 2 additions & 4 deletions proto/lorenzo/btcstaking/v1/staking_record.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
syntax = "proto3";
package lorenzo.btcstaking.v1;

import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
// import "cosmos/staking/v1beta1/staking.proto";

Expand All @@ -19,8 +17,8 @@ message BTCStakingRecord {
uint64 plan_id = 8;
}

// BTCBStakingRecord defines the message for btcb staking record
message BTCBStakingRecord {
// XBTCStakingRecord defines the message for btc staking record
message XBTCStakingRecord {
// staking_idx defines the staking index
uint64 staking_idx = 1;
// contract defines the contract
Expand Down
10 changes: 5 additions & 5 deletions x/btcstaking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
btcstakingQueryCmd.AddCommand(
CmdGetParams(),
CmdGetBTCStakingRecord(),
CmdGetBTCBStakingRecord(),
CmdGetxBTCStakingRecord(),
)

return btcstakingQueryCmd
Expand Down Expand Up @@ -92,9 +92,9 @@ func CmdGetBTCStakingRecord() *cobra.Command {
return cmd
}

func CmdGetBTCBStakingRecord() *cobra.Command {
func CmdGetxBTCStakingRecord() *cobra.Command {
cmd := &cobra.Command{
Use: "btcb-staking-record [chain-id] [contract] [staking-idx]",
Use: "xbtc-staking-record [chain-id] [contract] [staking-idx]",
Short: "get the btcb staking record",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -110,8 +110,8 @@ func CmdGetBTCBStakingRecord() *cobra.Command {
return fmt.Errorf("invalid staking idx: %s", args[0])
}

res, err := queryClient.BTCBStakingRecord(cmd.Context(),
&types.QueryBTCBStakingRecordRequest{
res, err := queryClient.XBTCStakingRecord(cmd.Context(),
&types.QueryxBTCStakingRecordRequest{
ChainId: uint32(chainID),
Contract: args[1],
StakingIdx: stakingIdx,
Expand Down
8 changes: 4 additions & 4 deletions x/btcstaking/keeper/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (e *eventHandler) Execute(ctx sdk.Context, chainID uint32, events []*ccevty
return err
}

if e.keeper.hasBTCBStakingRecord(ctx, chainID, event.Contract.Bytes(), event.Identifier) {
if e.keeper.hasxBTCStakingRecord(ctx, chainID, event.Contract.Bytes(), event.Identifier) {
return types.ErrDuplicateStakingEvent.Wrapf("duplicate event,planID %d,stakingIdx %d,contract %s",
event.PlanID,
event.Identifier,
Expand All @@ -52,7 +52,7 @@ func (e *eventHandler) Execute(ctx sdk.Context, chainID uint32, events []*ccevty
result := ""
totalStBTCAmt = totalStBTCAmt.Add(totalStBTCAmt, amount)

btcbStakingRecord := &types.BTCBStakingRecord{
xbtcStakingRecord := &types.XBTCStakingRecord{
StakingIdx: event.Identifier,
Contract: event.Contract.Bytes(),
ReceiverAddr: event.Sender.String(),
Expand All @@ -62,9 +62,9 @@ func (e *eventHandler) Execute(ctx sdk.Context, chainID uint32, events []*ccevty
PlanId: event.PlanID,
}

e.keeper.addBTCBStakingRecord(ctx, btcbStakingRecord)
e.keeper.addxBTCStakingRecord(ctx, xbtcStakingRecord)
// emit an event
ctx.EventManager().EmitTypedEvent(types.NewEventBTCBStakingCreated(btcbStakingRecord)) //nolint:errcheck,gosec
ctx.EventManager().EmitTypedEvent(types.NewEventXBTCStakingCreated(xbtcStakingRecord)) //nolint:errcheck,gosec
}

// mint stBTC to the bridgeAddr
Expand Down
12 changes: 6 additions & 6 deletions x/btcstaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ func (k Keeper) StakingRecord(c context.Context, req *types.QueryStakingRecordRe
return &types.QueryStakingRecordResponse{Record: stakingRecord}, nil
}

// BTCBStakingRecord retrieves the BTCB staking record for the given contract and staking index.
// XBTCStakingRecord retrieves the xBTC staking record for the given contract and staking index.
//
// Parameters:
// - c: The context.Context object for the request.
// - req: The QueryBTCBStakingRecordRequest object containing the contract address and staking index.
// - req: The QueryxBTCStakingRecordRequest object containing the contract address and staking index.
//
// Returns:
// - *types.QueryBTCBStakingRecordResponse: The BTCB staking record response.
// - *types.QueryxBTCStakingRecordResponse: The BTCB staking record response.
// - error: An error if the staking record retrieval fails.
func (k Keeper) BTCBStakingRecord(c context.Context, req *types.QueryBTCBStakingRecordRequest) (*types.QueryBTCBStakingRecordResponse, error) {
func (k Keeper) XBTCStakingRecord(c context.Context, req *types.QueryxBTCStakingRecordRequest) (*types.QueryxBTCStakingRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

contract := common.HexToAddress(req.Contract)
// get the staking record
stakingRecord, err := k.getBTCBStakingRecord(ctx, req.ChainId, contract[:], req.StakingIdx)
stakingRecord, err := k.getxBTCBtakingRecord(ctx, req.ChainId, contract[:], req.StakingIdx)
if err != nil {
return nil, err
}
return &types.QueryBTCBStakingRecordResponse{Record: stakingRecord}, nil
return &types.QueryxBTCStakingRecordResponse{Record: stakingRecord}, nil
}

// NewQuerierImpl returns an implementation of the captains QueryServer interface.
Expand Down
20 changes: 10 additions & 10 deletions x/btcstaking/keeper/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (k Keeper) DepositBTCB(
totalStBTCAmt := new(big.Int)
for i := range events {
event := events[i]
if k.hasBTCBStakingRecord(ctx, event.ChainID, event.Contract.Bytes(), event.Identifier) {
if k.hasxBTCStakingRecord(ctx, event.ChainID, event.Contract.Bytes(), event.Identifier) {
return types.ErrDuplicateStakingEvent.Wrapf("duplicate event,planID %d,stakingIdx %d,contract %s",
event.PlanID,
event.Identifier,
Expand Down Expand Up @@ -72,7 +72,7 @@ func (k Keeper) DepositBTCB(

totalStBTCAmt = totalStBTCAmt.Add(totalStBTCAmt, amount)

btcbStakingRecord := &types.BTCBStakingRecord{
btcbStakingRecord := &types.XBTCStakingRecord{
StakingIdx: event.Identifier,
Contract: event.Contract.Bytes(),
ReceiverAddr: event.Sender.String(),
Expand All @@ -82,7 +82,7 @@ func (k Keeper) DepositBTCB(
PlanId: event.PlanID,
}

k.addBTCBStakingRecord(ctx, btcbStakingRecord)
k.addxBTCStakingRecord(ctx, btcbStakingRecord)

// emit an event
ctx.EventManager().EmitTypedEvent(types.NewEventBTCBStakingCreated(btcbStakingRecord)) //nolint:errcheck,gosec
Expand Down Expand Up @@ -126,26 +126,26 @@ func (k Keeper) DepositxBTC(
return k.ccevKeeper.VerifyAndCallback(ctx, chainID, number, receiptBz, proofBz, &eventHandler{k})
}

func (k Keeper) addBTCBStakingRecord(ctx sdk.Context, record *types.BTCBStakingRecord) {
func (k Keeper) addxBTCStakingRecord(ctx sdk.Context, record *types.XBTCStakingRecord) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(record)
store.Set(types.KeyBTCBStakingRecord(record.ChainId, record.Contract, record.StakingIdx), bz)
store.Set(types.KeyxBTCStakingRecord(record.ChainId, record.Contract, record.StakingIdx), bz)
}

func (k Keeper) hasBTCBStakingRecord(ctx sdk.Context, chainID uint32, contract []byte, stakingIdx uint64) bool {
func (k Keeper) hasxBTCStakingRecord(ctx sdk.Context, chainID uint32, contract []byte, stakingIdx uint64) bool {
store := ctx.KVStore(k.storeKey)
key := types.KeyBTCBStakingRecord(chainID, contract, stakingIdx)
key := types.KeyxBTCStakingRecord(chainID, contract, stakingIdx)
return store.Has(key)
}

func (k Keeper) getBTCBStakingRecord(ctx sdk.Context, chainID uint32, contract []byte, stakingIdx uint64) (*types.BTCBStakingRecord, error) {
func (k Keeper) getxBTCBtakingRecord(ctx sdk.Context, chainID uint32, contract []byte, stakingIdx uint64) (*types.XBTCStakingRecord, error) {
store := ctx.KVStore(k.storeKey)
key := types.KeyBTCBStakingRecord(chainID, contract, stakingIdx)
key := types.KeyxBTCStakingRecord(chainID, contract, stakingIdx)
bz := store.Get(key)
if len(bz) == 0 {
return nil, types.ErrStakingRecordNotFound
}
var record types.BTCBStakingRecord
var record types.XBTCStakingRecord
k.cdc.MustUnmarshal(bz, &record)
return &record, nil
}

Check failure on line 151 in x/btcstaking/keeper/staking.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

syntax error: unexpected var after top level declaration
Loading

0 comments on commit a711a7b

Please sign in to comment.