Solidity Workshop for beginners --> Create your own coin
Create your own crypto on Ethereum network with Solidity smart contracts
Introduction
In this workshop, we are going to build our own cryptocurrency on the ethereum network with solidity. The features of our coin will be as follows:
- Only the owner can make new coins
- Anyone can send coins to each other without a need for registration. You need is the ethereum address.
Let's start: Head over to Remix IDE
pragma solidity ^0.8.4;
contract Coin {
}
Since everything is accessed by a smart contract, we need to create this coin through a smart contract itself.
Make 2 global variables:
minter
balances
The balances
will be a mapping reference of solidity and it will have the key value pairs of the receiver's address and the amount each of them will get.
We will set the adress of the minter
in the constructor so that the one who deploys the contract becomes the owner of the cryptocurrency.
pragma solidity ^0.8.4;
contract SubCurrency {
address public minter;
mapping (address => uint) public balances;
constructor () {
minter = msg.sender;
}
}
Now, we will make a mint
function which will enable us to do 2 things:
- Make new coins and send them to address
- Only the owner can send the coins
pragma solidity ^0.8.4;
contract SubCurrency {
address public minter;
mapping (address => uint) public balances;
constructor () {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
}
Make an event for our clients to interact to the changes in the contracts
This event will contain the address of the owner, the address of the client and the amount we send to them
pragma solidity ^0.8.4;
contract SubCurrency {
address public minter;
mapping (address => uint) public balances;
constructor () {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
event Sent(address from, address to, uint amount);
}
This even allows us to store the arguments passed in the transaction logs and these logs are stored on the blockchain and can be accessed using an address of the contract.
Make the function to send coins
This function will enable us to send anyamount ot coins to existing addresses.
While we trigger this function, the amount of the owner should decrease the amount to be given to the reciever needs to increase.
function send(address receiver, uint amount) public {
balances[msg.sender] -= amount;
balances[receiver] += amount;
}
But here's the catch. If the owner onwr himself doesn't have enough coins then also it can send the coins to its reciever. So we need to modify this function and make sure that the amount to be sent must not be greater than the balance of the owner. And last but not the least, we need to emit the event.
error insuffieientBalance(uint requested, uint available);
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert insuffieientBalance({
requested: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, reciever, amount);
}
The final code
pragma solidity ^0.8.4;
contract SubCurrency {
address public minter;
mapping (address => uint) public balances;
constructor () {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
event Sent(address from, address to, uint amount);
error insuffieientBalance(uint requested, uint available);
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert insuffieientBalance({
requested: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
Deploy the contract after compiling and test it. You are good to go.