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