Hitchhiker’s Guide to Developing Ontology Smart Contracts in SmartX (Python)

An introduction to the Ontology chain and SmartX, and its smart contract development environment

Tim Feng
Tim Feng
Aug 2, 2019 · 10 min read
Image for post
Image for post

Introduction

It will be especially useful if:

  • You have a firm grasp of, and a lot of experience in, programming with typical data structures and algorithms.
  • You are fluent in Python or are comfortable enough to use Python while filling in the gaps with Stack Overflow.
  • You are aware of a few of the basic high-level applications of blockchain (public data storage, transaction handling).

If you want to skip the hand-holding and just want to figure things out yourself, feel free!

Open-Source Resources

Here are just a few to get started:

For those who have decided to stay, the resources provided above will still be of use if any confusion arises as you follow along with this guide.

With that, let’s move forward with the content of the tutorial.

1. Getting Started With SmartX

You can access SmartX at https://smartx.ont.io and the login page should look like this:

Image for post
Image for post

If you don’t have an Ontology wallet file or you don’t know what that is, don’t worry. You can click the symbol underneath “Third-party account login” to log in using your GitHub account. If you don’t have a GitHub account, create one.

2. Cyano Wallet

Cyano Wallet is a web extension wallet which holds Ontology’s two tokens (ONT and ONG), as well as any tokens that you decide to create following Ontology’s OEP4 token protocol.

You can install Cyano Wallet for Chrome here:

Once installed, Cyano Wallet can be accessed from where all your other Chrome extensions are.

It will ask you to create an account and memorize a mnemonic phrase and a private key — make sure to keep these recorded in a safe place.

Once you’re past this phase, Cyano Wallet should look like this:

Image for post
Image for post

While many of your smart contracts will depend on Cyano Wallet for handling token transfers to and from wallet addresses, Cyano Wallet is most important for holding ONG, which is the Ontology token that serves as “Gas” for smart contract deployment and invocation.

For those unfamiliar with Gas, it’s basically a transaction fee, paid out when a developer deploys a smart contract to the Ontology blockchain, or when a user invokes a function of a deployed smart contract.

For Ethereum, the fee is a small fraction of ETH; for Ontology, the fee is ONG.

So at this point, you may be wondering: “How am I supposed to get ONG? Do I even want to make the investment of purchasing ONG if I’m just trying out developing smart contracts on Ontology in the first place?”

These are all valid concerns that are also immediately addressable.

First, in your Cyano Wallet settings, you can select “TestNet” in the first dropdown menu and choose any node address in the second.

Then, on https://developer.ont.io/, you can find the application for TestNet ONT and ONG by scrolling to the bottom of the home page.

3. Developing Your Smart Contract

Now, with your first smart contract project opened and ready for development, it’s time to jump right in!

Or so you’d think. However, there are a few crucial precautions you need to know before you actually begin developing your smart contract.

Because the smart contract is programmed in Python, it is very natural to assume that all the typical rules of Python hold true here. However, that isn’t the case, and here’s what you need to know:

No external modules

As for all other modules that make your Python experience efficient — you’re going to have to find a way to implement them yourself.

No abstract data types

In effect, the functions you program will have to store the data you would typically handle in a constructor.

This storage would happen on the Ontology blockchain, using imported functions from certain Ontology libraries.

Limited number handling

Fortunately, there are workarounds for these issues. Floating-point numbers can be represented by working with numbers multiplied by a power of 10, depending on how many decimal places you want.

For example, if you want to represent floating-point numbers up to the hundredths place, multiply your number of interest by 100.

As for negative numbers, setting your “zero” as a large positive number and adapting your code to interpret positive and negative numbers according to this “zero” is an immediate fix.

4. Developing Your Smart Contract (for Real This Time)

As mentioned before, there are a few Ontology-specific libraries that you are allowed to import into SmartX. It is very important that you master these libraries and understand how they interact with the Ontology blockchain and accomplish your goals as a smart contract developer.

The relevant resources that introduce you to the documentation, as well as example smart contracts utilizing these libraries, are listed at the top of this tutorial.

Moving forward, it is important to note that your smart contract must follow a very specific format.

For your contract to compile and interact with SmartX correctly, you must include your import statements at the top of the project (just like any normal Python script).

