DEV Community

loading...
Cover image for Smart Contracts: Execution & Examples

Smart Contracts: Execution & Examples

betaworldpeace profile image βετα ・4 min read

What is a smart contract? At its simplest, it’s a contract between two parties that cuts out the middleman. Smart contracts digitally facilitate, verify, or enforce the negotiation of a contract, allowing you to exchange money, property, or anything of value in a transparent, conflict-free way. And since a blockchain exists in a decentralized system between all permitted parties, there’s no need to pay intermediaries. Of course, blockchains can present their own problems, but they are faster, cheaper, and more secure than traditional systems, which is why even banks and governments are turning to them.

Two Aspects of Smart Contracts

There are two notable aspects of smart contracts that make them so enticing to clients. One aspect is the non-functional property that blockchain provides, a consequence of block processing’s distributed nature that still maintains its trustworthiness and non-repudiability. While smart contracts’ public key crypto technology is built on many established ideas, theirs is still a novel approach.

The other aspect of smart contracts is a flexibility that allows even non-programmers to specify, analyze, simulate, and ultimately execute them. The non-functional properties of blockchains are especially beneficial in these instances. This really is a contract, a process where multiple involved parties can make a sequence of decisions. And if you trust a central entity, you can delegate the execution of the contract to that entity and use crypto to control who is allowed to do what as part of a contract. This second quality of smart contracts is very much in line with the computerification of other non-technical domains, be it computational law or computational governance.

Architecture for Smart Contract Development

Alt Text

Execution of a smart contract is made simple thanks to blockchain technology. You can deploy a contract to the blockchain and execute it there, benefiting from the guarantees provided by the blockchain. As with any binding contract, it is necessary that the infrastructure itself provides correctness guarantees, which is why various projects are under way to formally verify and enhance the solidity compiler so as to support advanced checking through integration. This is where the importance of the right contract development languages and tools come in handy.

Contract development should rely on a language stack. For contract-style programs, functional languages are useful because they can be relatively easy to verify and can also easily support in-memory transactions. On top of its functionality, language extensions can directly support additional contract needs: decisions, auctions, agreements or resource allocations. Each of those can additionally be broken down into a whole range of configuration options to determine the specific behavior.

In the above diagram, Executable Multi-Party Contract Language (EMPCL) contains language constructs typical in most Smart Contract building blocks. Once a language (or a language extension) is stable, it is easier to write the correct code rather than using frameworks or libraries. Language constructs –at an appropriate level of abstraction– means that most lower-level mistakes cannot be made in the first place. The contract is to some degree correct-by-construction. It also means that there is less code to write, so it’s ultimately easier to understand and review. One can even interactively play with the contracts and explore their behavior or else write test cases that can be executed immediately. There’s even a realistic possibility that non-programmer domain experts could read or write the code.

An Example of a Smart Contract

Below is an example of a smart contract based on trades:

daml 1.2
module Trade where

data TradeData = TradeData
with
buyer : Party
seller : Party
uuid : Text
currency : Text
amount : Int
volume : Int
deriving (Eq, Show)

type TradeCid = ContractId NewTrade

template NewTrade
with
facilitator : Party
observers : [Party]
trade: TradeData
where
ensure (trade.amount > 0 && trade.volume > 0)

signatory trade.buyer, facilitator

observer observers

controller facilitator can
  MakePayment : () do return ()
Enter fullscreen mode Exit fullscreen mode

template TradeProposal
with
newTrade: NewTrade
where
signatory newTrade.facilitator

controller newTrade.trade.buyer can
  AcceptTradeIssuance: TradeCid
    do create newTrade
Enter fullscreen mode Exit fullscreen mode

happyPath = scenario do

authority <- getParty "SunWater"
facilitator <- getParty "WaterLedger"
seller <- getParty "Alice"
buyer <- getParty "Bob"

let
uuid = "abc123"; currency = "AUD"; amount = 1000; volume = 50
observers = [seller]
trade = TradeData with ..
newTrade = NewTrade with ..

createProposal <- submit facilitator do create TradeProposal with newTrade
acceptProposal <- submit buyer do exercise createProposal AcceptTradeIssuance
facilitatorPaid <- submit facilitator do exercise acceptProposal MakePayment

return()

This particular example is formally defined, so no different interpretations are possible and it can be executed automatically.

Smart Contracts and Language

At the moment, the languages most commonly associated with blockchain are DAML, Golang, Corda, Solidity, and FLETA. As you can see, these languages are not beholden to blockchain or even much resemble a domain specific language specifically intended to represent contracts. These are often common general purpose languages. It is but one limitation of smart contracts.

The other is that while smart contracts are ideal when it comes to an exchange of virtual assets or to transfer data, they still aren’t quite capable of replacing real-life contracts when it comes to the physical exchange of goods. For all of its benefits in business, it still cannot operate changes on the real world or measure them directly. The highly specific language of legal documents and the highly specific technical language of developers don’t always overlap. But in an increasingly online world, smart contracts will continue to be the go-to for most readily transferable assets.

Transparent and conflict-free, smart contracts’ non-centralized attributes and ease of construction make them ideal for more and more transactions. Thanks to their flexibility and fluidity, the demand for smart contracts will continue to grow as more businesses, banks, and even governments pivot towards them.

Discussion (0)

pic
Editor guide