Making blockchain real: Ephemeral smart contracts

In this third episode of the Making blockchain real Series, we will take a look at Disposable Smart Contracts and apply their utilities in a blockchain environment.
In case you don’t know us, Making blockchain real presents a Series of episodes about blockchain projects and reviews their programmability by delivering a showcase integration using said project(s).
Smart contracts represent one of the cornerstones of blockchain innovation. Find out in this article, how you can make use of smart contracts in a disposable environment.

Table of contents

Network Environment

We will be using a set of building blocks of the Symbol from NEM technology which include namespaces, mosaics (assets), transactions, blocks, accounts, metadata and restrictions. As a matter of fact, writing your first Smart Contract with NEM is easy and simple as will demonstrate this short case study.
In the aim of facilitating the integration and execute our smart contracts on the Public Testnet for Symbol, we will be using a TypeScript SDK. Adding to it, you can use the Explorer client application to verify the data being published / created with our smart contracts.
Impatient? Have a look at the source code of the software at Github.

You will need symbol.xym mosaics to pay fees
Should you be using a new account which does not own any symbol.xym, feel free to use the online faucet application which you can find here. This software will send a small amount of symbol.xym mosaics to your account which should cover more than the required fees – fees on the Public Testnet for Symbol are of, approximately 0.03 symbol.xym for a transfer transaction.

Into some code

In the optic of minimizing the required time-to-delivery of this case study application, we will be using clime to create a command line tool as the outcome. This tool will let us focus on our disposable smart contracts scope.
The first step of this case study will be to install the symbol-smart-contracts software with your terminal as well as compile the software package so that it can be used on your local machine. This step is simple enough and you can do it using your terminal:

Preparing your Environment

git clone https://github.com/usingblockchain/symbol-smart-contracts
cd symbol-smart-contracts && npm install && npm run build

We can now use the symbol-smart-contracts package to create disposable smart contracts. As you will see, in this package are a few Short-lived smart contracts that can be executed out-of-the-box. We will go through each of them during this article.

Smart Contract #1: Creation of Named Assets

This first disposable smart contract will permit us to create a mosaic (asset), then register a namespace and publish an alias for the created asset. This smart contract can be used to create named assets with Symbol from NEM.
This smart contracts consists of executing the following steps – atomically:

  • Create and configure an asset using Mosaics.
  • Register a namespace that expires after one year.
  • Link the created namespace to the created asset.

These steps will be executed in one aggregate transaction that will be broadcast to the Public Testnet for Symbol.

Smart Contract for the Creation of Named Assets
Particularity: The particularity about this smart contract is that after its execution, it is possible to use your newly created asset by referring to it by its name.

Execution of the Smart Contract

This smart contract can be executed multiple times to create multiple assets. Please, make sure to backup the account private key that is used to generate the owner account.

./symbol-smart-contracts AssetCreation --name organization.asset --supply 1500000

Smart Contract Inputs

This disposable smart contract accepts following inputs:

ParameterDescriptionExample
–nameSets a friendly name–name evias.asset
–divisibilitySets the divisibility (number of decimal places)–divisibility 2
–supplySets the initial supply (number of units)–supply 10
–flagsSet mosaic flags–flags SupplyMutable

Smart Contract #2: Escrow of assets

This second disposable smart contract will permit us to perform a decentralized swap of assets. This smart contract can be used to perform a decentralized swap of assets between two accounts and using two different mosaics.
This smart contracts consists of executing the following steps:

  • Create the transfer of mosaic M1 from Alice to Bob.
  • Create the transfer of mosaic M2 from Bob to Alice.

These steps will be executed in one aggregate transaction that will be broadcast to the Public Testnet for Symbol.

Smart Contract for the Escrow of Assets
Particularity: The particularity about this smart contract is that it locks funds for both accounts until both parties have confirmed the intention of swap.

Execution of the Smart Contract

This smart contract can be executed multiple times to escrow funds with different parties. Escrow contracts expire after 48 hours if one of the two involved parties does not confirm the transaction.

./symbol-smart-contracts AssetEscrow --asset1 10 symbol.xym --asset2 1 theater.ticket --taker 4574367DCF64AE285AEA5BA69B9EE7F507DB43FCA3CBB45C201DAC9639D4F3B4

Smart Contract Inputs

This disposable smart contract accepts following inputs:

ParameterDescriptionExample
–asset1Sets the left-hand asset–asset1 10 symbol.xym
–asset2Sets the right-hand asset–asset1 1 theater.ticket
–takerSets the right-hand (taker) account public key–taker 4574…F3B4
–lock(Optional) Sets the mosaic that is used for spam protection–lock 10 symbol.xym

Smart Contract #3: Request mosaic from a friend

