Operation Formatters

Formatting RC delegation operations

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

const chain = await createHiveChain();

const tx = await chain.createTransaction();

tx.pushOperation(new ResourceCreditsOperation()
  .delegate("initminer", 4127361273, "gtg", "null")
  .removeDelegation("initminer", "null")
  .authorize("initminer"));

const output = chain.formatter.format(tx.transaction.operations);

console.log(output);
[{
  custom_json_operation: {
    delegatees: [ "gtg", "null" ],
    from: "initminer",
    rc: {
      amount: "4127361273",
      nai: "@@000000037",
      precision: 6,
    }
  }
}, {
  custom_json_operation: {
    delegatees: [ "null" ],
    from: "initminer",
    rc: {
      amount: "0",
      nai: "@@000000037",
      precision: 6,
    }
  }
}]

Not implemented yet — planned for a future release.

Test it yourself on github codespace: src/rust/formatters/operation_formatters/rc_delegate.rs
use wax::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create chain
    let chain = create_hive_chain(None)?;

    // Create transaction with data from remote
    let mut tx = chain.create_transaction(None).await?;

    // Create resource credits operation new instance
    let rc_operation = ResourceCreditsOperation::new()
        .delegate("initminer", 4127361273, vec!["gtg".into(), "null".into()])?
        .remove_delegation("initminer", vec!["null".into()])?
        .authorize(vec!["initminer".into()], Vec::new())?;

    // Push operations of resource credits operation into the created transaction
    tx.push_complex_operation(&chain, rc_operation)?;

    let output = chain.formatter().format(&tx.transaction().operations)?;

    println!("{}", serde_json::to_string_pretty(&output)?);

    Ok(())
}
[
  {
    "custom_json_operation": {
      "delegatees": ["gtg", "null"],
      "from": "initminer",
      "rc": {
        "amount": "4127361273",
        "nai": "@@000000037",
        "precision": 6
      }
    }
  },
  {
    "custom_json_operation": {
      "delegatees": ["null"],
      "from": "initminer",
      "rc": {
        "amount": "0",
        "nai": "@@000000037",
        "precision": 6
      }
    }
  }
]

Formatting community operations

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

const chain = await createHiveChain();

const tx = await chain.createTransaction();

tx.pushOperation(new CommunityOperation()
  .flagPost("mycomm", "gtg", "first-post", "note")
  .mutePost("mycomm", "gtg", "first-post", "note")
  .pinPost("mycomm", "gtg", "first-post")
  .subscribe("mycomm")
  .unmutePost("mycomm", "gtg", "first-post", "note")
  .unpinPost("mycomm", "gtg", "first-post")
  .unsubscribe("mycomm")
  .setUserTitle("mycomm", "gtg", "first-post")
  .updateProps("mycomm", { title: "Custom title" })
  .authorize("gtg"));

const output = chain.formatter.format(tx.transaction.operations);

console.log(output);
[{
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.FLAG_POST,
      account: "gtg",
      permlink: "first-post",
      notes: "note",
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.MUTE_POST,
      account: "gtg",
      permlink: "first-post",
      notes: "note",
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.PIN_POST,
      account: "gtg",
      permlink: "first-post",
      notes: undefined,
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.SUBSCRIBE,
      account: undefined,
      permlink: undefined,
      notes: undefined,
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.UNMUTE_POST,
      account: "gtg",
      permlink: "first-post",
      notes: "note",
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.UNPIN_POST,
      account: "gtg",
      permlink: "first-post",
      notes: undefined,
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.UNSUBSCRIBE,
      account: undefined,
      permlink: undefined,
      notes: undefined,
      props: undefined,
      title: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.SET_USER_TITLE,
      account: "gtg",
      title: "first-post",
      permlink: undefined,
      notes: undefined,
      props: undefined
    }
  }
}, {
  custom_json_operation: {
    accounts: [ "gtg" ],
    community: "mycomm",
    data: {
      action: ECommunityOperationActions.UPDATE_PROPS,
      props: {
        about: "",
        description: "",
        title: "Custom title",
        flag_text: "",
        is_nsfw: false,
        lang: "en",
      },
      account: undefined,
      permlink: undefined,
      notes: undefined,
      title: undefined
    }
  }
}]

Not implemented yet — planned for a future release.

The formatted tree is plain JSON data — actions are their wire-form strings and absent optional fields are simply omitted (there is no undefined in serde_json).

