diff --git a/app/upgrades/v4_1_0/upgrades.go b/app/upgrades/v4_1_0/upgrades.go index cf3a6386..9d773403 100644 --- a/app/upgrades/v4_1_0/upgrades.go +++ b/app/upgrades/v4_1_0/upgrades.go @@ -5,7 +5,6 @@ import ( "time" "cosmossdk.io/math" - "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/params" "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -65,7 +64,7 @@ func CreateUpgradeHandler( // migrateMultisigVesting moves the vested and reward token from the ContinuousVestingAccount -> the new multisig vesting account. // - Retrieves the old multisig vesting account // - Instantly finish all redelegations, then unbond all tokens. -// - Transfer all tokens vested and reward tokens to the new multisig vesting (including the previously held balance) +// - Transfer all tokens vested and reward tokens to the new multisig account (including the previously held balance) // - Delegates the vesting coins to the top 10 validators func migrateMultisigVesting(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, @@ -103,8 +102,6 @@ func processMigrateMultisig(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, fmt.Printf("currentAddr Instant Redelegations: %s\n", redelegated) fmt.Printf("currentAddr Instant Unbonding: %s\n", unbonded) - // get vested + reward balance - totalBalance := bankKeeper.GetBalance(ctx, currentAddr, params.BaseDenom) // delegate vesting coin to validator err = delegateToValidator(ctx, stakingKeeper, currentAddr, oldAcc.GetVestingCoins(ctx.BlockTime())[0].Amount) @@ -112,13 +109,14 @@ func processMigrateMultisig(ctx sdk.Context, stakingKeeper stakingKeeper.Keeper, panic(err) } - fmt.Println("total balance before migration ", totalBalance) - balanceCanSend := totalBalance.Amount.Sub(oldAcc.GetVestingCoins(ctx.BlockTime())[0].Amount) - fmt.Printf("total balance send to new multisig addr: %s\n", balanceCanSend) - // send vested + reward balance no newAddr - err = bankKeeper.SendCoins(ctx, currentAddr, newAddr, sdk.NewCoins(sdk.NewCoin(params.BaseDenom, balanceCanSend))) - if err != nil { - panic(err) + // get vested + reward balance + for _, coin := range bankKeeper.GetAllBalances(ctx, currentAddr) { + fmt.Printf("demom %s, total balance send to new multisig addr: %v\n", coin.Denom, coin.Amount) + // send vested + reward balance no newAddr + err = bankKeeper.SendCoins(ctx, currentAddr, newAddr, sdk.NewCoins(sdk.NewCoin(coin.Denom, coin.Amount))) + if err != nil { + panic(err) + } } } @@ -203,16 +201,20 @@ func delegateToValidator(ctx sdk.Context, listValidator := stakingKeeper.GetBondedValidatorsByPower(ctx) totalValidatorDelegate := math.Min(10, len(listValidator)) balanceDelegate := totalVestingBalance.Quo(math.NewInt(int64(totalValidatorDelegate))) - fmt.Printf("balanceDelegate each validator %v, total validator %d\n", balanceDelegate, totalValidatorDelegate) - + totalBalanceDelegate := math.ZeroInt() for i, validator := range listValidator { if i >= totalValidatorDelegate { break } - _, err := stakingKeeper.Delegate(ctx, accAddr, balanceDelegate, stakingtypes.Unbonded, validator, true) + if i == totalValidatorDelegate-1 { + balanceDelegate = totalVestingBalance.Sub(totalBalanceDelegate) + } + newShare, err := stakingKeeper.Delegate(ctx, accAddr, balanceDelegate, stakingtypes.Unbonded, validator, true) if err != nil { return err } + fmt.Printf("delegate %v to validator %v, newShare: %v\n", balanceDelegate, validator.OperatorAddress, newShare) + totalBalanceDelegate = totalBalanceDelegate.Add(balanceDelegate) } return nil } diff --git a/app/upgrades/v4_1_0/upgrades_test.go b/app/upgrades/v4_1_0/upgrades_test.go index e0a7c128..e27ca594 100644 --- a/app/upgrades/v4_1_0/upgrades_test.go +++ b/app/upgrades/v4_1_0/upgrades_test.go @@ -4,8 +4,6 @@ import ( "fmt" "testing" - "cosmossdk.io/math" - "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/params" v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_0" sdk "github.com/cosmos/cosmos-sdk/types" @@ -39,11 +37,29 @@ func (s *UpgradeTestSuite) TestUpgrade() { newVal1 := s.SetupValidator(stakingtypes.Bonded) newVal2 := s.SetupValidator(stakingtypes.Bonded) newVal3 := s.SetupValidator(stakingtypes.Bonded) + newVal4 := s.SetupValidator(stakingtypes.Bonded) + newVal5 := s.SetupValidator(stakingtypes.Bonded) + newVal6 := s.SetupValidator(stakingtypes.Bonded) + newVal7 := s.SetupValidator(stakingtypes.Bonded) + newVal8 := s.SetupValidator(stakingtypes.Bonded) + newVal9 := s.SetupValidator(stakingtypes.Bonded) + newVal10 := s.SetupValidator(stakingtypes.Bonded) + newVal11 := s.SetupValidator(stakingtypes.Bonded) + newVal12 := s.SetupValidator(stakingtypes.Bonded) // Delegate tokens of the vesting multisig account - s.StakingHelper.Delegate(vestingAddr, newVal1, sdk.NewInt(100)) - s.StakingHelper.Delegate(vestingAddr, newVal2, sdk.NewInt(200)) + s.StakingHelper.Delegate(vestingAddr, newVal1, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal2, sdk.NewInt(300)) s.StakingHelper.Delegate(vestingAddr, newVal3, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal4, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal5, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal6, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal7, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal8, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal9, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal10, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal11, sdk.NewInt(300)) + s.StakingHelper.Delegate(vestingAddr, newVal12, sdk.NewInt(300)) // Undelegate part of the tokens from val2 (test instant unbonding on undelegation started before upgrade) s.StakingHelper.Undelegate(vestingAddr, newVal3, sdk.NewInt(10), true) @@ -52,8 +68,8 @@ func (s *UpgradeTestSuite) TestUpgrade() { _, err := s.App.StakingKeeper.BeginRedelegation(s.Ctx, vestingAddr, newVal2, newVal3, sdk.NewDec(1)) s.Require().NoError(err) - // Confirm delegated to 3 validators - s.Require().Equal(3, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) + // Confirm delegated to 12 validators + s.Require().Equal(12, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) // == UPGRADE == upgradeHeight := int64(5) @@ -68,26 +84,22 @@ func (s *UpgradeTestSuite) TestUpgrade() { _, ok := accAfter.(*vestingtypes.ContinuousVestingAccount) s.Require().True(ok) - s.Require().Equal(1, len(s.App.BankKeeper.GetAllBalances(s.Ctx, vestingAddr))) - s.Require().Equal(4, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) + s.Require().Equal(0, len(s.App.BankKeeper.GetAllBalances(s.Ctx, vestingAddr))) + // now delegated to top 10 validator + s.Require().Equal(10, len(s.App.StakingKeeper.GetAllDelegatorDelegations(s.Ctx, vestingAddr))) s.Require().Equal(0, len(s.App.StakingKeeper.GetRedelegations(s.Ctx, vestingAddr, 65535))) // check old multisign address balance oldMultisigBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NotionalMultisigVestingAccount)) fmt.Printf("Old multisign address Upgrade Balance: %s\n", oldMultisigBalance) + s.Require().True(oldMultisigBalance.Empty()) totalDelegateBalance := s.App.StakingKeeper.GetDelegatorBonded(s.Ctx, sdk.MustAccAddressFromBech32(v4.NotionalMultisigVestingAccount)) fmt.Printf("old multisign address totalDelegateBalance %v\n", totalDelegateBalance) - s.Require().True(totalDelegateBalance.Add(oldMultisigBalance[0].Amount).GTE(unvested)) + s.Require().True(totalDelegateBalance.Equal(unvested)) // check new multisign address balance newBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, sdk.MustAccAddressFromBech32(v4.NewNotionalMultisigAccount)) vestedBalance := cVesting.GetVestedCoins(s.Ctx.BlockTime()) fmt.Printf("New multisign Upgrade Balance: %s, vestedBalance %s\n", newBalance, vestedBalance) - s.Require().True(vestedBalance.AmountOf(params.BaseDenom).GTE(newBalance.AmountOf(params.BaseDenom))) -} - -func (s *UpgradeTestSuite) TestMath() { - s.Require().Equal(math.NewInt(7), math.NewInt(76).Quo(math.NewInt(10))) - s.Require().Equal(math.NewInt(7), math.NewInt(79).Quo(math.NewInt(10))) - s.Require().Equal(math.NewInt(1), math.NewInt(5).Quo(math.NewInt(3))) + s.Require().True(vestedBalance.AmountOf(params.BaseDenom).Equal(newBalance.AmountOf(params.BaseDenom))) }