Skip to content

Channels

Each Comment in ECP is associated with a Channel. If not specified, the comment is associated with the default channel with id 0. Channels are ERC721 NFTs that can be owned, transferred, or traded. The Channel owner can set the Hooks for their channel that enable flexible and powerful behaviors for comments in the channel, such as:

  • Gating who can comment - by onchain primitives like NFTs or tokens
  • Charging a small fee to post a comment
  • Integrating with other protocols, such as tokens or NFTs

Together with Hooks, Channels create a flexible system for building on-chain communities and apps with customizable rules.

Getting Started

You can create channels and hooks using our TypeScript SDK or by interacting with our smart contracts.

For full code examples used in this guide, check out the examples.

TypeScript Examples

Creating a Channel

Import the necessary functions from the @ecp.eth/sdk package:

import {
  createChannel,
  getChannelCreationFee,
} from "@ecp.eth/sdk/channel-manager";
...
import { privateKeyToAccount } from "viem/accounts";

Initialize the account using viem. Ensure the account has enough balance to cover the channel creation fee:

const account = privateKeyToAccount(privateKey);

Creating a channel requires paying a fee. The code below retrieves the channel creation fee and calls the createChannel function from the SDK to create a new channel:

const { fee } = await getChannelCreationFee({
  readContract: publicClient.readContract,
});
const { wait } = await createChannel({
  name: "Ethereum Comments Protocol Updates",
  description:
    "Latest updates and announcements from the Ethereum Comments Protocol",
  metadata: JSON.stringify({
    category: "blog",
    rules: ["Be respectful", "No spam"],
  }),
  hook: "0x0000000000000000000000000000000000000000", // No hook initially
  fee: fee,
  writeContract: walletClient.writeContract,
});

Retrieve the Channel ID from Event Logs

Due to EVM limitations, return values from state-changing (write) contract calls are not propagated to off-chain callers.

To make things easier, the SDK provides a helper function to wait for the transaction to be mined and return the channel data from the event logs:

const { wait } = await createChannel({...});
 
const createChannelEvent = await wait({
  getContractEvents: publicClient.getContractEvents,
  waitForTransactionReceipt: publicClient.waitForTransactionReceipt,
});
 
if (!createChannelEvent) {
  throw new Error("Channel creation event not found");
}
 
console.log("Channel created, id:", createChannelEvent.channelId);
  • See the full example here.
  • For more details about the SDK functionality, check out our SDK Reference.

Solidity Examples

Creating a Channel

First, retrieve the current channel creation fee:

uint256 fee = channelManager.getChannelCreationFee();

Then create the channel and pass the fee:

uint256 channelId = channelManager.createChannel{value: fee}(
    "My Channel",
    "Description",
    "{}",
    address(0)
);
  • See the full example here.

Links