Hackernoon logoA Brief Guide to Advanced Smart Contracts by@falconite

A Brief Guide to Advanced Smart Contracts

Author profile picture

@falconiteRohit Chatterjee

The roaring popularity of blockchain technology owes a great deal to the brilliance of smart contracts. Conceptualized by computer scientist Nick Szabo in 1994, a smart contract can be considered to be a set of instructions (usually in the form of a computer program) which automatically execute when the terms of a ‘contract’ are fulfilled.
Fast forward to 2013, when Vitalik Buterin introduced smart contracts to the world of blockchain through ‘Ethereum’. An open-source,
blockchain-based, distributed computing platform which supports smart contracts, Ethereum quickly turned out to be a game-changer.
Smart contracts paved the way for a variety of trustless distributed applications (DApps) and encouraged a host of blockchain projects, each trying to bring about a paradigm shift in its own way.
(Vitalik Buterin - the man who introduced smart contracts to blockchain)
As blockchain technology continues to spread its wings, smart contracts continue to be the wind underneath. A host of articles are readily available on the Internet which talk about how smart contracts are revolutionizing the world today. In this article, we’ll take a slightly different route – we’ll look at different types of smart contracts in existence, and glance at projects which are implementing them.

Time-Lock Contract

Time-lock contracts are one of the simplest and most common forms of smart contracts. As the name suggests, digital assets deposited to these contracts are ‘locked’ for a pre-defined period of time, before the assets can be retrieved by the sender. Such contracts typically come with a
‘time-lock’ function, which sets a numerical timestamp for each deposit
address. Tokens can be withdrawn to a certain address only after the associated timestamp has expired; prior to that, the lock contract only accepts deposits.
Time-lock contracts are ubiquitous in DeFi applications.
Services such as Compound and Aave, which let users loan their digital assets for a certain amount of time (and earn interest), make use of lock contracts to automatically allow users to retrieve their funds on expiry of the loan period.   
A special form of time-lock Contract, known as Hashed Time-Lock Contract (HTLC), is used in payment channels to eliminate the risk of counterfeit. In case of a HTLC transaction, the recipient of funds has to acknowledge the payment by submitting a cryptographic proof within a specified timeframe, otherwise the funds are returned to the original sender.
Apart from the time-lock function, a HTLC also contains a hash-lock – a function that prevents the recipient from spending the funds until a certain cryptographic proof is publicly broadcasted.
Lightning Network, a scaling solution for Bitcoin transactions, is one of the most popular use cases of HTLCs.
Funds can be transferred between two users in a trustless manner user through interconnected payment channels (more on that later), even when they are not directly connected. HTLCs are also used in atomic swaps, where users can ‘swap’ cryptocurrency from one type to another, without the need of any centralized intermediaries.
(Lightning Network is using HTLCs to ensure faster and secure transaction settlements)

Portal Payment Channel Contract

Payment channels are an important aspect of off-chain scaling solutions in blockchain networks. Any blockchain transaction has to be verified, included in a block, and publicly broadcast to all the nodes.
Each transaction also involves a mining fee, which might end up being a significant portion of the actual amount being transferred. For cases which involve multiple small-value transactions, the entire process becomes extremely costly and time-consuming.
Payment channels use a smart contract to enable users to transact multiple times, without recording every single transaction on the blockchain.
Intermediate transactions are recorded off-chain, and on completion, when the payment channel is closed, the final state of the participants is published on the blockchain.
Irrespective of the number of transactions, only one block has to be mined when payment channels are used. As already mentioned in the context of Lightning Network, by creating a mesh of payment channels, a secure and distributed network connecting multiple users can be created.
However, simple time-lock or hash-lock features might be insufficient for payment channel smart contracts, and additional features might be necessary to make them robust. For example, the CLTV (check lock-time
verify) feature was introduced to such smart contracts to avoid malleability attacks. Other upgrades include time-locking transactions in a manner
such that the last state of the channel always has the lowest time-lock, and is the first one that can be broadcast on the blockchain.
V Systems, an open-source blockchain platform project, has recently implemented payment channel smart contracts in their version 2.0 upgrade.
These smart contracts consider 3 variables – the accumulated load that the sender loads into the channel, the accumulated payment that the sender has already paid, and the expiration timestamp beyond which the receiver can no longer accept funds. These three variables can only increase, which ensures the presence of a certain amount of funds in the channel for the receiver, and enables the recipient to withdraw the collected funds without waiting for the channel to terminate.
(Off-chain settlement techniques frequently use payment channel smart contracts)

