Community

How to Write a Smart Contract with Python — (Part 2)

Ontology Gas | 09.24| 217

How to Write a Smart Contract with Python — (Part 2)

Foreword

In the last article, we introduced the Blockchain & Block API of Ontology’s smart contract. Some of you might have already tried to write and run smart contracts on Ontology with Python. If you have any issues while using SmartX, feel free to contact us.

Today we will discuss how to use the second module: Storage API. The Storage API has five related APIs that enable addition, deletion, and changes to persistent storage in blockchain smart contracts. Here’s a brief description of the five APIs:

Let’s take a closer look at how to use these five APIs. First, create a new contract SmartX and then follow the steps below. Aa usual, at the end of the article, we will provide the GitHub link of the source code.

2 How to Use Storage API

2.1 GetContext & GetReadOnlyContext

GetContext & GetReadOnlyContext gets the context in which the current smart contract runs. The return value is the reverse of the current smart contract hash. As the name implies, GetReadOnlyContext gets the context of the read-only mode. In the example below, the return value is the reverse of the contract hash displayed in the upper right corner.

2.2 Put

The Put function is responsible for storing the data on the blockchain in the form of a dictionary. As shown, Put accepts three parameters. GetContext gets the context of the current smart contract running, the key is the key value that needs to store data, and value is the value of the data that needs to be stored. Please note that if the key value is already in storage, the function will update its corresponding value.

2.3 Get

The Get function is responsible for reading the data in the existing blockchain through the key value. In the example below, you can fill in the key value in the parameter panel on the right to run the function and read the data corresponding to the key value in the blockchain:

2.4 Delete

The delete function is responsible for deleting the data in the blockchain through the key value. In the example below, you can fill in the key value to run the function in the parameter panel on the right and delete the data corresponding to the key value in the blockchain:

3 Storage API Sample Code

The following code gives a detailed example of the use of five APIs: GetContext; Get; Put; Delete; and GetReadOnlyContext. You can try to run these APIs on SmartX.

from ontology.interop.System.Storage import GetContext, Get, Put, Delete, GetReadOnlyContext
from ontology.interop.System.Runtime import Notify

def Main(operation,args):
if operation == 'get_sc':
return get_sc()
if operation == 'get_read_only_sc':
return get_read_only_sc()
if operation == 'get_data':
key=args[0]
return get_data(key)
if operation == 'save_data':
key=args[0]
value=args[1]
return save_data(key, value)
if operation == 'delete_data':
key=args[0]
return delete_data(key)
return False

def get_sc():
return GetContext()

def get_read_only_sc():
return GetReadOnlyContext()

def get_data(key):
sc=GetContext()
data=Get(sc,key)
return data

def save_data(key, value):
sc=GetContext()
Put(sc,key,value)

def delete_data(key):
sc=GetContext()
Delete(sc,key)

Afterword

Blockchain storage is the core of the entire blockchain system. The use of the Ontology Storage API is very simple and developer-friendly.

On the other hand, storage is the focus of hackers, such as the security threat we mentioned in one previous article: storage injection attack, developers must pay special attention to code security when writing storage-related code.

Find the detailed tutorial on GitHub here.

In the next article we will discuss how to use the Runtime API. Stay tuned!

Are you a developer? Make sure you have joined our tech community on Discord. Also, take a look at the Developer Center on our website, there you can find developer tools, documentation, and more.

Find Ontology elsewhere

Ontology website / Ontology GitHub / ONTO website / OWallet (GitHub)

Telegram (English)Discord

Twitter / Reddit / FacebookLinkedIn


How to Write a Smart Contract with Python — (Part 2) was originally published in OntologyNetwork on Medium, where people are continuing the conversation by highlighting and responding to this story.

Comment 0

delete

Are you sure you want to delete this post?