【问链-EOS公开课】第十一课 EOS 智能合约相互调用

news/2025/1/12 13:38:58/

EOS中合约之间是可以相互调用的,主要通过inline action完成合约之间的调用。

譬如在掷骰子游戏中,存在两个玩家先下注资金,然后比较骰子大小后决定胜负,赢的那一方将获得所有的下注资金。在eosio源代码eos/build/contract/dice 智能合约示例中,Alice和Bob是两个玩家,他们各自将资金账户里的3个EOS作为赌注,先抵押给Dice账户,然后在游戏决出胜负后,Dice账户就自动把赌注归还给胜方。这其中就涉及到代币的转账操作,因此很自然的可以想到,在deposit(下注抵押资金)和最后的withdraw(胜方赢取资金)这两个操作中,就可以通过调用eosio.token的transfer动作来完成。

dice合约说明
此合约实现了一个简单的骰子游戏,两位玩家各有 50% 几率获胜

玩家 1 下注 3 EOS,并提交一个 SHA256 加密的密钥1
玩家 2 下注 3 EOS,并提交一个 SHA256 加密的密钥2
因为两玩家下注数量相同,所以匹配,游戏开始

一位玩家公布密钥
五分钟倒计时后,如果第二位玩家没有公布密钥,则第一位玩家直接获胜
另一位玩家公布密钥,基于两个密钥决出获胜者,并支付赌注
游戏结束后,胜者可以取回奖励

下面是deposit 和 withdraw 的函数实现。其中均对eosio.token的action—— transfer进行了调用。

//@abi action  void deposit( const account_name from, const asset& quantity ) {//下注抵押资金eosio_assert( quantity.is_valid(), "invalid quantity" );eosio_assert( quantity.amount > 0, "must deposit positive quantity" );auto itr = accounts.find(from);//在账户列表中查找要转出代币的账号是否存在if( itr == accounts.end() ) {//不存在的话就插入账号列表itr = accounts.emplace(_self, [&](auto& acnt){acnt.owner = from;});}//对eosio.token的transfer进行合约调用,玩家开始下注资金//账户(from)对账户( _self)进行转账,对应于游戏中,抵押代币到action(permission_level{ from, N(active) },N(eosio.token), N(transfer),std::make_tuple(from, _self, quantity, std::string(""))).send();accounts.modify( itr, 0, [&]( auto& acnt ) {//修改转出账号的代币余额acnt.eos_balance += quantity;});}
//@abi actionvoid withdraw( const account_name to, const asset& quantity ) {//胜方赢取资金require_auth( to );eosio_assert( quantity.is_valid(), "invalid quantity" );eosio_assert( quantity.amount > 0, "must withdraw positive quantity" );auto itr = accounts.find( to );//在账户列表中查找要转入代币的账号是否存在eosio_assert(itr != accounts.end(), "unknown account");accounts.modify( itr, 0, [&]( auto& acnt ) {//转出代币eosio_assert( acnt.eos_balance >= quantity, "insufficient balance" );acnt.eos_balance -= quantity;});//对eosio.token的transfer进行合约调用,Dice主账号对胜利一方发送资金//代表在资金撤回操作中,通过调用eosio.token的transfer,账户(_self)对账户(to)进行转账,对应于游戏中,Dice主账户为胜利的一方发放所赢的赌注action(permission_level{ _self, N(active) },N(eosio.token), N(transfer),std::make_tuple(_self, to, quantity, std::string(""))).send();if( itr->is_empty() ) {accounts.erase(itr);}}

1.启动nodeos

nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin

2.设置 Bios 合约

cleos set contract eosio build/contracts/eosio.bios -p eosio

3. 部署eosio.token合约

