I create my first coin in Ethereum with Smarts Contracts [Solidity language]

in HiveDevs3 years ago

Today I am going to create my first coin on the Ethereum blockchain through a smart contract using solidity as a programming language. I will use the IDE Remix (browser)

It should be noted that I am in the learning process and I do these tutorials as a refresher of knowledge.

To start we will create a new file called MyCoin.sol with the following content:

The first thing we will do is place the keyword pragma followed by the name of the language to use in this case solidity and the version.

// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;

Now we will write the keyword contract followed by the name in this case MyCoin, open and close brackets
contract MyCoin{}

Inside our contract we first declare the variables and then the functions
// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;
contract MyCoin{
    address public minter;
    mapping(address => uint) public balances;
}

As you can see, we have two variables, the first address is assigned to the person who displays the contract and the person who will create the currency and we have a mapping that will accumulate how many coins we have.
// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;
contract MyCoin{
    address public minter;
    mapping(address => uint) public balances;
    constructor(uint amount){
        minter = msg.sender;
        balances[msg.sender] += amount;
    }
}

Now we will create a function that will be the first to be executed and the only time it will do so, in it we will assign the address to our minter and we will assign some coins that we will pass at the time of displaying the contract.

// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;
contract MyCoin{
    address public minter;
    mapping(address => uint) public balances;
    constructor(uint amount){
        minter = msg.sender;
        balances[msg.sender] += amount;
    }
    function mint(address receiver, uint amount) public{
        require(msg.sender == minter);
        require(amount < 1e60);
        balances[receiver] += amount;
    }
}

We will create our first function called mint which is the one that will create new coins and assign them to an address. It should be noted that only the minter can create coins.
// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;
contract MyCoin{
    address public minter;
    mapping(address => uint) public balances;
    constructor(uint amount){
        minter = msg.sender;
        balances[msg.sender] += amount;
    }
    function mint(address receiver, uint amount) public{
        require(msg.sender == minter, 'Only the minter can create coins.');
        require(amount < 1e60);
        balances[receiver] += amount;
    }
    function sendCoins(address receiver, uint amount) public {
        require(amount <= balances[msg.sender], 'Not enough coins');
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
    }
}


We will create a function to send our coins to another address, first we ask if we have enough coins then we decrease the amount of the one who sends, then we increase the amount of the one who receives the coins.

// HIVE JFDESOUSA 22/02/2021
pragma solidity >=0.7.0 <0.9.0;
contract MyCoin{
    address public minter;
    mapping(address => uint) public balances;
    event Sent(address from, address to, uint amount);
    constructor(uint amount){
        minter = msg.sender;
        balances[msg.sender] += amount;
    }
    function mint(address receiver, uint amount) public{
        require(msg.sender == minter, 'Only the minter can create coins.');
        require(amount < 1e60);
        balances[receiver] += amount;
    }
    function sendCoins(address receiver, uint amount) public {
        require(amount <= balances[msg.sender], 'Not enough coins');
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}


Finally we will create an event that we will call Sent, this event will save a log of our transaction within the block of the blockchain.

With this ready we will proceed to have fun for a while testing our smart contract.

Sort:  

Check out the https://openzeppelin.com framework for making ERC-20 or other ETH based contracts. It's the industry standard.