Giter VIP home page Giter VIP logo

example-tictactoe's Introduction

Build status

Tic-Tac-Toe on Solana

This project demonstrates how to use the Solana Javascript API to build, deploy, and interact with programs on the Solana blockchain, implementing an interactive tic-tac-toe game between two users. To see the final product, go to https://solana-example-tictactoe.herokuapp.com/ and wait for another player to join. (Direct a second browser window to the web app to play against yourself.)

The project comprises:

  • The on-chain Tic-Tac-Toe program, a BPF program written in Rust program-bpf-rust and C program-bpf-c
  • Easy program build and deployment using the @solana/web3.js library
  • Command-line and web front-end: src/

Learn about Solana

More information about how Solana works is available in the Book

Getting Started

The following dependencies are required to build and run this example, depending on your OS they may already be installed:

$ npm --version
$ docker -v
$ wget --version
$ rustc --version

Next fetch the npm dependencies, including @solana/web3.js, by running:

$ npm install

Select a Network

The example connects to a local Solana cluster by default.

To enable on-chain program logs, set the RUST_LOG environment variable:

$ export RUST_LOG=solana_runtime::native_loader=trace,solana_runtime::system_instruction_processor=trace,solana_runtime::bank=debug,solana_bpf_loader=debug,solana_rbpf=debug

To start a local Solana cluster run:

$ npm run localnet:update
$ npm run localnet:up

Solana cluster logs are available with:

$ npm run localnet:logs

To stop the local solana cluster run:

$ npm run localnet:down

For more details on working with a local cluster, see the full instructions.

Build the BPF program

$ npm run build:bpf-rust

or

$ npm run build:bpf-c

The compiler places output files in dist/program. Program build scripts contain the compiler settings and can be found in the Solana SDK

Run the Command-Line Front End

After building the program,

$ npm run start

This script uses the Solana Javascript API BpfLoader to deploy the Tic-Tac-Toe program to the blockchain. Once the deploy transaction is confirmed on the chain, the script calls the program to instantiate a new dashboard to track the open and completed games (findDashboard), and starts a new game (dashboard.startGame), waiting for an opponent.

To play the game, open a second terminal and again run the npm run start script.

To see the program or game state on the blockchain, send a getAccountInfo JSON-RPC request to the cluster, using the id printed by the script, eg.:

  • Dashboard programId: HFA4x4oZKWeGcRVbUYaCHM59i5AFfP3nCfc4NkrBvVtP
  • Dashboard: HmAEDrGpsRK2PkR51E9mQrKQG7Qa3iyv4SvZND9uEkdR
  • Advertising our game (Gx1kjBieYgaPgDhaovzvvZapUTg5Mz6nhXTLWSQJpNMv)

Run the WebApp Front End

After building the program,

$ npm run dev

This script deploys the program to the blockchain and also boots up a local webserver for gameplay.

To instantiate a dashboard and game, open your browser to http://localhost:8080/.

Customizing the Program

To customize Tic-Tac-Toe, make changes to the program in program-bpf-rust/src, rebuild it, and restart the network. Now when you run npm run start, you should see your changes.

To deploy a program with a different name, edit src/server/config.js.

Pointing to a public Solana cluster

Solana maintains three public clusters:

  • devnet - Development cluster with airdrops enabled
  • testnet - Tour De Sol test cluster without airdrops enabled
  • mainnet-beta - Main cluster

Use npm scripts to configure which cluster.

To point to devnet:

$ npm run cluster:devnet

To point back to the local cluster:

$ npm run cluster:localnet

example-tictactoe's People

Contributors

criesofcarrots avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar devonbeard avatar garious avatar jackcmay avatar jstarry avatar mvines avatar sakridge avatar wushenshen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example-tictactoe's Issues

Enable automatic Heroku deploys

Deploys are currently manual but should be triggered off of the master branch

I believe that deploys are manual because the Heroku buildpacks on the TicTacToe Heroku app are not configured to build the Rust program. We can leverage additional heroku buildpacks as we already do in the Message Feed example app

TypeError: Expected a value of type `undefined` for `result.rent_epoch` but received `4`.

I recently ran the example tictactoe program on the solana net docker image provided.

But it crashes when it starts looking for players in the network.

The BPF program loads successfully and returns the ID as expected.

