Lab
6 Details for MPCS 56605
Each
lab
will consist of a small problem and details of how to proceed.
Each lab is intended to give every student hands-on experience
with the core concepts and technologies covered during the
course. A student may concentrate, as a team member, on
one technology over another for the final project, but labs are
designed to give each and every student exposure to all the
technologies that come into play. You need to submit labs
to the TAs for grading--see submission instructions below.
Generally, unless otherwise specified, you will have one week to
complete each assigned lab.
See
the
syllabus for information on grading. Turning in lab
assignments on time is required, without exception, and all late deliveries will be
penalized, regardless
of cause. Submit your assignments to the
subversion repository according to the directions on the
syllabus page.
Lab 6
Due: 4:00 pm,
Monday, July 31, 2023
Problem 1:
Smart Contracts: Testing Smart
Contracts:
BACKGROUND:
Like
all programming problems, learning a new technology is not an
exercise in reading but rather an exercise in thinking and
typing. In this lab, you will continue programming smart
contracts on Ethereum. This lab is designed to give you
more hands-on experience in some fundamental skills involved in
smart contract development and testing. In this lab, you
will create from scratch a smart contract using Visual Studio
Code that implements a new ERC-20 Token in solidity with truffle
and ganache. You will write tests to confirm that your
functions are executing properly. You will then deploy
your smart contract on Ganache and run the tests to confirm
correct execution.
WHAT YOU NEED TO DO:
Like
all programming problems, learning a new technology is not an
exercise in reading but rather an exercise in thinking and
typing. In this lab, you will continue programming smart
contracts on Ethereum. This lab is designed to give you
more hands-on experience in some fundamental skills involved in
smart contract development and testing. In this lab, you
will create from scratch a smart contract using Visual Studio
Code that implements a new ERC-20 Token in solidity with truffle
and ganache to run a basic ...???. Whenever you ..., you
get a token. You will write tests to confirm that your
math functions are executing properly. You will then
deploy your smart contract on Ganache and run the tests to
confirm correct execution.
STEP 1:
Since we are
developing a new smart contract that manages a new token
according to the ERC-20 specification, it would behoove you to
become familiar with it! Before beginning this lab, read
through the ERC-20 token standard on ethereum.org:
ERC-20: Token
Standard (ethereum.org)
Also read through the background information at the ERC20 wiki
here:
ERC20
Token Standard - IndexUniverse Crypto
and the
ERC-20 FAQs here:
What
is the ERC-20 Ethereum Token Standard? - CoinDesk
Also,
make sure you are very familiar with Section 7.6 of Infante's
Building Ethereum Dapps. You will find it helpful as you
work on this lab. You may examine the sample
implementations referenced from ConsenSys and OpenZepplin for
clarification of the implementation ideas (only). Now
that you've got a clue where we're going, let's get started.
STEP 2:
First, let's
create a lab workspace. Create a Lab.6/metacoin directory
wherever you want (e.g., under ~/56605/src/LABS) and initialize
the truffle framework by unboxing a truffle boilerplate that
will help us along with this lab. You can see a listing of
the current truffle boxes available here:
Truffle Boxes -
Truffle Suite. You can initialize tutorial
boilerplates for a pet shop, a React project, a bazaar, an
Angular-based Truffle Daap, a room thermostat hosted in Azure,
etc. Some of the boxes are deprecated, including (alas)
tutorialtoken. We're just going to use the following
command (from your Lab.6/metacoin (or wherever you want it)
directory):
$ truffle unbox
When you get your prompt back, here's what you have in your
directories:
$ find .
.
./migrations
./migrations/1_initial_migration.js
./migrations/2_deploy_contracts.js
./.DS_Store
./test
./test/metacoin.js
./test/TestMetacoin.sol
./contracts
./contracts/MetaCoin.sol
./contracts/ConvertLib.sol
./contracts/Migrations.sol
./truffle-config.js
Replace the truffle-config.js file that was created by truffle
with this
version here which uncomments the development network
section and changes the port to ganache listening on port 7545
and sets the default compiler version to our default of
0.5.16.
Next, open ganache and create a new workspace, and call it
Metacoin and link the project .../Lab.6/metacoin to it
(remember? by selecting "truffle-config.js"...). If
your memory fails you, look back to labs 4 and 5 for detailed
instructions on this. You should have a new ganache
workspace that looks something like this (again, account numbers
and mnemonic will of course differ...):