Test it yourself on github codespace: src/rust/formatters/operation_formatters/community.rs
use wax::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create chain
    let chain = create_hive_chain(None)?;

    // Create transaction with data from remote
    let mut tx = chain.create_transaction(None).await?;

    // Create community operation new instance
    let community_operation = CommunityOperation::new()
        .flag_post("mycomm", "gtg", "first-post", "note")
        .mute_post("mycomm", "gtg", "first-post", "note")
        .pin_post("mycomm", "gtg", "first-post")
        .subscribe("mycomm")
        .unmute_post("mycomm", "gtg", "first-post", "note")
        .unpin_post("mycomm", "gtg", "first-post")
        .unsubscribe("mycomm")
        .set_user_title("mycomm", "gtg", "first-post")
        .update_props(
            "mycomm",
            CommunityProps {
                title: "Custom title".into(),
                ..Default::default()
            },
        )
        .authorize(vec!["gtg".into()], Vec::new())?;

    // Push operations of community operation into the created transaction
    tx.push_complex_operation(&chain, community_operation)?;

    let output = chain.formatter().format(&tx.transaction().operations)?;

    println!("{}", serde_json::to_string_pretty(&output)?);

    Ok(())
}
[
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "flagPost",
        "notes": "note",
        "permlink": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "mutePost",
        "notes": "note",
        "permlink": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "pinPost",
        "permlink": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "action": "subscribe"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "unmutePost",
        "notes": "note",
        "permlink": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "unpinPost",
        "permlink": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "action": "unsubscribe"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "account": "gtg",
        "action": "setUserTitle",
        "title": "first-post"
      }
    }
  },
  {
    "custom_json_operation": {
      "accounts": ["gtg"],
      "community": "mycomm",
      "data": {
        "action": "updateProps",
        "props": {
          "about": "",
          "description": "",
          "flag_text": "",
          "is_nsfw": false,
          "lang": "en",
          "title": "Custom title"
        }
      }
    }
  }
]

Formatting follow operations

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

const chain = await createHiveChain();

const tx = await chain.createTransaction();

tx.pushOperation(new FollowOperation()
  .followBlacklistBlog("initminer", "gtg", "null")
  .followMutedBlog("initminer", "gtg")
  .resetAllBlog("initminer", "gtg", "null")
  .resetBlacklistBlog("initminer", "gtg")
  .resetFollowBlacklistBlog("initminer", "gtg", "null")
  .resetFollowMutedBlog("initminer", "gtg")
  .unblacklistBlog("initminer", "gtg", "null")
  .unfollowBlacklistBlog("initminer", "gtg")
  .unfollowBlog("initminer", "gtg", "null")
  .unfollowMutedBlog("initminer", "gtg")
  .reblog("initminer", "gtg", "first-post")
  .authorize("initminer"));

const output = chain.formatter.format(tx.transaction.operations);

