美文网首页
SC Storage

SC Storage

作者: 五角场李小白 | 来源:发表于2018-12-28 13:13 被阅读0次

<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.

相关文章

网友评论

      本文标题:SC Storage

      本文链接:https://www.haomeiwen.com/subject/ookjkqtx.html