Smart Contracts: What, How and Why

Swetlana AI
6 min readFeb 4, 2023

--

Smart contracts are self-executing digital agreements that use blockchain technology to enforce the terms and conditions of a contract without the need for intermediaries. This article will explore the basics of smart contracts, their benefits and limitations, and provide examples of how they are being used in the real world.

Image by DALL-E & me (here is my Instagram)

Smart Contracts allow for secure, transparent, and automated transactions between parties, making them a valuable tool in various industries such as finance, real estate, and supply chain management. With their ability to automate complex processes and reduce the risk of human error, smart contracts are revolutionizing the way we do business.

Here are a few examples of how smart contracts are being used in the real world:

  1. Supply Chain Management: Smart contracts can be used to automatically track and verify the movement of goods and products through the supply chain, reducing the risk of fraud and ensuring transparency.
  2. Real Estate: Smart contracts can be used to automate the process of buying and selling property, reducing the need for intermediaries and speeding up the process.
  3. Decentralized Finance (DeFi): Smart contracts are widely used in the DeFi ecosystem, allowing for decentralized and trustless exchange of digital assets and financial products.
  4. Identity Management: Smart contracts can be used to securely store and manage personal identity information, allowing for greater privacy and security.
  5. Voting Systems: Smart contracts can be used to securely and transparently manage voting systems, reducing the risk of tampering and improving the accuracy of results.

These are just a few examples of how smart contracts are being used in the real world. The versatility and security offered by smart contracts make them a valuable tool in many industries, and their use is expected to grow as more businesses recognize their potential.

What does such a smart contract look like?

Let’s look at example code for a smart contract written in Solidity. This one is a draft for an artist selling their artwork on the blockchain:

pragma solidity ^0.8.0;

contract Artist {
// Artist information
string name;
uint age;
string country;

// Artwork information
string artworkName;
uint price;
string description;
uint dateCreated;

// Events
event NewArtwork(string artworkName, uint price);
event ArtworkSold(string artworkName);

// Constructor
constructor(string memory _name, uint _age, string memory _country) public {
name = _name;
age = _age;
country = _country;
}

// Functions
function createArtwork(string memory _artworkName, uint _price, string memory _description, uint _dateCreated) public {
artworkName = _artworkName;
price = _price;
description = _description;
dateCreated = _dateCreated;
emit NewArtwork(artworkName, price);
}

function sellArtwork() public {
emit ArtworkSold(artworkName);
}
}

A smart contract code typically consists of several key components:

  1. State Variables: State variables are data stored on the blockchain that represent the current state of the contract. They can be of different types, such as integers, strings, arrays, or custom data structures.
  2. Functions: Functions are the executable code in the contract that perform specific tasks. Functions can be called by sending transactions to the contract address.
  3. Events: Events are a way for the contract to emit notifications about changes to its state or the execution of specific functions. They can be listened to by client software and used to trigger other actions.
  4. Modifiers: Modifiers are reusable pieces of code that can be applied to functions to add additional behavior. For example, a modifier might be used to check that a function can only be executed by the contract owner.
  5. Contracts: Contracts are a way to create reusable and modular code in smart contracts. Contracts can be used to create libraries or base contracts that can be inherited by other contracts.
  6. Structs: Structs are custom data structures that can be used to store complex data within the contract.

These are the main components of a smart contract code. The specific components and the structure of the code will vary depending on the programming language and the specific use case of the contract.

However, these components are typically included in most smart contracts to provide a complete and functional solution.

What happens after you’ve written your smart contract?

After the smart contract is written, the next step is to deploy it to a blockchain network, such as the Ethereum network. This involves several steps:

  1. Compiling the contract: The contract code is compiled into machine-readable bytecode that can be executed on the blockchain.
  2. Deployment: The compiled contract code is then deployed to the blockchain network. This involves sending a transaction that includes the bytecode and any necessary data, such as initial values for contract state variables.
  3. Contract address: After the contract is deployed, it will have a unique contract address on the blockchain network. This address can be used to interact with the contract and execute its functions.
  4. Interacting with the contract: Once the contract is deployed and has a contract address, it can be interacted with by sending transactions to the contract address. For example, the createArtwork or sellArtwork functions can be executed by sending transactions to the contract address.

