Commit 1836d50a authored by Son of Odin's avatar Son of Odin 💬

Merge branch 'last-unstake-bnb' into 'master'

[bugfix] last bnb unstake exclude portion of bnb for gas

See merge request !687
parents 9cd67ec0 5d8a24c3
Pipeline #129315537 failed with stages
in 52 minutes and 56 seconds
......@@ -141,6 +141,9 @@ func (k *TestEndPoolHandleKeeper) GetAdminConfigDefaultPoolStatus(_ sdk.Context,
func (s *HandlerEndPoolSuite) TestHandle(c *C) {
ctx, _ := setupKeeperForTest(c)
asset, err := common.NewAsset("BNB.LOK-3C0")
c.Assert(err, IsNil)
activeNodeAccount := GetRandomNodeAccount(NodeActive)
activeNodeAccount.Bond = sdk.NewUint(common.One * 100)
bnbAddr := GetRandomBNBAddress()
......@@ -149,13 +152,13 @@ func (s *HandlerEndPoolSuite) TestHandle(c *C) {
currentPool: Pool{
BalanceRune: sdk.ZeroUint(),
BalanceAsset: sdk.ZeroUint(),
Asset: common.BNBAsset,
Asset: asset,
PoolUnits: sdk.ZeroUint(),
PoolAddress: "",
Status: PoolEnabled,
},
poolStaker: PoolStaker{
Asset: common.BNBAsset,
Asset: asset,
TotalUnits: sdk.ZeroUint(),
Stakers: nil,
},
......@@ -176,13 +179,13 @@ func (s *HandlerEndPoolSuite) TestHandle(c *C) {
stakeTxHash,
bnbAddr,
GetRandomBNBAddress(),
common.Coins{common.NewCoin(common.BNBAsset, sdk.OneUint())},
common.Coins{common.NewCoin(asset, sdk.OneUint())},
common.BNBGasFeeSingleton,
"",
)
msgSetStake := NewMsgSetStakeData(
tx,
common.BNBAsset,
asset,
sdk.NewUint(100*common.One),
sdk.NewUint(100*common.One),
bnbAddr,
......@@ -194,7 +197,7 @@ func (s *HandlerEndPoolSuite) TestHandle(c *C) {
stakeResult := stakeHandler.Run(ctx, msgSetStake, ver, constAccessor)
c.Assert(stakeResult.Code, Equals, sdk.CodeOK)
p, err := keeper.GetPool(ctx, common.BNBAsset)
p, err := keeper.GetPool(ctx, asset)
c.Assert(err, IsNil)
c.Assert(p.Empty(), Equals, false)
c.Assert(p.BalanceRune.Uint64(), Equals, msgSetStake.RuneAmount.Uint64())
......@@ -204,10 +207,10 @@ func (s *HandlerEndPoolSuite) TestHandle(c *C) {
txOutStore.NewBlock(1, constAccessor)
// EndPool again
msgEndPool1 := NewMsgEndPool(common.BNBAsset, tx, activeNodeAccount.NodeAddress)
msgEndPool1 := NewMsgEndPool(asset, tx, activeNodeAccount.NodeAddress)
result1 := handler.handle(ctx, msgEndPool1, ver, constAccessor)
c.Assert(result1.Code, Equals, sdk.CodeOK, Commentf("%+v\n", result1))
p1, err := keeper.GetPool(ctx, common.BNBAsset)
p1, err := keeper.GetPool(ctx, asset)
c.Assert(err, IsNil)
c.Check(p1.Status, Equals, PoolBootstrap)
c.Check(p1.BalanceAsset.Uint64(), Equals, uint64(0))
......
......@@ -227,7 +227,7 @@ func (HandlerSuite) TestHandleTxInUnstakeMemo(c *C) {
c.Assert(pool.Status, Equals, PoolBootstrap)
c.Assert(pool.PoolUnits.Uint64(), Equals, uint64(0))
c.Assert(pool.BalanceRune.Uint64(), Equals, uint64(0))
c.Assert(pool.BalanceAsset.Uint64(), Equals, uint64(0))
c.Assert(pool.BalanceAsset.Uint64(), Equals, uint64(75000)) // leave a little behind for gas
}
func (HandlerSuite) TestRefund(c *C) {
......
......@@ -71,7 +71,7 @@ func (s *ThorchainSuite) TestStaking(c *C) {
pool, err = keeper.GetPool(ctx, common.BNBAsset)
c.Assert(err, IsNil)
c.Check(pool.BalanceRune.IsZero(), Equals, true)
c.Check(pool.BalanceAsset.IsZero(), Equals, true)
c.Check(pool.BalanceAsset.Uint64(), Equals, uint64(75000)) // leave a little behind for gas
c.Check(pool.PoolUnits.IsZero(), Equals, true)
// stake for user1, again
......@@ -87,7 +87,7 @@ func (s *ThorchainSuite) TestStaking(c *C) {
pool, err = keeper.GetPool(ctx, common.BNBAsset)
c.Assert(err, IsNil)
c.Check(pool.BalanceRune.Equal(sdk.NewUint(200*common.One)), Equals, true)
c.Check(pool.BalanceAsset.Equal(sdk.NewUint(200*common.One)), Equals, true)
c.Check(pool.BalanceAsset.Equal(sdk.NewUint(20000075000)), Equals, true, Commentf("%d", pool.BalanceAsset.Uint64()))
c.Check(pool.PoolUnits.IsZero(), Equals, false)
}
......
......@@ -84,6 +84,18 @@ func unstake(ctx sdk.Context, version semver.Version, keeper Keeper, msg MsgSetU
return sdk.ZeroUint(), sdk.ZeroUint(), sdk.ZeroUint(), sdk.NewError(DefaultCodespace, CodeUnstakeFail, err.Error())
}
// If the pool is empty, and there is a gas asset, subtract required gas
if common.SafeSub(poolUnits, fStakerUnit).Add(unitAfter).IsZero() {
// TODO: make this not chain specific
// minus gas costs for our transactions
if pool.Asset.IsBNB() {
withDrawAsset = common.SafeSub(
withDrawAsset,
common.BNBGasFeeSingleton[0].Amount.MulUint64(uint64(2)),
)
}
}
withdrawRune = withdrawRune.Add(stakerUnit.PendingRune) // extract pending rune
stakerUnit.PendingRune = sdk.ZeroUint() // reset pending to zero
......
......@@ -392,7 +392,7 @@ func (UnstakeSuite) TestUnstake(c *C) {
},
ps: getInMemoryPoolStorageForUnstake(c),
runeAmount: sdk.NewUint(100 * common.One),
assetAmount: sdk.NewUint(100 * common.One),
assetAmount: sdk.NewUint(100 * common.One).Sub(sdk.NewUint(75000)),
expectedError: nil,
},
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment