Repository navigation
Keep contract burn_tokens working after the tokenfactory removal #4549
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
Merged
+164
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce7cfe2
Keep contract burn_tokens working after the tokenfactory removal
alexander-sei 66e6436
Add the changelog entry for #4549
alexander-sei c835f7c
Refuse a burn_tokens message that leaves out the amount
alexander-sei cceb21b
Merge branch 'main' into alex/legacy-burn-tokens-alias
alexander-sei ae97af9
Merge branch 'main' into alex/legacy-burn-tokens-alias
masih File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,48 @@ | ||
| package wasmbinding | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strings" | ||
|
|
||
| sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" | ||
| sdkerrors "github.com/sei-protocol/sei-chain/sei-cosmos/types/errors" | ||
| wasmkeeper "github.com/sei-protocol/sei-chain/sei-wasmd/x/wasm/keeper" | ||
| wasmtypes "github.com/sei-protocol/sei-chain/sei-wasmd/x/wasm/types" | ||
| wasmvmtypes "github.com/sei-protocol/sei-chain/sei-wasmvm/types" | ||
| ) | ||
|
|
||
| // legacyBurnTokensMsg is the burn_tokens custom message of the retired tokenfactory module. | ||
| type legacyBurnTokensMsg struct { | ||
| BurnTokens *struct { | ||
| Amount sdk.Coin `json:"amount"` | ||
| } `json:"burn_tokens"` | ||
| } | ||
|
|
||
| // LegacyBurnTokensHandler burns coins from the calling contract's own balance for the | ||
| // retired tokenfactory burn_tokens custom message. It accepts only denoms the contract | ||
| // created, of the form factory/<contract>/<subdenom>, and leaves every other message to | ||
| // the next handler. | ||
| func LegacyBurnTokensHandler(burner wasmtypes.Burner) wasmkeeper.MessageHandlerFunc { | ||
| return func(ctx sdk.Context, contractAddr sdk.AccAddress, _ string, msg wasmvmtypes.CosmosMsg, _ wasmvmtypes.MessageInfo, _ wasmtypes.CodeInfo) ([]sdk.Event, [][]byte, error) { | ||
| var parsed legacyBurnTokensMsg | ||
| if msg.Custom == nil || json.Unmarshal(msg.Custom, &parsed) != nil || parsed.BurnTokens == nil { | ||
| return nil, nil, wasmtypes.ErrUnknownMsg | ||
| } | ||
| amount := parsed.BurnTokens.Amount | ||
| // Validate dereferences the amount, which is nil when the message leaves it out. | ||
| if amount.Amount.IsNil() || amount.Validate() != nil || !amount.IsPositive() { | ||
| return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "burn_tokens amount %s", amount) | ||
| } | ||
| if !strings.HasPrefix(amount.Denom, "factory/"+contractAddr.String()+"/") { | ||
| return nil, nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s was not created by %s", amount.Denom, contractAddr) | ||
| } | ||
| coins := sdk.NewCoins(amount) | ||
| if err := burner.SendCoinsFromAccountToModule(ctx, contractAddr, wasmtypes.ModuleName, coins); err != nil { | ||
| return nil, nil, sdkerrors.Wrap(err, "transfer to module") | ||
| } | ||
| if err := burner.BurnCoins(ctx, wasmtypes.ModuleName, coins); err != nil { | ||
| return nil, nil, sdkerrors.Wrap(err, "burn coins") | ||
| } | ||
| return nil, nil, nil | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,112 @@ | ||
| package wasmbinding | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" | ||
| sdkerrors "github.com/sei-protocol/sei-chain/sei-cosmos/types/errors" | ||
| wasmtypes "github.com/sei-protocol/sei-chain/sei-wasmd/x/wasm/types" | ||
| wasmvmtypes "github.com/sei-protocol/sei-chain/sei-wasmvm/types" | ||
| "github.com/sei-protocol/sei-chain/wasmbinding" | ||
| minttypes "github.com/sei-protocol/sei-chain/x/mint/types" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| var legacyBurnContract = sdk.AccAddress("legacy-burn-contract") | ||
|
|
||
| // legacyBurnTokens returns the burn_tokens custom message a contract built on | ||
| // sei-cosmwasm sends for amount of denom. | ||
| func legacyBurnTokens(denom, amount string) wasmvmtypes.CosmosMsg { | ||
| return wasmvmtypes.CosmosMsg{Custom: []byte(fmt.Sprintf(`{"burn_tokens":{"amount":{"denom":%q,"amount":%q}}}`, denom, amount))} | ||
| } | ||
|
|
||
| func TestLegacyBurnTokensBurnsTheContractsOwnBalance(t *testing.T) { | ||
| testWrapper, _ := SetupWasmbindingTest(t) | ||
| ctx, bank := testWrapper.Ctx, testWrapper.App.BankKeeper | ||
| denom := "factory/" + legacyBurnContract.String() + "/stake" | ||
| mintLegacyBurnBalance(t, bank, ctx, sdk.NewInt64Coin(denom, 100)) | ||
|
|
||
| _, _, err := wasmbinding.LegacyBurnTokensHandler(bank). | ||
| DispatchMsg(ctx, legacyBurnContract, "", legacyBurnTokens(denom, "40"), wasmvmtypes.MessageInfo{}, wasmtypes.CodeInfo{}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, sdk.NewInt64Coin(denom, 60), bank.GetBalance(ctx, legacyBurnContract, denom)) | ||
| require.Equal(t, sdk.NewInt64Coin(denom, 60), bank.GetSupply(ctx, denom)) | ||
| } | ||
|
|
||
| func TestLegacyBurnTokensRefusesWhatItCannotBurn(t *testing.T) { | ||
| testWrapper, _ := SetupWasmbindingTest(t) | ||
| ctx, bank := testWrapper.Ctx, testWrapper.App.BankKeeper | ||
| own := "factory/" + legacyBurnContract.String() + "/stake" | ||
| foreign := "factory/" + sdk.AccAddress("another-denom-creatr").String() + "/stake" | ||
| mintLegacyBurnBalance(t, bank, ctx, sdk.NewInt64Coin(own, 100)) | ||
| mintLegacyBurnBalance(t, bank, ctx, sdk.NewInt64Coin(foreign, 100)) | ||
| mintLegacyBurnBalance(t, bank, ctx, sdk.NewInt64Coin("usei", 100)) | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| msg wasmvmtypes.CosmosMsg | ||
| error *sdkerrors.Error | ||
| }{ | ||
| {"another creator's denom", legacyBurnTokens(foreign, "1"), sdkerrors.ErrUnauthorized}, | ||
| {"a native denom", legacyBurnTokens("usei", "1"), sdkerrors.ErrUnauthorized}, | ||
| {"a zero amount", legacyBurnTokens(own, "0"), sdkerrors.ErrInvalidCoins}, | ||
| {"a missing amount", wasmvmtypes.CosmosMsg{Custom: []byte(fmt.Sprintf(`{"burn_tokens":{"amount":{"denom":%q}}}`, own))}, sdkerrors.ErrInvalidCoins}, | ||
| {"more than the balance", legacyBurnTokens(own, "101"), sdkerrors.ErrInsufficientFunds}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, _, err := wasmbinding.LegacyBurnTokensHandler(bank). | ||
| DispatchMsg(ctx, legacyBurnContract, "", tc.msg, wasmvmtypes.MessageInfo{}, wasmtypes.CodeInfo{}) | ||
| require.ErrorIs(t, err, tc.error) | ||
| }) | ||
| } | ||
| for _, denom := range []string{own, foreign, "usei"} { | ||
| require.Equal(t, sdk.NewInt64Coin(denom, 100), bank.GetBalance(ctx, legacyBurnContract, denom), denom) | ||
| } | ||
| } | ||
|
|
||
| func TestLegacyBurnTokensLeavesOtherMessagesToTheNextHandler(t *testing.T) { | ||
| testWrapper, _ := SetupWasmbindingTest(t) | ||
| handler := wasmbinding.LegacyBurnTokensHandler(testWrapper.App.BankKeeper) | ||
| for _, msg := range []wasmvmtypes.CosmosMsg{ | ||
| {}, | ||
| {Bank: &wasmvmtypes.BankMsg{Burn: &wasmvmtypes.BurnMsg{Amount: wasmvmtypes.Coins{{Denom: "usei", Amount: "1"}}}}}, | ||
| {Custom: []byte(`{"mint_tokens":{"amount":{"denom":"factory/creator/subdenom","amount":"1"}}}`)}, | ||
| {Custom: []byte(`{"call_evm":{"to":"0x0000000000000000000000000000000000000001","data":""}}`)}, | ||
| {Custom: []byte(`not json`)}, | ||
| } { | ||
| _, _, err := handler.DispatchMsg(testWrapper.Ctx, legacyBurnContract, "", msg, wasmvmtypes.MessageInfo{}, wasmtypes.CodeInfo{}) | ||
| require.ErrorIs(t, err, wasmtypes.ErrUnknownMsg, string(msg.Custom)) | ||
| } | ||
| } | ||
|
|
||
| // TestCustomMessageHandlerServesLegacyBurnTokens requires the Sei Wasm message | ||
| // handler to burn for burn_tokens while still refusing the other retired | ||
| // tokenfactory messages. | ||
| func TestCustomMessageHandlerServesLegacyBurnTokens(t *testing.T) { | ||
| testWrapper, _ := SetupWasmbindingTest(t) | ||
| ctx, bank := testWrapper.Ctx, testWrapper.App.BankKeeper | ||
| denom := "factory/" + legacyBurnContract.String() + "/stake" | ||
| mintLegacyBurnBalance(t, bank, ctx, sdk.NewInt64Coin(denom, 100)) | ||
| messenger := wasmbinding.CustomMessageHandler( | ||
| testWrapper.App.MsgServiceRouter(), bank, &testWrapper.App.EvmKeeper, testWrapper.App.AppCodec()) | ||
|
|
||
| _, _, err := messenger.DispatchMsg(ctx, legacyBurnContract, "", legacyBurnTokens(denom, "25"), wasmvmtypes.MessageInfo{}, wasmtypes.CodeInfo{}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, sdk.NewInt64Coin(denom, 75), bank.GetSupply(ctx, denom)) | ||
|
|
||
| mint := wasmvmtypes.CosmosMsg{Custom: []byte(fmt.Sprintf(`{"mint_tokens":{"amount":{"denom":%q,"amount":"1"}}}`, denom))} | ||
| _, _, err = messenger.DispatchMsg(ctx, legacyBurnContract, "", mint, wasmvmtypes.MessageInfo{}, wasmtypes.CodeInfo{}) | ||
| require.Equal(t, wasmvmtypes.UnsupportedRequest{Kind: "Unknown Sei Wasm Message"}, err) | ||
| require.Equal(t, sdk.NewInt64Coin(denom, 75), bank.GetSupply(ctx, denom)) | ||
| } | ||
|
|
||
| func mintLegacyBurnBalance(t *testing.T, bank interface { | ||
| MintCoins(sdk.Context, string, sdk.Coins) error | ||
| SendCoinsFromModuleToAccount(sdk.Context, string, sdk.AccAddress, sdk.Coins) error | ||
| }, ctx sdk.Context, coin sdk.Coin) { | ||
| t.Helper() | ||
| coins := sdk.NewCoins(coin) | ||
| require.NoError(t, bank.MintCoins(ctx, minttypes.ModuleName, coins)) | ||
| require.NoError(t, bank.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, legacyBurnContract, coins)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.