Tutorial - Introduction to the smart contracts testing & programming

Smart contracts
Smart contracts - it is a feature that will allow us to fully develop the potential of blockchain technology. More and more blockchains are being developed to improve smart contracts techniques, tools and frameworks. We have ethereum, eos lisk, waves, even bitcoin(RSK) will have on smart contracts in the future. Also somwhere underneath steem smart media tokens, smart contracts will be runing.
I will try to explain what a smart contracts are and how to start programming in them based on tools available for Ethereum platform. The name Smart Contract was introduced by Nick Szabo in 1996(21 years ago!). Smart contract is self-executing, distributed computer program. As an ordinary contract, a smart contract has rules written in it, based on which individual contract functions will be performed. These rules are based on data available within Ethereum blockchain or on external data provided by the oracles.
Basics
There are some based concepts in the Ethereum blockchain which you should know.
- We have two type of accounts, each of them is identified by the address. Normal accounts, controlled by the users who has keys to them. Or contract accounts controled by the code(smart contracts accounts).
- Transactions - Simple kind of transactions is sending the ethere to the another account. But you can run different kind of transaction, you can data to the contract account so it will be able to execute the code.
- As i mentioned before, smart contracts are computer programs. To create programs we need programming language. Solidity is a programming language used to create smart contracts on the Ethereum blockchain. Solidity is compiled to the bytecode and run by the EVM (ethereum virtual machine, concept is similliar to the JVM).
Begining and Testing
To learn how to develop smart contracts, the best way to do this is to use a test network to create contracts without worrying about the real assets. Before we start, you should install npm it is really usefull here is great instruction how to do it. Now let's install etehereum test client test rpc, with npm it will be really simple.
npm install -g ethereumjs-testrpc and now run command testrpc this command will create a running instance of TestRPC with 10 test accounts.
Now you can install web3.js or truffle(whis uses web3). With the one of the following commands(you need git installed to instsall web3)npm install -g [email protected] or npm install -g truffle, now run truffle init if you have installed truffle, we will focus on web3 now.
Checking balance of the accounts
With the testrpc runing in the background, open node console(if you dont have node installed install it via npm).
Get the Web3 provider
Web3 = require('web3') provider = new Web3.providers.HttpProvider("http://localhost:8545") web3 = new Web3(provider)
Get ballance for accounts created by the testrpc
web3.eth.accounts.forEach(account => { bal = web3.eth.getBalance(account); console.log(bal);})
Ballance output is not in the ethereum but is in wei, smallest unit in the ethereum(base unit). 1 wei is 1E-18 ETH
Let's try change ballance of one of the accounts by sending transfer transaction
send = web3.eth.accounts[0] rec = web3.eth.accounts[1] transaction = { from: send, to: rec, value: 1000 } web3.eth.sendTransaction(transaction)
Right know you can confirm transactions by checking the accounts balance once again
Looks like transactions has been sent without any problems. That is all for now, in the next part i will try to create ticketing system based on the ethereum blockchain (something what is very popoular now in the ICO space).
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @whd I am @utopian-io. I have just upvoted you at 7% Power!
Achievements
Suggestions
Human Curation
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x