Application Logic Contract

Like blockchain, IoT is another upcoming technology which has captured the collective imagination of the technological world. Inter-connecting billions of devices and enabling them to exchange information literally opens the doors to limitless opportunities. However, such practices also raise concerns about data security and privacy.
Traditional IoT systems have a centralized architecture, where information is sent from an IoT device to the cloud for data processing, and then sent back to the device. Such a centralized system has very limited scalability, and a single point of failure when it comes to network security. This becomes even more critical in those scenarios where devices are expected to have the capability to initiate payments on their own.
This is where blockchain enters the picture. Smart contracts in blockchain networks, known as Application Logic Contracts (ALCs), allow devices to function securely and autonomously, ensuring greater automation, scalability, and cheaper transactions.
ALCs contain application specific code, which can work in conjunction with other smart contracts and programs on the blockchain.
They aid in communicating with and validating communication between different IoT devices. Since there is no third-party required to oversee transactions, these smart contracts can effectively oversee micro-payments between different IoT nodes.
Iota is one of the premier blockchain projects working in this field. Technically, it wouldn’t be correct to term it ‘blockchain’, as their data
structure is termed Tangle, but let’s not get into these technicalities
In a recent announcement, Iota stated that they would be developing their ALCs off-chain, outside the core protocol of the tangle.
For on-chain smart contracts, every network node has to keep a copy of each contract’s program code and state, and execute the code when the smart contract is triggered. By transferring them off the tangle, Iota’s ALCs can be executed in a localized manner, greatly improving the network scalability.
(The growing importance of IoT and blockchain brings ALCs sharply into focus)

Non-Fungible Contract

A non-fungible token (NFT) is a cryptographic token that represents a unique digital asset. Fungibility is the property of an entity whereby the
individual units are interchangeable and indistinguishable from each other.
The most common example used to explain fungibility is fiat currencies, where a valid note of any denomination is virtually indistinguishable from any other such note for all intents and purposes.
On the other hand, each NFT is unique and cannot be replaced by another of its kind. In the digital world, NFTs are usually used to represent collectibles, art items, memorabilia, and the like.
NFTs are being increasingly used in blockchain-based games, to represent the ownership of unique items which are of value within the gaming ecosystem. Exchange of such items across different games is also possible through these smart contracts.
These NFTs are created using non-fungible smart contracts. What sets non-fungible contracts apart from other smart contracts is that each such
contract represents only one unique token.
When a non-fungible token contract is initialized, its constant is set to zero. Once the token has been issued and it begins to exist, its supply is permanently fixed to 1.  This guarantees the stability and security of every operation performed in the contract, and ensures that the token is provably unique.
Decentraland is one of the most prominent blockchain projects which make use of non-fungible smart contracts.
In Decentraland's virtual environment, players can explore, create scenes and artworks, and own and exchange in-game NFT items, in a decentralized virtual reality world. Other games like Gods Unchained and (the infamous) CryptoKitties also issue in-game characters and items through non-fungible smart contracts.
(Non-fungible contracts are one of the driving vectors in the crypto-collectible market place)
PS – Being from a non-blockchain background, searching for different
varieties of smart contracts on the Internet turned out to be a notoriously
difficult task. Thank god for Binance Academy and the announcement posts of blockchain projects upgrading their smart contracts! If you know of any that’s worth a read, please do leave a comment here. 


The Noonification banner

Subscribe to get your daily round-up of top tech stories!