C 352: Making a Coin with Solidity (10 pts)

What you need:

Background

We'll make a very simple Ethereum smart contract that creates a cryptocurrency.

We'll run it in a local blockchain within the browser, so we don't need any actual blockchain server.

These coins aren't worth any money, and won't be visible to anyone else.

I am following this tutorial: Introduction to Smart Contracts.

Opening the Remix IDE

In Chrome, go to
https://remix.ethereum.org/
If a "Help us to improve..." box pops up. Click Sure.

Creating the Coin Smart Contract

In the left pane, right-click contracts and click "New File", as shown below.

Type the name Coin.sol and press Enter.

A "Coin.sol" tab appears on the right side. Paste in this code, as shown below.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract Coin {
    // The keyword "public" makes variables
    // accessible from other contracts
    address public minter;
    mapping (address => uint) public balances;

    // Events allow clients to react to specific
    // contract changes you declare
    event Sent(address from, address to, uint amount);

    // Constructor code is only run when the contract
    // is created
    constructor() {
        minter = msg.sender;
    }

    // Sends an amount of newly created coins to an address
    // Can only be called by the contract creator
    function mint(address receiver, uint amount) public {
        require(msg.sender == minter);
        require(amount < 1e60);
        balances[receiver] += amount;
    }

    // Errors allow you to provide information about
    // why an operation failed. They are returned
    // to the caller of the function.
    error InsufficientBalance(uint requested, uint available);

    // Sends an amount of existing coins
    // from any caller to an address
    function send(address receiver, uint amount) public {
        if (amount > balances[msg.sender])
            revert InsufficientBalance({
                requested: amount,
                available: balances[msg.sender]
            });

        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

Compiling the Contract

On the left side, click the third icon, outlined in green in the image above. The "SOLIDITY COMPILER" appears. At the bottom, click the blue "Compile Coin.sol" button.

On the left side, the third icon now has a green check-mark on it, and at the lower left, in the CONTRACT section, a button appears labeled "Coin (Coin.sol)", as shown below.

Using the JavaScript VM

On the left side, click the fourth icon from the top, outlined in green in the image below.

The "DEPLOY & RUN TRANSACTIONS" pane opens.

Use an Environment of "JavaScript VM (London)". If the account balances are not all 100 Ether, switch to "JavaScript VM (Berlin)" and back to "JavaScript VM (London)" to refresh the blockchain to its initial state.

This is a simulated Ethereum blockchain in your browser, with some accounts already set up and funded to make testing easy.

To see the account balances, at the top left, on the right side of the ACCOUNT field, click the little double-arrow icon.

You see several account numbers, each with 100 ether, as shown below.

Deploying your Contract

Click the orange Deploy button.

On the lower left, in the Deployed Contracts section, you see "COIN...". Click the > sign next to "COIN" to expand that section and you'll see four buttons, starting with an orange mint button, as shown below.

Notice these things:

Minting Coins

At the top left, click the Copy icon next to your ACCOUNT number.

On the lower left, click the down-arrow to the right of the orange mint button to see its two arguments, as shown below.

Paste your ACCOUNT number into the receiver field in the "mint" section.

Enter an amount of

222000000000000000000
as shown below.

On the lower left, click the orange transact button.

On the lower right, the log shows a new item with a green checkmark, indicating that the transaction proceeded without errors, as shown below.

Checking your Balances

On the lower left, click the down-arrow to the right of the blue-gray balances button to see its argument, as shown below.

To get your current address, at the top left, click the Copy icon next to your ACCOUNT number.

On the lower left, paste that address into the address field in the "balances" section.

On the lower left, click the blue-gray call button.

The result shows that your account has a balance of

222000000000000000000
as shown below.

At the top left, on the right side of the ACCOUNT field, click the little double-arrow icon.

Your account has slightly less than 100 Ether left, as shown below.

Why are there two balances? Because there are now two currencies on this blockchain: Ether is the native currency, used to pay gas fees for transactions. The Coins you defined are a new currency that lives on the same blockchain, which can be bought and sold independently of Ether.

Sending Coins

At the top left, on the right side of the ACCOUNT field, click the little double-arrow icon.

Select the second account, as shown below. Click the Copy icon next to that ACCOUNT number.

This is the account we will send Coins to.

At the top left, on the right side of the ACCOUNT field, click the little double-arrow icon.

Select the first account again, as shown below.

This is the account we will send Coins from.

On the lower left, click the down-arrow to the right of the orange send button to see its two arguments, as shown below.

Paste the ACCOUNT number in your clipboard into the receiver field, as shown below.

Enter an amount of

5000000000000000000
as shown below.

On the lower left, click the orange transact button.

On the lower right, the log shows a new item with a green checkmark, indicating that the transaction proceeded without errors, as shown below.

C 352.1: Coins in First Account (5 pts)

Use the functions available to answer this question:
How many Coins are in the First Account?
The flag is covered by a green rectangle in the image below.

C 352.2: Ethers in Second Account (5 pts)

Use the functions available to answer this question:
How many Ethers are in the Second Account?
The flag is covered by a green rectangle in the image below.

References

Ethereum Basics

Introduction to Smart Contracts

Posted 5-15-2021
Renumbered from 336 to 352 5-16-2021
Explanation of the London Javascript environment added 10-26-21