Understanding ‘Transaction’ , the smallest unit of Ethereum activity
Blockchain is database. It is a chain of blocks. Each block contains many transactions.
In the above blog (It is in Thai. The English version is coming soon), it is all about hashing and how each block is chained together. This blog, I will go into more details than block unit, which is transactions. I will use Ethereum, as an example throughout this blog.
Ethereum is a blockchain that add Smart Contract.
Smart Contract is simply a contract with programming logic.
Normally, we can interact with smart contract through functions in the contract.
For example, A smart contract that is for exchanging ETH to ICO Token. There will be a function called “exchangeEtherToICOToken()”. We can smartly interact with this contract through calling this function with Ether. It will send us back ICO token.
Transaction
The only way to save data in Ethereum is through Transaction.
Transaction in the real world term, we mostly familiar with banking service such as, withdraw, transfer and deposit money.
Back to Ethereum, we call any one of these as transaction:
- Transfer Ether
- Create Smart Contract
- Purchase ERC20 Token
After Ethereum transaction is verified and saved to a block. It is unmodifiable.
Gas
In the blockchain world that there is no centralised bank, the ones who verify transaction are miners who turn on their graphic card all the time.
Miners are not that kind, they want something in return. We need to pay them something. We called it, gas.
Gas price is not fixed. The price fluctuates by transaction loads of the network at the time.
Miners does not care whether who come first. If you pay more, your transaction get verified and saved to Ethereum Network first.
Therefore, when we make a transaction we have to specify maximumgas
amount and gas
price in term of gasPrice
.
We use the word maxium gas
because gas
does not always used up.
For example,
gas=10000
gasPrice=2gwei
This means, we spend at most 20000 gwei
gas (gwei is the smallest unit of ether. Similar to cent to dollar)
Appropriate gas
and gasPrice
can be checked at https://ethgasstation.info/ .
Type of Transaction
To determine the types of transaction, you have to look at its pattern as follows:
Transaction to transfer Ether
Specify sender, receiver, amount and transfer fee in term of gas.
from: eth address of sender
to: eth address of receiver
value: amount of eth
gas: maximum gas amount
gasPrice: specified gas price (wei unit)
nonce: integer number. Used for referring back when you want to repeat transaction
For example,
{
from: "0x3f3783be816135bdc8210e998f9409559258295b",
to: "0x413e8682a1ff2f31da606cca7ca266c2fc71d959",
value: 10000,
gas: 90000,
gasPrice: 20000000000,
nonce: 1
}
Transaction to create Smart Contract
Specify sender, input as compiled smart contract, and transfer fee in term of gas.
* You must specify receiver as 0x0 and amount as 0x0.
from: eth address of sender
to: 0x0
value: 0x0
gas: maximum gas amount
gasPrice: specified gas price (wei unit)
input: compiled smart contract
nonce: integer number. Used for referring back when you want to repeat transaction
For example,
{
from: "0x3f3783be816135bdc8210e998f9409559258295b",
to: "0x0",
gas: 255620,
gasPrice: 1,
nonce: 2,
input: "0x608060405234801561001057600080fd5b506102fa806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806322faf03a146100515780633c1b81a5146100c4575b600080fd5b34801561005d57600080fd5b506100c2600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192908035906020019092919050505061015b565b005b3480156100d057600080fd5b506100d961017d565b6040518080602001838152602001828103825284818151815260200191508051906020019080838360005b8381101561011f578082015181840152602081019050610104565b50505050905090810190601f16801561014c5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b8160009080519060200190610171929190610229565b50806001819055505050565b6060600080600154818054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561021a5780601f106101ef5761010080835404028352916020019161021a565b820191906000526020600020905b8154815290600101906020018083116101fd57829003601f168201915b50505050509150915091509091565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061026a57805160ff1916838001178555610298565b82800160010185558215610298579182015b8281111561029757825182559160200191906001019061027c565b5b5090506102a591906102a9565b5090565b6102cb91905b808211156102c75760008160009055506001016102af565b5090565b905600a165627a7a72305820ff9c194b6a633e1439e4a686e4ee48387b8c278e9aa4e3e1d59af986c8158dbe0029"
}
You can create compiled smart contract through remix IDE
, solc
ofnpm
ortruffle framework
Transaction to call functions of Smart Contract
When you look into smart contract before compilation. It is a simple class in programming.
pragma solidity ^0.4.18;contract Coursetro {string fName;
uint age;function setInstructor(string _fName, uint _age) public {
fName = _fName;
age = _age;
}function getInstructor() public constant returns (string, uint) {
return (fName, age);
}
}
Information from:
https://coursetro.com/posts/code/99/Interacting-with-a-Smart-Contract-through-Web3.js-(Tutorial)
- Specify sender
- Specify receiver as smart contract address
- Amount depends on whether that function require or not
- Transfer fee in term of gas
- input is the function that we want to call
from: eth address of the requester
to: eth address smart contract
value: depends on the function
gas: maximum gas amount
gasPrice: specified gas price (wei unit)
input: function that we want to call (encoded format)
nonce: integer number. Used for referring back when you want to repeat transaction
For example,
{
from: "0x3f3783be816135bdc8210e998f9409559258295b",
to: "0x08adca2c2d03aef61790f8db24bef0eee4a7f1fb",
gas: 90000,
gasPrice: 1,
nonce: 4,
input: "0x22faf03a0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001900000000000000000000000000000000000000000000000000000000000000064e6578747a790000000000000000000000000000000000000000000000000000"
}
Such above input can be decoded as follows:
{
name: "setInstructor",
params: [
{name: "_fName", value: "Nextzy", type: "string"},
{name: "_age", value: "25", type: "uint256"}
]
}
decode by this library: https://github.com/ConsenSys/abi-decoder
You can see that the decoded inputs contain the same methods as our smart contract’s class.
More about nonce
When your transaction did not get verified, you can add more gas
and gasPrice
with the same nonce to accelerate that verification time.
Notice
Each transaction always contains from
and to
. It is implied that a transaction required 2 parties. This is what differentiate blockchain with other type of database. There is no such limitation in other types of database.
Smart Contract is humbly a field called input
inside the Transaction
that to
field is 0x0
.
Complex smart contract will only bloat the input
field. Surely, the transaction to this complex smart contract will require more gas.
We can never modify verified smart contract. We can only create new Smart Contract. We will have to inform our users to use our newly created Smart Contract.
1 Block has many transactions
The same picture from the top of this blog.
A lot of transactions is packed by miners into a block. The block will be linked with the previous block as follows:
{
"number": 1,
"hash": "0xcf470f494d508d09be84cb5ddcfec935eecc46095d67b34693cbecfa9ded7b66",
"parentHash": "0x116eb9eb1f9c54ace9977d5d0b725f3e08185578134614fe2b5a3630c6d1e116",
"mixHash": "0x1010101010101010101010101010101010101010101010101010101010101010",
"nonce": "0x0000000000000000",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"stateRoot": "0x470c50d5ac03f67d58da846f3c607f4082e6e06a5b05f694c684364009296cd8",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"miner": "0x0000000000000000000000000000000000000000",
"difficulty": "0",
"totalDifficulty": "0",
"extraData": "0x00",
"size": 1000,
"gasLimit": 6721975,
"gasUsed": 255620,
"timestamp": 1525595171,
"transactions": [
"0xdbef03bc672111b7261a3cc9fa42ba7df3045f61b5e8a6e5f4c2042fb3f1e23c"
],
"uncles": []
}
The information of a block is too much. Simply focus on the highlighted one.
- transactions: [] This field is an array. It can contain many transactions.
- parentHash This field refer to previous block by its hash. This is what chain this transaction to the previous block.
- hash This is what used to refer to itself.
Web3 for Ethereum Developer
The really raw data that get saved into ethereum network is not as readable as JSON. It is hashed, encoded or encryped to the point of unreadable. Web3 facilitates us greatly by decrypting those data to simple JSON.
You can learn more about Web3 with the above links.
I wish everyone understand different types of Ethereum transaction. There are 3 types in this blogs. Transfering Ethereum, creating Smart Contract, calling methods of Smart Contract. Each differs in term of fields sent to the network.
Moreover, transaction is the smallest unit of a block. Block is chained together through a field named parentHash
.
Lastly, Web3 is the solution for front-end developers that want to connect to Ethereum Blockchain.
Please like our Facebook page if you want more blogs like this. Thanks.