Bitcoin Forum
May 08, 2024, 12:05:36 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [Programming] Learning Smart Contracts  (Read 99 times)
Makkara (OP)
Full Member
***
Offline Offline

Activity: 1064
Merit: 166



View Profile
June 03, 2018, 01:04:41 PM
 #1

Hi,


being a programmer i have started looking into smart contracts, doing experiments in order to learn it. At the moment i'm just curious to know how is working and what you can do, without any particular end to it, since the time i have available is not much. 

I was wondering if anybody here wrote some smart contract and want to share some brief explanation of how some particular functionality works or ask for help / help somebody else that want to learn as well.

Here are some useful links to start:

Tools:

- http://remix.ethereum.org/
- https://metamask.io/
- http://truffleframework.com/ganache/

Test net:
- https://ropsten.etherscan.io
Guides:

- https://medium.com/@mvmurthy/ethereum-for-web-developers-890be23d1d0c
- https://cryptozombies.io/
- https://medium.com/@mycoralhealth/learn-to-securely-share-files-on-the-blockchain-with-ipfs-219ee47df54c - IPFS

Documentation:

- http://solidity.readthedocs.io




I share something i found out which was not very obvious imo.

I was trying to find a way to store with a smart contract an indeterminate amount of bytes, in a efficient way since the costs can be quite high as we all know. After several attempts i found out the EVM (ethereum virtual machine) is using internally word of 256bit -> 32byte, this means that if you try to save less than 32byte you still pay as if you would have transferred 32byte.


I verified this with this smart contract using http://remix.ethereum.org and Metamask


Code:
pragma solidity ^0.4.19;

contract SimpleStorage {
  bytes input;
  function setInput(bytes enterBytes){
    input = enterBytes;
  }
}

Trying to pass to setInput 1 byte
Code:
["0x00"]
or 32 bytes
Code:
["0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa"]

Metamask shows the data transmitted is not changing : Data included: 100 bytes

Finally passing 33 bytes
Code:
["0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa",
"0x00","0xaa","0xff","0xaa","0xaa","0xaa","0xaa","0xaa","0xaa"]

You get Data included: 132 bytes Shocked

 


another thing i have learned:

Once you create a token, for example with the code you can find here:
https://www.ethereum.org/token i was wondering how to make a system which would allow anyone to interact directly with the smart contract and receive them.


To make something like that working i used this function, but in reality you would need other checks which i didn’t add to make sure is safe and not abusable:

Code:
    // Notify x tokens were generated for the address specified 
    event TokenGenerated(address indexed _address, uint _reward);

    function getFreeTokens(){
        uint amount = 100  * 10 ** uint256(decimals);
       
        if (balanceOf[msg.sender] >= amount) throw;
     
        totalSupply += amount;
        balanceOf[msg.sender] += amount;
       
        TokenGenerated(msg.sender, amount);
       
    }

The interesting thing is that if in the event TokenGenerated you put a different value from the actual one, for example:

Code:
TokenGenerated(msg.sender, 1000  * 10 ** uint256(decimals));        

The owner of the address may assume he received 1000 tokens (the transaction coming in will show 1000 tokens), but obviously going to check the balance you could see the amount of tokens is 100.

Instead if we don’t call at all TokenGenerated, the owner of the address still own 100 tokens, but they will not appear in his balance.

All this to show how important are the events and necessary to have updated information on the balances.
More informations here: http://solidity.readthedocs.io/en/v0.4.21/contracts.html#events

You can also verify everything wrote above checking the results on the testnet Ropsten https://ropsten.etherscan.io







Pi Network - mining app for phone: https://minepi.com/spippolino
Blockfolio trading: https://blockfolio.com/trading?r=MvFRE7EX98
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!