Talk to My Smart Contract (Ethereum Smart Contracts Made Easy)

poki(32)
Published in
#smart
Words
797
Reading
4 min
Listen
Play
9y

So you hear the term cryptocurrency, bitcoin, Ethereum, smart contracts an so on a lot these days. When you think of a smart contract it sounds like a scary alien technology but fear not I will attempt to demystify the smart contract concept and try explain it in everyday mans terms.

Essentially what Ethereum offers is a use of a decentralized super computer, think of it as your own computer but every time you press a key or visit a website, every single other computer in the world replicated the exact same action. So if you dropped your computer and broke it, or it was stolen there is a record of your previous actions/data on every other computer around the world. This makes your program very secure and robust, unless the internet is shut down your data is safe.

How does Ethereum convince people to give up their computers to be used by you ?
Well first it's not free to use, you pay a transaction fee for number of computations you execute, also every 10-15 seconds a reward of 3.5 ether is randomly assigned to a miner who managed to solve a random math equation (It is a bit more complicated)

Between the transaction fee and rewards the people who give up their computers to be part of the network earn money.

So now the smart contract part, smart contract is essentially a state machine, a state machine refers to a timeline of events that cause change from previous state to a new state of the program. Let's say we have a glass of water, we can fill the glass or empty the glass, which are events that can change the state of our glass. We can also check the state of the glass to see if it is empty or not, this is not an event and does not alter the state of the glass.

In a smart contract events that can change the state of the smart contract cost gas and you must pay a small fee in order to change the contract state.
You can also check the state of a contract which does not cost anything.

A smart contract itself is a set of rules that defines how to respond to events. Let's take a simple ICO example,
A smart contract would define the owner, the owner would have the ability to withdraw ether from the contract, the contract also contains tokens, the user can buy tokens by sending ether.

Example transaction

  1. Initial state 0 ether 1000 tokens on the contract
  2. User sends 0.5 eth to contract, the contract has a token price set to 1 token for 0.001 eth
  3. Contract sends 500 tokens to user
  4. New state of contract is now 0.5 eth 500 tokens
  5. Owner of contract withdraws 0.5 eth
  6. State now 0 eth 500 token.

Just as an example I have created and posted an example smart contract to the Ethereum main net!
Essentially it is a very simple contract that can be found on truffle framework website.
All the contract does is allows you to store a number.

Execute the GET() function first, you will see the current state of the contract storage variable.
By executing a SET function you have the ability to change the number which might cost some gas (40 cent) in my case,
You can then execute the GET() function again which checks the state of the storage variable and returns the value. You should see the value you previously set returned by the contract. Note it might take a minute for the transaction to be mined.

You can check out the contract here here:
https://etherscan.io/address/0xF2C9cddfa42F51a05aF2482cF7e503FEC06EeEd7

If you are interested in interacting with this contract go to :
https://www.myetherwallet.com/#contracts

  1. Paste the contract address: 0xF2C9cddfa42F51a05aF2482cF7e503FEC06EeEd7
  2. In the ABI field paste this (It tells myether wallet how to talk to the contract):
    [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

  1. Click access and then you will see a drop down menu with functions, you should see get and set
  2. Click get and after some time a number will appear, that is the last set number and current state of the contract.
  3. You can try set function if you wish but it will cost you small ammount of gas to do so (Few cent)
  4. If you executed step 5 you should be able to read back the value you set when executing the get function again
  5. If you interacted with the contract, go back to look at the contract on etherscan you should now see your own transaction appear.

Congratulations!!!! You have officially communicated with a smart contract. Now you can dig deeper look at other smart contracts, read them and interact with them, all Ethereum smart contracts are public and available for anyone to see and interact with.

If you are feeling generous:
ETH:
0x2ff7ed2f8804d2209209d7ff4bb2c245160569a0

Talk to My Smart Contract (Ethereum Smart Contracts Made Easy) | Ecency