A smart contract is application code that resides at a specific address on the blockchain known as a contract address. Applications can call the smart contract functions, change their state, and initiate transactions. Smart contracts are written in programming languages such as Solidity and Vyper, and are compiled by the Ethereum Virtual Machine into bytecode and executed on the blockchain.
Ethereum Accounts
There are two types of accounts in Ethereum: Externally Owned Accounts (EOA) and Contract Accounts. An EOA is controlled by a private key, has no associated code, and can send transactions.
A contract account has an associated code that executes when it receives a transaction from an EOA. A contract account cannot initiate transactions on its own. Transactions must always originate from an EOA.
How Transactions Run on Ethereum
A transaction in Ethereum is a signed data message sent from one Ethereum account to another. It contains the transaction sender and recipient information, the option to include the amount of Ether to be transferred, the smart contract bytecode, and the transaction fee the sender is willing to pay to the network validators to have the transaction included in the blockchain, known as gas price and limit.
Transaction Payments on Ethereum
Payments can be made for transactions using Ether. Ether serves two purposes. First, it prevents bad actors from congesting the network with unnecessary transactions.
Second, it acts as an incentive for users to contribute resources and validate transactions (mining). Each transaction in Ethereum constitutes a series of operations to occur on the network (i.e. a transfer of Ether from one account to another or a complex state-changing operation in a smart contract).
Each of these operations have a cost, which is measured in gas, the fee-measure in Ethereum. Gas fees are are paid in Ether, and are often measured in a smaller denomination called gwei. [1 ether = 1,000,000,000 gwei (10^9)]
Ethereum on Applications
When a transaction triggers a smart contract, all nodes of the network execute every instruction. To do this, Ethereum implements an execution environment on the blockchain called the Ethereum Virtual Machine (EVM). All nodes on the network run the EVM as part of the block verification protocol.
In block verification, each node goes through the transactions listed in the block they are verifying and runs the code as triggered by the transactions in the EVM. All nodes on the network do the same calculations to keep their ledgers in sync. Every transaction must include a gas limit and a fee that the sender is willing to pay for the transaction.
Miners have the choice of including the transaction and collecting the fee or not. If the total amount of gas needed to process the transaction is less than or equal to the gas limit, the transaction is processed. If the gas expended reaches the gas limit before the transaction is completed, the transaction does not go through and the fee is still lost.
All gas not used by transaction execution is reimbursed to the sender as Ether. This means that it’s safe to send transactions with a gas limit above the estimates.
Signing a transaction generates a signature on a transaction using the private key of the transaction sender’s account. Transactions need to be signed before they are submitted to the network.
Deployment of Smart Contract on Ethereum
Transactions can also be used to publish smart contract code to the Ethereum blockchain. You can follow the transaction status with the method eth_getTransactionReceipt, which will also return the newly created smart contract address once it’s included on the blockchain. The resulting smart contract address cannot be chosen, as they are calculated using a hash function and can’t be easily predicted.
Hard Fork on Ethereum
A hard fork is a change to the underlying Ethereum protocol, creating new rules to improve the protocol that are not backwards compatible. All Ethereum clients need to upgrade; otherwise, they will be stuck on an incompatible chain following the old rules.
DApps Vs Smart Contracts
Smart contracts share similarities with decentralised applications(DApps), but there are a few key differences.
Like a smart contract, a dapp is an interface which connects a user to a provider’s service via a decentralised peer-to-peer network.
But where smart contracts require a fixed number of parties to be involved in this relationship, dapps have no limits on how many can participate at any given time. They’re also not confined to purely financial uses as smart contracts are: a dapp can essentially have any purpose that comes to mind.
What do you think of this article? Share your comments below.