This third disposable smart contract will permit us to request mosaic from a friend. This smart contract can be used to request assets from other accounts.
This smart contracts consists of executing the following steps:

  • Create a transfer transaction from your friend to your address.
  • Await your friend’s confirmation for the request.
  • These steps will be executed in one aggregate transaction that will be broadcast to the Public Testnet for Symbol.

Smart Contract for the Request of mosaics from a friend
Particularity: The particularity about this smart contract is that it lets you request any mosaic from any account. In fact, this permits for your to create a transaction which your friend has to confirm. In this way, it is possible to request the fees needed for a transaction, as a first step of any business layer logic that requires fees to be paid.

Execution of the Smart Contract

This smart contract can be executed multiple times to send multiple requests. The issuer account must hold network currency as it needs to send a hash lock transaction (SPAM protection) first.

./symbol-smart-contracts AssetRequest --asset 10 symbol.xym --from TDNNBX7B44CJZFQGU7XAG62SQSJQOWY4P5KACYPD

Smart Contract Inputs

This disposable smart contract accepts following inputs:

ParameterDescriptionExample
–assetThe asset to request (amount and mosaic)–asset 10 symbol.xym
–fromSets the recipient of the request–from TDNNBX7B44CJZFQGU7XAG62SQSJQOWY4P5KACYPD
–lock(Optional) Sets the mosaic that is used for spam protection–lock 10 symbol.xym

Smart Contract #4: Sending Partial Transaction Signatures

This last disposable smart contract will permit us to send co-signature transactions for partial transactions. This smart contract can be used to complete partial transactions when they are still missing co-signatures.
This smart contract consists of executing the following steps:

  • Read unsigned (partial) transactions
  • Issue co-signature transaction for a parent aggregate bonded transaction

These steps will be executed in one transaction that will be broadcast to the Public Testnet for Symbol.

Smart Contract for Co-signature of Partial Transactions
Particularity: The particularity about this smart contract is that it lets you send co-signatures for multiple parties involved in a partial transaction (aggregate bonded). This smart contract can be used whenever a partial transaction is a missing a co-signature from one of your accounts.

Execution of the Smart Contract

This smart contract can be executed multiple times to send multiple co-signatures. When there is no transaction missing co-signatures, the contract will simply abort.

./symbol-smart-contracts PartialCosignature --hash B38C791028F168FFC5F2DBDD3B89237BF4ABC7FCABF72C4A0B45A17F7AFB07C1

Smart Contract Inputs

This disposable smart contract accepts following inputs:

ParameterDescriptionExample
–hashSets the aggregate bonded transaction hash–hash B38C7910…07C1

One-Time Transparent Execution

What all these smart contracts have in common is that they can be executed at any given time, over and over. Their execution is guaranteed to either succeed and be verifiable – or cancel all actions and leave 0 traces due to the failure of one or more of the steps executed by said smart contract.
This type of one-time execution of pre-configured short-lived smart contracts permits to execute a lot of different actions affecting the state on-chain. Actions, which are either going to succeed or fail altogether. These smart contracts are said to be atomic because the execution of the actions depends on the validity of all actions executed one after the other.

Assemble the Building Blocks!

With this ground stone laid on how to create disposable smart contracts with Symbol, it is now time that we get more useful contract ideas that can then be integrated to this software package. You can basically think about any suite of transactions to execute in one contract; as long as your idea can be executed using out-of-the-box features of Symbol.
As a few ideas that came across my mind during the edition of this article are the following disposable smart contracts:

  • Transfer the ownership of an account
  • Create multi-level account hierarchies
  • Create named accounts
  • Whitelist/Blacklist usage of an asset (restrictions)
  • Feel free to add to this list!

The main principle with the symbol-smart-contracts package is that a contract executes by wrapping all configured transactions inside an aggregate transactions. This means that any list of valid transactions can effectively be wrapped as a disposable smart contract.
Using Symbol building blocks such as namespaces, mosaics (assets), transactions, blocks, accounts, metadata and restrictions – it is possible to cover business logic that was previously not even executable within multi-signature transactions.
With aggregate transactions, in fact, Symbol adds flexibility, extendability and atomicity to an already well-garnished feature set. Don’t miss out on these disposable smart contracts – Send us a Disposable Smart Contract Proposal in the form of an issue at Github.

Source code referensource-code-referencece

Because, what would a tech article look like without a few reference links and recommended books, right?

Book a call with a UBC blockchain expert for assistance or guidance on integrating the technology in your business.

We hope that this article was insightful for you and are looking forward to any feedback and messages. Please share your thoughts in the comments section below!

Disclaimer

This website may contain information about financial firms, employees of such firms, and/or their products and services such as real estate, stocks, bonds, and other types of investments. While this website may intend - as the author deem necessary - to provide information on financial matters and investments, such information or references should not be construed or interpreted as investment advice or viewed as an endorsement.