Ethereum Smart Contract Programming 201
Ethereum Smart Contract Programming 201
Welcome
In order to continue to the next lecture, you need to have the following software installed on your computer.
You should also have taken Ethereum Smart Contract Programming 101 before you take this course.
Setup Environment
Truffle Introduction
Install Truffle: npm install -g [email protected]
Download Ganache: https://github.com/trufflesuite/ganache/releases/tag/v2.1.1
Forum Discussion: https://forum.ivanontech.com/t/truffle-introduction/10049
Hello World
Truffle Introduction
The complete code can be found by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/truffle-introduction/10049
mkdir Helloworld;
cd Helloworld;
truffle init
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.5.12;
contract Helloworld {
string message = "Hello World";
Forum Discussion: https://forum.ivanontech.com/t/truffle-introduction/10049
module.exports = function(deployer) {
deployer.deploy(Helloworld);
};
truffle compile --list --all # To have a list of all installed compiler versions
truffle compile
In ganache in Contracts tab add truffle-config.js so that ganache can monitor the contracts deployed
truffle migrate
truffle console
Setter Function
Truffle Introduction
The complete code can be found by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/truffle-introduction/10049
In truffle console:
migrate
This will compile and upgrade the contract on ganache but do add --reset to re-deploy because sometimes
truffle doesn't "see" all the changes (for example when you change the migrate files)
migrate --reset
With truffle you can modify the Helloworld_deploy.js to add interaction with the contract (like set some init
state, transfer eth):
module.exports = function(deployer) {
deployer.deploy(Helloworld).then( instance => {
instance.setMessage("Mars!").then( receipt => {
//console.log("receipt = " + receipt);
instance.getMessage().then( message => {
console.log("message = " + message);
})
});
});
};
Error Handling
Truffle Introduction
The complete code can be found by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/truffle-introduction/10049
Divide your code in unit and run test for each unit
Yes - I commented out the following line (line #868) in the MOCHA library file runner.js ::
//process.on(‘uncaughtException’, uncaught);
Thanks …
/usr/lib/node_modules/truffle/node_modules/mocha/lib/runner.js
})
Importing the people contract
Unit Testing
Link to the contract: https://github.com/filipmartinsson/solidity-0.5.12-course/blob/master/Inheritance/
HelloWorld.sol
Forum Discussion: https://forum.ivanontech.com/t/unit-testing-in-truffle/10053
Forum Discussion: https://forum.ivanontech.com/t/unit-testing-in-truffle/10053
For verify require and asserts you need and additional truffle module to install:
npm init
npm install truffle-assertions
let instance;
before(async function(){
instance = await People.deployed()
});
});
});
});
Truffle Instances
Unit Testing
The final code is available by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/unit-testing-in-truffle/10053
before(async function(){
instance = await People.deployed()
});
and
We have also after and afterEach that executes at the end of the unit and at the end of each it() respectively
Value Assignment
Unit Testing
The final code is available by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/unit-testing-in-truffle/10053
Template Walkthrough
Dapp Intro
Forum Discussion: https://forum.ivanontech.com/t/dapp-introduction/10054
Project Setup
Dapp Intro
The final code is available by clicking here.
Forum Discussion: https://forum.ivanontech.com/t/dapp-introduction/10054
python -m http.server
Connect Metamask to ganache. Import an address from ganache to Metamask using the address private key
https://web3js.readthedocs.io/en/v1.2.11/
https://web3js.readthedocs.io/en/v1.2.7/web3-utils.html
$(document).ready(function() {
console.log("In ready function!");
window.ethereum.enable().then( accounts => {
//contractInstance = new web3.eth.Contract(abi, address, addressinjson)
contractInstance = new web3.eth.Contract(abi,
"0x581101d0622E110CB89f86364C00EE0277f48604", {from: accounts[0]});
console.log("contractInstance=", contractInstance);
})
})
Setting Data
Dapp Intro
The final code is available by clicking here.
$("#add_data_button").click(inputData)
function inputData() {
console.log("Hash=", hash)
})
console.log("confirmationNr=", confirmationNr)
})
console.log("Receipt=", receipt)
alert("Dones")
})
function showData() {
$("#name_output").text(res.name)
$("#age_output").text(res.age)
$("#height_output").text(res.height)
})
Project Introduction
Programming Project - Phase 1
Forum Discussion: https://forum.ivanontech.com/t/programming-project-phase-1/10055
Flip Project: You bet some ether and you can loose all or double the stake.
You are going to use:
- HTML/JS
- Web3.js
For the interface (web page)
and:
- Smart contract
- Truffle
Project Description
Programming Project - Phase 1
Pseudo Randomness
Programming Project - Phase 1
function random() private returns(uint) {
return now % 2;
}
Hand In
Programming Project - Phase 1
When you are done with your code, please take some screenshots or screen recordings of you dapp in action
and post it here.
For this first phase of the project, you don't have to post your code.
solidity contract
// SPDX-License-Identifier: MIT
import "./Ownable.sol";
_;
// Returns 0 or 1
return block.timestamp % 2;
uint win = 0;
if (random() == userBet) {
//if (1 == 1) {
win = msg.value * 2;
msg.sender.transfer(win);
msg.sender.transfer(address(this).balance);
main.js
$(document).ready(function() {
console.log("contractInstance=", contractInstance)
})
updateBalance()
backgroundBalance()
function backgroundBalance() {
window.setTimeout(function() {
updateBalance()
backgroundBalance()
}, 5000);
}
// hook bet-btn to bet function
$("#bet-btn").click(bet)
// bet function
function bet() {
return false
betAmount = web3.utils.fromWei(parseInt(balance/2).toString())
$("#bet").val(betAmount)
return false
//alert(betAmount)
let config = {
// Don't know why but res.events did not give event betResult by name but only [0]
console.log("win=", win)
if (win > 0) {
} else {
updateBalance()
})
/*
contractInstance.methods.bet(coinSide).send(config)
console.log("Receipt=", receipt.events[0].raw.data)
})
console.log("Error=", error)
})
*/
// Dani
// Dani2
//contractInstance.methods.bet(coinSide).send(config).then(async function(res){
//})
// Dani3
//})
})
function updateBalance() {
console.log("Contract balance=",balance)
})
/*
function inputData() {
let name = $("#name_input").val()
let config = {
console.log("Hash=", hash)
})
console.log("confirmationNr=", confirmationNr)
})
console.log("Receipt=", receipt)
alert("Done")
})
function showData() {
console.log("res=", res)
$("#name_output").text(res.name)
$("#age_output").text(res.age)
$("#height_output").text(res.height)
})
*/
})
Oracle Intro
Programming Project - Phase 2
Oracle Code
Programming Project - Phase 2
Link to Provable Ethereum API: https://github.com/provable-things/ethereum-api
ropsten: {
provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-
PROJECT-ID`),
network_id: 3, // Ropsten's id
gas: 5500000, // Ropsten has a lower block limit than mainnet
confirmations: 2, // # of confs to wait between deployments. (default: 0)
timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
skipDryRun: true // Skip dry run before migrations? (default: false for public nets )
},
Than npm init to make a node project then npm install truffle-hdwallet-provider ( It worked instead: npm
install --save-dev @truffle/hdwallet-provider )
import "provableApi.sol"