HW3: Simple CardinalToken dapp
CS 359B Spring 2018
Deadline: April 23, 2018 11:00 am
Office hours: April 20, 4:30 pm - 5:30 pm, at Thornton 110. We recommend working on this assignment ASAP and coming to office hours to get help if needed. Worst case, you are welcome to come work on this homework during office hours, so we can help you.
To get you ramped up with the Ethereum development workflow and environment, we are releasing a short assignment which involves replicating the CardinalToken demo presented in the lecture, and then adding a simple feature on top of it.
Development with Remix
This part will use Remix for deploying smart contracts. Remix is easier to setup and understand, but not very powerful. It is not used for production environments.
1. Play with Remix
Deliverable 1.1: The code of your contract. Just paste it as a code snippet in the README.md file of the git repo we provide you. (see submission below)
Deliverable 1.2: Report the number of gas units used for this deployment. Just write the number in the README.md file of the git repo we provide you.
Deliverable 1.3: Find and paste the ABI of this contract. Submit in the same way as above, code snippet in the README.md file.
Deliverable 1.4: The code of your contract
Deliverable 1.5: Report the number of gas units used for this deployment. Has the gas changed? (if not: makes sure you are “auto-compiling” or refresh the page)
Deliverable 1.6: Find and paste the ABI of this contract
Deliverable 1.7: The code of your contract
Deliverable 1.8: Report the number of gas units used for this deployment.
Deliverable 1.9: Find and paste the ABI if this contract
Deliverable 1.10: Report the number of gas units used for this deployment.
Deliverable 1.11: Find and paste the ABI if this contract
2. Connect Remix with Ganache
Deliverable 2.1: Report the number of gas units used for this deployment, as reported by Ganache. Is it the same as in deliverable 1.10?
3. Simple web dapp using above components
- Follow the process described between slide 15 and slide 22, to create a simple website that connects to Ganache, displays the account balance in Cardinal Tokens of the 2nd account of Ganache. (HINT: your solution should modify only about 1 line of code)
Deliverable 3.1: What is the CDT balance reported by your website?
Deliverable 3.2: Submit the resulting working source code into the cardinal-token-remix folder of the git repo we provide you. (see submission below)
Note on IDE: You can use any IDE you want. If you are looking for a recommendation for a simple IDE that supports Solidity, we recommend using Visual Studio Code. Unlike the other software of a similar name, this is actually a very lightweight and straightforward IDE based on ElectronJS. Immediately after installation, click “Install support for Solidity” on the “welcome message” and you will be good to go. If you want an embedded terminal, like the one we used in class, install the extension “Terminal”.
Development with Truffle
Close Remix. We will not use it anymore for deploying contracts. This part will use Truffle for deploying smart contracts. Truffle is more complex to setup and understand because it requires writing migrations and more. But it is more powerful and is generally the community’s choice for production environments.
4. Reproduce simple web dapp
- Follow steps depicted from slide 24 till the end of the presentation, to reproduce the website we showed in class.
Deliverable 4.1: Submit the resulting working source code into the cardinal-token-truffle folder of the git repo we provide you. (see submission below)
5. Improve dapp
- Solidity Part: Create a new function called “mint” in the CardinalToken.sol contract. This function should (i) raise an error if not called by the creator of the contract, (ii) get one variable as an argument, called amount, (iii) increase the total supply of tokens by “amount” and give those tokens to the caller.
Website part: Add a new button with label “Mint”. When this button is pressed, the “mint” function of the contract should be called with the value of the “amount” textbox, to mint this amount of new token and give it to the contract creator.
Deliverable 5.1: Submit the resulting working source code
- Remember to compile your updated contract (`truffle compile`) and fix any errors (ignore the constructor warning)
- Remember to re-migrate your updated contract. Note: if you try to run `truffle migrate` it will say “Network up to date” because truffle already deployed your contract before and doesn’t know you changed it. Since you have made changes and you actually want to redeploy your contract, type `truffle migrate --reset` instead.
- If MetaMask works for the contract owner and crashes or experiences unexpected behavior for everyone else, then you have correctly completed the assignment. It means that your contract throws an error as expected. What happens is that MetaMask and Ganache are smart enough to detect the error before your code is even submitted as a transaction. However, MetaMask doesn't always handle contract errors well yet and reacts either by also crashing, or by displaying zero gas. A better behavior would have been that it informs you of the error. I am sure they will fix it.
- In general, if at any point Metamask does not correctly compute the gas for you and it shows all zeros, there is a problem. It most likely means that your contract throws an error early on and it does not even get posted on Ethereum, so the is no need to pay any gas. (Which may be the intended functionality when you are trying to mint tokens as a user who is not the contract creator.)
- If this functionality is not intended, then do not try to manually set the gas, it will not help. The problem is either with your code, or just simply an issue with metamask or ganache. To solve it try the following things:
- Close all browser tabs of your dapp. We notice problems with metamask when there are multiple tabs of different versions of the same dapp open.
- Look at your code for errors
- Try the workaround in the last slide
Submission - Github Classroom
We will be using github classroom for submitting code in this class. Use the link below to submit your answers and code for this homework:
(Answers go directly in the file README.md, which you can edit from the web, and code goes in the appropriate folders using git)
The above link will create a new private git repository for you in the class’ github account, and invite you to use it. Work directly on this repo, commit frequently. This way if you have questions, we can see the state of your code right away and provide you answers within that context.
You can update your submission as many times as you want before the deadline. Github is automatically sending us the version of your solution as it stands on the deadline.
It’s simple. Submit correct solutions to get full score.