EOS [EOS DAPP 开发进阶] 02 - 微文 DAPP 整合 Oracle 服务

pendingauth · 2019年09月06日 · 28 次阅读

本文转载自币乎,作者松果,原文链接: https://bihu.com/article/1241268165

上一篇文章介绍了如何使用Oracle为EOS智能合约引入链外数据,使用的是Provable(oraclize)提供的Oracle服务;

这篇文章将会把Oracle服务整合到微文DAPP中,为了和【从零开始DAPP开发】系列区分开,拷贝之前项目的代码,重建一个weiwen-dapp2项目,Github地址如下:

https://github.com/songguo6/weiwen-dapp2

微文DAPP整合Oracle服务

现在添加这样一个功能:发微文时记录当时的EOS价格,需要用到上一篇文章中使用的oraclize提供的Oracle服务。步骤如下:

1、在Jungle测试网为weiwen-dapp2项目重新注册一个合约账号(weiwendapps2)和一个通证合约账号(weiwentoken2);

2、修改weiwen-dapp2项目源码中的合约名,使用全局搜索查找替换:

weiwendappss -> weiwendapps2
weiwentokens -> weiwentoken2

3、编译、部署通证合约:

cd weiwentoken2
eosio-cpp -I . -o weiwentoken2.wasm weiwentoken2.cpp --abigen
cd ..
cleos -u http://jungle2.cryptolions.io set contract weiwentoken2 weiwentoken2 -p weiwentoken2

4、创建WEI代币

cleos -u http://jungle2.cryptolions.io push action weiwentoken2 create '["weiwendapps2", "100000000000.0000 WEI"]' -p weiwentoken2

这里指定WEI代币的发行账户为weiwendapps2,发行总量为1000亿。weiwentokens合约和weiwentoken2合约发行的代币都叫"WEI",但并不冲突,因为转账时调用的合约不同。

5、从oraclize Github克隆eos-api.hpp头文件到合约目录weiwendapps2,并修改eos-api的配置:

6、修改微文表(posttable),增加一项发文时EOS价格字段(eos_price):

7、修改weiwendapps2::post Action代码:

ACTION post(name author, const std::string& content, uint32_t attachtype, const std::string& attachment) {
  require_auth(author);

  check_user(author);
  if(attachtype != 0){
    check(attachment != "", "attachment can not be empty");
  }

  post_t posts(_self, _self.value);
  auto pitr = posts.emplace(author, [&](auto& post){
    post.id = posts.available_primary_key();
    post.author = author;
    post.content = content;
    post.attachtype = attachtype;
    post.attachment = attachment;
    post.eos_price = std::string("");            
    post.time = time_point_sec(current_time_point()); 
    post.balance = asset(0, TOKEN_SYMBOL);
    post.like_num = 0;
    post.comment_num = 0;
  });

  auto itr = users.find(author.value);
  users.modify(itr, same_payer, [&](auto& user){
    user.post_num++;
  });

  checksum256 query_id = oraclize_query("URL", "json(https://api.binance.com/api/v3/ticker/price?symbol=EOSUSDT).price");
  map_t maps(_self, _self.value);
  maps.emplace(author, [&](auto& map){
    map.post_id = pitr->id;
    map.query_id = query_id;
  });
}

8、增加callback Action处理请求到数据的回调:

ACTION callback(checksum256 queryId, std::vector<unsigned char> result, std::vector<unsigned char> proof){
  require_auth(provable_cbAddress());

  std::string price = vector_to_string(result);

  map_t maps(_self, _self.value);
  auto secondary = maps.get_index<"byqueryid"_n>();
  auto itr = secondary.find(queryId);

  post_t posts(_self, _self.value);
  auto pitr = posts.find(itr->post_id);

  posts.modify(pitr, _self, [&](auto& post){
    post.eos_price = price;
  });
}

9、因为oraclize_query和callback不支持传参,需要再定义一张multi_index表来保存post_id到queryId的映射:

TABLE postquerymap {
  uint64_t post_id;
  checksum256 query_id;

  uint64_t primary_key() const { return post_id; }
  checksum256 get_secondary_1() const { return query_id; }
};

typedef multi_index<"postquerymap"_n, postquerymap,
  indexed_by<"byqueryid"_n, const_mem_fun<postquerymap, checksum256, &postquerymap::get_secondary_1>>
> map_t;

10、编译、部署weiwendapps2合约:

cd weiwendapps2
eosio-cpp -I . -o weiwendapps2.wasm weiwendapps2.cpp --abigen
cd ..
cleos -u http://jungle2.cryptolions.io set contract weiwendapps2 weiwendapps2 -p weiwendapps2
cleos -u http://jungle2.cryptolions.io set account permission weiwendapps2 active --add-code


Tips

项目代码在Github同步更新:https://github.com/songguo6/weiwen-dapp2

更多内容

EOS开发系列目录

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册