Skip to content

ThomyLorenzatti/workshop-crypto-token-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 

Repository files navigation

Workshop

UnderStand BlockChain and create deploy your first Smart-Contract

Prerequisites

Setup project, install dependencies

Global dependencies

npm install -g truffle

webApp

cd webApp
npm install

smartContract

cd smartContract
npm install
touch .env

Lets create our first ERC20 Token

Goal of this workshop is to create a ERC20 Token and deploy it on Polygon (Matic) Mumbai Testnet. We will use truffle to compile and deploy our smart contract. Get it touch with the contract file : [smartContract/contracts/MyToken.sol](We will code our smart contract in solidity language).

Our smart contract will use the openzeppelin library to inherit some functions and avoid to code them from scratch. GameToken is already inheriting the ERC20 contract from openzeppelin library.

Lets code the smart contract

Constructor function

(https://docs.openzeppelin.com/contracts/2.x/erc20-supply)

The constructor is the function that is called when the contract is deployed. It is used to initialize the contract state.

  • Create constructor function and pass the name, symbol as constructor parameters.
  • The owner of the contract (adress used to deploy the contract) will be the owner of 1000 GMT tokens.
  • The adress of the contract will be the owner of 100 GMT tokens. Mint function

Override the transfer function

The goal of the workshop is to create a token where users on a web app can use 10 GMT to play a game. The game will be a simple wheel where user pay 10 GMT to spin the wheel and has following caracteristics:

  • 10% chance to win 50 GMT
  • 20% chance to draw 10 GMT
  • 70% chance to loose 10 GMT already paid

To manage to do that we will override the transfer function from ERC20 contract.

  • Override the transfer function, paramets should be an recipient address and an uint256 amount.
  • Raise an InsufficientBalance error if the player doesn't have enough GMT to play.
  • Raise an AmountError error if the player doesn't paid 10 GMT.
  • Transfer 10 GMT to the contract address.

Way to generate a random number between 0 and 100:

uint random = uint(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 100;
  • Has 10% chance to transfer 50 GMT to the player address and emit a Win event.
  • Has 20% chance to transfer 10 GMT to the player address and emit a Draw event.

Compile the smart contract and deploy it on Polygon (Matic) Mumbai Testnet

Compile the smart contract

truffle compile

Deploy the smart contract

truffle migrate --network mumbai

  • You should get the contract adress into the terminal after the deployment.

Lets interact with our smar contract on the webApp

Add smart contract adress into the webApp

  • Add the smart contract adress into the smart_contract_address variable in the webApp/nuxt.config.ts file.

Run the webApp

cd webApp
npm run dev

Play the game

  • Click on the button "lancer la roue" to play the game.

Congratulation you just created your first ERC20 Token and deployed it on Polygon (Matic) Mumbai Testnet

Check all transactions of your smart contract on mumbai polygonscan.
Paste your smart contract adress into the search bar.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published