# ELO Project Overview:
### Project description
The goal is to create an ELO rating smart contract that manages individual leagues, with each league represented by an NFT. This modular approach will allow the ELO contract to handle multiple leagues, each with its own unique set of rules and participants.
[SMARTCONTRACT FILE](https://hackmd.io/R5Oa6K2pS520paSnL7SQ8Q?edit)
**Tasks:**
1. Create the ELO smart contract:
- Design and develop a smart contract to manage ELO ratings, incorporating the necessary functions for calculating and updating ratings.
- Ensure modularity by enabling the smart contract to interact with multiple league NFTs.
### ELO Smart Contract Functions
**Create league**: A function to create a new league, which includes generating an NFT representing the league and a corresponding Tableland table for storing league data.
**Join league**: A function allowing players to join a league by paying a deposit, selecting a referee or becoming a referee themselves, and providing their initial ELO rating.
**Update ELO ratings**: A function to update the ELO ratings of the players after a match. This function should handle both agreed-upon results and disputed results by entering a challenge state.
**Submit match resul**t: A function for players to submit their match results, triggering an update in ELO ratings or entering a challenge state if there's a disagreement.
**Referee voting:** A function for referees to vote on disputed match results during a challenge state. The voting should be weighted based on the number of players who delegated their vote to each referee.
**Resolve challenge:** A function to tally referee votes, update ELO ratings based on the majority vote, slash deposits of the player with the incorrect result, and distribute rewards to referees who voted correctly.
**Delegate referee:** A function allowing players to delegate their vote to a referee or change their referee delegation.
**Leave league:** A function for players to leave a league and withdraw their remaining deposit, provided they have no pending challenges or penalties.
**Get ELO rating**: A function to query the ELO rating of a player in a specific league.
Get league details: A function to retrieve league-specific information, such as league size, list of referees, and league NFT details.
These functions form the core of the ELO Rating contract, enabling the creation and management of leagues, player participation, ELO rating updates, dispute resolution, and referee delegation
## Lifecycle
Here's an overview of the typical lifecycle of the app using the provided contracts:
**Deployment**: The EloContract.sol, EloRatingSystem.sol, and MembershipNFT.sol contracts are deployed to the blockchain. The owner of the contracts sets the necessary configuration, such as the K-factor for different leagues in the EloRatingSystem contract.
**Table creation**: The owner calls the createTable function in the EloContract to create a new table on Tableland for storing player data.
**Joining a league**: A player decides to join a league. They interact with the MembershipNFT contract's joinLeague function, providing their address, an initial Elo rating, a deposit amount, a league key, and a player ID. The MembershipNFT contract forwards this information to the EloContract's joinLeague function, which handles the deposit and adds the player to the league in the Tableland table. The MembershipNFT contract then mints an NFT representing the player's membership in the league.
**Match results submission**: When a match between two players is completed, the owner of the NFTs representing the players' memberships submits the match result using the submitMatchResult function in the MembershipNFT contract. This function checks the ownership of the NFTs and forwards the result to the EloContract, which calculates the new Elo ratings for both players and updates their ratings in the Tableland table.
**Deposit and withdrawa**l: Players can deposit additional tokens into the EloContract's vault by calling the deposit function, and they can withdraw tokens by calling the withdraw function.
**Claiming vault shares**: Players can claim their share of the vault by calling the claimVaultShare function in the MembershipNFT contract, which checks their NFT ownership and forwards the request to the EloContract.
Throughout the lifecycle of the app, the owner can pause and unpause the EloContract using the pause and unpause functions, if necessary. An off-chain component (e.g., using the Tableland SDK) would be used to read player data from the Tableland table and display it on the app's user interface.
## ASCII Diagram
```
+------------------+ +------------------+ +-----------------+
| League Owner | | Player | | ERC-4626 Vault |
+------------------+ +------------------+ +-----------------+
| | | | | |
| Create League +----->| Join League | | Deposit Assets |
| (ELO Contract) | | (Pay Deposit) | | (Yield bearing) |
| | | | | |
| | | Update ELO | | |
+------------------+ +------------------+ +-----------------+
| | |
| v |
+------------------+ +------------------+ +-----------------+
| Tableland | | ELO Contract | | Prize Pool |
+------------------+ +------------------+ +-----------------+
| | | | | |
| Manage Leagues | | Track ELO & | | Distribute |
| (ELO Ratings) | | Deposits | | (Based on ELO) |
| | | | | |
+------------------+ +------------------+ +-----------------+
The league owner creates a league using the ELO contract, giving control authority to the ELO contract.
Players join the league by paying a deposit, which is split between a security deposit and the prize pool.
The deposited assets are sent to the ERC-4626 vault, where they are turned into yield-bearing assets.
As players compete, their ELO ratings are updated based on their performance.
Tableland manages the leagues and ELO ratings.
The ELO contract tracks the ELO ratings and deposits for each player.
At the end of the league, the prize pool is distributed among players based on their ELO ratings via the ELO contract. The security deposits are returned, and a percentage of the prize pool is paid out according to the players' ELO standings. Additionally, a percentage of the prize pool is paid to the league owner as profit.
```
2. Design the League NFT smart contract:
- Develop a separate smart contract for the league NFTs, which will store league-specific information such as rules, participants, and match results.
- Implement functions to create, transfer, and manage league NFTs, as well as interact with the ELO smart contract to update ratings.
3. Integrate Tableland or a similar decentralized protocol for storing match data and participant information:
- Use a decentralized database protocol to store and manage match data, participant information, and league-specific details.
- Leverage the protocol's access control features to ensure that only authorized users can modify league information.
4. Develop a user interface for league and ELO management:
- Create a frontend application that allows users to interact with the ELO and league smart contracts, view league details, and manage their own leagues.
- Implement features to create new leagues, join existing leagues, and manage league settings.
- Implement an event-driven architecture for real-time updates:
5. Use an event-driven approach to ensure that the ELO smart contract and league NFTs are updated in real-time as matches are played and results are submitted.
- Utilize smart contract events to trigger updates in the frontend application and decentralized database.
- Perform extensive testing and security audits:
