-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: migrate fund from dead account (#419)
* feat: add upgrade handler * feat: test migrate balance * chore: fix comments * add update cosmos-sdk, comet, ibc * bet --------- Co-authored-by: Khanh Hoa <[email protected]>
- Loading branch information
Showing
7 changed files
with
266 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package v4 | ||
|
||
// UpgradeName defines the on-chain upgrade name for the Migaloo v4.2.5 upgrade. | ||
// this upgrade includes the fix for pfm | ||
const ( | ||
UpgradeName = "v4.2.6" | ||
) | ||
|
||
const ( | ||
Denom = "ibc/BC5C0BAFD19A5E4133FDA0F3E04AE1FBEE75A4A226554B2CBB021089FF2E1F8A" | ||
DeadContract = "migaloo1qelh4gv5drg3yhj282l6n84a6wrrz033kwyak3ee3syvqg3mu3msgphpk4" | ||
Foundation = "migaloo10zqfqhw44e6gvu97frjzcghunndskhu40uyztwu00y6dr9qxrz6qcjfrf7" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package v4_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_2_6" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
const ( | ||
MockDeadContractBalance = 1000000000000000000 | ||
) | ||
|
||
type UpgradeTestSuite struct { | ||
apptesting.KeeperTestHelper | ||
} | ||
|
||
func TestUpgradeTestSuite(t *testing.T) { | ||
suite.Run(t, new(UpgradeTestSuite)) | ||
} | ||
|
||
func (s *UpgradeTestSuite) MockBankBalances() { | ||
deadContractAddr := sdk.MustAccAddressFromBech32(v4.DeadContract) | ||
|
||
coins := sdk.NewCoins( | ||
sdk.NewCoin(v4.Denom, math.NewInt(MockDeadContractBalance)), | ||
) | ||
|
||
// Mint coins to the dead contract | ||
err := s.App.BankKeeper.MintCoins(s.Ctx, "mint", coins) | ||
s.Require().NoError(err) | ||
err = s.App.BankKeeper.SendCoinsFromModuleToAccount(s.Ctx, "mint", deadContractAddr, coins) | ||
s.Require().NoError(err) | ||
|
||
// require the balance is correct | ||
balance := s.App.BankKeeper.GetAllBalances(s.Ctx, deadContractAddr) | ||
s.Require().Equal(coins, balance) | ||
} | ||
|
||
// Ensures the test does not error out. | ||
func (s *UpgradeTestSuite) TestUpgrade() { | ||
s.Setup(s.T()) | ||
// == CREATE MOCK VESTING ACCOUNT == | ||
s.MockBankBalances() | ||
|
||
// == UPGRADE == | ||
upgradeHeight := int64(5) | ||
|
||
// Execute upgrade | ||
s.ConfirmUpgradeSucceeded(v4.UpgradeName, upgradeHeight) | ||
|
||
// Dead contract balance get drained | ||
deadContractAddr := sdk.MustAccAddressFromBech32(v4.DeadContract) | ||
contractBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, deadContractAddr) | ||
s.Require().Equal(int64(0), contractBalance.AmountOf(v4.Denom).Int64()) | ||
|
||
// Foundation balance is increased | ||
foundationAddr := sdk.MustAccAddressFromBech32(v4.Foundation) | ||
foundationBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, foundationAddr) | ||
s.T().Logf("balance: %v", foundationBalance) | ||
s.Require().Equal(int64(MockDeadContractBalance), foundationBalance.AmountOf(v4.Denom).Int64()) // Add int64 cast | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package v4 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
// CreateUpgradeHandler that migrates the chain from v4.2.5 to v4.2.6 | ||
func CreateUpgradeHandler( | ||
mm *module.Manager, | ||
bankKeeper bankKeeper.Keeper, | ||
configurator module.Configurator, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { | ||
// ignore the error if any | ||
if err := migrateFundFromDeadContacts(ctx, bankKeeper); err != nil { | ||
ctx.Logger().Error("migrateFundFromDeadContacts", "error", err) | ||
} | ||
|
||
return mm.RunMigrations(ctx, configurator, fromVM) | ||
} | ||
} | ||
|
||
// migrate fund from dead contacts to foundation | ||
func migrateFundFromDeadContacts( | ||
ctx sdk.Context, | ||
bankKeeper bankKeeper.Keeper, | ||
) error { | ||
deadContractAddr := sdk.MustAccAddressFromBech32(DeadContract) | ||
foundationAddr := sdk.MustAccAddressFromBech32(Foundation) | ||
|
||
// transfer token from dead contract to foundation | ||
|
||
// Get all balances from the dead contract | ||
allBalances := bankKeeper.GetAllBalances(ctx, deadContractAddr) | ||
if allBalances.IsZero() { | ||
return nil // Return early if no balances | ||
} | ||
|
||
return bankKeeper.SendCoins(ctx, deadContractAddr, foundationAddr, allBalances) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.