Recipe ID: hsts-r36
We offer blockchain introduction, Hyperledger for system admin, Ethereum, Solidity, Corda R3, Hyperledger for developers, blockchain cybersecurity and more classes in self-paced video format starting at $60. Click here to learn more and register. For complete self-paced blockchain training, visit our Complete Blockchain Development Training page.
The latest version of this tutorial along with all updated source codes is available via the below link:
https://myhsts.org/blog/ethereum-dapp-with-evm-remix-golang-truffle-and-solidity-part1.html
Ethereum is a general-purpose blockchain that is more suited to describing business logic, through advanced scripts, also known as smart contracts. Ethereum was designed with a broader vision, as a decentralized or world computer that attempts to marry the power of the blockchain, as a trust machine, with a Turing-complete contract engine. Although Ethereum borrows many ideas that were initially introduced by bitcoin, there are many divergences between the two.
The Ethereum virtual machine and smart contracts are key elements of Ethereum, and constitute its main attraction. In Ethereum, smart contracts represent a piece of code written in a high-level language (Solidity, LLL, Viper) and stored as bytecode in the blockchain, in order to run reliably in a stack-based virtual machine (Ethereum Virtual Machine), in each node, once invoked. The interactions with smart contract functions happen through transactions on the blockchain network, with their payloads being executed in the Ethereum virtual machine, and the shared blockchain state being updated accordingly.
For those who are not familiar with blockchain technology reading History and Evolution of Blockchain Technology from Bitcoin article is strongly recommended. Also, if you wish to learn and practice Hyperledger blockchain development, visit Comprehensive Hyperledger Training Tutorials page to get the outline of our Hyperledger tutorial articles.
We have written two sets of tutorials to explore Ethereum and Solidity programming in depth. First set covers the following nine recipes:
In short, you learn about how to set up and configure Ethereum and develop blockchain applications using Solidity programming language. We explore its key components, including smart contracts and Web3.JS API via an Auction Decentralized Application (DApp) step-by-step.
In second set, we will discuss more advance topics in Ethereum blockchain development and solidity while building a Tontine DApp game step-by-step. Specifically, we cover Truffle and Drizzle. For instance, we show you how a tool such as Truffle can be an assistant in building, testing, debugging, and deploying DApps. In summary, we are going to cover four main topics:
The 2nd set consists of 8 recipes as follows:
IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.
If you love compiling and running code using command lines, you can use the Solidity compiler, solc. To use the latest stable version of the Solidity compiler, run the following:
sudo apt-get update
sudo apt-get install solc
Start by copying the contract code into a file named auction.sol, and afterwards run the following command to compile the contract and generate the needed ABI and bytecode.
The output will be saved in a details.js file as follows:
echo "var ContractDetails=`solc --optimize --combined-json abi,bin auction.sol`" > details.js
If you see a set of warnings, neglect them and then run in succession the following commands in your Geth CLI, in succession:
loadScript(“your Path/details.js”)
var ContractAbi = ContractDetails.contracts["auction.sol:MyAuction"].abi; var ContractInstance = eth.contract(JSON.parse(ContractAbi));
var ContractBin = "0x" + ContractDetails.contracts["auction.sol:MyAuction"].bin;
Before you continue, make sue you have unlocked your first account:
var deploymentTransationObject = { from: eth.accounts[0], data: ContractBin, gas: 1000000 };
var auctionInstance = ContractInstance.new(deploymentTransationObject); var AuctionAddress =
eth.getTransactionReceipt(auctionInstance.transactionHash).contractAddress;
From the console, to interact with the contract, you can create an object pointing to your contract's address as follows:
var Mycontract = ContractInstance.at(AuctionAddress);
This is similar to what we have done so far in Web3Js and, depending on the nature of the function, we can execute a call using Mycontract.FunctionName.call() or send a transaction using Mycontract.FunctionName.sendTransaction(FunctionArguments, {from: eth.accounts[0], gas: 1000000}).
In order to validate your transaction, you'll need to run the mining process in Geth using miner.start() or use the mining script we used earlier when we configured the private chain.
Here, we have seen a winding path for compiling and deploying a contract using solc, web3.js, and a local blockchain. Tools such as Truffle (that you'll discover in our follow-up recipes) do all that for you under the hood, and make perfect alternatives for quick development and testing.
In the previous procedure of running a local network, we used the Geth client with POW mining to validate the transaction. However, you may find mining a daunting process as its difficulty will rise, causing annoying, heavy computations reducing your system capacity even if we set it as low as possible in the genesis file.
To solve this issue, you have two choices: either configure the network to use an alternative mining mechanism called POA, or fix the mining difficulty in the Geth client source code. Let's have a look at these possibilities.
To explore this option, you'll need to do some Golang hacking! Don't worry; there's nothing complex.
In Geth's code (the consensus.go file at; https://github.com/ethereum/go-ethereum/blob/master/consensus/ethash/consensus.go , there is a function called CalcDifficulty that represents the difficulty adjustment algorithm. It returns the difficulty that a new block should have when created, given the parent block's time and difficulty. To fix the difficulty, we need to fix the returned value as follows:
func CalcDifficulty(config *params.ChainConfig, time uint64, parent
*types.Header) *big.Int { return big.NewInt(1)
}
Afterwards, compile your new Geth client following the instructions presented in the official Geth GitHub repository at https://github.com/ethereum/go-ethereum/wiki/Installing-Geth
An alternative solution to avoid relying on proof of work (POW) mining is to use a replacement consensus mechanism known as POA, in which we trust a set of known validators (authorities) to accept transactions. In other words, POA validators are staking their identity and voluntarily disclosing who they are in exchange for the right to validate the blocks. Unlike POW, POA is a centralized mechanism that delivers comparatively fast transactions, thus POA deployment is used by private (enterprise) and some public testing networks (for example, the popular Kovan and Rinkeby test networks).
To set up a private chain with Geth and POA, you will need to reiterate the same Puppeth procedure that was presented earlier, except for choosing the consensus engine. Start Puppeth, follow the questionnaire, and choose POA as the consensus engine. In this case, you'll need to determine the validator accounts (accounts that are allowed to seal). For that, copy and paste the Ethereum accounts that were created earlier by Geth. Of course, you can define as many sealers as you like but POA is able to work with a single node and only one sealer:
Keep in mind that POA doesn't have mining rewards, and therefore it's highly recommended that you allocate enough ether (defined in the unit of wei) to your accounts first, and then initialize your nodes with the resultant POA genesis file.
After laying out the different available deployment environments, it's time to run our auction DApp in our next recipe.
To conclude this recipe, we like to recommend our Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs, and Become Blockchain Certified Security Architect in 30 hours courses to those interested in pursuing a blockchain development career. This recipe is written by Brian Wu who is our senior Blockchain instructor in Washington DC. His Blockchain By Example book is highly recommended for learning more about blockchain development.
Hands-on Node.JS, MongoDB and Express.js Training
Advance JavaScript, jQuery Using JSON and Ajax
Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs
Learn Blockchain Hyperledger Development & Get Certified in 30 Hrs
Become Blockchain Certified Security Architect in 30 hours
Blockchain Certified Solution Architect in 30 hours
Introduction to Python Programming
Object Oriented Programming with UML
We offer private coding classes for beginners online and offline (at our Virginia site) with custom curriculum for most of our classes for $59 per hour online or $95 per hour in virginia. Give us a call or submit our Private Coding Classes for Beginners form to discuss your needs.