Skip to content

Commit

Permalink
[v18] Juno x/burn module (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Sep 23, 2023
1 parent b03500a commit 8473deb
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 79 deletions.
1 change: 1 addition & 0 deletions .github/workflows/interchaintest-E2E.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
- "ictest-unity-gov"
- "ictest-pob"
- "ictest-drip"
- "ictest-burn"
- "ictest-clock"
fail-fast: false

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ ictest-pob: rm-testcache
ictest-drip: rm-testcache
cd interchaintest && go test -race -v -run TestJunoDrip .

ictest-burn: rm-testcache
cd interchaintest && go test -race -v -run TestJunoBurnModule .

ictest-clock: rm-testcache
cd interchaintest && go test -race -v -run TestJunoClock .

Expand Down
8 changes: 7 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,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/v18/x/burn"
clockkeeper "github.com/CosmosContracts/juno/v18/x/clock/keeper"
clocktypes "github.com/CosmosContracts/juno/v18/x/clock/types"
dripkeeper "github.com/CosmosContracts/juno/v18/x/drip/keeper"
Expand Down Expand Up @@ -121,10 +122,11 @@ var maccPerms = map[string][]string{
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
icatypes.ModuleName: nil,
ibcfeetypes.ModuleName: nil,
wasmtypes.ModuleName: {authtypes.Burner},
wasmtypes.ModuleName: {},
tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner},
globalfee.ModuleName: nil,
buildertypes.ModuleName: nil,
junoburn.ModuleName: {authtypes.Burner},
}

type AppKeepers struct {
Expand Down Expand Up @@ -526,6 +528,10 @@ func NewAppKeepers(
})
wasmOpts = append(wasmOpts, querierOpts)

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

appKeepers.WasmKeeper = wasmkeeper.NewKeeper(
appCodec,
appKeepers.keys[wasmtypes.StoreKey],
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module github.com/CosmosContracts/juno/v18

go 1.21

toolchain go1.21.0
// heighliner does not like.
// toolchain go1.21.0

require (
cosmossdk.io/api v0.3.1
Expand Down
77 changes: 0 additions & 77 deletions interchaintest/contract_unity_submit_test.go

This file was deleted.

1 change: 1 addition & 0 deletions interchaintest/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ A list of the contracts here which are pre-compiled in other repos.
> cw_template -> <https://github.com/CosmWasm/cw-template>
> cw_unity_prop -> <https://github.com/CosmosContracts/cw-unity-prop> v0.3.4-alpha
> ibchooks_counter.wasm -> <https://github.com/osmosis-labs/osmosis/blob/64393a14e18b2562d72a3892eec716197a3716c7/tests/ibc-hooks/bytecode/counter.wasm>
> cw_testburn.wasm -> <https://github.com/Reecepbcups/cw-testburn>
> clock_example.wasm -> <https://github.com/Reecepbcups/cw-clock-example>
Binary file added interchaintest/contracts/cw_testburn.wasm
Binary file not shown.
60 changes: 60 additions & 0 deletions interchaintest/module_burn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package interchaintest

import (
"fmt"
"strconv"
"testing"

"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/stretchr/testify/assert"

helpers "github.com/CosmosContracts/juno/tests/interchaintest/helpers"
)

// TestJunoBurnModule ensures the junoburn module register and execute sharing functions work properly on smart contracts.
// This is required due to how x/mint handles minting tokens for the target supply.
// It is purely for developers ::BurnTokens to function as expected.
func TestJunoBurnModule(t *testing.T) {
t.Parallel()

// Base setup
chains := CreateThisBranchChain(t, 1, 0)
ic, ctx, _, _ := BuildInitialChain(t, chains)

// Chains
juno := chains[0].(*cosmos.CosmosChain)

nativeDenom := juno.Config().Denom

// Users
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", int64(10_000_000), juno, juno)
user := users[0]

// Upload & init contract
_, contractAddr := helpers.SetupContract(t, ctx, juno, user.KeyName(), "contracts/cw_testburn.wasm", `{}`)

// get balance before execute
balance, err := juno.GetBalance(ctx, user.FormattedAddress(), nativeDenom)
if err != nil {
t.Fatal(err)
}

// execute burn of tokens
burnAmt := int64(1_000_000)
helpers.ExecuteMsgWithAmount(t, ctx, juno, user, contractAddr, strconv.Itoa(int(burnAmt))+nativeDenom, `{"burn_token":{}}`)

// verify it is down 1_000_000 tokens since the burn
updatedBal, err := juno.GetBalance(ctx, user.FormattedAddress(), nativeDenom)
if err != nil {
t.Fatal(err)
}

// Verify the funds were sent, and burned.
fmt.Println(balance, updatedBal)
assert.Equal(t, burnAmt, balance-updatedBal, fmt.Sprintf("balance should be %d less than updated balance", burnAmt))

t.Cleanup(func() {
_ = ic.Close()
})
}
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
50 changes: 50 additions & 0 deletions x/burn/burner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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"

mintkeeper "github.com/CosmosContracts/juno/v18/x/mint/keeper"
)

// used to override Wasmd's NewBurnCoinMessageHandler

type BurnerWasmPlugin struct {
bk bankkeeper.Keeper
mk mintkeeper.Keeper
}

var _ wasmtypes.Burner = &BurnerWasmPlugin{}

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

func (k *BurnerWasmPlugin) BurnCoins(ctx sdk.Context, _ string, amt sdk.Coins) error {
// first, try to burn the coins on bank module
err := k.bk.BurnCoins(ctx, ModuleName, amt)
if err != nil {
return err
}

// get mint params
params := k.mk.GetParams(ctx)

// loop the burned coins
for _, amount := range amt {
// if we are burning mint denom, reduce the target staking supply
if amount.Denom == params.MintDenom {
if err := k.mk.ReduceTargetSupply(ctx, amount); err != nil {
return err
}
}
}

return nil
}

func (k *BurnerWasmPlugin) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, _ string, amt sdk.Coins) error {
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"
)
26 changes: 26 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 All @@ -141,6 +153,20 @@ func (k Keeper) MintCoins(ctx sdk.Context, newCoins sdk.Coins) error {
return k.bankKeeper.MintCoins(ctx, types.ModuleName, newCoins)
}

func (k Keeper) ReduceTargetSupply(ctx sdk.Context, burnCoin sdk.Coin) error {
params := k.GetParams(ctx)

if burnCoin.Denom != params.MintDenom {
return fmt.Errorf("tried reducing target supply with non staking token")
}

minter := k.GetMinter(ctx)
minter.TargetSupply = minter.TargetSupply.Sub(burnCoin.Amount)
k.SetMinter(ctx, minter)

return nil
}

// AddCollectedFees implements an alias call to the underlying supply keeper's
// AddCollectedFees to be used in BeginBlocker.
func (k Keeper) AddCollectedFees(ctx sdk.Context, fees sdk.Coins) error {
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 8473deb

Please sign in to comment.