Skip to content

Commit

Permalink
chore: cleanup wasm_hook.go
Browse files Browse the repository at this point in the history
Updated few comments and simplified the code.
  • Loading branch information
robert-zaremba authored Feb 27, 2024
1 parent f055ce5 commit 8373d09
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions modules/ibc-hooks/wasm_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ func jsonStringHasKey(memo, key string) (found bool, jsonObject map[string]inter
// If the key doesn't exist, there's nothing to do on this hook. Continue by passing the packet
// down the stack
_, ok := jsonObject[key]
if !ok {
return false, jsonObject
}

return true, jsonObject
return ok, jsonObject
}

func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, contractAddr sdk.AccAddress, msgBytes []byte, err error) {
Expand All @@ -181,7 +177,6 @@ func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, cont
fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, "wasm metadata is not a valid JSON map object")
}

// Get the contract
contract, ok := wasm["contract"].(string)
if !ok {
// The tokens will be returned
Expand All @@ -195,19 +190,16 @@ func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, cont
fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] is not a valid bech32 address`)
}

// The contract and the receiver should be the same for the packet to be valid
if contract != receiver {
return isWasmRouted, sdk.AccAddress{}, nil,
fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] should be the same as the receiver of the packet`)
}

// Ensure the message key is provided
if wasm["msg"] == nil {
return isWasmRouted, sdk.AccAddress{}, nil,
fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `Could not find key wasm["msg"]`)
}

// Make sure the msg key is a map. If it isn't, return an error
_, ok = wasm["msg"].(map[string]interface{})
if !ok {
return isWasmRouted, sdk.AccAddress{}, nil,
Expand Down Expand Up @@ -236,7 +228,8 @@ func (h WasmHooks) SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap
return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) // continue
}

// We remove the callback metadata from the memo as it has already been processed.
// We remove the meta.callback provides instructions for post-send processing. This instruction are saved
// in the keepr (at the end of this function), and at the end - it is set as as it has already been processed.

// If the only available key in the memo is the callback, we should remove the memo
// from the data completely so the packet is sent without it.
Expand Down Expand Up @@ -411,9 +404,9 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string {

var denom string
if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) {
// remove prefix added by sender chain
// if we receive back a token, that was originally sent from "this" chain, then we need to remove
// prefix added by the sender chain: port/channel/base_denom -> base_denom.
voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())

unprefixedDenom := data.Denom[len(voucherPrefix):]

// coin denomination used in sending from the escrow address
Expand All @@ -422,7 +415,7 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string {
// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path != "" {
if !denomTrace.IsNativeDenom() {
denom = denomTrace.IBCDenom()
}
} else {
Expand Down

0 comments on commit 8373d09

Please sign in to comment.