This is the main method at the top which calls all the functions you define later in the program and those very functions have to follow the main method at the beginning.

For example, if you were to implement a classic “Hello World” project in this format, it would look like this:

Image for post
Image for post

Notice how the main method includes an if statement to check if the operation called matches the name of a defined function later in the contract and sets the argument(s) as input for the function.

The main method needs to include this kind of formatting for every function you define in the smart contract so SmartX can invoke your functions after deployment.

5. Deploying Your Smart Contract

Deploying your smart contract is effectively sending it to the blockchain, so that your functions that interact with the blockchain, in storage or transactional aspects, for example, can work.

Once you’ve navigated to the “Deploy” tab of SmartX and filled out the required fields that describe information about the smart contract, you select the option to deploy your contract.

If installed correctly, Cyano Wallet should pop up like this:

Image for post
Image for post

Note that “Gas price” and “Gas limit” are both fields that you can edit — usually, the only field you need to change is “Gas limit”, and that’s only if your deployment fails and the console logs a notification that the Gas limit is lower than it needs to be:

Image for post
Image for post

Usually, that just means you need to deploy your contract again and set the Gas limit to that number.

Even though you don’t need to change the “Gas price” field, you still need to pay attention to its value, making sure that your Cyano Wallet account has enough ONG to cover the cost.

It’s important to recognize that longer smart contracts have higher Gas prices and Gas limits than shorter ones.

6. Invoking Your Smart Contract’s Functions

After you navigate over to the “Run” tab of SmartX, you can select the function you would like to run from a dropdown menu. Your functions will only show up in the dropdown menu if you include them in the main method, correctly formatted as described previously.

With this in mind, you can intentionally leave functions out of the main method if they only serve as “helper” methods that don’t need to be invoked outside of the smart contract.

Image for post
Image for post

However, if you wish to check the values that your function is working with, you are still allowed to use a return statement to view that value by selecting the “Pre-Run” option, which is to the left of the “Run” option.

This will run your function without interacting with the blockchain and log the value that your function’s return statement is meant to display.

This can be an effective means of ensuring the functionality of your smart contract function or for discovering bugs that need to be fixed.

7. Debugging Your Smart Contract

The unfamiliar development environment may be a little intimidating at first and some of the error messages may seem a little cryptic, but we can make it through this together:

Notify, Notify, Notify

This is less of an after-the-fact fix and more of an in-the-process solution.

SmartX supports a function called Notify that prints publicly to the blockchain and is immediately visible in your console as shown below.

A good practice, while developing smart contracts, is to include a Notify with a distinguishable method after every important block of code.

This is a common debugging technique, even on off-chain programs — everybody has used print statements at some point to pinpoint where exactly their code has gone wrong.

Here’s an example of what Notify returns to the console after using the “Run” option:

Image for post
Image for post

When the content of your Notify statement is returned to the console, it will be in the form of a hexadecimal string, so make sure to navigate to the “Tool” tab of SmartX to find an appropriate string-conversion tool to decode the statement.

Another thing to note is that, in SmartX, you might need to use the “Debug Run” to view the Notify statements.

Often, when you use “Run” on code with bugs, it will get canceled once a bug is detected, rather than logging the Notify right before the issue arises.

This can be frustrating because it doesn’t give much information as to where your code might have an issue, but the “Debug Run” option is a useful substitute if it happens.

Get adventurous

You’ve tried everything and are on the verge of giving up on your dreams of becoming a premier smart contract developer.

Don’t give up!

You can copy and paste the function call’s transaction hash into the Ontology Blockchain Explorer, and, after selecting MainNet or TestNet (depending on which one you deployed your smart contract to), you can see details of your smart contract event.

All function calls and smart contract deployments will produce a similar transaction hash, and they can all be traced in the Explorer for your benefit.

6. Conclusion

You have a lot of flexibility in how you choose to implement your smart contracts in the future and the Ontology public chain is a great place to start in that endeavor.

For those interested in building DApps, Ontology provides a lot of open-source resources that can help you get started in turning your smart contracts into consumer-facing products:

With that, best of luck!

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tim Feng

Written by

Tim Feng

Better Programming

Advice for programmers.

Tim Feng

Written by

Tim Feng

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store