Skip to content

Commit

Permalink
[feat] added bedrock distribution for master wallet (#1390)
Browse files Browse the repository at this point in the history
* [feat] added bedrock distribution for master wallet

* [feat] registered upgrade in app

* [fix] updated naming convention for upgrades

* [new] added readme in upgrade

* [fix] minnor fix readme.md upgrade
  • Loading branch information
abdullahjankhan1 authored Nov 21, 2022
1 parent f70432c commit 8eff3b5
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 31 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -821,6 +822,11 @@ func (app *PylonsApp) setupUpgradeHandlers() {
v4.UpgradeName,
v4.CreateUpgradeHandler(app.mm, app.configurator, app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper, &app.PylonsKeeper),
)
// v1.1.0 mainnet upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(
v5.UpgradeName,
v5.CreateUpgradeHandler(app.mm, app.configurator, app.BankKeeper, &app.AccountKeeper, &app.StakingKeeper),
)
}

func (app *PylonsApp) setupUpgradeStoreLoaders() {
Expand Down
28 changes: 28 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apptesting

import (
"fmt"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -18,6 +20,7 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Pylons-tech/pylons/app"
"github.com/Pylons-tech/pylons/x/pylons/types"
)

type KeeperTestHelper struct {
Expand Down Expand Up @@ -115,3 +118,28 @@ func CreateRandomAccounts(numAccts int) []sdk.AccAddress {

return testAddrs
}

// set up googleIAPOder
func (s *KeeperTestHelper) SetUpGoogleIAPOrder(ctx sdk.Context, n int, productID string) []types.GoogleInAppPurchaseOrder {
items := make([]types.GoogleInAppPurchaseOrder, n)
creators := types.GenTestBech32List(n)
for i := range items {
items[i].Creator = creators[i]
items[i].PurchaseToken = strconv.Itoa(i)
items[i].ProductId = productID
s.App.PylonsKeeper.AppendGoogleIAPOrder(ctx, items[i])
}
return items
}

// set up test addresses
func (s *KeeperTestHelper) SetUpTestAddrs(n int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, n)
for id := range testAddrs {
testAcc := types.GenTestBech32FromString(fmt.Sprint(id))
testAddr, err := sdk.AccAddressFromBech32(testAcc)
s.Require().NoError(err)
testAddrs[id] = testAddr
}
return testAddrs
}
27 changes: 27 additions & 0 deletions app/upgrades/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Pylons Upgrades

This folder contains sub-folders for every pylons upgrade. It also defines upgrade structs,
that each upgrade implements. These then get included in the application
`app.go` to run the upgrade.

## Version History

- v3 - migration to cosmos SDK v46
- v4 - Pre-Mainnet Upgrade
- v5 - Mainnet Upgrade

## Upgrade types

Upgrade defines a struct containing necessary fields that a `SoftwareUpgradeProposal`
must have written, in order for the state migration to go smoothly.
An upgrade must implement this `upgrade struct`, and then set it in the `app.go`.
The `app.go` will then define the `upgrade handler`.

```go
type Upgrade struct {
// Upgrade version name, for the upgrade handler, e.g. `v7`
UpgradeName string
// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades storetypes.StoreUpgrades
}
```
36 changes: 5 additions & 31 deletions app/upgrades/v4/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v4_test

import (
"fmt"
"strconv"
"testing"

"cosmossdk.io/math"
Expand Down Expand Up @@ -135,8 +134,8 @@ func (suite *UpgradeTestSuite) TestCleanUpylons() {
amountOfCoinsTest := sdk.NewCoins(sdk.NewCoin(types.PylonsCoinDenom, sdk.NewInt(10_000_000)))

//create Google IAP order and test addresses
items := suite.setUpGoogleIAPOrder(suite.Ctx, 2, productID)
testAddrs := suite.setUpTestAddrs(5)
items := suite.SetUpGoogleIAPOrder(suite.Ctx, 2, productID)
testAddrs := suite.SetUpTestAddrs(5)

for _, tc := range []struct {
desc string
Expand Down Expand Up @@ -186,40 +185,15 @@ func (suite *UpgradeTestSuite) TestCleanUpylons() {
}
}

// set up googleIAPOder
func (suite *UpgradeTestSuite) setUpGoogleIAPOrder(ctx sdk.Context, n int, productID string) []types.GoogleInAppPurchaseOrder {
items := make([]types.GoogleInAppPurchaseOrder, n)
creators := types.GenTestBech32List(n)
for i := range items {
items[i].Creator = creators[i]
items[i].PurchaseToken = strconv.Itoa(i)
items[i].ProductId = productID
suite.App.PylonsKeeper.AppendGoogleIAPOrder(ctx, items[i])
}
return items
}

// set up test addresses
func (suite *UpgradeTestSuite) setUpTestAddrs(n int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, n)
for id := range testAddrs {
testAcc := types.GenTestBech32FromString(fmt.Sprint(id))
testAddr, err := sdk.AccAddressFromBech32(testAcc)
suite.Require().NoError(err)
testAddrs[id] = testAddr
}
return testAddrs
}

func (suite *UpgradeTestSuite) TestRefundIAPNFTBUY() {
// run iap nft buy refund
suite.Setup()
productID := "pylons_10"
amountValid := 10_000_000
amountOfCoinsTest := sdk.NewCoins(sdk.NewCoin(types.PylonsCoinDenom, sdk.NewInt(10_000_000)))
// create Google IAP order and test addresses
items := suite.setUpGoogleIAPOrder(suite.Ctx, 2, productID)
testAddrs := suite.setUpTestAddrs(5)
items := suite.SetUpGoogleIAPOrder(suite.Ctx, 2, productID)
testAddrs := suite.SetUpTestAddrs(5)

// create a cookbook
creator := testAddrs[0]
Expand Down Expand Up @@ -358,7 +332,7 @@ func (suite *UpgradeTestSuite) TestRefundIAPNFTBUY() {
func (suite *UpgradeTestSuite) TestRefundLuxFloralis() {
suite.Setup()
// Make recipe executions records
testAddrs := suite.setUpTestAddrs(5)
testAddrs := suite.SetUpTestAddrs(5)
creator := testAddrs[0]
amountValid := int64(10_000_000)

Expand Down
22 changes: 22 additions & 0 deletions app/upgrades/v5/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v5

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.0"
)

// TODO: Update StoreUpgrades

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

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 (
// Master Wallet address
MasterWallet = "pylo1vnwhaymaazugzz9ln2sznddveyed6shz3x8xwl"
)

var (
TotalUbedrock = math.NewIntFromUint64(1_000_000_000_000_000) // 1 bedrock = 1_000_000 ubedrock
MasterWalletbalance = math.NewIntFromUint64(1e15)
)

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()

if types.IsMainnet(ctx.ChainID()) {

bankBaseKeeper, _ := bankKeeper.(bankkeeper.BaseKeeper)
BurnToken(ctx, accKeeper, &bankBaseKeeper, staking)
MintUbedrockForInitialAccount(ctx, &bankBaseKeeper, staking)
}
return mm.RunMigrations(ctx, configurator, fromVM)
}
}

// Burn stripeUSD denom token
func BurnToken(ctx sdk.Context, accKeeper *authkeeper.AccountKeeper, bank *bankkeeper.BaseKeeper, staking *stakingkeeper.Keeper) {
// only burn stripe usd token
denom := types.StripeCoinDenom
// Get all account balances
accs := bank.GetAccountsBalances(ctx)
for _, acc := range accs {
balance := acc.Coins.AmountOf(denom)
// Check if denom token amount GT 0
if balance.GT(math.ZeroInt()) {
amount := sdk.NewCoin(denom, balance)
// Send denom token to module
err := bank.SendCoinsFromAccountToModule(ctx, sdk.MustAccAddressFromBech32(acc.Address), 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)
}
}
}
}

// Mint ubedrock for master wallet
func MintUbedrockForInitialAccount(ctx sdk.Context, bank *bankkeeper.BaseKeeper, staking *stakingkeeper.Keeper) {
// Get currect balance of master wallet address
balance := bank.GetBalance(ctx, sdk.MustAccAddressFromBech32(MasterWallet), types.StakingCoinDenom)

// check difference in amount to add
toAdd := MasterWalletbalance.Sub(balance.Amount)

// Mint coin for module
err := bank.MintCoins(ctx, types.PaymentsProcessorName, sdk.NewCoins(sdk.NewCoin(types.StakingCoinDenom, toAdd)))
if err != nil {
panic(err)
}
// Send coin required to meet master wallet balance from module to account
err = bank.SendCoinsFromModuleToAccount(
ctx,
types.PaymentsProcessorName,
sdk.MustAccAddressFromBech32(MasterWallet),
sdk.NewCoins(sdk.NewCoin(types.StakingCoinDenom, toAdd)),
)
if err != nil {
panic(err)
}
}
57 changes: 57 additions & 0 deletions app/upgrades/v5/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package v5_test

import (
"testing"

"cosmossdk.io/math"
"github.com/Pylons-tech/pylons/app/apptesting"
v5 "github.com/Pylons-tech/pylons/app/upgrades/v5"
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"
stripeCoinDenom string = "ustripeusd"
defaultAcctFundsStripeCoin sdk.Coins = sdk.NewCoins(
sdk.NewCoin(stripeCoinDenom, 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_Ustripeusd() {
suite.Setup()
// Fund Ustripeusd to test account
for _, acc := range suite.TestAccs {
suite.FundAcc(acc, defaultAcctFundsStripeCoin)
}
// Get Ustripeusd total supply
totalAmount := suite.App.BankKeeper.GetSupply(suite.Ctx, stripeCoinDenom)
suite.Require().Equal(totalAmount.Amount, math.NewInt(30_000_000))
// Burn Ustripeusd
bankBaseKeeper, _ := suite.App.BankKeeper.(bankkeeper.BaseKeeper)
v5.BurnToken(suite.Ctx, &suite.App.AccountKeeper, &bankBaseKeeper, &suite.App.StakingKeeper)
// Check Ustripeusd total supply (should equal 0)
totalAmount = suite.App.BankKeeper.GetSupply(suite.Ctx, stripeCoinDenom)
suite.Require().Equal(totalAmount.Amount, math.ZeroInt())
}

func (suite *UpgradeTestSuite) TestMintUbedrockForInitialAccount() {
suite.Setup()
// Burn ubedrock
bankBaseKeeper, _ := suite.App.BankKeeper.(bankkeeper.BaseKeeper)
// Mint ubedrock for initial account
v5.MintUbedrockForInitialAccount(suite.Ctx, &bankBaseKeeper, &suite.App.StakingKeeper)
// Check token in all initial account
accAmount := suite.App.BankKeeper.GetBalance(suite.Ctx, sdk.MustAccAddressFromBech32(v5.MasterWallet), stakingCoinDenom)
suite.Require().Equal(accAmount.Amount, v5.MasterWalletbalance)
}

0 comments on commit 8eff3b5

Please sign in to comment.