Tutorial - Introduction to the smart contracts testing & programming

in #utopian-io9 years ago


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.

  1. 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).
  2. 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.
  3. 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

Sort:  

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • I introduced a competition factor. My vote is based also on how competitive the category used is.

Human Curation

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.079
BTC 63244.09
ETH 1685.25
USDT 1.00
SBD 0.39