美文网首页
EOS Dice 官方合约填坑运行实录

EOS Dice 官方合约填坑运行实录

作者: dabao005 | 来源:发表于2018-09-19 12:49 被阅读0次

    此文章基于官方 README.md 对 eos/contracts/dice 合约进行部署调用,及一些官方文档与最新代码有冲突的地方

    合约说明

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

    1. 玩家 1 下注 1 EOS,并提交一个 SHA256 加密的密钥1
    2. 王家 2 下注 1 EOS,并提交一个 SHA256 加密的密钥2

    因为两玩家下注数量相同,所以匹配,游戏开始

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

    使用 Cleos 操作

    预备

    钱包导入两个密匙
    5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 5Jmsawgsp1tQ3GD6JyGCwy1dcvqKZgX6ugMVMdjirx85iv5VyPR

    设置 Bios 合约

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

    创建 eosio.token 账号

    cleos create account eosio eosio.token EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
    

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

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

    创建 dice 账号

    cleos create account eosio dice EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
    

    给 dice 设置 dice 合约

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

    创建 SYS 代币

    此处官方文档为创建 EOS 代币,但是现在版本本地的默认代币改为 SYS,如创建 EOS 则之后代码会报错 only core token allowed

    cleos push action eosio.token create '[ "eosio", "1000000000.0000 SYS", 0, 0, 0]' -p eosio.token
    

    创建 alice 账号

    cleos create account eosio alice EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
    

    创建 bob 账号

    cleos create account eosio bob EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
    

    空投 1000 个 SYS 给 alice

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

    空投 1000 个 SYS 给 bob

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

    alice 给 dice 设置转账权限

    此处官方文档为 "actor":"dice","permission":"active",应为 "actor":"dice","permission":"eosio.code",下面一样

    cleos set account permission alice active '{"threshold": 1,"keys": [{"key": "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p alice
    

    bob 给 dice 设置转账权限

    cleos set account permission bob active '{"threshold": 1,"keys": [{"key": "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4","weight": 1}],"accounts": [{"permission":{"actor":"dice","permission":"eosio.code"},"weight":1}]}' owner -p bob
    

    alice 存入 100 SYS 到合约

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

    bob 存入 100 SYS 到合约

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

    alice 生成一个密匙

    openssl rand  -hex 32
    28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905
    

    alice SHA256 加密密匙

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

    alice 下注 3 SYS

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

    bob 生成一个密匙

    openssl rand  -hex 32
    15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12
    

    bob SHA256 加密密匙

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

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

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

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

    cleos get table dice dice account
    {
      "rows": [{
          "owner": "alice",
          "eos_balance": "97.0000 SYS",
          "open_offers": 0,
          "open_games": 1
        },{
          "owner": "bob",
          "eos_balance": "97.0000 SYS",
          "open_offers": 0,
          "open_games": 1
        }
      ],
      "more": false
    }
    
    cleos get table dice dice game
    {
      "rows": [{
          "id": 1,
          "bet": "3.0000 SYS",
          "deadline": "1970-01-01T00:00:00",
          "player1": {
            "commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883",
            "reveal": "0000000000000000000000000000000000000000000000000000000000000000"
          },
          "player2": {
            "commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129",
            "reveal": "0000000000000000000000000000000000000000000000000000000000000000"
          }
        }
      ],
      "more": false
    }
    

    bob 公布密匙

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

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

    cleos get table dice dice game
    {
      "rows": [{
          "id": 1,
          "bet": "3.0000 SYS",
          "deadline": "2018-04-17T07:45:49",
          "player1": {
            "commitment": "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883",
            "reveal": "0000000000000000000000000000000000000000000000000000000000000000"
          },
          "player2": {
            "commitment": "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129",
            "reveal": "15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12"
          }
        }
      ],
      "more": false
    }
    

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

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

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

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

    alice 取出金额

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

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

    查看 alice 账户余额

    cleos get currency balance eosio.token alice sys
    1003.0000 SYS
    

    相关文章

      网友评论

          本文标题:EOS Dice 官方合约填坑运行实录

          本文链接:https://www.haomeiwen.com/subject/fpvvnftx.html