Note: Deploying a smart contract requires a fee to be paid in the network’s native cryptocurrency, such as Ether in the Ethereum network. This fee is used to compensate network participants for the computational resources they use to validate and execute the contract.

Digging deeper into the compiling part

Step 1 in deploying a smart contract is compiling the contract. This involves transforming the human-readable code written in a high-level programming language (such as Solidity in the Ethereum network) into machine-readable bytecode that can be executed on the blockchain.

Compiling the contract involves several steps:

  1. Syntax checking: The compiler will check the code for syntax errors and ensure that it follows the correct syntax for the programming language used.
  2. Optimization: The compiler will optimize the bytecode generated, reducing its size and improving its performance.
  3. Bytecode generation: The compiler will generate the bytecode that represents the contract in machine-readable format.
  4. ABI generation: The compiler will also generate the Application Binary Interface (ABI) for the contract. The ABI is a specification that describes how the contract can be called and interacted with. It is used by client software to encode and decode the data that is sent to and from the contract.

Once the contract is compiled, the generated bytecode and ABI can be used to deploy the contract to the blockchain network. The bytecode is included in the deployment transaction, and the ABI can be used by client software to interact with the contract once it is deployed.

Deploying a smart contract on a blockchain typically involves the following steps:

  1. Compilation (as mentioned above): Compile the contract code into machine-readable bytecode and generate the Application Binary Interface (ABI) that describes how the contract can be interacted with.
  2. Account setup: Create or obtain an account on the blockchain network that will be used to deploy the contract. Ensure that the account has sufficient funds to cover the cost of deployment.
  3. Deployment transaction: Create a deployment transaction that includes the contract bytecode and any constructor arguments. The deployment transaction must be signed by the account that will be used to deploy the contract.
  4. Broadcasting: Broadcast the deployment transaction to the blockchain network. This will cause the contract to be executed and deployed to the blockchain.
  5. Contract deployment confirmation: Wait for the deployment transaction to be confirmed by the network. Once confirmed, the contract will be deployed and its address will be available for use.
  6. Interaction: Interact with the contract using its address and the ABI generated during the compilation step. This can be done using client software or web3 libraries.

These steps can vary depending on the specific blockchain network and the tools used to deploy the contract. However, these are the general steps involved in deploying a smart contract to a blockchain. Once deployed, the contract will be immutable and can be used to automate transactions and processes on the blockchain.

Here’s an example of a bytecode for a simple smart contract in Solidity (a contract-oriented programming language for the Ethereum blockchain):

6060604052341561000f57600080fd5b60405161023738038061023783398101604052808051820191905050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610081929190610088565b505061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b61020f8061013c6000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b21461005c578063d5dcf1271461008c575b600080fd5b341561006757600080fd5b61008a6004808035906020019091905050610113565b005b341561009757600080fd5b61009f610129565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100df5780820151818401526020810190506100c4565b50505050905090810190601f16801561010c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061013192919061013f565b5050565b61013b61014f565b151561014757600080fd5b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101d75780601f106101ac576101008083540402835291602001916101d7565b820191906000526020600020905b8154815290600101906020018083116101ba57829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a72305820c5c1272a54f7d4024fcb0ff30bbcf93b922c9a092e3b3c3b3f17d

Let’s say I want to send an artwork to my smart contract. This involves several steps:

  1. Converting the artwork into a digital format that can be stored on the blockchain, such as a PNG or JPEG file.
  2. Hashing the digital representation of the artwork using a cryptographic hash function, such as SHA-256.
  3. Storing the hash of the artwork in the smart contract, along with any other relevant information such as the name of the artwork, the artist’s name, and the date of creation.
  4. Triggering the smart contract by sending a transaction that calls the appropriate function, such as an “addArtwork” function, and passing the hash of the artwork as an argument.
  5. The smart contract stores the information in its permanent database, which is maintained across the entire blockchain network.

Note: The actual artwork file itself is not stored in the blockchain, only the hash of the file is stored. This reduces the size of the data stored on the blockchain and improves efficiency.

[This article has been written with the help of ChatGPT]

--

--

No responses yet