create account

每天进步一点点:使用rust编程读取hive区块链 by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$48.30
每天进步一点点:使用rust编程读取hive区块链
前几天简单地了解了一下Rust语言,然后我就一直在思索,能用Rust编程做点啥呢?不能就写个Hello World啊,那学来也没啥意义呀。


![image.png](https://images.hive.blog/DQmYC7SCfexShnpzYKSdbJDo9PtT3RKeQUQ9sQUyQX66JAN/image.png)
(图源 :[pixabay](https://pixabay.com/photos/coding-programming-working-macbook-924920/))

然后我想,能不能用Rust写个程序,实现对HIVE区块链的访问(读取信息)以及操作(发布信息,转账、点赞等)?

当然,作为一个初学者,要实现这样的功能,无疑是高难度挑战,但是我们可以一点点来嘛,咋说来着,***九尺之台,起于垒土,千里之行,始于足下***。

要用rust实现对HIVE区块链的访问,大致有三个要点需要攻克(把大象装冰箱分几步?):
* 了解响应的API
* JSON编解码
* 网络访问

# 了解响应的API

我们可以在[Hive Developer Portal](https://developers.hive.io/)找到有关HIVE上各种API的详细信息。

以获取账户信息为例,相应的API为:[condenser_api.get_accounts](https://developers.hive.io/apidefinitions/#condenser_api.get_accounts),下边是使用curl访问API的一个简单示例:
>`curl -s --data '{"jsonrpc":"2.0", "method":"condenser_api.get_accounts", "params":[["hiveio"]], "id":1}' https://api.hive.blog`

其它各个API,基本上大同小异。

# JSON编解码

通过上述示例,我们不难发现,数据以JSON形式传递给API节点,相应的,返回数据也是JSON格式。

这样就需要我们的程序,对JSON数据进行编码和解码,以便于传输数据和解读数据。

在rust中,有很多JSON库可以使用,`serde_json`就是其中一个很好用的库。

# 网络访问

访问API节点的过程,其实就是把编码好的JSON数据发送到网络,并读取回复的过程。

比如前边我们用curl的例子,就是把相应的get_accounts的JSON数据,发送给https://api.hive.blog节点。

同样,在rust中有很多支持访问网络的库,据说较为常用的是`reqwest`库,但是我试着写了几行代码,没用明白。所以我选择了`curl`库。

# 测试代码

下边是一段简单的测试代码:

```
use curl::easy::Easy;
use serde_json::{json, from_str, to_string_pretty, Value};

fn main() {
    let account = "oflyhigh";
    let json_data = json!({
        "jsonrpc": "2.0",
        "method": "condenser_api.get_accounts",
        "params": [
            [account]
        ],
        "id": 1
    }).to_string();

    let mut easy = Easy::new();
    easy.url("https://api.hive.blog").unwrap();
    easy.post(true).unwrap();
    easy.post_field_size(json_data.len() as u64).unwrap();
    easy.post_fields_copy(json_data.as_bytes()).unwrap();

    let mut response = Vec::new();
    {
        let mut transfer = easy.transfer();
        transfer.write_function(|data| {
            response.extend_from_slice(data);
            Ok(data.len())
        }).unwrap();
        transfer.perform().unwrap();
    }

    let response_string = String::from_utf8(response).unwrap();
    let v:Value = from_str(&response_string).unwrap();
    let pretty_json = to_string_pretty(&v).unwrap();

    println!("{}", pretty_json);
}
```

若要成功编译上述代码,还需要在项目的Cargo.toml文件中添加如下内容:
```
[dependencies]
serde_json = "1.0"
curl = "0.4"
```
然后运行`cargo run`就会下载安装相应的依赖并编译运行程序,成功运行后,就会格式化输出我账户的全部内容啦。

限于篇幅,只截取部分内容:
>![image.png](https://images.hive.blog/DQmTnK7goFepA67cfRRreNLh6uopkhYjuLkKheR66esVyWh/image.png)

所以,貌似使用rust编程读取hive区块链也很简单的啦(中间之曲折我就不多哭诉了)。

小伙伴们可以开动啦,用rust做一个HIVE交易机器人咋样?帮我赔钱,呜呜呜。


# 相关链接
* [Hive Developer Portal](https://developers.hive.io/)
* [condenser_api.get_accounts](https://developers.hive.io/apidefinitions/#condenser_api.get_accounts)
* https://crates.io/crates/curl
* https://crates.io/crates/serde_json
* https://docs.rs/serde_json/latest/serde_json/
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 118 others
👎  
properties (23)
authoroflyhigh
permlinkrust-hive
categoryhive-105017
json_metadata{"tags":["cn","life","blog","rust","cn-programming","programming"],"image":["https://images.hive.blog/DQmYC7SCfexShnpzYKSdbJDo9PtT3RKeQUQ9sQUyQX66JAN/image.png","https://images.hive.blog/DQmTnK7goFepA67cfRRreNLh6uopkhYjuLkKheR66esVyWh/image.png"],"links":["https://pixabay.com/photos/coding-programming-working-macbook-924920/"],"app":"hiveblog/0.1","format":"markdown"}
created2022-12-20 09:24:36
last_update2022-12-20 09:24:36
depth0
children11
last_payout2022-12-27 09:24:36
cashout_time1969-12-31 23:59:59
total_payout_value24.160 HBD
curator_payout_value24.136 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,868
author_reputation6,395,273,341,971,803
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,247,853
net_rshares119,471,190,529,156
author_curate_reward""
vote details (183)
@aellly ·
>当然,作为一个初学者,要实现这样的功能,无疑是高难度挑战,

从平时你的帖子来看,从头看到脚。
怎么看都看不出来你是个初学者。大佬级别我倒是信了。
properties (22)
authoraellly
permlinkre-oflyhigh-20221220t214740763z
categoryhive-105017
json_metadata{"tags":["cn","life","blog","rust","cn-programming","programming"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2022-12-20 13:47:39
last_update2022-12-20 13:47:39
depth1
children0
last_payout2022-12-27 13:47:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length74
author_reputation874,216,966,497,117
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,252,060
net_rshares0
@deanliu ·
>use curl::easy::Easy;

也太過驕傲了,一開頭就說curl代碼很簡單是吧... 😠
properties (22)
authordeanliu
permlinkre-oflyhigh-rn6qfh
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2022.12.1"}
created2022-12-20 10:28:15
last_update2022-12-20 10:28:15
depth1
children2
last_payout2022-12-27 10:28:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length52
author_reputation3,100,979,937,647,200
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,248,724
net_rshares0
@aellly ·
艾玛,刘美女也是IT的。
properties (22)
authoraellly
permlinkre-deanliu-20221220t214823212z
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2022-12-20 13:48:24
last_update2022-12-20 13:48:24
depth2
children0
last_payout2022-12-27 13:48:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation874,216,966,497,117
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,252,077
net_rshares0
@oflyhigh ·
不好,一下子就被你发现大秘密了
properties (22)
authoroflyhigh
permlinkrn6rf2
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2022-12-20 10:49:51
last_update2022-12-20 10:49:51
depth2
children0
last_payout2022-12-27 10:49:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length15
author_reputation6,395,273,341,971,803
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,248,971
net_rshares0
@lilacse ·
没想到这里也能看见rust,还真是越来越普及了。

曾经有兴趣过,但是看到那个语法是有点提不起劲… 感觉回到了写C++的时代啊哈哈。
properties (22)
authorlilacse
permlinkre-oflyhigh-20221220t2015485z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog","rust","cn-programming","programming"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2022-12-20 12:01:06
last_update2022-12-20 12:01:06
depth1
children1
last_payout2022-12-27 12:01:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length66
author_reputation19,250,085,524,596
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,250,120
net_rshares0
@oflyhigh ·
哈哈,我是初学者
了解一下
properties (22)
authoroflyhigh
permlinkrn8p1r
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2022-12-21 11:53:54
last_update2022-12-21 11:53:54
depth2
children0
last_payout2022-12-28 11:53:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation6,395,273,341,971,803
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,274,421
net_rshares0
@love5200 ·
唉,又是看不懂系列😂
properties (22)
authorlove5200
permlinkre-oflyhigh-20221221t05527887z
categoryhive-105017
json_metadata{"tags":["cn","life","blog","rust","cn-programming","programming"],"app":"ecency/3.0.30-vision","format":"markdown+html"}
created2022-12-20 16:56:06
last_update2022-12-20 16:56:06
depth1
children0
last_payout2022-12-27 16:56:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation544,881,010,390,945
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,255,779
net_rshares0
@lovelingling ·
虎熊熊来O哥这报个到。😂
properties (22)
authorlovelingling
permlinkre-oflyhigh-20221220t185139593z
categoryhive-105017
json_metadata{"tags":["hive-105017","cn","life","blog","rust","cn-programming","programming"],"app":"ecency/3.0.19-mobile","format":"markdown+html"}
created2022-12-20 10:51:39
last_update2022-12-20 10:51:39
depth1
children1
last_payout2022-12-27 10:51:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation733,241,648,985,367
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,248,995
net_rshares0
@oflyhigh ·
我不喜欢熊熊,我喜欢牛牛😂
properties (22)
authoroflyhigh
permlinkrn8p2i
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2022-12-21 11:54:21
last_update2022-12-21 11:54:21
depth2
children0
last_payout2022-12-28 11:54:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation6,395,273,341,971,803
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,274,430
net_rshares0
@tgarcialvarez ·
We have seen that you have previously supported proposals for the improvement of Hive.

We would love to have your support in our proposal that seeks to add a new tool to the Hive environment that will help ensure that your content continues to prioritize quality and above all originality.

You can vote here:
[Peakd](https://peakd.com/me/proposals/252)
[Ecency](https://ecency.com/proposals/252)

We would appreciate your support, criticism, and collaboration. Thank you for considering this proposal.
properties (22)
authortgarcialvarez
permlinkre-oflyhigh-rnfsp6
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2022.12.1"}
created2022-12-25 22:31:15
last_update2022-12-25 22:31:15
depth1
children0
last_payout2023-01-01 22:31:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length503
author_reputation5,546,142,716,574
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,381,644
net_rshares0
@xiaoyaodidi ·
进步使人变强
properties (22)
authorxiaoyaodidi
permlinkre-oflyhigh-20221220t192432328z
categoryhive-105017
json_metadata{"tags":["cn","life","blog","rust","cn-programming","programming"],"app":"ecency/3.0.35-mobile","format":"markdown+html"}
created2022-12-20 11:24:30
last_update2022-12-20 11:24:30
depth1
children0
last_payout2022-12-27 11:24:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6
author_reputation420,725,591,905,600
root_title每天进步一点点:使用rust编程读取hive区块链
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,249,449
net_rshares0