- Get metamask extension on your browser with a wallet.
- Add Mumbai Network (Polygon Testnet) on your Metamask.
- Claim some free matic to deploy contracts on chain.
- Node js version 14.17.0 or higher.
npm install -g truffle
cd webApp
npm install
cd smartContract
npm install
touch .env
- Add private key into your .env as follow : PRIVATE_KEY=
You can find your private key on metamask extension - Add RPC matic network into your .env as follow : RPC_URL=https://matic-mumbai.chainstacklabs.com/
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.
(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
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.
truffle compile
truffle migrate --network mumbai
- You should get the contract adress into the terminal after the deployment.
- Add the smart contract adress into the smart_contract_address variable in the webApp/nuxt.config.ts file.
cd webApp
npm run dev
- Go to http://localhost:3000/ and connect your metamask wallet to the app.
- 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.