Skip to main content
This page demonstrates how to vote on posts and comments on the Hive blockchain.

Basic Upvote

A simple upvote with 100% voting weight.
import { Transaction, PrivateKey } from 'hive-tx'

async function upvotePost() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: 10000  // 10000 = 100% upvote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('Vote successful!', result.result.tx_id)
}

VoteOperation Type

All vote operations use the following structure:
interface VoteOperation {
  voter: string      // Your account name
  author: string     // Author of the post/comment
  permlink: string   // Post/comment permlink
  weight: number     // Vote weight: -10000 to 10000
}

Vote Weight Explained

The weight parameter determines the strength and direction of your vote:
  • 10000 = 100% upvote (maximum)
  • 5000 = 50% upvote
  • 2500 = 25% upvote
  • 0 = Remove vote
  • -2500 = 25% downvote
  • -10000 = 100% downvote (maximum)

Downvote Example

Downvoting uses negative weight values.
import { Transaction, PrivateKey } from 'hive-tx'

async function downvotePost() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: -10000  // -10000 = 100% downvote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('Downvote successful!', result.result.tx_id)
}

Partial Vote Examples

Vote with less than 100% weight to preserve voting power.

50% Upvote

import { Transaction, PrivateKey } from 'hive-tx'

async function partialUpvote() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: 5000  // 50% upvote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('50% upvote successful!', result.result.tx_id)
}

25% Upvote

import { Transaction, PrivateKey } from 'hive-tx'

async function smallUpvote() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: 2500  // 25% upvote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('25% upvote successful!', result.result.tx_id)
}

10% Downvote

import { Transaction, PrivateKey } from 'hive-tx'

async function smallDownvote() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: -1000  // -1000 = 10% downvote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('10% downvote successful!', result.result.tx_id)
}

Remove Vote

Set weight to 0 to remove an existing vote.
import { Transaction, PrivateKey } from 'hive-tx'

async function removeVote() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'post-author',
    permlink: 'post-permlink',
    weight: 0  // 0 = remove vote
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('Vote removed!', result.result.tx_id)
}

Vote on a Comment

Voting on comments uses the same operation as voting on posts.
import { Transaction, PrivateKey } from 'hive-tx'

async function voteOnComment() {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'comment-author',
    permlink: 'comment-permlink',  // The comment's permlink
    weight: 10000
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('Comment vote successful!', result.result.tx_id)
}

Multiple Votes in One Transaction

You can vote on multiple posts/comments in a single transaction.
import { Transaction, PrivateKey } from 'hive-tx'

async function voteMultiplePosts() {
  const tx = new Transaction()
  
  // Vote on first post
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'alice',
    permlink: 'alices-post',
    weight: 10000
  })
  
  // Vote on second post
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'bob',
    permlink: 'bobs-post',
    weight: 5000
  })
  
  // Vote on a comment
  await tx.addOperation('vote', {
    voter: 'your-username',
    author: 'charlie',
    permlink: 'charlies-comment',
    weight: 2500
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  console.log('All votes successful!', result.result.tx_id)
}

Vote Weight Calculator

Helper function to calculate vote weight from percentage.
function percentageToWeight(percentage: number): number {
  // percentage can be -100 to 100
  return Math.round(percentage * 100)
}

// Usage examples:
const weight100 = percentageToWeight(100)   // 10000
const weight50 = percentageToWeight(50)     // 5000
const weight25 = percentageToWeight(25)     // 2500
const weightMinus50 = percentageToWeight(-50) // -5000

Vote with Percentage Helper

import { Transaction, PrivateKey } from 'hive-tx'

async function voteWithPercentage(
  voter: string,
  author: string,
  permlink: string,
  percentage: number  // -100 to 100
) {
  const tx = new Transaction()
  
  await tx.addOperation('vote', {
    voter,
    author,
    permlink,
    weight: Math.round(percentage * 100)
  })
  
  const key = PrivateKey.from('your-posting-key')
  tx.sign(key)
  
  const result = await tx.broadcast()
  return result.result.tx_id
}

// Usage:
await voteWithPercentage('myaccount', 'alice', 'post-permlink', 75)  // 75% upvote
await voteWithPercentage('myaccount', 'bob', 'bad-post', -50)       // 50% downvote

Important Notes

  • Vote weight must be between -10000 and 10000
  • You can only vote once per post/comment (but can change your vote)
  • Votes after 7 days have no effect (payout window closed)
  • Use posting key to sign vote operations
  • Frequent voting depletes voting power (regenerates at 20% per day)
Using lower vote weights (e.g., 25-50%) allows you to vote more frequently without depleting your voting power.

Voting Power Consideration

Each vote consumes voting power based on the weight used. A 100% vote uses more power than a 50% vote. Voting power regenerates at 20% per day. If your voting power drops below 80%, consider using lower vote weights to preserve it.

Build docs developers (and LLMs) love