Page 56 - MSDN Magazine, November 2019
P. 56
A smart contract holds the economic and transactional logic that includes the elements of an ordinary contract (for example, offer, acceptance, revocation), as well as the execution of the terms (such as payment, price variation, penalties) that comprise the contract. It enables the processing of transactional actions at pre- determined times and/or based in relation to the occurrence or non-occurrence of an external trigger, such as an exchange rate threshold, delivery of an asset or expiration of put/call.
The key capabilities enabled using a smart contract involve:
• The ability to authenticate parties and counterparties,
ownership of assets and claims of right.
• The ability to access and refer to information and data
both on the blockchain platform and outside of the smart
contract (and the blockchain) to trigger transactions.
• The ability to automate the execution of transactions and
(economic) protocols on the blockchain platform.
What Can Smart Contracts Do?
The potential benefits of smart contracts throughout an economic transaction include standardization (standardized schemas and pro- tocols reducing the cost of negotiations and agreements), security (transactions are encrypted and stored on a blockchain platform designed to be immutable), latency (reduced transaction times and the streamlining or elimination of redundant manual processes) and transaction certainty (programmed execution, which reduces counterparty risk and settlement risk) and more.
As we examine the institutional landscape today, the use cases for smart contracts cover a broad range of applications:
• Self-Sovereign Identity: Enable individuals to own and control their digital identity, which contains reputation, data and digital assets.
• Securities: Simplify capitalization table management and the circumvention of intermediaries in the chain of securities custody.
• Trade Finance: Facilitate streamlined international transfers of goods through faster letters of credit and trade payment initiation, while enabling higher liquidity of financial assets.
• Data Governance and Compliance: Enable uniform financial data across organizations and improved financial reporting while reducing auditing and assurance costs.
• Supply Chains: Use IoT to track products as they move from the factory floor to the store shelf, enabling real-time visibility across the entire supply chain.
• Clinical Research and Trials: Increase cross-institutional visibility and data sharing while preserving privacy.
With a perspective on the potential of smart contracts, let’s explore the structure and development of our first smart contract.
The Hello World Smart Contract
Following hallowed computer science traditions, let’s consider our first Hello World contract. Figure 1 shows the code.
Every smart contract inherits the SmartContract base class, which is in the Neo Framework and provides core methods. The Neo namespace surfaces the APIs made available by the NEO block- chain platform, providing a way for the smart contract to access
blockchain data (all the data on the entire blockchain, including complete blocks and transactions, as well as each of their fields) and to manipulate the persistent store (every smart contract deployed has a storage space that can only be accessed by the contract itself ).
Inside the contract class, properties defined with static readonly or const are contract properties, which can be used as constants. For instance, to define the owner of a contract or the factor number that will be used in subsequent asset transfers, we define them like this:
// Represents the owner of this contract, which is a fixed address public static readonly byte[] Owner =
"ATrzHaicmhRj15C3Vv6e6gLfLqhSD2PtTr".ToScriptHash();
// A constant factor number
private const ulong factor = 100000000;
In addition, you can define static methods in the contract class and return a constant.
When you develop a smart contract, the blockchain platform provides a means to store your application data on the blockchain. All data in a smart contract’s storage is automatically persisted across invocations of the smart contract. Full nodes in the block- chain store the state of every smart contract on the chain.
The NEO blockchain platform provides data access interfaces based on key-value pairs. Our first smart contract uses the Storage class to read and write to persistent storage. For instance, to store the total supply of your token, you use this code:
// Key is totalSupply and value is 100000000 Storage.Put(Storage.CurrentContext, "totalSupply", 100000000);
Here, CurrentContext returns the current storage context. After obtaining the storage context, the object can be passed as an argument to other contracts (as a way of authorization), allowing them to perform read and write operations on the persistent store of the current contract.
With that, let’s explore a simple real-world scenario.
A Real-World Smart Contract
Consider a simplified DNS scenario where we want to register, query and delete a domain name associated with a given user, as shown in Figure 2.
In theory, smart contracts can have any entry point, but usually the Main function serves as the entry point for ease of invocation. Triggers A smart contract trigger is a mechanism that activates the execution of a smart contract. The most commonly used trig- gers are verification triggers and application triggers. Typically,
you handle the triggers in the Main function.
Figure 1 The Hello World Smart Contract
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo; using System;
namespace HelloWorld {
public class HelloWorld : SmartContract {
private const string test_str = "Hello World";
public static String Main(string operation, object[] args) {
Storage.Put("Hello", "World");
return test_str; }
} }
44 msdn magazine
Blockchain