A “smart contract” is simply a program that runs on the Ethereum blockchain. It’s a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.
Smart contracts are a type of Ethereum account. This means they have a balance and they can send transactions over the network. However they are not controlled by a user, instead they are deployed to a network and run as programmed. User accounts can then interact with a smart contract by submitting transactions that execute a function defined on the smart contract. Smart contracts can define rules, like a regular contract, and automatically enforce them via the code. Smart contracts cannot be deleted by default, and interactions with them are irreversible.
Smart Contracts Prerequisites
If you’re just getting started or looking for a less technical introduction, I recommend you start by studying the terms of contracts. When you fully understand how contracts works, learning smart contracts becomes very easy.
So an example of contracts can be a vending machine process.
Smart contracts digitize agreements by turning the terms of an agreement into computer code that automatically executes when the contract terms are met.
From the explanation above, we can proceed to studying how digital contracts works using the vending machine.
A digital vending machine
A simple metaphor for a smart contract is a vending machine, which works somewhat similar to a smart contract, specific inputs guarantee predetermined outputs.
- You select a product
- The vending machine returns the amount required to purchase the product
- You insert the correct amount
- The vending machine verifies you have inserted the correct amount
- The vending machine dispenses the product of choice
Here, the vending machine will only dispense your desired product after all requirements are met. If you don’t select a product or insert enough money, the vending machine won’t give out your product.
The logic programmed into a vending machine. Here is a simple example of how the vending machine might look like as a smart contract:
contract VendingMachine {
// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;
// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 50
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 50;
}
// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}
// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
cupcakeBalances[address(this)] -= amount;
cupcakeBalances[msg.sender] += amount;
}
}
PERMISSIONLESS
Anyone can write a smart contract and deploy it to the network. You just need to learn how to code in a smart contract language, and have enough ETH to deploy your contract. Deploying a smart contract is technically a transaction, so you need to pay your Gas in the same way that you need to pay gas for a simple ETH transfer. Gas costs for contract deployment are far higher. However,
Ethereum has developer-friendly languages for writing smart contracts, such as;
- Solidity
- Vyper
But they must be compiled before they can be deployed so that Ethereum’s virtual machine can interpret and store the contract.
COMPOSABILITY
Smart contracts alone cannot get information about real world events, because they can’t send HTTP requests. This is by design, so they rely on external information could ruin consensus, which is very important for security and decentralization. Another limitation of smart contracts is the maximum contract size. A smart contract can be a maximum of 24KB or else, it will run out of gas. This can be avoided by using The Diamond Pattern
MULTISIG CONTRACTS
Multisig (multiple-signature) contracts are smart contract accounts that require multiple valid signatures to execute a transaction. This is very useful for avoiding single points of failure for contracts holding substantial amounts of ether or other tokens. Multisigs also divide responsibility for contract execution and key management between multiple parties and prevent the loss of a single private key leading to irreversible loss of funds.