Skip to content

Armin-FalDiS/near-treasureboard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation


Logo

NEAR - TreasureBoard

Near TreasureBoard is a game which has a user create a treasure board putting a prize in it. Then, players can reserve a slot on the board by paying 1 NEAR. After half the slots on the board have been reserved, the game is closed which allows the creator to reveal the position of the bombs on the board and initiate a prize payout.

Table of Contents
  1. About The Project
  2. Getting Started

About The Project

A game starts when a creator chooses a size and creates a new treasure board. The creator has to deposit NEAR tokens proportional to the size of the treasure board which are Small(4), Medium(16) and Big(36). The creator also has to provide the hash of the solution which is the slots that contain a bomb. Thus, half the slots on the treasureboard have bombs in them.
The remaining half of the treasureboard contains a random piece of the treasure (creator's deposit) in each of it's slots. Each player has to deposit 1 NEAR to reserve a slot on the treasureboard. After half of the slots have been reserved, the treasureboard closes which allow the creator to reveal the solution.
The hash of the revealed solution has to match the originally provided hash upon game creation. During a successful reveal, each answer is checked and if an answer triggers a bomb, the player loses their 1 NEAR to the creator of the game. On the other hand, if a player chose a slot that was not bombed, they get a random cut of the treasure. It is possible to have all or most of the treasure in one slot or a slot with no treasure in it but this will not be known since it is calculated after the revelation.

(back to top)

Built With

(back to top)

Getting Started

This project has a backend which is a NEAR smart contract written in Rust using near-sdk-rs.
There is also a CLI included that is written in NodeJS using near-api-js.

The Command Line Interface can be used to communicate with the smart contract deployed on the blockchain with ease.

The CLI

The simple CLI can be found in the "interface" directory but to use it, you have to first create a wallet on NEAR's testnet. This can be achieved with the help of near-cli.

Prerequisites

First and foremost, you have to install NodeJS. Do note that you have to install npm along with it so be sure to have npm selected when you see it on NodeJS's installation wizard. You can get NodeJS from here.
After that, you have to install near-cli. This can be achieved by running this one line in your command prompt / terminal.

npm install -g near-cli

Now that you have near-cli up and running, you have to login to a test account wallet. This is way easier than it sounds and can be done by running this command:

  near login

By running that command, your browser will open up taking you to a user friendly website allowing you to create your wallet. This is very straight forward. After you have created your wallet, simply enter the id (name) of the created account in the already opened terminal.
Congrats ! Now you have a wallet on NEAR testnet. Do remember your account Id since you will need it later.

Installation

  1. Clone the repo (Or download zip from github)
    git clone https://github.com/armin-faldis/near-treasureboard
    
  2. Open a terminal in the "interface" directory
  3. Install NPM packages
    npm install
    
  4. Now you can run the CLI
    node .
    

(back to top)

Usage

This is a very simple and straight forward CLI which if you ran successfully should show you a list of actions that you can do by entering their corresponding number. The interface will prompt you to enter information in a step-by-step manner and inform you of any errors.
Happy treasureboarding !!

(back to top)

The Smart Contract

The smart contract which can be found in the "contract" directory is already deployed to the testnet and the CLI can be utilized to use it so there is no need to redeploy it.

Contract Prerequisites

To get into the smart contract, you can use rustup which is a installer making it easy to get started with rust. It can can downloaded from here. You would also need cargo which is a package(crate?) manager included with rustup.

Contract Installation

  1. Clone the repo (Or download zip from github)
    git clone https://github.com/armin-faldis/near-treasureboard
    
  2. Open a terminal in the "contract" directory
  3. Install cargo packages
    cargo update
    
  4. Add the WebAssembly toolchain if you already haven't
    rustup target add wasm32-unknown-unknown
    
  5. Done. You can now use cargo to build or test the contract
    cargo build
    cargo test
    
  6. To generate a wasm file which can be used to deploy to the blockchain use the provided build files
    // Windows
    build.bat
    // Linux
    ./build.sh
    

Contract Usage

The smart contract can be executed either by unit tests (cargo test) or deploying the generated wasm file to the testnet and using the provided CLI.

Notes and Remarks

The smart contract locks NEARs equal to the number of slots on the board which makes creators pay for the storage cost of their game's information by the very prize they deposit upon game creation. This essentially charges players for the staked storage and refunds them (by the prize payout) when the game is finished and the information is removed from the state freeing the storage stake. As such, a seperate mechanism to charge for storage stake is entirely unnecessary.
Furthermore, the economical logic of this game is as sound as it could be (without KYC). No attempts were made in any part of the code to prevent an account from playing more than once at any given game since obviously they could just make another account. A player can create a game and reserve all the slots then reveal the answer getting their tokens back burning gas on every transaction or they could reserve half the playable slots which they know are not bombed but since treasure distribution is random they could end up losing all their tokens anyways.
All in all, given the fact that there are no possible sure fire ways for someone to always make money in this game coupled with the solution checking mechanism using a hash function makes this game fair to both the creator and the players.

(back to top)

About

My submission for NEAR Certified Developer program

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages