Ethereum Smart Contract development ⛓ + 📑

Date: 29.03.2021

Cryptocurrencies such as Bitcoin (BTC) or Ethereum (ETH) and the underlying Blockchain technology is getting more and more traction. Special attention is given to so-called Smart Contracts: programmable contracts that execute actions depending on user interactions or other events. Decentralized Finance (DeFi) applications heavily rely on Smart Contracts to offer their goods in an environment not requiring trust: as the contracts are public and the code can be verified by everyone, DeFi aims to resolve trust issues with Centralized Finance (CeFi), represented by banks or other financial actors in the off-chain world. In order to better understand the functioning of Blockchains in the context of Cryptocurrencies, I wanted to try to implement my own Decentralized App (dApp) using Smart Contracts on the Ethereum Blockchain.

My idea was to implement a rudimentary casino application that accepts bets by players and then draws to random cards: the highest card wins. The players are playing against the casino itself. As any good casino, it offers its own token that can be exchanged against another cryptocurrency token. In order to not have to deal with fluctuation in the value of the casino token, I pegged my token to the USDC Stablecoin: a stablecoin’s value does not change as much as for example the Bitcoin price or the ETH price. These two cryptocurrencies are not bound to any other ‘physical’ currency in price. Their value is completely determined by supply and demand on crypto exchanges.

This price fluctuation of BTC and ETH is one criticism for using them as a currency or a store of value: as the daily price differences can exceed 10%, unstable cryptocurrencies are considered as not a good choice for buying physical goods whose value is fixed. In contrast a stablecoin aims to remain relatively static in its value thus being more reliable as a currency.

My casino allows the exchange of the USDC Stablecoin for Casino Tokens. The Ethereum Blockchain defines a standard for exchangeable tokens in the Ethereum Request For Comment 20 (ERC-20). The tokens can be transferred between addresses (an address either denominates a wallet or a Smart Contract). The token issued by the casino itself is an ERC-20 conform token, meaning everyone with an Ethereum wallet can receive or send them (or use them in the casino).

The casino is implemented as a Smart Contract implemented in Solidity and is designed to run on the Ethereum Blockchain.

The Ethereum Blockchain has the ability to execute code and add the results of these calculations as transactions into the ledger. Smart Contracts are executed on the node that is mining a new block in the EVM (Ethereum Virtual Machine).

The casino token contract defines 2 custom operations: mint and burn. mint creates casino tokens and collects the corresponding USDC token amount from the requester. The casino token contract stores the USDC tokens that were used to mint new casino tokens. The burn operation will destroy casino tokens and transfer the USDC token amount to the person who sent the casino tokens to be burnt. This is similar to the bank of a physical casino that will exchange money for tokens that can be used for playing in the casino. Upon leaving the casino they can be redeemed back for money.

After buying the casino tokens the player can place a bet in the casino. The casino has a payout factor implemented: when winning the game the player will receive the payout back. When losing the bet will be transferred to the casino contract. The casino has a house edge: it will win in case both drawn cards are the same.

As Smart Contracts are fully deterministic, drawing random numbers is not easily possible. For that purpose the Ethereum Blockchain supports so-called oracles. These are special contracts that can interact with out-of-chain systems such as web services and retrieve data. The random number generator of the casino is implemented in a similar fashion: the contract itself implements a pseudo-random number generator: https://en.wikipedia.org/wiki/Lehmer_random_number_generator. To add some degree of randomization the seed for the generator can be plugged in from the outside thus allowing ‘resetting’ the generator.

The casino can be accessed via a ReactJS webapp using the ethers.js library and the MetaMask wallet browser extension: these two allow connecting an Ethereum wallet to the casino app and placing bets as well as exchanging tokens. The combination of ethers.js and MetaMask enables ‘web3’ capabilities in the browser: Web3 definition.

The source code of the project can be found in the Github repo: https://github.com/brakid/TokenCasino. The project has not been deployed to the Ethereum mainnet chain, but instead I have used the Truffle Suite. This suite integrates the Solidity compiler, testing and a local Blockchain instance to locally run Smart Contracts (Ganache).

Next steps for this project may include deploying the contracts to an Ethereum testnet, or deploying it to the Binance Smart Chain, a blockchain hosted by the Binance crypto exchange that is EVM-compatible. Further steps could include replacing the random number generator by a ‘true’ oracle that emits an event to trigger the request of a random number generated off-chain thus making it more difficult for miners to exploit the random number generator and place a bet and then also mine the block and knowing which values will be returned by the random number generator.

I will definitely continue experimenting with Smart Contracts to also cover other topics such as NFTs (ERC-721). Happy coding 🔥