Skip to content

Commit

Permalink
Updates to the IBC doc in the cosmwasm section (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
emperorjm authored Nov 1, 2024
1 parent 243c762 commit 707dfaa
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions content/2.developers/3.smart-contracts/14.cosmwasm-ibc.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ IBC enables multiple blockchains to participate in shared governance systems. Co

## Using IBC with CosmWasm

### Getting started

To get started, you need to enable the `stargate` feature of the `cosmwasm-std` crate. This will
enable additional functionality that is not available on all chains, including IBC support.

```toml
cosmwasm-std = { version = "2.0.3", features = ["stargate"] }
```

<br />

::alert{variant="info"}
The naming "stargate" is somewhat confusing. It is a reference to the Cosmos SDK 0.40 upgrade with the same name. This upgrade introduced (among other things) IBC.
#title
Info
::

<br />

### Basic concepts

In order to understand how IBC works, it is important to understand some basic concepts:

- **Port**: Every instantiation of an ibc-enabled contract creates a unique port, similarly to how
it creates a unique address. Native Cosmos SDK modules can also have one or multiple unique ports.
- **Channel**: A connection between two ports on different blockchains that allows them to send
packets to each other. Each port can have multiple channels.
- **Packet**: A piece of binary data that is sent through a channel. It can time out if it is not
delivered within a certain time frame.
- **Relayer**: An off-chain service that is responsible for passing packets between blockchains. The
relayer watches for new packet commitments on one chain and submits them to the other chain. This
is the way in which your packets actually reach the other chain. Anyone can run a relayer.

We will go into more detail on how these concepts are related to CosmWasm in the later sections, but
this should give you some basic terminology to start with. If you want to learn more about IBC, you
can read the [IBC specification](https://github.com/cosmos/ibc) or check out the [IBC documentation](https://ibc.cosmos.network/main).

### ICS standards

CosmWasm supports established IBC standards such as ICS20 and ICS721, which enable token transfers and NFT transfers across chains, respectively. These standards are implemented in CosmWasm and can be leveraged to build cross-chain applications. Additionally, developers can implement custom ICS protocols within their contracts, such as [cw20-ics20](https://github.com/CosmWasm/cw-plus/tree/main/contracts/cw20-ics20), which extends the CW20 token standard for cross-chain transfers. The following [guide](/developers/guides/cw20-ibc-transfer/introduction) gives a walkthrough of enabling cross-chain CW20 token transfers with CW20-ICS20.
Expand All @@ -52,6 +89,68 @@ To enable IBC communication, smart contracts must implement the following six en
5. **ibc_packet_ack**: Handles packet acknowledgments (`MsgAcknowledgement`).
6. **ibc_packet_timeout**: Handles packet timeouts (`MsgTimeout`).

Three for the channel lifecycle:

```rust
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_open(
deps: DepsMut,
env: Env,
msg: IbcChannelOpenMsg
) -> StdResult<IbcChannelOpenResponse> {
Ok(None)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_connect(
deps: DepsMut,
env: Env,
msg: IbcChannelConnectMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_close(
deps: DepsMut,
env: Env,
msg: IbcChannelCloseMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new())
}
```

And three for the packet lifecycle:

```rust
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_receive(
deps: DepsMut,
env: Env,
msg: IbcPacketReceiveMsg,
) -> StdResult<IbcReceiveResponse> {
Ok(IbcReceiveResponse::new(b""))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_ack(
deps: DepsMut,
env: Env,
msg: IbcPacketAckMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_timeout(
_deps: DepsMut,
_env: Env,
_msg: IbcPacketTimeoutMsg,
) -> StdResult<IbcBasicResponse> {
Ok(IbcBasicResponse::new())
}
```

### IBC counter example

To demonstrate the implementation of an IBC-enabled smart contract in CosmWasm, consider the IBC Counter example. This contract counts the number of messages sent and received across two connected chains. You can find the code for this example in the [IBC Counter GitHub repository](https://github.com/0xekez/cw-ibc-example).
Expand Down

0 comments on commit 707dfaa

Please sign in to comment.