> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/mahdiyari/hive-tx/llms.txt
> Use this file to discover all available pages before exploring further.

# Operations

> Understanding Hive blockchain operations and how to use them with type safety

Operations are the atomic actions that modify the Hive blockchain state. Every transaction contains one or more operations that execute sequentially when the transaction is included in a block.

## What Are Operations?

In Hive, operations represent specific actions like voting, transferring funds, publishing content, or updating account settings. Each operation has:

* A unique **name** (e.g., `vote`, `transfer`, `comment`)
* A structured **body** containing the operation's parameters
* Specific **authority requirements** (which key type is needed)

## Operation Structure

All operations follow the same tuple format:

```typescript theme={null}
type Operation = [operationName, operationBody]
```

For example, a vote operation:

```typescript theme={null}
const voteOp: Operation = [
  'vote',
  {
    voter: 'alice',
    author: 'bob',
    permlink: 'awesome-post',
    weight: 10000  // 100% upvote
  }
]
```

<Info>
  The tuple format `[name, body]` is Hive's protocol-level structure. hive-tx preserves this for compatibility while adding TypeScript type safety.
</Info>

## Type-Safe Operations

hive-tx provides full TypeScript types for all operations. When you call `addOperation()`, you get autocomplete and type checking:

```typescript theme={null}
await tx.addOperation('transfer', {
  from: 'alice',
  to: 'bob',
  amount: '5.000 HIVE',
  memo: 'Thanks!'  // TypeScript knows this field exists
})

// TypeScript error: Property 'amount' is missing
await tx.addOperation('transfer', {
  from: 'alice',
  to: 'bob',
  memo: 'Oops, forgot amount!'
})
```

### Type Inference

The `addOperation` method uses conditional types to ensure the body matches the operation name:

```typescript src/Transaction.ts:59 theme={null}
async addOperation<O extends OperationName>(
  operationName: O,
  operationBody: OperationBody<O>
): Promise<void>
```

This means TypeScript enforces that a `vote` operation must have `VoteOperation` structure, a `transfer` must have `TransferOperation`, etc.

## Common Operations

### Voting

Upvote or downvote content:

```typescript src/types.ts:45 theme={null}
await tx.addOperation('vote', {
  voter: 'alice',         // Your username
  author: 'bob',          // Post/comment author
  permlink: 'post-slug',  // Post/comment identifier
  weight: 10000           // -10000 to 10000 (100% downvote to 100% upvote)
})
```

**Authority Required:** Posting key

<Accordion title="Vote Weight Explained">
  Vote weight ranges from `-10000` to `10000`:

  * `10000` = 100% upvote
  * `5000` = 50% upvote
  * `0` = Remove vote
  * `-10000` = 100% downvote (flag)
</Accordion>

### Transfer

Send HIVE or HBD between accounts:

```typescript src/types.ts:62 theme={null}
await tx.addOperation('transfer', {
  from: 'alice',
  to: 'bob',
  amount: '10.000 HIVE',  // Must include 3 decimal places and symbol
  memo: 'Payment for services'
})
```

**Authority Required:** Active key

<Warning>
  Amounts must be formatted as strings with exactly 3 decimal places and the asset symbol (HIVE, HBD, or VESTS). Example: `'1.000 HIVE'`
</Warning>

### Comment (Post/Reply)

Publish posts or replies:

```typescript src/types.ts:52 theme={null}
// Create a new post
await tx.addOperation('comment', {
  parent_author: '',              // Empty for top-level posts
  parent_permlink: 'hive',        // Category/tag for posts
  author: 'alice',
  permlink: 'my-post-slug',
  title: 'My First Post',
  body: 'This is the post content in markdown.',
  json_metadata: JSON.stringify({
    tags: ['hive', 'introduction'],
    app: 'my-app/1.0.0'
  })
})

// Create a reply
await tx.addOperation('comment', {
  parent_author: 'bob',           // Author of post you're replying to
  parent_permlink: 'post-slug',   // Permlink of parent post
  author: 'alice',
  permlink: 're-post-slug-20260304',  // Must be unique
  title: '',                      // Empty for replies
  body: 'Great post!',
  json_metadata: '{}'
})
```

**Authority Required:** Posting key

### Custom JSON

Execute custom operations for apps and games:

```typescript src/types.ts:153 theme={null}
await tx.addOperation('custom_json', {
  required_auths: [],              // Active key authorities (use for financial ops)
  required_posting_auths: ['alice'], // Posting key authorities
  id: 'follow',                    // Operation identifier
  json: JSON.stringify([
    'follow',
    {
      follower: 'alice',
      following: 'bob',
      what: ['blog']  // Follow bob's blog
    }
  ])
})
```

**Authority Required:** Posting key (if using `required_posting_auths`) or Active key (if using `required_auths`)

<Tip>
  Custom JSON operations are perfect for building social features, games, and app-specific logic without modifying the blockchain protocol.
</Tip>

## All Available Operations

The complete operation types are defined in `types.ts`:

