Skip to content

Commit

Permalink
fix: btcstaking module's CreateBTCStaking interface (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonleedev authored Aug 15, 2024
1 parent 7b5face commit 560055a
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 72 deletions.
3 changes: 3 additions & 0 deletions proto/lorenzo/btcstaking/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ option go_package = "github.com/Lorenzo-Protocol/lorenzo/v3/x/btcstaking/types";
// BTC staking creation event
message EventBTCStakingCreated { BTCStakingRecord record = 1; }

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

message EventBurnCreated {
string signer = 1;
string btc_target_address = 2;
Expand Down
2 changes: 2 additions & 0 deletions proto/lorenzo/btcstaking/v1/staking_record.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ message BTCStakingRecord {
string agent_btc_addr = 5;
uint32 chain_id = 6;
string mint_yat_result = 7;
uint64 plan_id = 8;
}

// BTCBStakingRecord defines the message for btcb staking record
Expand All @@ -35,4 +36,5 @@ message BTCBStakingRecord {
uint32 chain_id = 5;
// mint_yat_result defines the mint yat result
string mint_yat_result = 6;
uint64 plan_id = 7;
}
31 changes: 17 additions & 14 deletions x/btcstaking/keeper/btc_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
func (k Keeper) Delegate(
ctx sdk.Context,
btcStakingRecord *types.BTCStakingRecord,
mintToAddr sdk.AccAddress,
receiverAddr sdk.AccAddress,
btcAmount uint64,
planId uint64,
Expand All @@ -35,26 +36,28 @@ func (k Keeper) Delegate(
return errorsmod.Wrapf(types.ErrMintToModule, "failed to mint coins: %v", err)
}

if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receiverAddr, coins); err != nil {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, mintToAddr, coins); err != nil {
return errorsmod.Wrapf(types.ErrTransferToAddr, "failed to send coins from module to account: %v", err)
}

// Mint Yat can be wrong if plan not found or agentId not match
plan, found := k.planKeeper.GetPlan(ctx, planId)
if !found {
btcStakingRecord.MintYatResult = PlanNotFound
} else if plan.AgentId != agentId {
btcStakingRecord.MintYatResult = AgentIdNotMatch
} else {
// mint yat
yatMintErr := k.planKeeper.Mint(ctx, planId, common.BytesToAddress(receiverAddr), toMintAmount.BigInt())
if yatMintErr != nil {
btcStakingRecord.MintYatResult = yatMintErr.Error()
if planId != 0 {
// Mint Yat can be wrong if plan not found or agentId not match
plan, found := k.planKeeper.GetPlan(ctx, planId)
if !found {
btcStakingRecord.MintYatResult = PlanNotFound
} else if plan.AgentId != agentId {
btcStakingRecord.MintYatResult = AgentIdNotMatch
} else {
btcStakingRecord.MintYatResult = Success
// mint yat
yatMintErr := k.planKeeper.Mint(ctx, planId, common.BytesToAddress(receiverAddr), toMintAmount.BigInt())
if yatMintErr != nil {
btcStakingRecord.MintYatResult = yatMintErr.Error()
} else {
btcStakingRecord.MintYatResult = Success
}
}
btcStakingRecord.PlanId = planId
}

if err := k.AddBTCStakingRecord(ctx, btcStakingRecord); err != nil {
return errorsmod.Wrapf(types.ErrRecordStaking, "failed to record staking: %v", err)
}
Expand Down
115 changes: 114 additions & 1 deletion x/btcstaking/keeper/btc_staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
func (suite *KeeperTestSuite) TestDelegate() {
type tmpRequest struct {
btcStakingRecord *types.BTCStakingRecord
mintAddr sdk.AccAddress
receiverAddr sdk.AccAddress
btcAmount uint64
planId uint64
Expand All @@ -44,6 +45,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
AgentBtcAddr: "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq",
ChainId: 8329,
},
mintAddr: testAdmin,
receiverAddr: testAdmin,
btcAmount: 1e7,
planId: 0,
Expand All @@ -66,7 +68,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
suite.Require().NoError(err)
btcStakingRecord := suite.keeper.GetBTCStakingRecord(suite.ctx, *txHash)
suite.Require().NotNil(btcStakingRecord)
suite.Require().Equal(btcStakingRecord.MintYatResult, keeper.PlanNotFound)
suite.Require().Equal(btcStakingRecord.MintYatResult, "")
},
expectErr: false,
},
Expand All @@ -80,6 +82,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
AgentBtcAddr: "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq",
ChainId: 8329,
},
mintAddr: testAdmin,
receiverAddr: testAdmin,
btcAmount: 1e7,
planId: 1,
Expand Down Expand Up @@ -144,6 +147,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
AgentBtcAddr: "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq",
ChainId: 8329,
},
mintAddr: testAdmin,
receiverAddr: testAdmin,
btcAmount: 1e7,
planId: 1,
Expand Down Expand Up @@ -218,6 +222,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
AgentBtcAddr: "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq",
ChainId: 8329,
},
mintAddr: testAdmin,
receiverAddr: testAdmin,
btcAmount: 1e7,
planId: 1,
Expand Down Expand Up @@ -279,6 +284,113 @@ func (suite *KeeperTestSuite) TestDelegate() {
btcStakingRecord := suite.keeper.GetBTCStakingRecord(suite.ctx, *txHash)
suite.Require().NotNil(btcStakingRecord)
suite.Require().Equal(btcStakingRecord.MintYatResult, keeper.Success)

events := suite.ctx.EventManager().Events()
abciEvents := events.ToABCIEvents()
for _, abciEvent := range abciEvents {
if abciEvent.Type == "mint_yat" {
eventAttribute := abciEvent.GetAttributes()

suite.Require().Equal(eventAttribute[0].Key, "plan_id")
suite.Require().Equal(eventAttribute[0].Value, fmt.Sprintf("%d", request.planId))
suite.Require().Equal(eventAttribute[1].Key, "account")
suite.Require().Equal(eventAttribute[1].Value, common.BytesToAddress(request.receiverAddr).Hex())
suite.Require().Equal(eventAttribute[2].Key, "amount")
suite.Require().Equal(eventAttribute[2].Value, toMintAmount.String())

}
}
},
expectErr: false,
},
{
name: "success - mintAddr not equal to receiverAddr",
request: &tmpRequest{
btcStakingRecord: &types.BTCStakingRecord{
Amount: 1e7,
ReceiverAddr: testAdmin,
AgentName: "lorenzo",
AgentBtcAddr: "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq",
ChainId: 8329,
},
mintAddr: testAdmin,
receiverAddr: sdk.AccAddress("lrz1cpldpp5960ed8s63w4v9fml84w875wv0emcda5"),
btcAmount: 1e7,
planId: 1,
agentId: 1,
},
malleate: func(request *tmpRequest) {
suite.Commit()
txHash, err := hex.DecodeString("84b6addf9aca33604ab20453b71a5da8ab2bd9b773a12a9aecb9944a1123639a")
suite.Require().NoError(err)
request.btcStakingRecord.TxHash = txHash

// create agent
name := "lorenzo"
btcReceivingAddress := "tb1p97g0dpmsm2fxkmkw9w7mpasmxprsye3k0v49qknwmclwxj78rfjqu6nacq"
ethAddr := ""
description := "lorenzo"
url := "https://lorenzo-protocol.io"
agentId := suite.lorenzoApp.AgentKeeper.AddAgent(
suite.ctx,
name, btcReceivingAddress, ethAddr, description, url)
suite.Require().NotEqual(agentId, 0)
suite.Require().Equal(agentId, uint64(1))
yatAddr, err := suite.lorenzoApp.PlanKeeper.DeployYATContract(
suite.ctx, "lorenzo", "ALRZ")
suite.Require().NoError(err)
// create plan
planReq := plantypes.Plan{
Name: "lorenzo-stake-plan",
PlanDescUri: "https://lorenzo-protocol.io/lorenzo-stake-plan",
AgentId: uint64(1),
PlanStartTime: uint64(time.Now().UTC().Unix()) - 100000,
PeriodTime: 1000,
YatContractAddress: yatAddr.Hex(),
}

_, err = suite.lorenzoApp.PlanKeeper.AddPlan(suite.ctx, planReq)
suite.Require().NoError(err)
},
validation: func(request *tmpRequest) {
balance := suite.lorenzoApp.BankKeeper.GetBalance(
suite.ctx, request.mintAddr, types.NativeTokenDenom)
toMintAmount := sdkmath.NewIntFromUint64(request.btcAmount).Mul(sdkmath.NewIntFromUint64(keeper.SatoshiToStBTCMul))
suite.Require().Equal(balance.Amount, toMintAmount)
plan, planFound := suite.lorenzoApp.PlanKeeper.GetPlan(suite.ctx, request.planId)
suite.Require().True(planFound)
yatContractAddress := common.HexToAddress(plan.YatContractAddress)
receiverAddr := common.BytesToAddress(request.receiverAddr)
yatAmount, err := suite.lorenzoApp.PlanKeeper.BalanceOfFromYAT(
suite.ctx,
yatContractAddress,
receiverAddr,
)
suite.Require().NoError(err)
suite.Require().Equal(yatAmount, toMintAmount.BigInt())

// check btc staking record
txHash, err := chainhash.NewHash(request.btcStakingRecord.TxHash)
suite.Require().NoError(err)
btcStakingRecord := suite.keeper.GetBTCStakingRecord(suite.ctx, *txHash)
suite.Require().NotNil(btcStakingRecord)
suite.Require().Equal(btcStakingRecord.MintYatResult, keeper.Success)

events := suite.ctx.EventManager().Events()
abciEvents := events.ToABCIEvents()
for _, abciEvent := range abciEvents {
if abciEvent.Type == "mint_yat" {
eventAttribute := abciEvent.GetAttributes()

suite.Require().Equal(eventAttribute[0].Key, "plan_id")
suite.Require().Equal(eventAttribute[0].Value, fmt.Sprintf("%d", request.planId))
suite.Require().Equal(eventAttribute[1].Key, "account")
suite.Require().Equal(eventAttribute[1].Value, common.BytesToAddress(request.receiverAddr).Hex())
suite.Require().Equal(eventAttribute[2].Key, "amount")
suite.Require().Equal(eventAttribute[2].Value, toMintAmount.String())

}
}
},
expectErr: false,
},
Expand All @@ -293,6 +405,7 @@ func (suite *KeeperTestSuite) TestDelegate() {
err := suite.keeper.Delegate(
suite.ctx,
tc.request.btcStakingRecord,
tc.request.mintAddr,
tc.request.receiverAddr,
tc.request.btcAmount,
tc.request.planId,
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (ms msgServer) CreateBTCStaking(goCtx context.Context, msg *types.MsgCreate

// mint stBTC to mintToAddr and record the staking
if err := ms.k.Delegate(ctx,
stakingRecord, mintToAddr, btcAmount, planId, msg.AgentId); err != nil {
stakingRecord, mintToAddr, receiverAddr, btcAmount, planId, msg.AgentId); err != nil {
return nil, err
}

Expand Down
12 changes: 10 additions & 2 deletions x/btcstaking/keeper/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ func (k Keeper) DepositBTCB(
}

totalStBTCAmt = totalStBTCAmt.Add(totalStBTCAmt, amount)
k.addBTCBStakingRecord(ctx, &types.BTCBStakingRecord{

btcbStakingRecord := &types.BTCBStakingRecord{
StakingIdx: event.Identifier,
Contract: event.Contract.Bytes(),
ReceiverAddr: event.Sender.String(),
Amount: math.NewIntFromBigInt(amount),
ChainId: event.ChainID,
MintYatResult: result,
})
PlanId: event.PlanID,
}

k.addBTCBStakingRecord(ctx, btcbStakingRecord)

// emit an event
ctx.EventManager().EmitTypedEvent(types.NewEventBTCBStakingCreated(btcbStakingRecord)) //nolint:errcheck,gosec

}

// mint stBTC to the bridgeAddr
Expand Down
Loading

0 comments on commit 560055a

Please sign in to comment.