This repository contains the smart contracts for BattleBlocks, a decentralized application (dapp) built on the Flow blockchain. The dapp includes various functionalities, each implemented as a separate smart contract in this repository.
The contracts in this repository are written in Cadence, the programming language for the Flow blockchain. To deploy and test the contracts, you will need to have the Flow CLI and emulator installed on your local machine.
BattleBlocksNFT is a simple implementation of a non-fungible token (NFT) contract. It is based on the ExampleNFT contract provided by the Flow team and is used for demonstrative purposes in the BattleBlocks hackathon.
BattleBlocksChildAccount is used for walletless account management. This contract allows users to create and manage child accounts within their main account, providing a more streamlined and user-friendly experience. The contract is based on the recently emerging ChildContract smart contract from this repo: https://github.com/onflow/linked-accounts
BattleBlocksGame is the game smart contract for BattleBlocks. It provides the logic and functionality for players to compete against each other in the game of "Battleships". At the begging of a game, each player sets up a board and person to first find out where the oponnent placed 10 of his blocks, wins. To ensure players are not lying about their block placement and keep their blocks unrevealed until it is time to reveal them, the smart contract uses a merklee tree proof system.
At the begging of the game, a player must submit a merkle tree root which is created by using the merkle tree algorithm
-
Leaves: The merkle tree leaves that are defined by our smart contract are a string of numbers that can be split up into three sections.
- The first digit indicates whether the coordinate on the grid contains a block: 1 = yes, 0 = no
- The second and third digit represent the coordinates of the block: 03 = x:0, y: 3
- The final 5 digits are a random 5 digit number nonce created with a random number generator (min = 10000, max 99999): 23923
- If we use the example values from above, we would finally end up with the resulting leaf that looks like this: 10323923. This leaf indicates that there is a block at the (0, 3) coordinates. We have also created a random nonce (23923) for it that we will use hash and create the merkle tree
-
Tree Generation: After creating all the nonces and leaves, each leaf is hashed with Keccak256 and a merkle tree is generated by using the merkle tree algorithm, an important thing to note is that in our implementation, odd nodes of the tree are duplicated. After the tree is generated, a tree root should be given as a parameter to the game creation or game joining transaction.
-
Proof: When each player makes a move, unless it is the first move. He needs to prove if the last spot the opposing player has fired at actually contains a block. This is actually why we use merkle trees in the first place. To do this, we define in the smart contract that the player needs to offer proof and raw leaf creation data for the opponents previous guess. After doing so, we prove the truthfullness of the intially supplied merkle tree root (or proof) by using the proof and raw leaf data. If the proof is valid, the game continues and the player score is calculated accordingly.
Contract | Address | Environment |
---|---|---|
FungibleToken | 0x9a0766d93b6608b7 | Testnet |
MetadataViews | 0x631e88ae7f1d7c20 | Testnet |
NonFungibleToken | 0x631e88ae7f1d7c20 | Testnet |
FiatToken | 0xa983fecbed621163 | Testnet |
FlowToken | 0x7e60df042a9c0868 | Testnet |
BattleBlocksNFT | 0xf95724cf2202cd00 | Testnet |
BattleBlocksAccounts | 0xf95724cf2202cd00 | Testnet |
BattleBlocksGame | 0xf95724cf2202cd00 | Testnet |
This project is licensed under the MIT License.