An introduction to smart contracts
A smart contract is a piece of code stored on the blockchain. It contains a set of instructions and rules to trigger them. Once deployed, it becomes immutable, but a user can trigger the execution of the code without modifying it.
Smart contract metaphor
Smart contracts can achieve different kinds of operations with coins and other smart contracts. They're comparable to snack vending machines:
- Each machine has a contract saying "Give me cryptocurrency, then I give you a food item or drink."
- Each machine can have a different smart contract for various food or drink items.
- Other smart contracts can work with the machines behind the scenes, such as a contract that represents an employee gathering the money from the machines.
Each machine doesn't operate until enough currency is delivered (Gas). Note that the quantities of foods or drinks change while their types can't (ever).
Of course, smart contracts go beyond this metaphor. Thanks to transparency and immutability, they allow an agreement to be secured between two or more parties.
For example, it is common to create financial instruments like various tokens (usually worth a fraction of the blockchain's coin) with different usability and characteristics inside a multiple smart contracts system. Other more or less complex projects can propose lending, stablecoins, or crowdfunding.
In most cases, smart contracts remove intermediates and drastically reduce costs compared to classic paper contracts and their validations.
Notice that a smart contract can only run and interact with the blockchain it's stored on. It can't interact with the outside world. That's where decentralized applications or "dApps" come in ,because they provide interfaces for the outside world.