ProHoster > Blog > Administration > How to write a smart contract in Python on the Ontology network. Part 2: Storage API
How to write a smart contract in Python on the Ontology network. Part 2: Storage API
This is the second part of a series of tutorials on creating smart contracts in Python on the Ontology blockchain network. In the previous article, we met Blockchain & Block API smart contract Ontology.
Today we will discuss how to use the second moduleβ Storage API. The Storage API has five associated APIs that allow adding, deleting, and modifying persistent storage in smart contracts on the blockchain.
Below is a brief description of these five APIs:
Let's take a closer look at how to use these five APIs.
GetContext ΠΈ GetReadOnlyContext get the context in which the current smart contract is executed. The return value is the reciprocal of the current hash of the smart contract. As the name suggests, GetReadOnlyContext takes a read-only mode context. In the example below, the return value is the reverse of the contract hash displayed in the upper right corner.
put
Function put is responsible for storing data on the blockchain in the form of a dictionary. As shown, put takes three parameters. GetContext takes the context of the currently executing smart contract, key is the key value that is needed to save the data, and value is the value of the data that needs to be saved. Note that if the value of the key is already in the store, then the function will update its corresponding value.
Function Get is responsible for reading the data in the current blockchain through the key value. In the example below, you can fill in the key value in the options panel on the right to execute the function and read the data corresponding to the key value in the blockchain.
Delete
Function Delete is responsible for deleting data in the blockchain through the key value. In the example below, you can fill in the key value for the function in the options panel on the right and delete the data corresponding to the key value in the blockchain.
2. Storage API code example
The code below gives a detailed example of using five APIs: GetContext, Get, Put, Delete, and GetReadOnlyContext. You can try running the API data in 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 Ontology Storage API is easy to use and developer friendly.
On the other hand, storage is the focus of hacker attacks, such as the security threat we mentioned in one of the previous articlesβ storage injection attack, developers are required to pay special attention to security when writing code that is related to the repository. You can find the complete guide on our GitHub here.
In the next article, we will discuss how to use Runtime API.
The article was translated by the editors of Hashrate&Shares especially for OntologyRussia. ΠΊΠ»ΠΈΠΊ
Are you a developer? Join our tech community at Discord. Also, take a look at Developer Center Ontology for more tools, documentation, and more.