-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from White-Whale-Defi-Platform/ibc-hooks
IBC Hooks
- Loading branch information
Showing
34 changed files
with
1,026 additions
and
109 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,72 @@ | ||
package osmoutils | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" | ||
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" | ||
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// NewEmitErrorAcknowledgement creates a new error acknowledgement after having emitted an event with the | ||
// details of the error. | ||
func NewEmitErrorAcknowledgement(ctx sdk.Context, err error, errorContexts ...string) channeltypes.Acknowledgement { | ||
attributes := make([]sdk.Attribute, len(errorContexts)+1) | ||
attributes[0] = sdk.NewAttribute("error", err.Error()) | ||
for i, s := range errorContexts { | ||
attributes[i+1] = sdk.NewAttribute("error-context", s) | ||
} | ||
|
||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
sdk.NewEvent( | ||
"ibc-acknowledgement-error", | ||
attributes..., | ||
), | ||
}) | ||
|
||
return channeltypes.NewErrorAcknowledgement(err) | ||
} | ||
|
||
// MustExtractDenomFromPacketOnRecv takes a packet with a valid ICS20 token data in the Data field and returns the | ||
// denom as represented in the local chain. | ||
// If the data cannot be unmarshalled this function will panic | ||
func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { | ||
var data transfertypes.FungibleTokenPacketData | ||
if err := json.Unmarshal(packet.GetData(), &data); err != nil { | ||
panic("unable to unmarshal ICS20 packet data") | ||
} | ||
|
||
var denom string | ||
if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) { | ||
// remove prefix added by sender chain | ||
voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) | ||
|
||
unprefixedDenom := data.Denom[len(voucherPrefix):] | ||
|
||
// coin denomination used in sending from the escrow address | ||
denom = unprefixedDenom | ||
|
||
// 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 != "" { | ||
denom = denomTrace.IBCDenom() | ||
} | ||
} else { | ||
prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom | ||
denom = transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom() | ||
} | ||
return denom | ||
} | ||
|
||
// IsAckError checks an IBC acknowledgement to see if it's an error. | ||
// This is a replacement for ack.Success() which is currently not working on some circumstances | ||
func IsAckError(acknowledgement []byte) bool { | ||
var ackErr channeltypes.Acknowledgement_Error | ||
if err := json.Unmarshal(acknowledgement, &ackErr); err == nil && len(ackErr.Error) > 0 { | ||
return true | ||
} | ||
return false | ||
} |
Oops, something went wrong.