console.log(output);
[{
  custom_json_operation: {
    action: EFollowActions.FOLLOW_BLACKLIST,
    follower: "initminer",
    following: [
      "gtg",
      "null"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.FOLLOW_MUTED,
    follower: "initminer",
    following: [
      "gtg"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.RESET_ALL_LISTS,
    follower: "initminer",
    following: [
      "gtg",
      "null"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.RESET_BLACKLIST,
    follower: "initminer",
    following: [
      "gtg"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.RESET_FOLLOW_BLACKLIST,
    follower: "initminer",
    following: [
      "gtg",
      "null"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.RESET_FOLLOW_MUTED_LIST,
    follower: "initminer",
    following: [
      "gtg"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.UNBLACKLIST,
    follower: "initminer",
    following: [
      "gtg",
      "null"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.UNFOLLOW_BLACKLIST,
    follower: "initminer",
    following: [
      "gtg"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.UNFOLLOW,
    follower: "initminer",
    following: [
      "gtg",
      "null"
    ]
  }
}, {
  custom_json_operation: {
    action: EFollowActions.UNFOLLOW_MUTED,
    follower: "initminer",
    following: [
      "gtg"
    ]
  }
}, {
  custom_json_operation: {
    account: "initminer",
    author: "gtg",
    permlink: "first-post"
  }
}]

Not implemented yet — planned for a future release.

Test it yourself on github codespace: src/rust/formatters/operation_formatters/follow.rs
use wax::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create chain
    let chain = create_hive_chain(None)?;

    // Create transaction with data from remote
    let mut tx = chain.create_transaction(None).await?;

    // Create follow operation new instance
    let follow_operation = FollowOperation::new()
        .follow_blacklist_blog("initminer", vec!["gtg".into(), "null".into()])?
        .follow_muted_blog("initminer", vec!["gtg".into()])?
        .reset_all_blog("initminer", vec!["gtg".into(), "null".into()])?
        .reset_blacklist_blog("initminer", vec!["gtg".into()])?
        .reset_follow_blacklist_blog("initminer", vec!["gtg".into(), "null".into()])?
        .reset_follow_muted_blog("initminer", vec!["gtg".into()])?
        .unblacklist_blog("initminer", vec!["gtg".into(), "null".into()])?
        .unfollow_blacklist_blog("initminer", vec!["gtg".into()])?
        .unfollow_blog("initminer", vec!["gtg".into(), "null".into()])?
        .unfollow_muted_blog("initminer", vec!["gtg".into()])?
        .reblog("initminer", "gtg", "first-post")
        .authorize(vec!["initminer".into()], Vec::new())?;

    // Push operations of follow operation into the created transaction
    tx.push_complex_operation(&chain, follow_operation)?;

    let output = chain.formatter().format(&tx.transaction().operations)?;

    println!("{}", serde_json::to_string_pretty(&output)?);

    Ok(())
}
[
  {
    "custom_json_operation": {
      "action": "follow_blacklist",
      "follower": "initminer",
      "following": ["gtg", "null"]
    }
  },
  {
    "custom_json_operation": {
      "action": "follow_muted",
      "follower": "initminer",
      "following": ["gtg"]
    }
  },
  {
    "custom_json_operation": {
      "action": "reset_all_lists",
      "follower": "initminer",
      "following": ["gtg", "null"]
    }
  },
  {
    "custom_json_operation": {
      "action": "reset_blacklist",
      "follower": "initminer",
      "following": ["gtg"]
    }
  },
  {
    "custom_json_operation": {
      "action": "reset_follow_blacklist",
      "follower": "initminer",
      "following": ["gtg", "null"]
    }
  },
  {
    "custom_json_operation": {
      "action": "reset_follow_muted_list",
      "follower": "initminer",
      "following": ["gtg"]
    }
  },
  {
    "custom_json_operation": {
      "action": "unblacklist",
      "follower": "initminer",
      "following": ["gtg", "null"]
    }
  },
  {
    "custom_json_operation": {
      "action": "unfollow_blacklist",
      "follower": "initminer",
      "following": ["gtg"]
    }
  },
  {
    "custom_json_operation": {
      "action": "",
      "follower": "initminer",
      "following": ["gtg", "null"]
    }
  },
  {
    "custom_json_operation": {
      "action": "unfollow_muted",
      "follower": "initminer",
      "following": ["gtg"]
    }
  },
  {
    "custom_json_operation": {
      "account": "initminer",
      "author": "gtg",
      "permlink": "first-post"
    }
  }
]

Formatting witness properties object

You can also parse the serialized witness_set_properties operation - props property from the chain:

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

const chain = await createHiveChain();

// Data from blockchain
const witness_props = {
  type: "witness_set_properties_operation",
  value: {
    owner: "null",
    props: [
      ["new_signing_key","3553544d365471534a61533161526a367036795a456f357869635"
                       + "83762764c6872666456716935546f4e724b78485533465242456457"],
      ["key", "029072da2e84ebd6eb520f944db3d1af718500b0f1ddf60e11e986f990acddd524"]
    ],
    extensions: []
  }
};

const output = chain.formatter.format(witness_props);

console.log(output.value.props);
{
  key: 'STM5z76mjZJnTZHHZjgnFxFadTb1ztc6R7EuDgCzd6dNiv6ETB2tj',
  new_signing_key: 'STM2nf9tLEWSdisd5pjocs2odhD3PvsnJTfMmK7Gm2Z2x8CvpXs1WC',
  account_creation_fee: undefined,
  url: undefined,
  hbd_exchange_rate: undefined,
  maximum_block_size: undefined,
  hbd_interest_rate: undefined,
  account_subsidy_budget: undefined,
  account_subsidy_decay: undefined
}

Not implemented yet — planned for a future release.

Only the properties present in the packed props appear in the output (there is no undefined in serde_json).

use serde_json::json;
use wax::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wax_api = create_wax_foundation(None);

    // Data from blockchain
    let witness_props = json!({
        "type": "witness_set_properties_operation",
        "value": {
            "owner": "null",
            "props": [
                ["new_signing_key", "3553544d365471534a61533161526a367036795a456f35786963583762764c6872666456716935546f4e724b78485533465242456457"],
                ["key", "029072da2e84ebd6eb520f944db3d1af718500b0f1ddf60e11e986f990acddd524"]
            ],
            "extensions": []
        }
    });

    let output = wax_api.formatter().format(&witness_props)?;

    println!("{}", serde_json::to_string_pretty(&output["value"]["props"])?);

    Ok(())
}
{
  "key": "STM5z76mjZJnTZHHZjgnFxFadTb1ztc6R7EuDgCzd6dNiv6ETB2tj",
  "new_signing_key": "STM2nf9tLEWSdisd5pjocs2odhD3PvsnJTfMmK7Gm2Z2x8CvpXs1WC"
}