# HiveApps Operations in Transaction interface

The Hive blockchain enables advanced operations using custom_json. These operations are standardized and recognized by the Hive Ecosystem, such as Hivemind. This allows us to utilize advanced features that go beyond typical Hive blockchain operations. These custom operations are known as HiveApps operations.

# What is a HiveApps Operation?

A HiveApps operation is essentially a custom_json operation. Through these operations, we can execute custom actions on the Hive blockchain which are recognized and processed by the Hive infrastructure, like Hivemind.

# Creating HiveApps Operations

To facilitate the construction and management of these operations, we use the HiveAppsOperation classes. Below are specific classes for different types of HiveApps operations:

# 1. ResourceCreditsOperation

This operation class is used to manage Resource Credits (RC) delegations. Resource Credits allow actions to be performed on the Hive blockchain without paying transaction fees.

# Example usage:

If you want to help a friend perform more operations on their account, you can delegate some of your Resource Credits to them. Meanwhile the other friends does not need your help anymore, so you can remove the delegation.

import { createHiveChain, ResourceCreditsOperation } from '@hiveio/wax';

// Create chain
const chain = await createHiveChain();

// Create transaction with data from remote
const tx = await chain.createTransaction();

const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; // It should be the public key of the account that you authorize the operation

// Your account name
const yourAccount = 'your-account';

// Friend's account
const friend = 'your-friend-account';

// Other friend's account
const otherFriend = 'other-friend-account'

// Create resource credits operation new instance
const rcOperation = new ResourceCreditsOperation();

// Push operations of resource credits operation into the created transaction
tx.pushOperation(
  rcOperation
  // Delegate 1000 RC from your account to a friend's account.
  .delegate(yourAccount, 1000, friend)
  // The account that authorizes underlying custom json operation is also reponsible for signing the transaction using its posting authority
  .authorize(yourAccount)
);

// Sign and build the transaction
tx.sign(wallet, publicKey1);

const otherTx = await chain.createTransaction();

otherTx.pushOperation(
  rcOperation
    // Remove delegation of RC from your account to a friend's account.
    .removeDelegation(yourAccount, otherFriend)
    .authorize(yourAccount) // The account that authorizes the operation must also sign the transaction// Build the current set of hive apps operation ready to be pushed into the transaction
);

// Sign and build the other transaction
otherTx.sign(wallet, publicKey1);

