Skip to content

Commit 707dfaa

Browse files
authored
Updates to the IBC doc in the cosmwasm section (#543)
1 parent 243c762 commit 707dfaa

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

content/2.developers/3.smart-contracts/14.cosmwasm-ibc.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,43 @@ IBC enables multiple blockchains to participate in shared governance systems. Co
2626

2727
## Using IBC with CosmWasm
2828

29+
### Getting started
30+
31+
To get started, you need to enable the `stargate` feature of the `cosmwasm-std` crate. This will
32+
enable additional functionality that is not available on all chains, including IBC support.
33+
34+
```toml
35+
cosmwasm-std = { version = "2.0.3", features = ["stargate"] }
36+
```
37+
38+
<br />
39+
40+
::alert{variant="info"}
41+
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.
42+
#title
43+
Info
44+
::
45+
46+
<br />
47+
48+
### Basic concepts
49+
50+
In order to understand how IBC works, it is important to understand some basic concepts:
51+
52+
- **Port**: Every instantiation of an ibc-enabled contract creates a unique port, similarly to how
53+
it creates a unique address. Native Cosmos SDK modules can also have one or multiple unique ports.
54+
- **Channel**: A connection between two ports on different blockchains that allows them to send
55+
packets to each other. Each port can have multiple channels.
56+
- **Packet**: A piece of binary data that is sent through a channel. It can time out if it is not
57+
delivered within a certain time frame.
58+
- **Relayer**: An off-chain service that is responsible for passing packets between blockchains. The
59+
relayer watches for new packet commitments on one chain and submits them to the other chain. This
60+
is the way in which your packets actually reach the other chain. Anyone can run a relayer.
61+
62+
We will go into more detail on how these concepts are related to CosmWasm in the later sections, but
63+
this should give you some basic terminology to start with. If you want to learn more about IBC, you
64+
can read the [IBC specification](https://github.com/cosmos/ibc) or check out the [IBC documentation](https://ibc.cosmos.network/main).
65+
2966
### ICS standards
3067

3168
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.
@@ -52,6 +89,68 @@ To enable IBC communication, smart contracts must implement the following six en
5289
5. **ibc_packet_ack**: Handles packet acknowledgments (`MsgAcknowledgement`).
5390
6. **ibc_packet_timeout**: Handles packet timeouts (`MsgTimeout`).
5491

92+
Three for the channel lifecycle:
93+
94+
```rust
95+
#[cfg_attr(not(feature = "library"), entry_point)]
96+
pub fn ibc_channel_open(
97+
deps: DepsMut,
98+
env: Env,
99+
msg: IbcChannelOpenMsg
100+
) -> StdResult<IbcChannelOpenResponse> {
101+
Ok(None)
102+
}
103+
104+
#[cfg_attr(not(feature = "library"), entry_point)]
105+
pub fn ibc_channel_connect(
106+
deps: DepsMut,
107+
env: Env,
108+
msg: IbcChannelConnectMsg,
109+
) -> StdResult<IbcBasicResponse> {
110+
Ok(IbcBasicResponse::new())
111+
}
112+
113+
#[cfg_attr(not(feature = "library"), entry_point)]
114+
pub fn ibc_channel_close(
115+
deps: DepsMut,
116+
env: Env,
117+
msg: IbcChannelCloseMsg,
118+
) -> StdResult<IbcBasicResponse> {
119+
Ok(IbcBasicResponse::new())
120+
}
121+
```
122+
123+
And three for the packet lifecycle:
124+
125+
```rust
126+
#[cfg_attr(not(feature = "library"), entry_point)]
127+
pub fn ibc_packet_receive(
128+
deps: DepsMut,
129+
env: Env,
130+
msg: IbcPacketReceiveMsg,
131+
) -> StdResult<IbcReceiveResponse> {
132+
Ok(IbcReceiveResponse::new(b""))
133+
}
134+
135+
#[cfg_attr(not(feature = "library"), entry_point)]
136+
pub fn ibc_packet_ack(
137+
deps: DepsMut,
138+
env: Env,
139+
msg: IbcPacketAckMsg,
140+
) -> StdResult<IbcBasicResponse> {
141+
Ok(IbcBasicResponse::new())
142+
}
143+
144+
#[cfg_attr(not(feature = "library"), entry_point)]
145+
pub fn ibc_packet_timeout(
146+
_deps: DepsMut,
147+
_env: Env,
148+
_msg: IbcPacketTimeoutMsg,
149+
) -> StdResult<IbcBasicResponse> {
150+
Ok(IbcBasicResponse::new())
151+
}
152+
```
153+
55154
### IBC counter example
56155

57156
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).

0 commit comments

Comments
 (0)