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

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

  1. 【问链-EOS公开课】第九课 EOS 数据库与持久化 API(二)

    上次的文章详细讲解了 EOS 数据库的架构,本文将以官方示例为基础,详解 EOS 数据库的开发实战. 基本步骤 在智能合约里与 EOS 数据库交互,首先要定义存储的数据: 定义对象:具体就是定义一个 ...

  2. 【问链-Eos公开课】第一课 EOS简介

    一.Eos简介 EOS(Enterprise Operation System),企业操作系统,是为企业级分布式应用设计的一款区块链操作系统.相比于目前区块链平台性能低.开发难度大以及手续费高等问题, ...

  3. 【问链-Eos公开课】第二课 EOS环境搭建(Ubuntu系统下)

    1.EOS三个组件: nodeos:服务端区块链节点组件,也就是区块生产节点,用于接受客户端的远端请求,并打包区块,主要包含四个插件,chain_plugin.http_plugin.net_plug ...

  4. 北大AI公开课第十一课--语言智能的进展by微软亚洲研究院周明

    想做的事情很多,但是时间却很有限.所以我们更应当在开始任何事情之前,做好足够的计划和准备,这样才能在有限的时间里做出最恰当的选择,为自己真正值得做的事争取出更多时间.之所以有这个感悟是因为最近太多事情 ...

  5. 一块链习公开课:从 0 到 1 构建去中心化应用 | ArcBlock 预告

    2019 年 6 月 16 日 上午 10 点,ArcBlock 区块基石研发副总裁陈天将出席"一块链习"主办的第 30 期<区块链技术公开课 100 讲>,探讨分享今 ...

  6. 盒子模型代码_果冻公开课第五课:五分钟理清盒模型的前世今生

    果冻公开课第五课:五分钟理清盒模型的前世今生 在前端程序员眼中,页面其实可以被视为一个个盒子组成的 那么,这些盒子是如何构建起整个页面的呢? 动画视频: 如何理解盒子模型 文字解析: 在上一节里面 我 ...

  7. 小学教师计算机课评课,信息技术公开课的评课

    信息技术公开课的评课 (即使没有听课的教师看本文也是有用的,能领会我的意思,并学到一点知识的.有几处是精彩之处,值得大家细看.) 用◎表示一点. 宏观指与本节无关的内容,如个人素质,总体印象,对课程理 ...

  8. python智能合约编程_NEO区块链编程日-用python来写智能合约

    活动信息: 主题:NEO区块链编程日-用python来写智能合约 时间:2018年5月26日13:00-18:30 地点:上海市杨浦区政学路77号 INNOSPACE 1楼 参与成员:NEO技术爱好者 ...

  9. 区块链技术进阶-深入详解以太坊智能合约语言 solidity(含源码)-熊丽兵-专题视频课程...

    区块链技术进阶-深入详解以太坊智能合约语言 solidity(含源码)-103人已学习 课程介绍         区块链开发技术进阶-深入详解以太坊智能合约语言 solidity视频培训教程:本课程是 ...

最新文章

  1. 肺炎疫情期间购买口罩小记
  2. DEDE如何调用指定文章ID来调用特定文档
  3. C#程序只允许运行一个实例的解决方案
  4. 你发这些什么目的_吸引女生冷知识:朋友圈发照片的“潜规则”,男生要懂
  5. 克服浮躁_设计思维:您克服并赢得低迷的最终工具。
  6. Scala程序将多行字符串转换为数组
  7. rabbitmq中文教程python版 - Topics
  8. Ext.js 进入页面直接加载数据、输入框设置初始值
  9. 避免畸形儿受孕时间有10忌
  10. 前端-【学习心得】-事件委托方法
  11. python查找指定字符所在行号_在python中获取当前位置所在的行号和函数名
  12. 十几减9的口算题_小学一年级下册十几减9数学教案
  13. jsch mysql_java Jsch实现跳板机操作远程数据库
  14. 计算机专业英语信息系统,信息系统项目管理师英语复习资料:计算机专业英语汇总[5]...
  15. 关于笔记本测试的面试准备 2021-10-11
  16. tplink控制上网设备_tp link无线路由器设置里的主人设备 访客网络
  17. android 表情工厂,表情工厂安卓版
  18. 计算机每天定时开机设置方法,电脑如何设置每天定时开机
  19. 加拿大电子计算机工程留学,【加中留学】加拿大计算机工程专业哪些大学比较好...
  20. java2048小游戏源码及解析_200行java代码实现2048小游戏

热门文章

  1. linux中强行执行root权限
  2. 什么是动态IP,动态IP服务怎么选?
  3. 计算机为什么无法使用高级恢复,电脑怎么还原系统,win7电脑还原系统高级恢复方法-...
  4. 计算机网络操作试题2级,计算机网络管理员操作模拟试题-二级.pdf
  5. 循序渐进全球化 镜像识别
  6. 前端拖拽drag的使用
  7. 3D建模师月薪2万也许是个坑,看完我慌了
  8. ID3算法详解及python实现
  9. 如何用Python控制LEGO Wedo 2.0的电机
  10. postgresql分库分表中间件开源实现方案