Welcome! We’re glad you took an interest in developing smart contracts with Ontology. This guide is meant to provide the best possible introductory experience to the Ontology chain and SmartX and its smart contract development environment.
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!
Here are just a few to get started:
- Introduction to SmartX, smart contract development, deployment, and invocation
- Ontology Python smart contract API
- Ontology Python smart contract API tutorials
- Google Docs
- Ontology Python smart contract demos
- Entire Ontology GitHub repository
- Ontology Developer Center
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
For the easiest initial setup experience, most of your smart contracts will be done in SmartX. SmartX is a one-stop, in-web browser IDE that is used to compile, deploy, and invoke smart contracts.
You can access SmartX at
https://smartx.ont.io and the login page should look like this:
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
Wait! Before we get into the details of smart contract development, you need to install 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:
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.
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
Once you’re logged in through your GitHub account, create your first project and select your programming language of choice (probably Python, if you’re reading this tutorial).
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
Import statements while developing in SmartX will be limited to Ontology specific libraries, which we will go over later.
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
For those used to object-oriented programming (OOP), another big adaptation awaits you in your smart contract development journey — SmartX only allows you to work with primitive types, lists, and maps.
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
SmartX doesn’t handle floating-point numbers and makes it difficult to work with negative numbers.
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)
Now, with all of the above in mind, you can finally begin developing your first smart contract.
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:
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
One of the biggest ways in which your smart contract differs from any other code you’ve written before is that you have to “deploy” it before you can test your functions.
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:
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:
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
This is the exciting part — you finally get to harvest the fruits of your labor and run the functions that you’ve coded in your smart contract.
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.
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
Bugs are an inevitable part of any programming experience and the same applies to smart contracts.
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:
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
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.
Sometimes, your function will run successfully, with its transaction hash being logged in the console as proof, but it won’t do what you expect it to do.
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.
Congrats! You’ve made it all the way to the end, and hopefully learned a thing or two about developing smart contracts in the process.
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!