Channels

Access Farcaster user's relationship with channels directly from your smart contract, either by fid or address.

To get Channel ID:

uint256 cid = uint256(keccak256(abi.encodePacked(channel_id_in_string_without_slash)));

Access user's membership metrics for a Farcaster channel

Fetch details like time of following, time of becoming a member, restriction and ban timestamps, along with lead and moderator status for a user in a specific channel. Also gets the last updated timestamp for this data.
function getChannelData(uint256 _cid, uint256 _fid) external view returns (ChannelUser memory)
YourContract.sol
1import "./IFaracle.sol";
2import "./FaracleHelper.sol";
3
4contract MintIfChannelMember {
5  
6  IFaracle public faracle = IFaracle(0x687d65ce04abbca3dac57cda6c358e497bfd320e);
7
8  struct ChannelUser {
9    // Timestamps
10    uint32 followedAt;      //4 Bytes Timestamp when followed
11    uint32 memberAt;        //4 Bytes Timestamp when became member
12    uint32 restrictedAt;    //4 Bytes Timestamp when restricted
13    uint32 bannedAt;        //4 Bytes Timestamp when banned
14
15    // meta 
16    uint32 timestamp;       //4 Bytes: When this data was last updated.
17
18    // Flags 
19    bool isLead;            //1 Bit:  Includes Lead and moderator status
20    bool isModerator;       //1 Bit:  Includes Lead and moderator status
21
22    // Rest of the slots are reserved for future use
23  }      
24
25  constructor() {}
26
27  uint256 public cid_example = uint256(keccak256(abi.encodePacked("examplechannel"))); // Example channel ID
28
29  function mintIfChannelMember() public {
30    
31    uint256 fid = faracle.getFidByVerifiedAddress(msg.sender);
32
33    require(fid != 0, "Address not linked to any FID!");
34
35    ChannelUser memory channelData = faracle.getChannelData(cid_example, fid);
36
37    require(channelData.memberAt != 0, "Not a channel member!");
38
39    // Proceed with minting logic
40
41  }
42
43}

Access user's membership metrics for multiple Farcaster channels

Fetch details like time of following, time of becoming a member, restriction and ban timestamps, along with lead and moderator status for a user in multiple channels. Also gets the last updated timestamp for this data.
function getChannelData(uint256[] memory _cids, uint256 _fid) external view returns (ChannelUser[] memory)
YourContract.sol
1import "./IFaracle.sol";
2import "./FaracleHelper.sol";
3
4contract MintIfChannelsMember {
5  
6  IFaracle public faracle = IFaracle(0x687d65ce04abbca3dac57cda6c358e497bfd320e);
7
8  constructor() {}
9
10  uint256 public cid_first = uint256(keccak256(abi.encodePacked("first_channel"))); // Example channel ID
11  uint256 public cid_second = uint256(keccak256(abi.encodePacked("second_channel"))); // Example channel ID
12
13  function mintIfChannelsMember() public {
14    
15    uint256 fid = faracle.getFidByVerifiedAddress(msg.sender);
16
17    require(fid != 0, "Address not linked to any FID!");
18
19    ChannelUser[] memory channelData = faracle.getChannelData([cid_first, cid_second], fid);
20
21    require(channelData[0].memberAt != 0 || channelData[1].memberAt != 0, "Not a channel member!");
22
23    // Proceed with minting logic
24
25  }
26
27}

Access multiple users' membership metrics for a Farcaster channel

Fetch details like time of following, time of becoming a member, restriction and ban timestamps, along with lead and moderator status for multiple users in a specific channel. Also gets the last updated timestamp for this data.
function getChannelData(uint256 _cid, uint256[] memory _fids) external view returns (ChannelUser[] memory)
YourContract.sol
1import "./IFaracle.sol";
2import "./FaracleHelper.sol";
3
4contract GiftIfYourFriendIsChannelMember {
5  
6  IFaracle public faracle = IFaracle(0x687d65ce04abbca3dac57cda6c358e497bfd320e);
7
8  constructor() {}
9
10  uint256 public cid_first = uint256(keccak256(abi.encodePacked("first_channel"))); // Example channel ID
11
12  function giftIfYourFriendIsChannelMember(uint256 _friendFid) public {
13    
14    uint256 fid = faracle.getFidByVerifiedAddress(msg.sender);
15
16    require(fid != 0, "Address not linked to any FID!");
17    require(_friendFid != 0, "Friend FID cannot be zero!");
18
19    ChannelUser[] memory channelData = faracle.getChannelData(cid_first, [fid, _friendFid]);
20
21    require(channelData[0].memberAt != 0, "You are not a channel member!");
22    require(channelData[1].memberAt != 0, "Your friend is not a channel member!");
23    
24    // Proceed with gifting logic
25
26  }
27
28}

Access multiple users' membership metrics for multiple Farcaster channels

Fetch details like time of following, time of becoming a member, restriction and ban timestamps, along with lead and moderator status for multiple users in multiple channels. Also gets the last updated timestamp for this data.
function getChannelData(uint256[] memory _cids, uint256[] memory _fids) external view returns (ChannelUser[][] memory)
YourContract.sol
1import "./IFaracle.sol";
2import "./FaracleHelper.sol";
3
4contract InviteChannelLeads {
5  
6  IFaracle public faracle = IFaracle(0x687d65ce04abbca3dac57cda6c358e497bfd320e);
7
8  mapping(uint256 => mapping(uint256 => uint256)) public invitedLeads; // fid => index => fid
9
10  mapping(uint256 => uint256) public invitedLeadsCount; // fid => count
11
12  constructor() {}
13
14  function inviteChannelLeads(uint256[] calldata _cids, uint256[] calldata _fids) public {
15
16    require(_cids.length > 0 && _cids.length == _fids.length, "Input Error");
17
18    uint256 _fid = faracle.getFidByVerifiedAddress(msg.sender);
19
20    require(_fid != 0, "Address not linked to any FID!");
21    
22    ChannelUser[] memory channelData = faracle.getChannelData(_cids, _fids);
23
24    uint256 index = invitedLeadsCount[_fid];
25
26    for (uint256 i = 0; i < _fids.length; i++) {
27
28      if (channelData[i].isLead) {
29        // Invite the lead (store in mapping for example)
30        
31        invitedLeads[_fid][index + i] = _fids[i];
32        invitedLeadsCount[_fid] += 1;
33        
34      }
35
36    }
37    // Proceed with further logic
38  }
39}