👍Likes

Like and Unlike posts

The Likes contract is a Solidity smart contract that provides a system for tracking likes on a certain message. The contract has functions to add and remove likes, get a list of users that liked a message, get a list of messages liked by a user, and more. It maintains mappings for easier access of like information based on message ID or user addresses.

Table of Contents


Events

logAddLike

Event that gets emitted when a like is added to a message.

Example with ethers.js

// Get the contract instance first
const contract = new ethers.Contract(contractAddress, contractABI, provider);

// Listen for logAddLike events
contract.on("logAddLike", (msgID, requester, event) => {
    console.log(`Like added on message ID ${msgID} by ${requester}`);
});

logRemoveLike

Event that gets emitted when a like is removed from a message.

Example with ethers.js

// Get the contract instance first
const contract = new ethers.Contract(contractAddress, contractABI, provider);

// Listen for logRemoveLike events
contract.on("logRemoveLike", (msgID, requester, event) => {
    console.log(`Like removed on message ID ${msgID} by ${requester}`);
});

Public Functions

getLikesFromMsgID

This function returns a list of users that liked a specific message.

Parameters

Name
Type
Description

msgID

uint256

The ID of the message.

startFrom

uint256

The number from where to start getting records.

Returns

Name
Type
Description

address[]

An array of addresses of users and groups that liked a message.

Example with ethers.js

// Get the contract instance first
const contract = new ethers.Contract(contractAddress, contractABI, provider);

// Define the message ID and startFrom number
const msgID = 123;
const startFrom = 0;

// Call the getLikesFromMsgID function
const result = await contract.getLikesFromMsgID(msgID, startFrom);
console.log("Addresses that liked this message: ", result);

getLikesByAddress

This function returns a list of messages liked by a specific user.

Parameters

Name
Type
Description

liker

address

The address of the user.

startFrom

uint256

The number from where to start getting records.

Returns

Name
Type
Description

uint256[]

An array of message IDs that were liked by the user.

Example with ethers.js

// Get the contract instance first
const contract = new ethers.Contract(contractAddress, contractABI, provider);

// Define the liker address and startFrom number
const liker = "0x123";
const startFrom = 0;

// Call the getLikesByAddress function
const result = await contract.getLikesByAddress(liker, startFrom);
console.log("Message IDs liked by this user: ", result);

Last updated