Skip to content
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

Nguyen/v2.0.x ibchooks #235

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"sort"
"strings"

ibchooks "github.com/White-Whale-Defi-Platform/migaloo-chain/v2/x/ibchooks"
ibchookskeeper "github.com/White-Whale-Defi-Platform/migaloo-chain/v2/x/ibchooks/keeper"
ibchookstypes "github.com/White-Whale-Defi-Platform/migaloo-chain/v2/x/ibchooks/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
Expand Down Expand Up @@ -281,6 +284,7 @@ type MigalooApp struct {
EvidenceKeeper evidencekeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
IBCFeeKeeper ibcfeekeeper.Keeper
IBCHooksKeeper *ibchookskeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
ICAHostKeeper icahostkeeper.Keeper
InterTxKeeper intertxkeeper.Keeper
Expand All @@ -289,6 +293,7 @@ type MigalooApp struct {
FeeGrantKeeper feegrantkeeper.Keeper
AuthzKeeper authzkeeper.Keeper
WasmKeeper wasm.Keeper
ContractKeeper *wasmkeeper.PermissionedKeeper
RouterKeeper routerkeeper.Keeper

ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand All @@ -299,6 +304,10 @@ type MigalooApp struct {
ScopedIBCFeeKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

// Middleware wrapper
Ics20WasmHooks *ibchooks.WasmHooks
HooksICS4Wrapper ibchooks.ICS4Middleware

// the module manager
mm *module.Manager

Expand Down Expand Up @@ -338,7 +347,7 @@ func NewMigalooApp(
evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey,
feegrant.StoreKey, authzkeeper.StoreKey, wasm.StoreKey, icahosttypes.StoreKey,
icacontrollertypes.StoreKey, intertxtypes.StoreKey, ibcfeetypes.StoreKey, tokenfactorytypes.StoreKey,
alliancemoduletypes.StoreKey,
alliancemoduletypes.StoreKey, ibchookstypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -503,6 +512,21 @@ func NewMigalooApp(
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(alliancemoduletypes.RouterKey, alliancemodule.NewAllianceProposalHandler(app.AllianceKeeper))

// Configure the hooks keeper
hooksKeeper := ibchookskeeper.NewKeeper(
app.keys[ibchookstypes.StoreKey],
)
app.IBCHooksKeeper = &hooksKeeper

migalooPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix()
wasmHooks := ibchooks.NewWasmHooks(app.IBCHooksKeeper, nil, migalooPrefix) // The contract keeper needs to be set later

app.Ics20WasmHooks = &wasmHooks
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
app.IBCKeeper.ChannelKeeper,
app.Ics20WasmHooks,
)

// RouterKeeper must be created before TransferKeeper
app.RouterKeeper = *routerkeeper.NewKeeper(
appCodec,
Expand All @@ -512,9 +536,8 @@ func NewMigalooApp(
app.IBCKeeper.ChannelKeeper,
app.DistrKeeper,
app.BankKeeper,
app.IBCKeeper.ChannelKeeper,
app.HooksICS4Wrapper,
)

// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec, keys[ibcfeetypes.StoreKey],
Expand Down Expand Up @@ -606,6 +629,14 @@ func NewMigalooApp(
var transferStack porttypes.IBCModule
transferStack = transfer.NewIBCModule(app.TransferKeeper)
transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper)
transferStack = router.NewIBCMiddleware(
transferStack,
&app.RouterKeeper,
0,
routerkeeper.DefaultForwardTransferPacketTimeoutTimestamp,
routerkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)
transferStack = ibchooks.NewIBCMiddleware(transferStack, &app.HooksICS4Wrapper)

// Create Interchain Accounts Stack
// SendPacket, since it is originating from the application to core IBC:
Expand Down Expand Up @@ -705,6 +736,7 @@ func NewMigalooApp(
tokenfactory.NewAppModule(app.TokenFactoryKeeper, app.AccountKeeper, app.BankKeeper),
router.NewAppModule(&app.RouterKeeper),
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants), // always be last to make sure that it checks for all invariants and not only part of them
ibchooks.NewAppModule(app.AccountKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -738,6 +770,7 @@ func NewMigalooApp(
wasm.ModuleName,
tokenfactorytypes.ModuleName,
alliancemoduletypes.ModuleName,
ibchookstypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -767,6 +800,7 @@ func NewMigalooApp(
wasm.ModuleName,
tokenfactorytypes.ModuleName,
alliancemoduletypes.ModuleName,
ibchookstypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -805,6 +839,7 @@ func NewMigalooApp(
wasm.ModuleName,
routertypes.ModuleName,
alliancemoduletypes.ModuleName,
ibchookstypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -891,6 +926,10 @@ func NewMigalooApp(
app.ScopedICAControllerKeeper = scopedICAControllerKeeper
app.ScopedInterTxKeeper = scopedInterTxKeeper

// set the contract keeper for the Ics20WasmHooks
app.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper)
app.Ics20WasmHooks.ContractKeeper = app.ContractKeeper

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(fmt.Sprintf("failed to load latest version: %s", err))
Expand Down Expand Up @@ -1044,6 +1083,7 @@ func RegisterSwaggerAPI(rtr *mux.Router) {
func (app *MigalooApp) setupUpgradeHandlers(cfg module.Configurator) {
for _, upgrade := range Upgrades {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
upgrade := upgrade
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ module github.com/White-Whale-Defi-Platform/migaloo-chain/v2
go 1.20

require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/CosmWasm/wasmd v0.30.0
github.com/cosmos/cosmos-sdk v0.46.13
github.com/cosmos/ibc-go/v6 v6.2.0
github.com/cosmos/interchain-accounts v0.4.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/prometheus/client_golang v1.14.0
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.5.0
Expand All @@ -27,7 +29,6 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/storage v1.28.1 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -90,7 +91,6 @@ require (
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
72 changes: 72 additions & 0 deletions osmosis-types/osmoutils/ibc.go
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/v6/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v6/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
}
Loading
Loading