Hi,
This thread has the purpose of showing small code example that should help to understand smart contract functionalities, so i invite everybody who wants to contributes to post example or anything interesting.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 they are working and what you can actually 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.ioGuides:
-
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
pragma solidity ^0.4.19;
contract SimpleStorage {
bytes input;
function setInput(bytes enterBytes){
input = enterBytes;
}
}
Trying to pass to setInput 1 byte
or 32 bytes
["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 bytesFinally passing 33 bytes
["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
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:
// 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:
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#eventsYou can also verify everything wrote above checking the results on the testnet Ropsten
https://ropsten.etherscan.io
How to implement a pattern which allow you to "change" your smart contract
A similar patter can be applied
Make the smart contract that will perform the operations and contain the data
pragma solidity ^0.4.9;
contract Son{
uint result;
function Son(uint initial_value) public {
result= initial_value;
}
function setRisultato(uint value) {
result= value;
}
function getResult() constant returns (uint) {
return result;
}
}
after you can create the "Official" contract which will be used, here we need to define the interface of the contract we plan to use underneath (Son), then create an instance of it and call the functions defined in the interface.
If after you want to change the logic, but you want to keep the "Official" contract which maybe is already in production, you can simply swap the address of the son contract.
pragma solidity ^0.4.9;
contract Son{
function setResult(uint value);
function getResult() constant returns(uint);
}
contract Father{
Son son;
function Father(address new_son) public {
son= Son(new_son);
}
function setNewSon(address new_son) {
son= Son(new_son);
}
function setResult(uint value) {
return son.setResult(value);
}
function getResult() constant returns(uint) {
return son.getResult();
}
}
There are different pattern that allow to change behavior of a smart contract.