Publishing Your First Smart Contract on IOST

IOST Foundation
IOST
Published in
5 min readMar 18, 2019

This article on publishing your first smart contract on IOST was selected from the IOST Tech Article Contest. We are welcoming all community developers to share with us their experiences, views on IOST and blockchain. Selected articles will be shared across all IOST channels and platforms. Authors will be rewarded with bounties once the article is selected!

Below is a guide on how to build a smart contract and publishing it via the command line on IOST. This guide is for developers interested in creating an IOST smart contract using JavaScript. Before we dive into coding, let’s look at into the basics of Decentralized Applications (DApps), along with evaluating what smart contracts are used for.

The Value of DApps

DApps provide value by economizing digital resources, creating a means to monetize digital assets that would otherwise be hard to exchange. This allows for competitive pressure which in turn affects the pricing of digital assets, increasing the range of values and providing for a wider market. Decentralized applications allow for token distribution by means of development, fund-raising, and mining. DApps provide an alternative way to conduct transactions and are being used to:

  • Allow for records like smart contracts;
  • Help prevent fraud by using tokens;
  • Enables the creation of distributed autonomous corporations (DACs).

If you are looking to build a DApp, the following criteria apply:

  • An application must be able to operate autonomously, open-source, the application has no entity controlling the majority of its tokens.
  • The application must be decentralized and take advantage of blockchain technology.
  • The application generates tokens (applying the standard cryptographic algorithm). This proves that the value nodes are contributing to the DApp.

Using Smart Contracts

A smart contract is a source of code which has the ability to regulate, conduct and implement an agreement. Ultimately, smart contracts are logical processes dependent on the blockchain with which they interact.

Smart Contracts: Opportunities

  • Certainty of code
  • Cost-effectiveness regarding continuous low-value transactions
  • Decentralization reduces the risk of permanent data loss and corruption
  • Transparency may assist in effective regulation of markets

Smart Contracts: Drawbacks

  • Can not apply deliberate ambiguity
  • Compared to centralized computing, decentralization can be more inefficient and also costs more
  • No centralized control might be of concern to regulators
  • Financial investors may have concerns regarding transparency

Creating an IOST Smart Contract using the Command Line

To begin this guide, we will setup iwallet, this is the command line tool for IOST blockchain. You can use this tool to connect IOST blockchain to transfer coins, create accounts, query balance or call contracts. Both iwallet and API use RPC API inside and they have similar features. If you are new to iwallet you can check out our installation guide Command Line Wallet Tool.

1. Importing the initial account for iwallet

You need to import an account first before you can use iwallet, try setting up a local admin account using the following command

iwallet account import admin 2yquS3ySrGWPEKywCPzX4RTJugqRh7kJSo5aehsLYPEWkUxBWA39oMrZ7ZxuM4fgyXYs2cPwh5n8aNNpH5x2VyK1

2. Writing your first contract

Let’s prepare your first contract by writing a JavaScript class, e.g HelloWorld.js

class HelloWorld {

init() {} // needs to provide an init function that will be called during deployment

hello(someone) {

return “hello, “+ someone

}

}

module.exports = HelloWorld;

The smart contract contains an interface that receives an input and then outputs hello, + enter. In order to allow this interface to be called outside the smart contract, you need to generate the ABI file using the command line (note: please make sure you have Node.js installed and have run npm install command inside iwallet/contract folder:

iwallet compile HelloWorld.js

This will generate a HelloWorld.js.abi file for you, or alternatively you can prepare it yourself manually:

{

“lang”: “javascript”,

“version”: “1.0.0”,

“abi”: [

{

“name”: “hello”,

“args”: [

“string”

]

}

]

}

3. Lets publish our contract via command line:

Publish to local test node

iwallet \

— expiration 10000 — gas_limit 1000000 — gas_ratio 1 \

— server localhost:30002 \

— account admin \

— amount_limit ‘*:unlimited’ \

publish HelloWorld.js HelloWorld.js.abi

Sample output

Test ABI call

iwallet \

— expiration 10000 — gas_limit 1000000 — gas_ratio 1 \

— server localhost:30002 \

— account admin \

— amount_limit ‘*:unlimited’ \

call “Contract96YFqvomoAnX6Zyj993fkv29D2HVfm8cjGhCEM1ymXGf” “hello” ‘[“developer”]’ # contract id needs to be changed to the id you received

Output

Transaction Receipt

After that, you can get TxReceipt at any time by the following command:

iwallet receipt GTUmtpWPdPMVvJdsVf8AiEPy9EzCBUwUCim9gqKjvFLc

Or alternatively via an HTTP request

curl -X GET \

http://localhost:30001/getTxReceiptByTxHash/GTUmtpWPdPMVvJdsVf8AiEPy9EzCBUwUCim9gqKjvFLc

Congratulations, your contract should now be live, you will be able to see the returned ‘Hello World’ in your console. This call will be permanently recorded by IOST and cannot be tampered with. Next, you can you can build a tradeable digital token that can be used as a currency, a representation of an asset, a virtual share, a proof of membership or anything at all.

PS: IOST’s would express its sincere thanks to @Corlyne O’Sullivan in IOST developer community who kindly submitted this article to IOST.

………………………………….

--

--