-
Notifications
You must be signed in to change notification settings - Fork 208
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 #936 from scrtlabs/v1-ibc
Cosmwasm V1 IBC
- Loading branch information
Showing
5 changed files
with
73 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
host "github.com/cosmos/ibc-go/v3/modules/core/24-host" | ||
) | ||
|
||
// bindIbcPort will reserve the port. | ||
// returns a string name of the port or error if we cannot bind it. | ||
// this will fail if call twice. | ||
func (k Keeper) bindIbcPort(ctx sdk.Context, portID string) error { | ||
cap := k.portKeeper.BindPort(ctx, portID) | ||
return k.ClaimCapability(ctx, cap, host.PortPath(portID)) | ||
} | ||
|
||
// ensureIbcPort is like registerIbcPort, but it checks if we already hold the port | ||
// before calling register, so this is safe to call multiple times. | ||
// Returns success if we already registered or just registered and error if we cannot | ||
// (lack of permissions or someone else has it) | ||
func (k Keeper) ensureIbcPort(ctx sdk.Context, contractAddr sdk.AccAddress) (string, error) { | ||
portID := PortIDForContract(contractAddr) | ||
if _, ok := k.capabilityKeeper.GetCapability(ctx, host.PortPath(portID)); ok { | ||
return portID, nil | ||
} | ||
return portID, k.bindIbcPort(ctx, portID) | ||
} | ||
|
||
const portIDPrefix = "wasm." | ||
|
||
func PortIDForContract(addr sdk.AccAddress) string { | ||
return portIDPrefix + addr.String() | ||
} | ||
|
||
// ClaimCapability allows the transfer module to claim a capability | ||
// that IBC module passes to it | ||
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error { | ||
return k.capabilityKeeper.ClaimCapability(ctx, cap, name) | ||
} |
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,17 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" | ||
) | ||
|
||
// PortKeeper defines the expected IBC port keeper | ||
type PortKeeper interface { | ||
BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability | ||
} | ||
|
||
type CapabilityKeeper interface { | ||
GetCapability(ctx sdk.Context, name string) (*capabilitytypes.Capability, bool) | ||
ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error | ||
AuthenticateCapability(ctx sdk.Context, capability *capabilitytypes.Capability, name string) bool | ||
} |
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,5 @@ | ||
package v106 | ||
|
||
const ( | ||
ModuleName = "tokenswap" | ||
) |