Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade v7 added: burn bedrock token #1707

Merged
merged 9 commits into from
Dec 16, 2022
14 changes: 10 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
evidencekeeper "github.com/cosmos/cosmos-sdk/x/evidence/keeper"

"github.com/Pylons-tech/pylons/app/upgrades"
v3 "github.com/Pylons-tech/pylons/app/upgrades/v3"
v4 "github.com/Pylons-tech/pylons/app/upgrades/v4"
v5 "github.com/Pylons-tech/pylons/app/upgrades/v5"
v6 "github.com/Pylons-tech/pylons/app/upgrades/v6"
v7 "github.com/Pylons-tech/pylons/app/upgrades/mainnet/v7"
v3 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v3"
v4 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v4"
v5 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v5"
v6 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v6"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -834,6 +835,11 @@ func (app *PylonsApp) setupUpgradeHandlers() {
v6.UpgradeName,
v6.CreateUpgradeHandler(app.mm, app.configurator, app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper),
)
// v1.1.2 mainnet upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v7.UpgradeName,
v7.CreateUpgradeHandler(app.mm, app.configurator, app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper),
)
}

func (app *PylonsApp) setupUpgradeStoreLoaders() {
Expand Down
22 changes: 22 additions & 0 deletions app/upgrades/mainnet/v7/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v7

import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/Pylons-tech/pylons/app/upgrades"
)

const (
// UpgradeName is the shared upgrade plan name for mainnet and testnet
UpgradeName = "v1.1.2"
)

// TODO: Update StoreUpgrades

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
StoreUpgrades: storetypes.StoreUpgrades{
Added: []string{},
Deleted: []string{},
},
}
94 changes: 94 additions & 0 deletions app/upgrades/mainnet/v7/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package v7

import (
"cosmossdk.io/math"
"github.com/Pylons-tech/pylons/x/pylons/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const (
// mainnet master wallet address
MasterWallet = "pylo1vnwhaymaazugzz9ln2sznddveyed6shz3x8xwl"
MaxSupply = 1_000_000_000_000_000
faisalnaveedRNS marked this conversation as resolved.
Show resolved Hide resolved
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
bankKeeper bankkeeper.Keeper,
accKeeper *authkeeper.AccountKeeper,
staking *stakingkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// logger := ctx.Logger()

bankBaseKeeper, _ := bankKeeper.(bankkeeper.BaseKeeper)
if types.IsMainnet(ctx.ChainID()) {
BurnToken(ctx, accKeeper, &bankBaseKeeper, staking)
}

vm, err := mm.RunMigrations(ctx, configurator, fromVM)
return vm, err
}
}

// Burn bedrock denom token
func BurnToken(ctx sdk.Context, accKeeper *authkeeper.AccountKeeper, bank *bankkeeper.BaseKeeper, staking *stakingkeeper.Keeper) {
// only burn bedrock token
denom := types.StakingCoinDenom
// Get all delegations
delegations := staking.GetAllDelegations(ctx)
// Get all account balances
accs := bank.GetAccountsBalances(ctx)
for _, acc := range accs {
found := false
balance := acc.Coins.AmountOf(denom)
// Check if denom token amount GT 0
if balance.GT(math.ZeroInt()) {
for _, delegator := range delegations {
// Check if account address is equal to delegator address, if equal do nothing
if acc.Address == delegator.DelegatorAddress || acc.Address == MasterWallet {
found = true
break
} else {
// If account address address is not equal to delegator address burn token
found = false
}
}
if !found {
amount := sdk.NewCoin(denom, balance)
BurnCoins(ctx, bank, acc.Address, amount)
}
}
}

supply := bank.GetSupply(ctx, denom)
maxSupply := math.NewInt(MaxSupply)
if supply.Amount.GT(maxSupply) {

extraSupply := supply.Amount.Sub(maxSupply)
extraCoins := sdk.NewCoin(denom, extraSupply)
BurnCoins(ctx, bank, MasterWallet, extraCoins)

}

}

func BurnCoins(ctx sdk.Context, bank *bankkeeper.BaseKeeper, acc string, amount sdk.Coin) {
// Send denom token to module
err := bank.SendCoinsFromAccountToModule(ctx, sdk.MustAccAddressFromBech32(acc), types.PaymentsProcessorName, sdk.NewCoins(amount))
if err != nil {
panic(err)
}
// Burn denom token in module
err = bank.BurnCoins(ctx, types.PaymentsProcessorName, sdk.NewCoins(amount))
if err != nil {
panic(err)
}

}
45 changes: 45 additions & 0 deletions app/upgrades/mainnet/v7/upgrage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v7_test

import (
"testing"

"cosmossdk.io/math"
"github.com/Pylons-tech/pylons/app/apptesting"
v7 "github.com/Pylons-tech/pylons/app/upgrades/mainnet/v7"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/stretchr/testify/suite"
)

var (
stakingCoinDenom string = "ubedrock"
defaultAcctFundsBedrockCoin sdk.Coins = sdk.NewCoins(
sdk.NewCoin(stakingCoinDenom, sdk.NewInt(10_000_000)),
)
)

type UpgradeTestSuite struct {
apptesting.KeeperTestHelper
}

func TestUpgradeTestSuite(t *testing.T) {
s := new(UpgradeTestSuite)
suite.Run(t, s)
}

func (suite *UpgradeTestSuite) TestBurnToken_Ubedrock() {
suite.Setup()
// Fund ubedrock to test account
for _, acc := range suite.TestAccs {
suite.FundAcc(acc, defaultAcctFundsBedrockCoin)
}
// Get ubedrock total supply
totalAmount := suite.App.BankKeeper.GetSupply(suite.Ctx, stakingCoinDenom)
suite.Require().Equal(totalAmount.Amount, math.NewInt(31_000_000))
// Burn ubedrock
bankBaseKeeper, _ := suite.App.BankKeeper.(bankkeeper.BaseKeeper)
v7.BurnToken(suite.Ctx, &suite.App.AccountKeeper, &bankBaseKeeper, &suite.App.StakingKeeper)
// Check ubedrock total supply (should equal 0)
totalAmount = suite.App.BankKeeper.GetSupply(suite.Ctx, stakingCoinDenom)
suite.Require().Equal(totalAmount.Amount, math.ZeroInt())
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/Pylons-tech/pylons/app"
v4 "github.com/Pylons-tech/pylons/app/upgrades/v4"
v4 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v4"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"cosmossdk.io/math"
"github.com/Pylons-tech/pylons/app/apptesting"
v4 "github.com/Pylons-tech/pylons/app/upgrades/v4"
v4 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v4"
"github.com/Pylons-tech/pylons/x/pylons/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"cosmossdk.io/math"
"github.com/Pylons-tech/pylons/app/apptesting"
v5 "github.com/Pylons-tech/pylons/app/upgrades/v5"
v5 "github.com/Pylons-tech/pylons/app/upgrades/testnet/v5"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/stretchr/testify/suite"
Expand Down
File renamed without changes.
File renamed without changes.