As Blox grows we get more and more talented peopel coming from other industries to join us. The transition can have a step learning curve, not only the technology but the market it self. Many find themselves buying their first crypto asset after talking with a friend at work. I wanted to find a way to help beginners to better understand the intricacies of the crypto market, what to pay attention to, explore new tokens and valuate their market strength.
I’ve had a chance in between flights to write up a little game i call War Of Tokens in solidity (has a front end using react). Its idea is very simple. users can deposit ERC-20 tokens into a smart contract. In order to become an active player you need to deposit a certain amount of CDT to unlock that status. Each token you deposit into your player account will add to your score depending on 3 variables, the token market cap, volume and market depth. Its a nice and simple way of experimenting with the very basics of crypto assets, how volume, cap and market depth influence (or should) the tokens you hold.
Once a player deposits tokens they can see other players who are active and attack them. They fight one another and depending on their score and the HODL spell (yes the HODL spell, i’ll get to it) a winner emerges that gets a cut of his opponent’s tokens. How much he gets depends on the ration between the 2 players and the HODL spell.
Lets dive into the code.
Deposit/ withdraw are handled very simply like in any other such contract. I’ve simply copied those functions from etherdelta, modified and updated them a bit. etherdelta was developed ages ago (in terms of soliidty code) so the throw functions and safemath library had to be updated a bit.
User score are calculated by iterating all user deposited tokens and performing some function that takes the number of tokens and that token market params and returns an added score. There is still a way to go there because the calc function is very rudimentary and does not take into account tokens with different decimal precisions. For token data i’ve used an oracle architecture to get the token volume, cap and market depth. I haven’t gone through and developed the oracle but there a few options
- Simple contract where Blox periodically can update those parameters
- Using an oracle service like oraclize
- If we really want to get sophisticated we can create a quorum of voters and use commit-reveal schemes to get those data points (similar to augur)
The attack function is what manages and determines the params for the attack and the following complete attack call (as part of the HODL function). it basically registers user scores, prizes and the destination HODL block number. So what is this HODL spell. Its a random number based on a future eth block hash that can add/ remove score points to on of the players and change the outcome of the attack. Why HODL, because it sounds cool. It adds a randomness element to the attacks so that very strong players won’t be just attacking weak players and getting their tokens.
The attack/ defence prize is in reverse proportions to the difference in score between the attacker and attackee. So if a very powerful attacker attacks a weak player and wins he will get a very small % of the attackee tokens. But the attackee wins he could potentially get a large % (in comparison to his tokens) prize.
Winner is choosen by the calculation above. We take the target ETH block number for the HODL spell and get from it the HODL spell power and its direction (attacker or attackee). This is what resolves the attack and grants the prizes to the users. It can me modified and played with to tip the the scale towards a certain side. In its current form it’s pretty rudimentary but makes the game fun and unpredictible.
Code is open at https://github.com/bloxapp/war_of_tokens