cleos create account eosio eosio.token EOS8YMWk6vxawJEDML7mYXA3mGUap7L68L3GroGUpDhpbuadwHXom
返回结果:
executed transaction: 968f488926441009ecfaf899379a2c304a7e771b46a5536330a3bd5c747550a3  200 bytes  135 useosio <= eosio::newaccount            {"creator":"eosio","name":"eosio.token","owner":{"threshold":1,"keys":[{"key":"EOS8YMWk6vxawJEDML7mY...
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

4.给 eosio.token 设置 eosio.token 合约用于发币

cleos set contract eosio.token build/contracts/eosio.token -p eosio.token

5.创建 dice 账号

cleos create account eosio dice EOS7EjVuYKxPRFECDkzsMtD57ootfQUMGQKrtmdmS3GPvTJTcwfSi
执行结果:
executed transaction: c9d7de164bf4749d958502ff9202087ca7d45a7463f72f8ab96054c589a694fb  200 bytes  169 useosio <= eosio::newaccount            {"creator":"eosio","name":"dice","owner":{"threshold":1,"keys":[{"key":"EOS7EjVuYKxPRFECDkzsMtD57oot...
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

6.给 dice 设置 dice 合约

cleos set contract dice build/contracts/dice -p dice

7.创建EOS代币

cleos push action eosio.token create '["eosio","100000000.0000 EOS"]' -p eosio.token
执行结果:
executed transaction: ff0a266a3125331d13d796a26c78d388ff6242e78e23ba404f86df6a2f28fbb0  120 bytes  437 useosio.token <= eosio.token::create          {"issuer":"eosio","maximum_supply":"100000000.0000 EOS"}
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

8.创建alice和bob账户

cleos create account eosio alice EOS5JUBZXokmgHR7yHFgxoZdQZyfvu2oCHiPBeGUE3fQyZ9MHdmi8
执行结果:
executed transaction: aac0663610d3763bb9f282c507a0923e1a1b973741525257f6e012fdac143535  200 bytes  183 useosio <= eosio::newaccount            {"creator":"eosio","name":"alice","owner":{"threshold":1,"keys":[{"key":"EOS5JUBZXokmgHR7yHFgxoZdQZy...cleos create account eosio bob EOS6FMtV7gnbBLQ2CbD7J3YAKA8hY6u6jZ2KGtcy628z8QQ8MvXBz
执行结果:
executed transaction: d53956cc2fea6e85d67dee1a17b1f3e85b9838f9acd02ad6d87a9672443774e3  200 bytes  138 useosio <= eosio::newaccount            {"creator":"eosio","name":"bob","owner":{"threshold":1,"keys":[{"key":"EOS6FMtV7gnbBLQ2CbD7J3YAKA8hY...

9.空投 1000 个 EOS 给 alice

cleos push action eosio.token issue '[ "alice", "1000.0000 EOS", "" ]' -p eosio

10.空投 1000 个 EOS 给 bob

cleos push action eosio.token issue '[ "bob", "1000.0000 EOS", "" ]' -p eosio

11.alice 给 dice 设置转账权限

cleos set account permission alice active '{"threshold": 1,"keys": [{"key": "EOS7EjVuYKxPRFECDkzsMtD57ootfQUMGQKrtmdmS3GPvTJTcwfSi","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p alice
执行结果:
executed transaction: 86fde62595343963366db70d9f9f70edab12928437d622d0f9246e00a62687b1  184 bytes  327 useosio <= eosio::updateauth            {"account":"alice","permission":"active","parent":"owner","auth":{"threshold":1,"keys":[{"key":"EOS7...
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

12.给bob赋予转账权限

cleos set account permission bob active '{"threshold": 1,"keys": [{"key": "EOS7EjVuYKxPRFECDkzsMtD57ootfQUMGQKrtmdmS3GPvTJTcwfSi","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p bob
执行结果:
executed transaction: dc21bcf296a9e749b4488d4188dbe42cb4f85ea7ed639dabcccaa682bdeab2df  184 bytes  183 useosio <= eosio::updateauth            {"account":"bob","permission":"active","parent":"owner","auth":{"threshold":1,"keys":[{"key":"EOS7Ej...
warning: transaction executed locally, but may not be confirmed by the network yet    ] 

13.alice 存入 100 EOS 到合约

cleos push action dice deposit '[ "alice", "100.0000 SYS" ]' -p alice

14.bob 存入 100 EOS 到合约

cleos push action dice deposit '[ "bob", "100.0000 SYS" ]' -p bob

15.alice 生成一个密匙

openssl rand  -hex 32
执行结果:
28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905

16.alice SHA256 加密密匙

echo -n '28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905' | xxd -r -p | sha256sum -b | awk '{print $1}'
d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883

17.alice 下注 3 EOS

cleos push action dice offerbet '[ "3.0000 EOS", "alice", "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883" ]' -p alice

18.bob 生成一个密匙

openssl rand  -hex 32
15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12

19.bob SHA256 加密密匙

echo -n '15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12' | xxd -r -p | sha256sum -b | awk '{print $1}'
50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129

20.bob 下注 3 SYS,随后游戏开始

cleos push action dice offerbet '[ "3.0000 EOS", "bob", "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129" ]' -p bob

21. 游戏开始后,查看 Dice 合约的表数据

cleos get table dice dice account
{"rows": [{"owner": "alice","eos_balance": "97.0000 EOS","open_offers": 0,"open_games": 1},{"owner": "bob","eos_balance": "97.0000 EOS","open_offers": 0,"open_games": 1}],"more": false
}

22.cleos get table dice dice game

{"rows": [{"id": 1,"bet": "3.0000 EOS","deadline": "1970-01-01T00:00:00","player1": {"commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883","reveal": "0000000000000000000000000000000000000000000000000000000000000000"},"player2": {"commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129","reveal": "0000000000000000000000000000000000000000000000000000000000000000"}}],"more": false
}

23.bob 公布密匙

cleos push action dice reveal '[ "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129", "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12" ]' -p bob

24.现在的表数据,game 表有个截止时间(5分钟后)

cleos get table dice dice game
{"rows": [{"id": 1,"bet": "3.0000 EOS","deadline": "2018-10-31T06:03:56","player1": {"commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883","reveal": "0000000000000000000000000000000000000000000000000000000000000000"},"player2": {"commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129","reveal": "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12"}}],"more": false
}

25.alice 公布密匙,决出胜者,游戏结束,game 表中移除数据

cleos push action dice reveal '[ "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883", "28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905" ]' -p alice

26.游戏结束后,查看 account 表数据,账户余额

cleos get table dice dice account
{"rows": [{"owner": "alice","eos_balance": "103.0000 EOS","open_offers": 0,"open_games": 0},{"owner": "bob","eos_balance": "97.0000 EOS","open_offers": 0,"open_games": 0}],"more": false
}

27.alice 取出金额

此处需先 dice 合约对 eosio.token 授权,才能进行转账

   cleos set account permission dice active '{"threshold": 1,"keys": [{"key": "EOS7EjVuYKxPRFECDkzsMtD57ootfQUMGQKrtmdmS3GPvTJTcwfSi","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p dicecleos push action dice withdraw '[ "alice", "103.0000 EOS" ]' -p alice

28.查看 alice 账户余额

cleos get currency balance eosio.token alice eos
1003.0000 EOS

http://www.ppmy.cn/news/159318.html

相关文章

eos代码阅读笔记08- 产生块过程

链的控制器controller 包含很多重要功能。 class controller { struct config {flat_set<account_name> actor_whitelist;白名单flat_set<account_name> actor_blacklist;黑名单flat_set<account_name> contract_whitelist;合约白名单flat_set<acc…

EOS 环境搭建

1. 硬件软件环境 因为EOS的版本太多&#xff0c;所以你需要先知道如何查看EOS对应版本的相关资料。 打开EOS Github官网https://github.com/EOSIO/eos,如下图所示&#xff0c;点击master,然后点击tag&#xff0c;你将会看到不同的版本及其对应的文档。 我在我电脑安装过各种各…

EOS合约开发2-投票合约开发

前言 合约是DAPP的基础&#xff0c;因此在开发DAPP之前&#xff0c;我们需要明白我们的项目是否需要去中心话以及哪些数据需要去中心化&#xff0c;目前的公链还不支持所有的资源都去中心话&#xff0c;这样才能更好的设计我们的项目和合约。接下来将以去中心话的投票系统为例…

EOS项目编译及操作入门

本次EOS项目编译操作是在mac上安装的的centos7进行的&#xff0c;可能是虚拟机的问题&#xff0c;本次编译时间较长。下面是本次操作的整个流程。 一、虚拟机安装&#xff1a;具体安装步骤省略&#xff0c;看资料需要我分配了8G内存和50G硬盘&#xff0c;参见下图配置 二、首先…

EOS区块链技术开发(一)环境搭建

强烈建议直接去看EOSIO官网上的教程&#xff1a;https://developers.eos.io/ 不要看我的??博文了&#xff0c;以下博文就是我当时随便写写的&#xff0c;现在也没有什么时间详细修改。如果有问题可以提问。 注&#xff1a;由于EOSIO仍在不断更新&#xff0c;所以以下内容的…

[服务器图片上传demo]

目录 前言: Java 实现华为云服务器图片上传和下载的示例代码&#xff1a; 图片下载 Java 实现阿里云服务器图片上传和下载的示例代码&#xff1a; 图片下载 Java 实现腾讯云服务器图片上传和下载的示例代码&#xff1a; 图片下载 依赖: 前言: 记录一…

哪家蓝牙耳机性价比高?盘点2022无线蓝牙耳机性价比排行

说到无线蓝牙耳机相信大家都不陌生吧&#xff0c;这玩意儿可以算的上是我们除了充电头和数据线之外最常用的手机配件了。目前市面上的蓝牙耳机产品也是非常众多&#xff0c;品牌各异。为了给用户带来更加极致的使用体验&#xff0c;很多手机厂商都开始致力于蓝牙耳机的生产。但…

android手机性价比,最新安卓手机性价比排行:荣耀小米霸榜

【手机中国新闻】国内知名的跑分平台安兔兔&#xff0c;每月都会定期更新智能手机性能榜单和性价比榜单等&#xff0c;这为换机用户提供了非常好的参考。近日&#xff0c;安兔兔官方又为大家带来了最新一期的Android手机性价比排行榜单&#xff0c;根据不同价格区间分类&#x…