Make sure to implement the prerequisite code, or else these examples will not work!
Allow a user to vote on a poll
You can allow a logged-in user to vote a specific Socios.com poll.
This is a code-based alternative to the POST /poll/{pollId}/vote endpoint, which was deprecated from the Socios.com API in Q1 2025.
import { createWalletClient, privateKeyToAccount } from 'viem';
const privateKey = '0xSenderPrivateKey'; // Replace with the sender's private key
const account = privateKeyToAccount(privateKey);
const walletClient = createWalletClient({
account,
transport: http('https://rpc.ankr.com/chiliz'),
});
async function vote(id, weight) {
const txHash = await walletClient.writeContract({
address: '0xPollAddress', // Replace with the poll contract address
abi: survey3ABI, // Poll contract ABI
functionName: 'vote', // Function to cast the vote
args: [id, BigInt(weight)], // Replace 'id' with answer id and 'weight' with the amount of tokens that should be used to cast the vote
});
return txHash;
}
// Example usage
vote('dab3d446-959f-47e7-8789-f618d969de1e', 1).then(console.log);
Retrieve user vote on a poll
You can know whether a user has already voted on a specific poll or not, and if they you can retrieve which answer they have selected.
This is a code-based alternative to the GET /poll/{pollId} endpoint, which was deprecated from the Socios.com API in Q1 2025.
import { createPublicClient, http, keccak256 } from 'viem';
import { abi as survey3ABI } from './Survey3ABI.json';
const client = createPublicClient({
transport: http('https://rpc.ankr.com/chiliz'),
});
const pollAddress = '0xPollAddress'; // Replace with the actual poll contract address
const userAddress = '0xUserAddress'; // Replace with the user's wallet address
// Returns hashes of answerIds that the user voted on
async function getUserVoteHashes() {
const votes = await client.readContract({
address: pollAddress, // Poll contract address
abi: survey3ABI, // Poll conttract ABI
functionName: 'getUserAnswers', // Function to retrieve the answers that the user voted on
args: [userAddress], // User's address whose votes you want to fetch
});
return votes;
}
// Returns votes in plain text
async function getUserVotesPlainText() {
// fetch all answers
const answers = await client.readContract({
address: pollAddress, // Poll contract address
abi: survey3ABI, // Poll conttract ABI
functionName: 'getAnswers', // Function to retrieve the answers
args: [], // User's address whose votes you want to fetch
});
// fetch user's votes
const votes = await client.readContract({
address: pollAddress, // Poll contract address
abi: survey3ABI, // Poll conttract ABI
functionName: 'getUserAnswers', // Function to retrieve the answers that the user voted on
args: [userAddress], // User's address whose votes you want to fetch
});
// filter answers, return the ones that the user voted on
const plainTextVotes = answers.filter(a => votes.includes(keccak256(a.id)));
return plainTextVotes;
}
// Example usage
getUserVotesPlainText().then(console.log);
getUserVoteHashes().then(console.log);