Skip to content

Commit

Permalink
update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed Feb 7, 2024
1 parent 6fd8ea3 commit dc0c9ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
30 changes: 16 additions & 14 deletions app/upgrades/v4_1_0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -103,22 +102,21 @@ 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)
if err != nil {
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)
}
}
}

Expand Down Expand Up @@ -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
}
44 changes: 28 additions & 16 deletions app/upgrades/v4_1_0/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)))
}

0 comments on commit dc0c9ac

Please sign in to comment.