SubstrateChess / pallet-chess

The Unlicense
11 stars 6 forks source link

♜♞♝♛♚ Substrate Chess ♔♕♗♘♖

Overview

This pallet provides a way to play on-chain chess. It benefits from cozy-chess and its ability to compile to WASM out-of-the-box (no_std compatible).

The chess board is represented on-chain as a Forsyth–Edwards Notation (FEN) string, as well as the moves.

Players

Each match has two players:

Match

When Challenger calls create_match, they establish the following parameters:

A Match Id is calculated by hashing the tuple (challenger, opponent, nonce), where the nonce is incremented for every new match created.

Match Bets

This pallet is loosely coupled with FRAME's pallet-assets (or any other pallet that implements Inspect + Transfer traits from frame_support::traits::fungibles).

In order to create a match, Challenger chooses an Asset Id and an amount. During the execution of create_match, a deposit of such asset amount is made from their account.

As soon as Opponent calls join_match, an equal deposit is made from their account.

The winner of the match receives both deposits as reward. In case of draws, both players get their deposits back.

Match Style

Match styles define how much time each player has to make their move. Time is measured in blocks, and each style is defined as a Config type.

Assuming 6s per block, the following values are recommended:

In case player A takes longer than the expected time for their move, then player B has the right to call clear_abandoned_match and claim victory, taking both deposits.

If B takes longer than 10 x _Period to claim their victory, then some third party C is incentivized to act as a "janitor" and call clear_abandoned_match on their behalf. In this case, C gets a percentage of the winner's prize. This percentage is defined as a Config type called IncentiveShare.

Bet deposits must cover janitor incentives such that 2 * Bet * IncentiveShare >= MinimumBalance. For example, if the asset has MinimumBalance = 100 and IncentiveShare = 10%, then the minimum allowed deposit is 500.

Extrinsic Weights

Although conveniently able to compile to WASM, cozy_chess crate wasn't written with Substrate in mind. That means that there is no guarantee that its execution will be linear. This has direct implications on how the extrinsic weights are calculated for this pallet.

The docs directory has a detailed description on the strategy used for benchmarking the extrinsic weights.