This repository demonstrates three smart contract written in C++ that run on the EOSIO blockchain.
The 3 contracts: recordbook, talk/chat, tictactoe gameplay.
The smart contracts exist on their own right now, however, the logical next step would be to integrate all three into a compete decentralized application. The following demonstrates how to deploy and interact with the smart contracts on a local single-node blockchain. These contracts can be deployed on the eosio testnet/mainnet blockchain if the contract owner desires.
-
Fork this repo to your personal GitHub account so that you can save your work into your personal Github account.
-
Point your browser to the following URL https://gitpod.io/#https://github.com/your-github-account/EOSIO-TicTacToe to start the IDE.
You can test drive the system by accessing the IDE at https://gitpod.io/#https://github.com/colinsteidtmann/EOSIO-TicTacToe (however you will not be able to save your work into the EOSIO-TicTacToe Github repository)
The following instructions assume that the Web IDE was started successfully (see Setup).
To open a terminal, use the Terminal drop-down menu in the IDE user interface.
╠═contract - smart contracts
╠══ recordbook - demonstrates keeping track of game records
╠══ talk - demonstrates chats between users and tests
╠══ tictactoe - demonstrates tactoe game
╚═webapp - UI to interact with talk smart contracts
Run this in a terminal:
cleos create account eosio bob EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos create account eosio jane EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CVThe source code for the recordbook smartcontract is at contract/recordbook/recordbook.cpp within the IDE. To compile the contract, run this in a terminal:
eosio-cpp contract/recordbook/recordbook.cpp -o contract/recordbook/recordbook.wasmThis will produce recordbook.abi and recordbook.wasm in the contract/recordbook/ directory.
Installing the contract
Run this in a terminal:
cleos create account eosio recordbook EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos set code recordbook contract/recordbook/recordbook.wasm
cleos set abi recordbook contract/recordbook/recordbook.abiAdding users
cleos push action recordbook adduser '[bob]' -p bob
cleos push action recordbook adduser '[jane]' -p janeView users
cleos get table recordbook recordbook usersshould output:
{ "rows": [{ "key": "bob", "gamesPlayed": 0, "wins": 0, "losses": 0, "ties": 0 },{ "key": "jane", "gamesPlayed": 0, "wins": 0, "losses": 0, "ties": 0 } ], "more": false }
Add record (win for bob)
cleos push action recordbook addrecord '{"winner":"bob", "loser":"jane", "tie":false}' -p recordbook@activeAdd record (tie)
cleos push action recordbook addrecord '{"winner":"bob", "loser":"jane", "tie":true}' -p recordbook@activeView updated user records
cleos get table recordbook recordbook usersshould output:
{ "rows": [{ "key": "bob", "gamesPlayed": 2, "wins": 1, "losses": 0, "ties": 1 },{ "key": "jane", "gamesPlayed": 2, "wins": 0, "losses": 1, "ties": 1 } ], "more": false }
Erase user and their record
cleos push action recordbook eraseuser '[bob]' -p bob
cleos get table recordbook recordbook usersshould output:
{ "rows": [{ "key": "jane", "gamesPlayed": 2, "wins": 0, "losses": 1, "ties": 1 } ], "more": false }
compile
eosio-cpp contract/talk/talk.cpp -o contract/talk/talk.wasminstall
cleos create account eosio talk EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos set code talk contract/talk/talk.wasm
cleos set abi talk contract/talk/talk.abiinteract
cleos push action talk post '[1000, 0, bob, "This is a new post"]' -p bob
cleos push action talk post '[2000, 0, jane, "This is my first post"]' -p jane
cleos push action talk post '[1001, 2000, bob, "Replying to your post"]' -p bob
cleos get table talk '' messageshould output:
{ "rows": [{ "id": 1000, "reply_to": 0, "user": "bob", "content": "This is a new post" },{ "id": 1001, "reply_to": 2000, "user": "bob", "content": "Replying to your post" },{ "id": 2000, "reply_to": 0, "user": "jane", "content": "This is my first post" } ], "more": false }
compile
eosio-cpp contract/tictactoe/tictactoe.cpp -o contract/tictactoe/tictactoe.wasminstall
cleos create account eosio tictactoe EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos set code tictactoe contract/tictactoe/tictactoe.wasm
cleos set abi tictactoe contract/tictactoe/tictactoe.abiinteract
#create game
cleos push action tictactoe create '{"challenger":"jane", "host":"bob"}' --permission bob@active
#host (bob) must make first move
cleos push action tictactoe move '{"challenger":"jane", "host":"bob", "by":"bob", "row":1, "column":0}' --permission bob@active
#game play
cleos push action tictactoe move '[jane, bob, jane, 0,0]' -p jane
cleos push action tictactoe move '[jane, bob, bob, 1,1]' -p bob
cleos push action tictactoe move '[jane, bob, jane, 0,1]' -p jane
#winning move!
cleos push action tictactoe move '[jane, bob, bob, 1,2]' -p bob
#error if jane tries to keep playing
cleos push action tictactoe move '[jane, bob, jane, 0,2]' -p jane
#show gameboard
cleos get table tictactoe bob games
#restart game
cleos push action tictactoe restart '{"challenger":"jane", "host":"bob", "by":"bob"}' --permission bob@active
#close game
cleos push action tictactoe close '{"challenger":"jane", "host":"bob"}' --permission bob@activethe winning gameboard should look like:
{ "rows": [{ "challenger": "jane", "host": "bob", "turn": "jane", "winner": "bob", "board": [ 2, 2, 0, 1, 1, 1, 0, 0, 0 ] } ], "more": false }
errors for invalid gameplay should look like:
Error Details: assertion failure with message: The game has ended.Error Details: assertion failure with message: it's not your turn yet!
The source code for the React WebApp is at webapp/src/index.tsx within the IDE. To preview the WebApp run this in a terminal:
gp preview $(gp url 8000)
The source code for the unit test is at the contract/talk/tests directory within the IDE. To build the tests, run this in the terminal:
./contract/talk/build-testsThis will produce the contract/talk/tester binary, which can be run from the terminal to start the actual unit test:
./contract/talk/testerThe unit test creates the talk_tests test suite and verifies that the following statements are executed without error:
-
Create user account
talk. -
Load the
talksmart contract in thetalkaccount sandbox. -
Create user accounts
johnandjane. -
Test the
postaction by performing the following: -
Push the
postaction fromtalktojohnwith message "post 1" identified as1and addressed to message0(sent by noone).
This posts the message 1 from john to noone in the chat.
- Push the
postaction fromtalktojanewith message "post 2" identified as2and addressed to message0(sent by noone).
This posts the message 2 from jane to noone in the chat.
- Push the
postaction fromtalktojohnwith message "post 3: reply" identified as3and addressed to message2(sent byjane).
This posts the reply message 3 from john to jane in the chat.
- Test failure of the
postaction if message is addressed to a non-existant message id.
To remove the existing chain and create another:
-
Switch to the terminal running
nodeos -
Press
ctrl+cto stop it -
Run the following
rm -rf ~/eosio/chain
nodeos --config-dir ~/eosio/chain/config --data-dir ~/eosio/chain/data -e -p eosio --plugin eosio::chain_api_plugin --contracts-console
Note: if the web app is currently open, then it will cause errors like the following. You may ignore them:
FC Exception encountered while processing chain.get_table_rows
See LICENSE for copyright and license terms.
All repositories and other materials are provided subject to the terms of this IMPORTANT notice and you must familiarize yourself with its terms. The notice contains important information, limitations and restrictions relating to our software, publications, trademarks, third-party resources, and forward-looking statements. By accessing any of our repositories and other materials, you accept and agree to the terms of the notice.
