C 351: Making a Solidity Contract (10 pts)

What you need:

Background

We'll make a very simple contract: a faucet that distributes small amounts of Ether. We'll continue to use the Ropsten test network so the ETH is free.

Opening the Remix IDE

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

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

Type the name Faucet.sol and press Enter.

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

// Version of Solidity compiler this program was written for
pragma solidity 0.6.4;

// Our first contract is a faucet!
contract Faucet {
    // Accept any incoming amount
    receive() external payable {}

    // Give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {
        // Limit withdrawal amount
        require(withdraw_amount <= 100000000000000000);

        // Send the amount to the address that requested it
        msg.sender.transfer(withdraw_amount);
    }
}

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

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

At the lower left, click the "Compilation Details" button.

Scroll down to see the ASSEMBLY code, as shown below.

This is the compiled code that will be published on the blockchain.

At the top right, click x to close the box showing the ASSEMBLY code.

On the left side, click the "Deploy & run transactions" icon, outlined in green in the image below.

The "DEPLOY & RUN TRANSACTIONS" pane opens.

Select an Environment of "Injected Provider - Metamask".

At the lower left, the Deploy button turns orange, as shown below.

Click the Deploy button.

A "MetaMask Notification" box pops up. Click Confirm. Click Connect.

After a few seconds, at the lower left, in the Deployed Contracts section, you see "FAUCET...". Click the > sign next to "FAUCET" to expand that section and you'll see an orange withdraw button, as shown below.

Viewing the Contract in Etherscan

In Chrome, open a new tab and go to
https://sepolia.etherscan.io/
In the Remix page, on the left side, under the "Deployed Contracts" label, in the "FAUCET AT..." line, click the two-page icon, outlined in green in the image above. This copies the contract's address to the clipboard.

On the Etherscan tab, paste in the address and press Enter to search the blockchain for it.

There is only one transaction for the contract: "Contract Creation", and this contract has a balance of "0 Ether", as shown below.

Funding the Contract

The faucet won't work until it has some ETH to distribute.

In Chrome, click the MetaMask tab.

Click Send.

Paste in the contract's address and send your contract 0.3 SEP, as shown below.

Click Next. Click Confirm.

Refresh the Etherscan tab.

You see two transactions, and a balance of 0.3 Ether, as shown below.

Withdrawing from Your Faucet

On the Remix tab, at the lower left, enter a withdraw amount of
"100000000000000000"
as shown below.

Click the orange withdraw button.

Click Confirm.

When the transaction completes, refresh the Etherscan tab.

You see three transactions, and a balance of 0.2 Ether, as shown below.

Notice that the latest transaction value is 0 Ether, not 0.1 Ether.

C 351.1: Internal Txns (10 pts)

In Etherscan, in the lower pane, click the "Internal Txns" tab.

Now the 0.1 Ether transaction amount appears, as shown below.

The flag is covered by a green rectangle in the image below.

References

Ethereum Basics

Posted 5-10-2021
Renumbered from 331 to 351 5-16-2021
Reference to C 330 added 10-26-21
Converted to use Sepolia testnet 9-8-22