Once you've done the above, let's run truffle deploy (the
details of the output will of course vary in your case):
$ truffle deploy
Compiling your contracts...
===========================
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to
/Users/mark/Dropbox/UofC/56605/src/LABS/Lab.6/metacoin/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash:
0x899b45fbf934ca5b605484b25bfb8f78e08ab5c150e2121ab8f6b2f86c1928d3
> Blocks:
0
Seconds: 0
> contract address:
0x1723D88C9A83Bb464Ba17c41B60F3eBf9A52Ba7f
> block
number: 189
> block timestamp:
1685482211
>
account:
0xa4208DA857fF1Bf784B7b80E48f5d0Cd764d0369
>
balance:
98.81432088
> gas
used:
225237 (0x36fd5)
> gas
price:
20 gwei
> value
sent: 0
ETH
> total
cost:
0.00450474 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total
cost:
0.00450474 ETH
2_deploy_contracts.js
=====================
Deploying 'ConvertLib'
----------------------
> transaction hash:
0x2ec53a834bbb14b0d903b60b1ea614b24bb2681c2d294ac321dacf8e20c72054
> Blocks:
0
Seconds: 0
> contract address:
0xFAC37BDDeC2d99a7f6f6C1FD611A592780C110D0
> block
number: 191
> block timestamp:
1685482212
>
account:
0xa4208DA857fF1Bf784B7b80E48f5d0Cd764d0369
>
balance:
98.81156422
> gas
used:
95470 (0x174ee)
> gas
price:
20 gwei
> value
sent: 0
ETH
> total
cost:
0.0019094 ETH
Linking
-------
* Contract: MetaCoin <--> Library:
ConvertLib (at address:
0xFAC37BDDeC2d99a7f6f6C1FD611A592780C110D0)
Deploying 'MetaCoin'
--------------------
> transaction hash:
0x3947361f616862c3ae83d043acbe509ed137a06498f23819e89434df0385237e
> Blocks:
0
Seconds: 0
> contract address:
0xF1d5612365CcE12C17880616f1BB98D9bCa126f0
> block
number: 192
> block timestamp:
1685482212
>
account:
0xa4208DA857fF1Bf784B7b80E48f5d0Cd764d0369
>
balance:
98.80583292
> gas
used:
286565 (0x45f65)
> gas
price:
20 gwei
> value
sent: 0
ETH
> total
cost:
0.0057313 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total
cost:
0.0076407 ETH
Summary
=======
> Total deployments: 3
> Final
cost:
0.01214544 ETH
Now, let's run the default tests:
$ truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Compiling ./test/TestMetacoin.sol
> Artifacts written to
/var/folders/mt/2wzh5nb144g44l496xrdkf3r0000gn/T/test--18374-YY5N6DoU07zS
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
TestMetacoin
✔
testInitialBalanceUsingDeployedContract (177ms)
✔
testInitialBalanceWithNewMetaCoin (116ms)
Contract: MetaCoin
✔ should put
10000 MetaCoin in the first account (67ms)
✔ should call
a function that depends on a linked library (86ms)
✔ should send
coin correctly (221ms)
5 passing (7s)
STEP 2:
Next, let's
create a new lab workspace. Create a Lab.6/MPCS20Token
directory wherever you want (e.g., under ~/56605/src/LABS) and
initialize the truffle framework using the following command
(from your Lab.6/MPCS20Token directory):
$ truffle init
After running init, open the blank project in MS Code.
Open a terminal window, and make the usual edits, you know the
drill by now, uncomment the development network section and
change the port to ganache listening on port 7545 and set the
default compiler version to our default of 0.5.16. Then
save your changes.
STEP 3:
Next, download
this
solidity interface file and place it in your contracts
subdirectory (e.g., in Lab.6/MPCS20Token/contracts). Then,
copy this
migrations file and place it in your migrations
subdirectory. This file will set up an initial deployment
of your new token with 10,000 "Uchicago Tokens" with symbol
"UCT". Also, copy over the Migrations.sol file from the
metacoin/contracts directory and the 1_initial_migration.js file
from the metacoin/migrations directory into your MPCS20Token
project.
Your next task is to implement the ERC-20 interface in solidity
inside a file called ERC20MPCSToken.sol.
You should use some require statements to make sure
parameter values are within range, and your implementation of TransferFrom()
should emit the Transfer event and your implementation of Approve()
should emit the Approval event.
When you've got all the right files in your project, your MS
Code Explorer window should look like this:

STEP 4:
Next, let's
add a test for our contract. In our MPCS20Token directory,
issue this command:
$ truffle create test ERC20MPCSToken
After running create test, a new test file with a JavaScript
test frame that truffle created is under the test directory in
your project in MS Code. Rename it to "erc20mpcsToken.js" if
necessary. Now, run the base test frame:
$ truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Contract: ERC20MPCSToken
✔ should
assert true
1 passing (111ms)
Ok, that initial test (just of deployment) ran just fine.
Let's build some real tests.
STEP 5:
Next, let's
create some new tests that test out our new coin. Inside
your test file erc20mpcsToken.js, create several new tests that
test account balances and transfers, etc. Specifically,
and at a minimum, you should verify that:
$ truffle test
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Contract: ERC20MPCSToken
✔ should
assert true as deployed
✔ should have
put 10000 ERC20MPCSTokens in the first account
✔ should send
10 coins correctly from accounts[0] to accounts[1] (224ms)
3 passing (369ms)
You might find the following helpful in your testing (you'll
have to convert it from metacoin to your ERC20MPCSToken
interface): Write
JavaScript tests - Truffle Suite (Note
you can skip the implementation of the test described as
"should call a function that depends on a linked library", as
we're not carrying that over).
References:
You
may
find the following references helpful (in addition to the links
from previous labs):
Submitting:
Each
assignment will have an invitation to create a Github
repository. You will use the lab[1-7]-YOUR_GITHUB_USERID to
submit your homework.