<AccordionGroup>
  <Accordion title="Social Operations">
    * `vote` - Upvote or downvote content
    * `comment` - Create posts or replies
    * `delete_comment` - Delete a post/comment
    * `custom_json` - Custom app operations
  </Accordion>

  <Accordion title="Financial Operations">
    * `transfer` - Send HIVE/HBD
    * `transfer_to_vesting` - Power up HIVE to HP
    * `withdraw_vesting` - Start power down
    * `transfer_to_savings` - Move to savings
    * `transfer_from_savings` - Withdraw from savings (3-day delay)
    * `claim_reward_balance` - Claim pending rewards
  </Accordion>

  <Accordion title="Market Operations">
    * `limit_order_create` - Place buy/sell order
    * `limit_order_create2` - Advanced order with exchange rate
    * `limit_order_cancel` - Cancel existing order
    * `convert` - Convert HBD to HIVE (3.5 day delay)
    * `collateralized_convert` - Instant conversion with collateral
  </Accordion>

  <Accordion title="Account Operations">
    * `account_create` - Create new account (paid)
    * `account_create_with_delegation` - Create with delegated HP
    * `create_claimed_account` - Create from claimed ticket
    * `claim_account` - Claim subsidized account creation
    * `account_update` - Update account authorities
    * `account_update2` - Enhanced account update
  </Accordion>

  <Accordion title="Governance Operations">
    * `account_witness_vote` - Vote for a witness
    * `account_witness_proxy` - Set witness vote proxy
    * `update_proposal_votes` - Vote on DHF proposals
    * `witness_update` - Update witness settings
    * `witness_set_properties` - Set witness properties
  </Accordion>

  <Accordion title="Advanced Operations">
    * `delegate_vesting_shares` - Delegate HP to another account
    * `comment_options` - Set post payout options and beneficiaries
    * `set_withdraw_vesting_route` - Route power down to another account
    * `escrow_transfer` - Three-party escrow transaction
    * `recurrent_transfer` - Set up recurring payments
  </Accordion>
</AccordionGroup>

## Operation Type Reference

View the full type definitions in the source:

```typescript src/types.ts:409 theme={null}
export type Operation =
  | ['vote', VoteOperation]
  | ['comment', CommentOperation]
  | ['transfer', TransferOperation]
  | ['transfer_to_vesting', TransferToVestingOperation]
  | ['withdraw_vesting', WithdrawVestingOperation]
  // ... 40+ operation types
```

Each operation body type is defined with all required and optional fields:

```typescript src/types.ts:45 theme={null}
export interface VoteOperation {
  voter: string
  author: string
  permlink: string
  weight: number
}
```

<Card title="Full Type Definitions" icon="file-code" href="https://github.com/openhive-network/hive-tx-js/blob/main/src/types.ts">
  View all operation types in the source code
</Card>

## Using Assets

Many operations require `Asset` values. You can use:

### String Format

Simplest approach - use properly formatted strings:

```typescript theme={null}
amount: '10.000 HIVE'   // Must have 3 decimals and symbol
amount: '5.123 HBD'
amount: '1000.000000 VESTS'  // VESTS use 6 decimals
```

### Asset Class

For calculations and conversions:

```typescript theme={null}
import { Asset } from 'hive-tx'

const amount = new Asset(10, 'HIVE')
await tx.addOperation('transfer', {
  from: 'alice',
  to: 'bob',
  amount: amount,  // Automatically serialized
  memo: ''
})
```

## Authority Requirements

Each operation requires specific key types:

| Operation Type                             | Required Authority |
| ------------------------------------------ | ------------------ |
| `vote`, `comment`, `custom_json` (posting) | Posting key        |
| `transfer`, `account_update`               | Active key         |
| `account_create`, `witness_update`         | Active key         |
| `owner` authority changes                  | Owner key          |

<Warning>
  Using the wrong key type will cause the transaction to fail validation. For example, you cannot transfer funds with a posting key.
</Warning>

## Multiple Operations in One Transaction

You can batch multiple operations together:

```typescript theme={null}
const tx = new Transaction()

// Post content
await tx.addOperation('comment', { /* ... */ })

// Set beneficiaries
await tx.addOperation('comment_options', { /* ... */ })

// Vote on your own post
await tx.addOperation('vote', {
  voter: 'alice',
  author: 'alice',
  permlink: 'my-post',
  weight: 10000
})

// All execute together when broadcast
const key = PrivateKey.fromLogin('alice', 'password', 'posting')
tx.sign(key)
await tx.broadcast()
```

<Tip>
  Batching operations saves bandwidth and ensures they execute atomically - either all succeed or all fail.
</Tip>

## Next Steps

<CardGroup cols={2}>
  <Card title="Transactions" icon="file-contract" href="/concepts/transactions">
    Learn the full transaction lifecycle
  </Card>

  <Card title="Keys & Signatures" icon="key" href="/concepts/keys-and-signatures">
    Understand authority and signing
  </Card>

  <Card title="Type Definitions" icon="code" href="https://github.com/openhive-network/hive-tx-js/blob/main/src/types.ts">
    Browse all operation types
  </Card>

  <Card title="Build a Voting App" icon="thumbs-up" href="/examples/voting">
    Practical operation examples
  </Card>
</CardGroup>
