-
Notifications
You must be signed in to change notification settings - Fork 608
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
Tokenfactory: Add Before send hooks #4382
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/osmosis-labs/osmosis/v14/x/tokenfactory/types" | ||
|
||
wasmKeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
) | ||
|
||
func (k Keeper) setBeforeSendHook(ctx sdk.Context, denom string, cosmwasmAddress string) error { | ||
// verify that denom is an x/tokenfactory denom | ||
_, _, err := types.DeconstructDenom(denom) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store := k.GetDenomPrefixStore(ctx, denom) | ||
|
||
// delete the store for denom prefix store when cosmwasm address is nil | ||
if cosmwasmAddress == "" { | ||
store.Delete([]byte(types.BeforeSendHookAddressPrefixKey)) | ||
return nil | ||
} | ||
|
||
_, err = sdk.AccAddressFromBech32(cosmwasmAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store.Set([]byte(types.BeforeSendHookAddressPrefixKey), []byte(cosmwasmAddress)) | ||
|
||
return nil | ||
} | ||
|
||
func (k Keeper) GetBeforeSendHook(ctx sdk.Context, denom string) string { | ||
store := k.GetDenomPrefixStore(ctx, denom) | ||
|
||
bz := store.Get([]byte(types.BeforeSendHookAddressPrefixKey)) | ||
if bz == nil { | ||
return "" | ||
} | ||
|
||
return string(bz) | ||
} | ||
|
||
func CWCoinsFromSDKCoins(in sdk.Coins) wasmvmtypes.Coins { | ||
var cwCoins wasmvmtypes.Coins | ||
for _, coin := range in { | ||
cwCoins = append(cwCoins, CWCoinFromSDKCoin(coin)) | ||
} | ||
return cwCoins | ||
} | ||
|
||
func CWCoinFromSDKCoin(in sdk.Coin) wasmvmtypes.Coin { | ||
return wasmvmtypes.Coin{ | ||
Denom: in.GetDenom(), | ||
Amount: in.Amount.String(), | ||
} | ||
} | ||
|
||
// Hooks wrapper struct for bank keeper | ||
type Hooks struct { | ||
k Keeper | ||
wasmkeeper wasmKeeper.Keeper | ||
} | ||
|
||
var _ types.BankHooks = Hooks{} | ||
|
||
// Return the wrapper struct | ||
func (k Keeper) Hooks(wasmkeeper wasmKeeper.Keeper) Hooks { | ||
return Hooks{k, wasmkeeper} | ||
} | ||
|
||
// implements BeforeSend hook in the Bank module. | ||
// Calls the stored before send hook for the denom specificed. | ||
func (h Hooks) BeforeSend(ctx sdk.Context, from, to sdk.AccAddress, amount sdk.Coins) error { | ||
cwCoins := CWCoinsFromSDKCoins(amount) | ||
|
||
for _, coin := range amount { | ||
cosmwasmAddress := h.k.GetBeforeSendHook(ctx, coin.Denom) | ||
if cosmwasmAddress != "" { | ||
cwAddr, err := sdk.AccAddressFromBech32(cosmwasmAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := types.SudoMsg{ | ||
BeforeSend: types.BeforeSendMsg{ | ||
From: from.String(), | ||
To: to.String(), | ||
Amount: cwCoins, | ||
}, | ||
} | ||
|
||
msgBz, err := json.Marshal(msg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
em := sdk.NewEventManager() | ||
|
||
_, err = h.wasmkeeper.Sudo(ctx.WithEventManager(em), cwAddr, msgBz) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to think about gas increase expectations for bank sends with tokenfactory denoms as the contract can consume unbounded gas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think its a possible option to gate usage of gas here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, we've been discussing something similar for wasm-hooks: (#4195). For the ibc case, I think non-atomic multimsg is enough, but for send hooks I think we may want to have a hard-cap on how much gas those hooks are allowed to take (something like: https://github.com/cosmos/ibc-go/blob/main/modules/core/keeper/msg_server.go#L451) and have users submit with that limit. There is no risk to the chain here though, it's more of a UX problem. If a contract (by malice or incompetence) has an infinite loop or just very high gas usage, users may need to submit their tx with increasing gas limits There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see I see, sounds fine for now for the UX tradeoff of gas problem, going ahead and merging this for now |
||
fmt.Println(err) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not related to the send hooks, right? Do we need these mint/burn changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! Srry meant to address this in a different PR must have been pushed together. Fixed