console.log(otherTx.transaction.operations[0]); // Delegate operation
console.log(otherTx.transaction.operations[1]); // Remove delegation operation
// Delegate
{
  custom_json: {
    id: 'rc',
    json: '["delegate_rc",{"from":"your-account","delegatees":["your-friend-account"],"max_rc":"1000","extensions":[]}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

// Remove delegation
{
  custom_json: {
    id: 'rc',
    json: '["delegate_rc",{"from":"your-account","delegatees":["other-friend-account"],"max_rc":"0","extensions":[]}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

# 2. FollowOperation

This operation class handles operations related to following, muting, blacklisting blogs, and reblogging posts.

# Example usage:

Here is an example of complex FollowOperation usage:

import { createHiveChain, FollowOperation } from '@hiveio/wax';

// Create chain
const chain = await createHiveChain();

// Create transaction with data from remote
const tx = await chain.createTransaction();

const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; // It should be the public key of the account that you authorize the operation

// Your account name
const yourAccount = 'your-account';

// Blog author name to follow
const blogToFollow = 'interesting-blog';

// Blog author name to mute
const blogToMute = 'spammer';

// Author of post to reblog
const toReblog = 'reblog-me';

// Create follow operation new instance
const followOperation = new FollowOperation();

// Push operations of follow operation into the created transaction
tx.pushOperation(
  followOperation
    .followBlog(yourAccount, blogToFollow)
    .muteBlog(yourAccount, blogToMute)
    .reblog(yourAccount, toReblog, 'post-permlink')
    // The account that authorizes underlying custom json operation is also reponsible for signing the transaction using its posting authority
    .authorize(yourAccount)
);

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction.operations[0]); // Follow operation
console.log(tx.transaction.operations[1]); // Mute operation
console.log(tx.transaction.operations[2]); // Reblog operation
// Follow blog
{
  custom_json: {
    id: 'follow',
    json: '["follow",{"follower":"your-account","following":"interesting-blog","what":["blog"]}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

// Mute blog
{
  custom_json: {
    id: 'follow',
    json: '["follow",{"follower":"your-account","following":"spammer","what":["ignore"]}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

// Reblog
{
  custom_json: {
    id: 'follow',
    json: '["reblog",{"account":"your-account","author":"reblog-me","permlink":"post-permlink"}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

# 3. CommunityOperation

This operation class manages operations specific to communities on the Hive blockchain.

# Example Usage:

# Community member:

You want to join a specific community to stay updated with its content and you want flag some post. Here is an example how you can do it:

import { createHiveChain, CommunityOperation } from '@hiveio/wax';

// Create chain
const chain = await createHiveChain();

// Create transaction with data from remote
const tx = await chain.createTransaction();

const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; // It should be the public key of the account that you authorize the operation

// Your account name
const yourAccount = 'your-account';

// Community name you want to join
const communityName = 'community-name'

// Create community operation new instance
const communityOperation = new CommunityOperation();

// Push operations of community operation into the created transaction
tx.pushOperation(
  communityOperation
    // Subscribe the community
    .subscribe(communityName)
    // Flag the post of the author (authoraccount) in the community (communityname) with the permlink (postpermlink)
    // Add notes regarding the violation (violation notes).
    .flagPost(communityName, 'author-account', 'post-permlink', 'violation notes')
    // The account that authorizes underlying custom json operation is also reponsible for signing the transaction using its posting authority
    .authorize(yourAccount)
);

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction.operations[0]); // Subscribe operation
console.log(tx.transaction.operations[1]); // Flag post operation
// Subscribe
{
  custom_json: {
    id: 'community',
    json: '["subscribe",{"community":"community-name"}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

// Flag post
{
  custom_json: {
    id: 'community',
    json: '["flagPost",{"community":"community-name","account":"author-account","permlink":"post-permlink","notes":"violation notes"}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}
# Updating Community Properties:

Since you are an community administrator, you can update its properties:

import { createHiveChain, CommunityOperation, ESupportedLanguages } from '@hiveio/wax';

// Create chain
const chain = await createHiveChain();

// Create transaction with data from remote
const tx = await chain.createTransaction();

const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData; // It should be the public key of the account that you authorize the operation

// Your account name
const yourAccount = 'your-account';

// Community name you want to join
const communityName = 'community-name'

// Create community operation new instance
const communityOperation = new CommunityOperation();

// Push operations of community operation into the created transaction
tx.pushOperation(
  communityOperation
    // Update the properties of the community
    .updateProps(communityName, {
        title: 'New Community Title',
        about: 'Community Description',
        is_nsfw: false,
        lang: ESupportedLanguages.ENGLISH,
        description: 'Detailed community description',
        flag_text: 'Post flagging rules'
    })
    // The account that authorizes underlying custom json operation is also reponsible for signing the transaction using its posting authority
    .authorize(yourAccount)
);

// Sign and build the transaction
tx.sign(wallet, publicKey1);

console.log(tx.transaction.operations[0]); // Update community properties operation
{
  custom_json: {
    id: 'community',
    json: '["updateProps",{"community":"community-name","props":{"title":"New Community Title","about":"Community Description","description":"Detailed community description","flag_text":"Post flagging rules","is_nsfw":false,"lang":"en"}}]',
    required_auths: [],
    required_posting_auths: [ 'your-account' ]
  }
}

# Summary

HiveApps operations enable the execution of complex tasks on the Hive blockchain using custom_json. By utilizing the appropriate operation classes, users and developers can efficiently create, manage, and authorize these operations, ensuring smooth and standardized interactions in the Hive ecosystem. This simplifies and automates the management of communities, resource credits, and subscriptions.