Hivejs开发实战指南

in #starnote21 days ago

Hivejs开发实战指南

Hive 是一个开源区块链平台,于 2020 年 3 月作为 Steem 区块链的分叉出现。 其创建目的是为内容创作者、开发者和用户提供一个去中心化的环境, 使他们能够在不受中心化干扰的情况下保留对其数据和互动的控制权。 该倡议由开发者和用户社区推动,他们希望创建一个优先考虑去中心化和用户所有权的平台。
-> 前往星空笔记

Sort:  

参考

https://api.openhive.network    @gtg
https://rpc.mahdiyari.info  @mahdiyari
https://api.deathwing.me        @deathwing

https://api.hive.blog           @blocktrades
https://techcoderx.com          @techcoderx
https://hive-api.arcange.eu @arcange
yarn add @hiveio/hive-js //^2.0.8
// npm install @hiveio/hive-js --save
//<script src="https://cdn.jsdelivr.net/npm/@hiveio/hive-js/dist/hive.min.js"></script>

import hive from '@hiveio/hive-js'
//初始化hive节点
const hiveConNode = 'https://api.openhive.network'
// const hiveConNode = 'https://rpc.mahdiyari.info'

hive.api.setOptions({ url: hiveConNode})

//使用测试网
hive.api.setOptions({
  address_prefix: 'TST',
  chain_id: '46d82ab7d8db682eb1959aed0ada039a6d49afa1602491f93dde9cac3e8e6c32',
  useTestNet: true,
});
Loading...

帐户数据几乎都在这里了,包括创建的时间,余额,点赞能量等等。

let result = await hive.api.getAccountsAsync([account])
console.log(256, result, result.length === 0)  //length为0表示帐户不存在
Loading...

发布文章或评论都是用的同一个函数,只是发帖的parent_author为空。

hive.broadcast.comment(wif, parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata, function(err, result) {
  console.log(err, result)
})  //也可用 commentAsync

//函数讲解
hive.broadcast.comment (
    wif,                 //发帖密钥
    parentAuthor,        // 如果是发帖则为空,如果是评论则为父帖作者
    parentPermlink,      // 如果是发帖则为主标签,如果是评论则为父帖地址
    author,              // 作者
    permlink,            // URL地址
    title,               // 标题 
    body,                // 内容
    jsonMetadata         // json
)
let wif = '5pfffff'
let parentAuthor = ''
let parentPermlink = 'cn'
let author = 'lemooljiang'
let permlink = 'kdk54gf'
let title = '这是标题'
let body = '这是正文' 
let tags = ['cn', 'miao', 'bitcoin']
let jsonMetadata = {"tags": tags, "dapp": "miao", "format": "markdown"}

hive.broadcast.comment(wif, parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata, function(err, result) {
  console.log(1234, err, result)
})

基本上所有的Hivejs函数都可以直接加上Async成为异步函数

let res = await hive.broadcast.commentAsync(wif, parentAuthor, parentPermlink, author, permlink, title, body, jsonMetadata) 

post.json_metadata还可以按需求写入其它的数据,有点类似MongoDB。这样的话易于对文章进行过滤,也可保存一些特殊的数据。

json_metadata: "{"tags":["cn","network-institute","markdown","html"],"app":"miao","format":"markdown"}"
eg:
let jsonMetadata = {"tags": tags, "dapp": "miao", "format": "markdown", "cover": hash, "text": text}
JSON.stringify(jsonMetadata)
Loading...
hive.broadcast.vote(wif, voter, author, permlink, weight, function(err, result) {
  console.log(err, result)
})

//函数讲解
hive.broadcast.vote (
    wif,         //发帖密钥
    voter,       //点赞人
    author,      //作者
    permlink,    // URL地址
    weight       //点赞比重,100*100
)

eg:
hive.broadcast.vote(
  "5KRSmEMffffff",
  "luck",
  "lemooljiang",
  '6ekczn-2019',
  10000,
  function(err, result) {
    console.log(8888, err, result)
  })
hive.api.getFollowers('lemooljiang', '', 'blog', 10, function(err, result) {
  console.log(111, err, result)
})
hive.api.getFollowing('lemooljiang', '', 'blog', 10, function(err, result) {
  console.log(222, err, result)
})
Loading...

getDiscussionsByAuthorBeforeDate 这个函数实现

//获取最新文章
hive.api.getDiscussionsByAuthorBeforeDate(author, startPermlink, beforeDate, limit, function(err, result) {
  console.log(err, result)
})

let author = "lemooljiang"
let beforeDate = new Date().toISOString().split('.')[0]
let startPermlink = null
let result = await hive.api.getDiscussionsByAuthorBeforeDateAsync(author, startPermlink, beforeDate, 10)
// console.log(655,result)  一次最多能取100篇
Loading...
hive.api.getContentReplies(author, permlink, function(err, result) {
  console.log(err, result)
})

eg:
hive.api.getContentReplies('lemooljiang', 'hivejs-dev', function(err, result) {
  console.log(err, result)
})

使用递归的方法获取

async function getAllReplies(author, permlink, res=[]) {
  let replies = await hive.api.getContentRepliesAsync(author, permlink)
  let children = []
  replies.forEach(item => {
    res.push(item)
    if(item.children > 0){
      //把得到的子数据塞进 .child 中
      children.push(getAllReplies(item.author, item.permlink, item.child=[]))
    }
  })
  await Promise.all(children)
  return res
}
let author = "lemooljiang"
let query = { limit : 30, start_author: author, start_permlink: ""}
hive.api.getDiscussionsByComments(query, function(err, result) {
  console.log(265, err, result)
}

从帐户历史中遍历获取

async function getToAuthorReplies(author, limit){
  //获取对作者的最新评论
  let replies = []
  let res = await hive.api.getAccountHistoryAsync(author, -1, limit)
  res.forEach(item => {
    if(item[1].op[0] == "comment" & item[1].op[1].parent_author == author){
      replies.push(item)
    }
  })
  return replies
}
hive.broadcast.transfer(wif, from, to, amount, memo, function(err, result) {
    console.log(err, result)
})
// wif,      from,       to,     amount, memo
// 转账私钥,发起转帐地址,转给谁, 金额,   备注
transfer("wif", "lemool", "lucky", "0.001 HBD", "You are awesome ! take some tokens")

eg:
async hiveTransfer(){
  let from = "lemooljiang"
  let to = "lucky"
  let wif = "5KNxxxxxxxxxxxxxxxx"
  // let ss = parseFloat(hivevalue).toFixed(3)
  // let amount = ss+' HIVE'
  let amount = "100.200 HIVE"  //必须是3位小数且带上单位的格式,
  let memo = "测试"
  await hive.broadcast.transferAsync(wif, from, to, amount, memo)
}
let author = "lemooljiang"
let permlink = "pvw4dotxg"
let res = await hive.api.getActiveVotesAsync(author, permlink)
console.log(566, res)
Loading...
Loading...
hive.api.getVestingDelegations('ned', '', 2, function(err, result) {
  console.log(err, result)
})  //null []

eg:
async getDelegateHP(){
  let user = 'lemooljiang'
  let res = await hive.api.getVestingDelegationsAsync(user, '', 2) 
  if(res.length == 0){
    return 0
  } else{
    return parseFloat(res[0].vesting_shares)
  }
 }