Skip to content

Commit

Permalink
Add a JunoBurn module/wasm plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Aug 21, 2023
1 parent f3618c3 commit 5ee21bd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

junoburn "github.com/CosmosContracts/juno/v17/x/burn"
dripkeeper "github.com/CosmosContracts/juno/v17/x/drip/keeper"
driptypes "github.com/CosmosContracts/juno/v17/x/drip/types"
feesharekeeper "github.com/CosmosContracts/juno/v17/x/feeshare/keeper"
Expand Down Expand Up @@ -122,6 +123,7 @@ var maccPerms = map[string][]string{
tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner},
globalfee.ModuleName: nil,
buildertypes.ModuleName: nil,
junoburn.ModuleName: nil,
}

type AppKeepers struct {
Expand Down Expand Up @@ -518,6 +520,15 @@ func NewAppKeepers(
})
wasmOpts = append(wasmOpts, querierOpts)

// burnOverride := wasmkeeper.WithMessageHandlerDecorator(func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
// customBurner := burn.NewBurnerPlugin(appKeepers.BankKeeper)
// return wasmkeeper.NewBurnCoinMessageHandler(customBurner)
// })

junoBurnerPlugin := junoburn.NewBurnerPlugin(appKeepers.BankKeeper)
burnOverride := wasmkeeper.WithMessageHandler(wasmkeeper.NewBurnCoinMessageHandler(junoBurnerPlugin))
wasmOpts = append(wasmOpts, burnOverride)

appKeepers.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
appKeepers.keys[wasmtypes.StoreKey],
Expand Down
7 changes: 7 additions & 0 deletions x/burn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# x/burn

This is a 'module' used solely to burn tokens properly in line with our x/mint module requirements.

## Burn address

- juno1mj7t69y4r2adl3cnuq8y9uundkzawvx6avu7nj
31 changes: 31 additions & 0 deletions x/burn/burner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package burn

import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

// used to override Wasmd's NewBurnCoinMessageHandler

type BurnerWasmPlugin struct {
bk bankkeeper.Keeper
}

var _ wasmtypes.Burner = &BurnerWasmPlugin{}

func NewBurnerPlugin(bk bankkeeper.Keeper) *BurnerWasmPlugin {
return &BurnerWasmPlugin{bk: bk}
}

func (k *BurnerWasmPlugin) BurnCoins(_ sdk.Context, _ string, _ sdk.Coins) error {
// instead of burning, we just hold in balance and subtract from the x/mint module's total supply
// return k.bk.BurnCoins(ctx, moduleName, amt)
return nil
}

func (k *BurnerWasmPlugin) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, _ string, amt sdk.Coins) error {
// we override the default send to instead sent to the "junoburn" module. Then we subtract that from the x/mint module in its query
return k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, ModuleName, amt)
}
5 changes: 5 additions & 0 deletions x/burn/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package burn

const (
ModuleName = "junoburn"
)
7 changes: 6 additions & 1 deletion x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

junoburn "github.com/CosmosContracts/juno/v17/x/burn"
"github.com/CosmosContracts/juno/v17/x/mint/keeper"
"github.com/CosmosContracts/juno/v17/x/mint/types"
)
Expand All @@ -26,8 +27,12 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
params := k.GetParams(ctx)
currentBlock := uint64(ctx.BlockHeight())

// get juno's burn module account
acc := k.GetAccountKeeper().GetModuleAccount(ctx, junoburn.ModuleName)
totalBurnedAmt := k.GetBalance(ctx, acc.GetAddress(), params.MintDenom)

// fetch current total supply
totalSupply := k.TokenSupply(ctx, params.MintDenom)
totalSupply := k.TokenSupply(ctx, params.MintDenom).Sub(totalBurnedAmt)

// check if we need to change phase
nextPhase := minter.NextPhase(params, totalSupply)
Expand Down
12 changes: 12 additions & 0 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
stakingKeeper types.StakingKeeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
feeCollectorName string

Expand Down Expand Up @@ -47,6 +48,7 @@ func NewKeeper(
storeKey: key,
stakingKeeper: sk,
bankKeeper: bk,
accountKeeper: ak,
feeCollectorName: feeCollectorName,
authority: authority,
}
Expand Down Expand Up @@ -110,6 +112,10 @@ func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
return p
}

func (k Keeper) GetAccountKeeper() types.AccountKeeper {
return k.accountKeeper
}

// ______________________________________________________________________

// StakingTokenSupply implements an alias call to the underlying staking keeper's
Expand All @@ -124,6 +130,12 @@ func (k Keeper) TokenSupply(ctx sdk.Context, denom string) math.Int {
return k.bankKeeper.GetSupply(ctx, denom).Amount
}

// GetBalance implements an alias call to the underlying bank keeper's
// GetBalance to be used in BeginBlocker.
func (k Keeper) GetBalance(ctx sdk.Context, address sdk.AccAddress, denom string) math.Int {
return k.bankKeeper.GetBalance(ctx, address, denom).Amount
}

// BondedRatio implements an alias call to the underlying staking keeper's
// BondedRatio to be used in BeginBlocker.
func (k Keeper) BondedRatio(ctx sdk.Context) sdk.Dec {
Expand Down
1 change: 1 addition & 0 deletions x/mint/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type AccountKeeper interface {
// dependencies.
type BankKeeper interface {
GetSupply(ctx sdk.Context, denom string) sdk.Coin
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error
MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error
Expand Down

0 comments on commit 5ee21bd

Please sign in to comment.