Crypto 101 with Vin: Smart Contracts

Kakavarna
4 min readMar 3, 2022

What Is a Smart Contract?

A vending machine takes some money and your choice then it dispenses the snack or drink that you picked.

A smart contract, like a vending machine, collects some input and fees and it gives back some result to whoever interacts with it. It does not need an intermediary like a cashier to execute its logic.

In a literal sense, smart contracts are programs that can be deployed to and executed on a blockchain network given that a blockchain can support smart contracts. Anyways, this means the contracts inherit the properties of the blockchain. The most important being immutability. That means the contracts are tamper-proof. ANYONE can develop and deploy the contracts and ANYBODY can view the contents of the contract. That is the transparency aspect of blockchains in perspective of smart contracts.

Properties of Smart Contracts

  1. Autonomous — Smart contracts automate the process of executing upon an agreed set of conditions. There is no waiting for middlemen to verify the conditions before a settlement can occur. Smart contracts execute as soon as the predetermined condition is met
  2. Secure — Blockchains networks are inherently decentralized and immutable. This is due to not having a single point of failure (decentralization) and redundant verification done by the network of nodes (consensus)
  3. Equitable — Because the contract can complete and enforce execution of an agreement based only on code, It ensures fairness by removing any value lost to middlemen and vagueness.
  4. Composable — This just means smart contracts can call upon other smart contracts. As long as smart contracts are public, a contract can reference and interact with other contracts.

Constraints of Smart Contracts

Size is the most prevalent constraint. Storage resources are finite, so blockchain networks have to limit the size of blocks. The smart contracts can only be as big as the blockchain network has dictated or at maximum, the size of a block minus the size of some overhead data.

What can be done is splitting out certain bits of code, functions, that repeatedly get used into their own smart contracts and referencing them when they are needed. Functions are just fragments of code that perform a predefined task. This is why composability is an important property to circumvent size constraints.

The other constraint is that smart contracts exist on isolated networks. Blockchain networks have no way of knowing what’s going on outside the network. It’s exactly like having a LAN, local network of computers, but no internet connection to the outside. It’s not so useful unless you only want to interact with users on your LAN.

Fortunately, data oracles exist to interface blockchains to the outside world. Oracles can help in getting data onto and off of the blockchain. You can learn more about Oracles at this article here.

Breaking Down An Example

Below is an example of a smart contract. You can find the full source on the Ethereum Developer Site with comments written inside the contract. This explanation provides additional context and programming knowledge for non-technical readers.

The Example Contract

pragma solidity 0.8.7;contract VendingMachine {
address public owner;
mapping (address => uint) public cupcakeBalances;
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 100;
}
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}
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;
}
}

Explanation

The first line pragma solidity 0.8.7; specifies the version of the programming language used to write the contract. You can use different versions depending on what is needed or it may not even be necessary depending on the language.

The actual contents of the contract is enclosed in brackets and preceded by contract VendingMachine . The name can be anything, but for the sake of readers of the contract, developers should keep it to the point.

address public owner; sets the deploying address as the owner of the contract. In technical terms this is a variable object. The type of object is specified as ‘address’ and it is public, so it is a variable that contracts outside this one can reference. mapping (address => uint) public cupcakeBalances; is also a variable , but it assigns a number to the contract’s address which indicates the amount of available cupcakes.

constructor (){…} is a special type of function that executes as soon as the contract is deployed. In this case it is setting the owner and cupcakeBalance to some default values. The parentheses next to it are empty meaning it does not need any input to run properly.

Afterwards you will see two functions being defined

function refill(uint amount) public {...}

The refill function verifies that the owner is the one refilling and it also updates the cupcakeBalance by the number,uint amount, supplied inside the parentheses. A uint is just another object type that can be represented by a positive number hence the u- meaning unsigned.

function purchase(uint amount) public payable {...}

The purchase function just checks that you are paying at least the minimum amount of ETH needed to purchase the number of cupcakes you want and that it has enough cupcakes. If you did not supply enough ETH or there aren’t enough cupcakes, it will not send any cupcakes to your address.

As you may have figured out, if I deployed the contract and you wanted to purchase my cupcakes, you would only interact with the purchase function.

In conclusion,

The use cases for smart contracts are endless. It can be used to facilitate escrow, betting, insurance claims, etc. You name it, it can probably be done with smart contracts. Decentralized apps are also a thing meaning there are applications on the internet that run autonomously. It’s like we’re living in the future. If you’re interested in building the future, learning to program is a great start and I’m certain there will be a great demand for smart contract developers once the technology is embraced by institutions and corporations.

Thanks for reading and check in next time.

Kakavarna.eth — Automation Engineer(9–5), crypto investor, and gamer with opinions

--

--

Kakavarna

Kakavarna.eth — Automation Engineer(9–5), crypto investor, and gamer with opinions