Likes signal appreciation and enhance content visibility, triggering incremental rewards in Sage Protocol.React Hook Example for Liking Posts:
Copy
import { usePost } from '@sageprotocol/sdk/react';import { useWallet } from '@suiet/wallet-kit';function LikePost({ authorSharedUserId, postId, ownedUserId }) { const { like } = usePost(); const { account, signAndExecuteTransaction } = useWallet(); async function handleLike() { const result = await like({ amounts: [0, 0], // Required protocol fees appId: 'your-app-id', // Explicitly required application ID network: 'testnet', // 'testnet' or 'mainnet' authorSharedUserId, // Original post author's shared user ID postId, // ID of the post to like ownedUserId, // Liker’s owned user ID self: account.address // Liker’s wallet address }); if (result.ok) { await signAndExecuteTransaction({ transaction: result.transaction }); console.log("Post liked successfully!"); } else { console.error("Error liking post:", result.err); } } return <button onClick={handleLike}>Like Post</button>;}export default LikePost;
Direct JavaScript Example for Liking Posts
Copy
import { SageClient } from '@sageprotocol/sdk';const sageClient = new SageClient({ appId: 'your-app-id', // Required application ID channelRegistryId: 'your-registry-id', // Required channel registry ID network: 'testnet' // 'testnet' or 'mainnet'});await sageClient.initialize();async function likePost(authorSharedUserId, postId, ownedUserId, walletAddress) { const result = await sageClient.likePost({ amounts: [0, 0], // Required protocol fees authorSharedUserId, // Original post author's shared user ID postId, // ID of the post to like ownedUserId, // Liker’s owned user ID self: walletAddress // Liker’s wallet address }); if (result.ok) { await signAndExecuteTransaction({ transaction: result.transaction }); console.log("Post liked successfully!"); } else { console.error("Error liking post:", result.err); }}likePost('author-shared-user-id', 'target-post-id', 'your-owned-user-id', walletAddress);