<h1 align="center">How to Use Storage API in Smart Contract</h1>
<p align="center" class="version">Version 0.1</p>
1. Introduction
Storage API supports basic storage operation such as get, put, and delete data.
Storage API:
- The GetContext is used for calling storage of a smart contract.
- The Get is used for getting data from storage
- The Put is used for saving data to storage
- The Delete is used for deleting data from storage
For more details, you can view the API-doc and the source code here.
2. How to use Storage API
from ontology.interop.System.Storage import GetContext, Get, Put, Delete
from ontology.interop.System.Runtime import Notify
def Main(operation,args):
if operation == 'get_sc':
return get_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_data(key):
sc=GetContext()
data=Get(sc,key)
Notify(data)
def save_data(key, value):
sc=GetContext()
Put(sc,key,value)
def delete_data(key):
sc=GetContext()
Delete(sc,key)
Note1: Put, delete, and get operation should be implemented in the same contract. Otherwise, the function you run would fail to execution.
Note2: The data you save in the blockchain can be accessed by anyone.
网友评论