Skip to content

Latest commit

 

History

History
101 lines (71 loc) · 2.98 KB

README.md

File metadata and controls

101 lines (71 loc) · 2.98 KB

Coin Flip Contract

The smart contract implements a flip coin game in which the player tries to guess the next outcome. The player gets a point on each correct guess, and losses one for each wrong one.

function simulateCoinFlip(): Side {
  const randomString: string = near.randomSeed();
  return randomString.charCodeAt(0) % 2 ? 'heads' : 'tails';
}

flip_coin({ player_guess }: { player_guess: Side  }): Side {
  // Check who called the method
  const player = near.predecessorAccountId(); 
  near.log(`${player} chose ${player_guess}`);

  // Simulate a Coin Flip
  const outcome = simulateCoinFlip();

  // Get the current player points
  let player_points: number = (this.points.get(player) || 0) as number
  
  // Check if their guess was right and modify the points accordingly
  if(player_guess == outcome) {
    near.log(`The result was ${outcome}, you get a point!`);
    player_points += 1;
  } else {
    near.log(`The result was ${outcome}, you lost a point`);
    player_points = player_points? player_points - 1 : 0;
  }

  // Store the new points
  this.points.set(player, player_points)

  return outcome
}

Quickstart

  1. Make sure you have installed node.js >= 16.
  2. Install the NEAR CLI

1. Build and Deploy the Contract

You can automatically compile and deploy the contract in the NEAR testnet by running:

npm run deploy

Once finished, check the neardev/dev-account file to find the address in which the contract was deployed:

cat ./neardev/dev-account
# e.g. dev-1659899566943-21539992274727

2. Get the Score

points_of performs read-only operations, therefore it is a view method.

View methods can be called for free by anyone, even people without a NEAR account!

# Use near-cli to get the points
near view <dev-account> points_of '{"player": "<dev-account>"}'

3. Flip a Coin and Try to Guess the Outcome

flip_coin takes a guess ("heads" or "tails"), simulates a coin flip and gives/removes points to the player.

It changes the contract's state, for which it is a call method.

Call methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction.

# Use near-cli to play
near call <dev-account> flip_coin '{"player_guess":"tails"}' --accountId <dev-account>

Tip: If you would like to call flip_coin using your own account, first login into NEAR using:

# Use near-cli to login your NEAR account
near login

and then use the logged account to sign the transaction: --accountId <your-account>.

A Note on Random Numbers

Generating random numbers in an adversarial environment such as a blockchain is very difficult. This spawns from the fact that everything is public on the network.

Please check our documentation to learn more about handling random numbers in a blockchain.