Connecting to network...
Using http://localhost:8899 (http://localhost:8899)
findDashboard: Expected a value of type `{jsonrpc,id,error} | {jsonrpc,id,error,result}` for `result` but received `{"data":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"executable":false,"lamports":9997,"owner":[65,163,87,159,4,48,133,107,197,231,226,206,66,25,75,160,182,222,93,219,241,140,68,228,38,10,53,247,9,23,154,146],"rent_epoch":3}`.
Using BPF program
Loading BPF program...
Dashboard programId: 2q41KNuLTKRgwU4qhuEyp4ryyWZAsYpBrFWN4TApwXgU
Dashboard: 6sZki97CtDN3GzzURQwSyKhHYgTsKzdoqDPrPCCYP5Yx
Total games played: 0

Looking for another player
/home/siddharth/Solana/example-tictactoe/node_modules/superstruct/lib/index.js:1194
        throw new StructError(error);
        ^

TypeError: Expected a value of type `undefined` for `result.rent_epoch` but received `4`.
    at Function.Struct.assert.value [as assert] (/home/siddharth/Solana/example-tictactoe/node_modules/superstruct/src/superstruct.js:63:15)
    at Struct (/home/siddharth/Solana/example-tictactoe/node_modules/superstruct/src/superstruct.js:47:21)
    at Connection._wsOnAccountNotification (/home/siddharth/Solana/example-tictactoe/node_modules/@solana/web3.js/src/connection.js:879:17)
    at Client.emit (/home/siddharth/Solana/example-tictactoe/node_modules/rpc-websockets/node_modules/eventemitter3/index.js:181:35)
    at WebSocket.<anonymous> (/home/siddharth/Solana/example-tictactoe/node_modules/rpc-websockets/dist/lib/client.js:424:67)
    at WebSocket.emit (events.js:189:13)
    at WebSocket.EventEmitter.emit (domain.js:441:20)
    at Receiver.receiverOnMessage (/home/siddharth/Solana/example-tictactoe/node_modules/rpc-websockets/node_modules/ws/lib/websocket.js:720:20)
    at Receiver.emit (events.js:189:13)
    at Receiver.EventEmitter.emit (domain.js:441:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `babel-node src/cli/main.js "dev"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/siddharth/.npm/_logs/2019-09-01T12_46_25_343Z-debug.log

Keep alive transaction is failing

Expected

npm run start should start the tictactoe dashboard and keep it alive until a competitor joins

Actual

Keep alive fails to keep the dashboard up

Server logs
Looking for another player
Advertising our game (ABtm1oa5pyCFRZioWmpGZAyuiUynmdUjFjnr3v1qP5tw)
keepAlive() failed #1: Error: Transaction 5ipfueWMrCdVGs5rK4XzaziKr5zJd9U7fugvNZ8ACGjRE83M7UKc3QJMYSigE4iFshGHnbgzSPRfQrA9Lyf4uVoW failed ({"Err":{"InstructionError":[0,"GenericError"]}})
keepAlive() failed #2: Error: Transaction 4JSW8W98w1dq3ZSAzB31sui9aUi7vY2ah4e95jGY5Ew8AFxXLMVhd2qgqpF7VBAFS1Qmn5gnBepVNG7vvF25BnFM failed ({"Err":{"InstructionError":[0,"GenericError"]}})
keepAlive() failed #3: Error: Transaction 4qe7bdUJ2NBuyCUyUdQ29Yoi5mNHSmjnNiTBHQXNWW8qmXd1oG537h7y6iYHXhoxpaWSQ1NkbEEZbbLQ1VRiRnwc failed ({"Err":{"InstructionError":[0,"GenericError"]}})
keepAlive() failed #4: Error: Transaction 67kGkCLTnCUcmsJ2mJXwh9WFjK4vNUDZymQNhNfM7GiabpCYJCqTFkcPerWKeGFSRcqp2RRJs7sQ6fMkbJxhkgWb failed ({"Err":{"InstructionError":[0,"GenericError"]}})
Error: game disconnected
    at TicTacToeDashboard._callee2$ (/Users/starry/Workspace/solana/example-tictactoe/src/program/tic-tac-toe-dashboard.js:222:17)
    at tryCatch (/Users/starry/Workspace/solana/example-tictactoe/node_modules/regenerator-runtime/runtime.js:62:40)
    at Generator.invoke [as _invoke] (/Users/starry/Workspace/solana/example-tictactoe/node_modules/regenerator-runtime/runtime.js:296:22)
    at Generator.prototype.<computed> [as next] (/Users/starry/Workspace/solana/example-tictactoe/node_modules/regenerator-runtime/runtime.js:114:21)
    at step (/Users/starry/Workspace/solana/example-tictactoe/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /Users/starry/Workspace/solana/example-tictactoe/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
Localnet logs:
[2019-08-05T19:31:04.759005300Z INFO  solana_bpf_loader_api::helpers] info!: "tic-tac-toe program entrypoint"
[2019-08-05T19:31:04.759092100Z INFO  solana_bpf_loader_api::helpers] info!: 0x5, 0x0, 0x0, 0x0, 0x0
[2019-08-05T19:31:04.759119800Z INFO  solana_bpf_loader_api::helpers] info!: "Command_KeepAlive"
[2019-08-05T19:31:04.759137900Z INFO  solana_bpf_loader_api::helpers] info!: "Invalid player x keep_alive"
[2019-08-05T19:31:04.759148900Z INFO  solana_bpf_loader_api::helpers] info!: 0x0, 0x0, 0x0, 0x0, 0x0

(affects bpf-c only)

Error loading BPF program

do as your doc said, always failed to do npm run start. both local docker and live=1 testnet.

await BpfLoader.load(connection, loaderAccount, elf); not working

Build the BPF C program failed

Hi guys,

I'm trying to run Tic-Tac-Toe to a local Solana.
When I try to build the BPF C program on macOS Mojave v.10.14.1 and execution failed with following stack:

$ V=1 make -C program-bpf
+ rm -rf 'criterion*'
+ mkdir criterion
+ cd criterion
+ wget --progress=dot:mega https://github.com/Snaipe/Criterion/releases/download/v2.3.2/criterion-v2.3.2-osx-x86_64.tar.bz2
../node_modules/@solana/web3.js/bpf-sdk//scripts/install.sh: line 21: wget: command not found

[test cc] ../dist/program/test_tictactoe (test/tictactoe.c)
mkdir -p ../dist/program
../node_modules/@solana/web3.js/bpf-sdk/llvm-native//bin/clang -Werror -O2 -fno-builtin -std=c17 -isystem../node_modules/@solana/web3.js/bpf-sdk/inc -isystem../node_modules/@solana/web3.js/bpf-sdk/llvm-native//lib/clang/8.0.0/include  -DSOL_TEST -isystem ../node_modules/@solana/web3.js/bpf-sdk/criterion/include -L ../node_modules/@solana/web3.js/bpf-sdk/criterion/lib -rpath /Users/vladislavivanov/Projects/Solana/example-tictactoe/node_modules/@solana/web3.js/bpf-sdk/criterion/lib -lcriterion   -o ../dist/program/test_tictactoe test/tictactoe.c -MD -MF ../dist/program/test_tictactoe.d
make: ../node_modules/@solana/web3.js/bpf-sdk/llvm-native//bin/clang: No such file or directory
make: *** [../dist/program/test_tictactoe] Error 1

Please, check and the output for starting local Solana:

npx solana-localnet update
Docker version 18.09.0, build 4d60db4
+ docker pull solanalabs/solana:edge
edge: Pulling from solanalabs/solana
05d1a5232b46: Pull complete 
5d005cbf2b08: Pull complete 
5398c64aa99e: Pull complete 
Digest: sha256:c009436c924b6d1220ad36b62d2d07ea65169151523cd4acd9c7c9e01b464f30
Status: Downloaded newer image for solanalabs/solana:edge
Vladislavs-MBP:example-tictactoe vladislavivanov$ npm run localnet:up

> [email protected] localnet:up /Users/vladislavivanov/Projects/Solana/example-tictactoe
> set -x; solana-localnet down; set -e; solana-localnet up

+ solana-localnet down
Docker version 18.09.0, build 4d60db4
+ docker stop --time 0 solana-localnet
Error response from daemon: No such container: solana-localnet
+ set -e
+ solana-localnet up
Docker version 18.09.0, build 4d60db4
+ RUST_LOG=solana=warn,solana_bpf=info,solana_jsonrpc=info,solana::rpc=info,solana_fullnode=info,solana::drone=info,solana::bank=info,solana::banking_stage=info,solana::system_program=info
+ docker run --detach --name solana-localnet --rm --publish 8899:8899 --publish 8900:8900 --tty --env RUST_LOG=solana=warn,solana_bpf=info,solana_jsonrpc=info,solana::rpc=info,solana_fullnode=info,solana::drone=info,solana::bank=info,solana::banking_stage=info,solana::system_program=info solanalabs/solana:edge
a7d6bfdabd73678e26992c990bcd91c5dbf0ca9640868a377c131e41976be835
+ for _ in 1 2 3 4 5
+ curl -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1, "method":"getTransactionCount"}' http://localhost:8899
curl: (52) Empty reply from server
+ sleep 1
+ for _ in 1 2 3 4 5
+ curl -X POST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1, "method":"getTransactionCount"}' http://localhost:8899
{"jsonrpc":"2.0","result":0,"id":1}
+ break

Error response from daemon: No such container: solana-localnet

Any ideas where is my mistake?
Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.