NAV Navbar
c# python
  • Introduction
  • Blockchain
  • Header
  • Block
  • Transaction
  • Contract
  • Storage
  • Runtime
  • ExecutionEngine
  • Native
  • Convert byte[] and String
  • Using Map and Struct
  • Action and Notify
  • Calling other Smart Contract
  • Introduction

    The API of smart contract defines the interfaces between the smart contract program and the ledger layer. These interfaces provide functions that can access data in the blockchain ledger, persistent storage, smart contracts, and native contracts. These interfaces can be roughly divided into four categories.

    1. Blockchain ledger: A contract program can use the API of smart contract to access all data in the blockchain ledger including transaction information, block information, smart contract information, and so on.

    2. Persistent storage: A smart contract deployed on the blockchain can operate its own storage area such as adding, updating, querying, and deleting its storage area.

    3. Smart contract: A contract program can use the API of smart contract to create, update, and delete a contract.

    4. Native contract: The Ontology provides a series of native contract services including OntID contract, Auth contract, Ont contract, Ong contract, and so on. They can access basic services of Ontology through the native contract interface.

    Blockchain

    This class provides a set of methods for accessing blockchain data.

    Blockchain.GetHeight

    
    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             uint height = Blockchain.GetHeight();
         }
    }
    }
    
    from ontology.interop.System.Blockchain import GetHeight
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        height = GetHeight()
        return height
    

    This interface is used to get current block height.

    Parameters

    Parameter Type Description

    Return Value

    uint

    Blockchain.GetHeader

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
            }
        }
    }
    
    from ontology.interop.System.Blockchain import GetHeader
    
    
    def Main(operation, args):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        block_height = 10
        header = GetHeader(block_height)
        return header
    

    This interface is used to get block header by height.

    Parameters

    Parameter Type Description
    height uint header of block

    Return Value

    Header

    Blockchain.GetBlock

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
            }
        }
    }
    
    from ontology.interop.System.Blockchain import GetBlock
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        block = GetBlock(1408)
        return block
    
    

    This interface is used to get block by block hash.

    Parameters

    Parameter Type Description
    hash byte[] The hash of the block

    Return Value

    Block

    Blockchain.GetTransaction

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
             public static void Main()
             {
                 byte[] txid = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                 Transaction transaction = Blockchain.GetTransaction(txid);
             }
        }
    }
    
    from ontology.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        # tx_hash = "9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
        tx_hash = bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
        tx = GetTransactionByHash(tx_hash)
        return tx
    

    This interface is used to get transaction by transaction hash.

    Parameters

    Parameter Type Description
    txid byte[] The id of the transaction

    Return Value

    Transaction

    Blockchain.GetContract

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
             public static void Main()
             {
                 byte[] script_hash = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                 Contract contract = Blockchain.GetContract(script_hash);
             }
        }
    }
    
    from ontology.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        # contract_hash="d81a75a5ff9b95effa91239ff0bb3232219698fa"
        contract_hash = bytearray(b"\xfa\x98\x96\x21\x32\x32\xbb\xf0\x9f\x23\x91\xfa\xef\x95\x9b\xff\xa5\x75\x1a\xd8")
        contract = GetContract(contract_hash)
        return contract
    

    This method is to get contract by contract hash.

    Parameters

    Parameter Type Description
    script_hash byte[] The script hash of the contract

    Return Value

    Contract

    Blockchain.GetTransactionHeight

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             byte[] txid = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
             uint height = Blockchain.GetTransactionHeight(txid);
         }
    }
    }
    
    from ontology.interop.System.Blockchain import GetHeight, GetHeader, GetBlock, GetTransactionHeight, GetContract, GetTransactionByHash
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        # tx_hash = "9f270aa3a4c13c46891ff0e1a2bdb3ea0525669d414994aadf2606734d0c89c1"
        tx_hash = bytearray(b"\xc1\x89\x0c\x4d\x73\x06\x26\xdf\xaa\x94\x49\x41\x9d\x66\x25\x05\xea\xb3\xbd\xa2\xe1\xf0\x1f\x89\x46\x3c\xc1\xa4\xa3\x0a\x27\x9f")
        height = GetTransactionHeight(tx_hash)
        return height
    

    This interface is used to get height of transaction in block by transaction hash.

    Parameters

    Parameter Type Description
    txid byte[] The id of the transaction

    Return Value

    uint

    Header

    Header.Hash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             Header header = Blockchain.GetHeader(9999);
             byte[] hash = header.Hash;
         }
    }
    
    

    This interface is used to get hash of block

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Header.Version

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                uint version = header.Version;
            }
        }
    }
    

    This interface is used to get block version number

    Parameters

    Parameter Type Description

    Return Value

    uint

    Header.PrevHash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                byte[] prevHash = header.PrevHash;
            }
        }
    }
    

    This interface is used to get hash of previous block

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Header.Index

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                uint index = header.Index;
            }
         }
    }
    

    This interface is used to get height of block

    Parameters

    Parameter Type Description

    Return Value

    uint

    Header.MerkleRoot

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                byte[] mRoot = header.MerkleRoot;
            }
        }
    }
    

    This interface is used to get root of Merkle Tree for all transactions in block

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Header.Timestamp

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                uint timestamp = header.Timestamp;
            }
        }
    }
    

    This interface is used to get block's timestamp

    Parameters

    Parameter Type Description

    Return Value

    uint

    Header.ConsensusData

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                ulong cdata = header.ConsensusData;
            }
        }
    }
    

    This interface is used to get block's consensusData

    Parameters

    Parameter Type Description

    Return Value

    ulong

    Header.NextConsensus

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Header header = Blockchain.GetHeader(9999);
                byte[] nextConsensus = header.NextConsensus;
            }
         }
    }
    

    This interface is used to get hash of next bookkeeping

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Block

    Block.GetTransactionCount()

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                int count = bl.GetTransactionCount();
            }
         }
    }
    
    from ontology.interop.System.Blockchain import GetBlock
    from ontology.interop.System.Block import GetTransactionCount
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        block = GetBlock(1408)
        count = GetTransactionCount(block)
        return count
    
    

    This interface is used to get the count of transactions in block

    Parameters

    Parameter Type Description

    Return Value

    int

    Block.GetTransactions()

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                Transaction[] transactions = bl.GetTransactions();
            }
        }
    }
    
    from ontology.interop.System.Blockchain import GetBlock
    from ontology.interop.System.Block import GetTransactionCount
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        block = GetBlock(1408)
        txs = GetTransactions(block)
        return txs
    
    

    This interface is used to get all transactions in block

    Parameters

    Parameter Type Description

    Return Value

    Transaction[]

    Block.GetTransaction(int index)

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                Transaction transaction = bl.GetTransaction(1);
            }
        }
    }
    
    from ontology.interop.System.Blockchain import GetBlock
    from ontology.interop.System.Block import GetTransactionByIndex
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        block = GetBlock(1408)
        tx = GetTransactionByIndex(block, 0)  # index starts from 0.
        return tx
    
    

    This interface is used to get transaction according index in block

    Parameters

    Parameter Type Description
    index int transaction index of block

    Return Value

    Transaction

    Transaction

    Transaction.Hash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                Transaction transaction = bl.GetTransaction(1);
                byte[] hash = transaction.Hash;
            }
        }
    }
    
    
    from ontology.interop.System.Transaction import GetTransactionHash
    from ontology.interop.System.ExecutionEngine import GetScriptContainer
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        txHash = GetTransactionHash(GetScriptContainer())
        return txHash
    

    This interface is used to get hash of transaction

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Transaction.Type

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                Transaction transaction = bl.GetTransaction(1);
                byte ttype = transaction.Type;
            }
        }
    }
    
    

    This interface is used to get type of the current transaction

    Parameters

    Parameter Type Description

    Return Value

    byte

    Transaction.GetAttributes()

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] block = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Block bl = Blockchain.GetBlock(block);
                Transaction transaction = bl.GetTransaction(1);
                TransactionAttribute[] attributes = transaction.GetAttributes();
            }
        }
    }
    
    

    This interface is used to get attributes of transaction

    Parameters

    Parameter Type Description

    Return Value

    TransactionAttribute[]

    Contract

    Contract.Script

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] script_hash = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
                Contract contract = Blockchain.GetContract(script_hash);
                byte[] scrip = contract.Script;
            }
        }
    }
    

    This interface is used to get script of contract according to contract script hash.

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Contract.Create

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] script = new byte[] { 116, 107, 0, 97, 116, 0, 147, 108, 118, 107, 148, 121, 116, 81, 147, 108, 118, 107, 148, 121, 147, 116, 0, 148, 140, 108, 118, 107, 148, 114, 117, 98, 3, 0, 116, 0, 148, 140, 108, 118, 107, 148, 121, 97, 116, 140, 108, 118, 107, 148, 109, 116, 108, 118, 140, 107, 148, 109, 116, 108, 118, 140, 107, 148, 109, 108, 117, 102 }; 
    
                byte[] parameter_list = { 2, 2 };
                byte return_type = 2;
                bool need_storage = false;
                string name = "contractName";
                string version = "1";
                string author = "test";
                string email = "test@ont.io";
                string description = "this is a test contract";
    
                Contract contract = Contract.Create(script, true,name,version,author,email,description);
            }
        }
    }
    

    This interface is used to create a smart contract

    Parameters

    Parameter Type Description
    script byte[] contract script bytes
    need_storage bool contract need storage or not
    name string contract name
    version string contract version
    author string contract author
    email string contract email
    desc string contract description

    Return Value

    Contract

    Contract.Migrate

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main(byte[] signature)
            {
                byte[] script = new byte[] { 116, 107, 0, 97, 116, 0, 147, 108, 118, 107, 148, 121, 116, 81, 147, 108, 118, 107, 148, 121, 147, 116, 0, 148, 140, 108, 118, 107, 148, 114, 117, 98, 3, 0, 116, 0, 148, 140, 108, 118, 107, 148, 121, 97, 116, 140, 108, 118, 107, 148, 109, 116, 108, 118, 140, 107, 148, 109, 116, 108, 118, 140, 107, 148, 109, 108, 117, 102 }; 
    
                byte[] parameter_list = { 2, 2 };
                byte return_type = 2;
                bool need_storage = true;
                string name = "contractName";
                string version = "1";
                string author = "test";
                string email = "test@ont.io";
                string description = "this is a test contract";
    
                Contract contract = Contract.Migrate(script, true,name,version,author,email,description);
             }
        }
    }
    
    
    from ontology.interop.Ontology.Contract import Migrate
    from ontology.interop.System.Contract import Destroy
    from ontology.interop.System.Runtime import Notify
    from ontology.libont import AddressFromVmCode
    
    
    def Main(operation, args):
        if operation == "migrate_contract":
            if len(args) != 7:
                return False
            avm_code = args[0]
            need_storage = args[1]
            name = args[2]
            version = args[3]
            author = args[4]
            email = args[5]
            description = args[6]
            return migrate_contract(avm_code, need_storage, name, version, author, email, description)
    
        return False
    
    
    def migrate_contract(avm_code, need_storage, name, version, author, email, description):
        res = Migrate(avm_code, need_storage, name, version, author, email, description)
        if res:
            Notify(["Migrate successfully"])
            new_contract_hash = AddressFromVmCode(avm_code)
            Notify(new_contract_hash)
            return True
        else:
            return False
    
    

    This interface is used to migrate / update a smart contract,it should be implemented in contract which need to migrate.old contract storage will automatic migrate to new contract.

    Parameters

    Parameter Type Description
    script byte[] contract script bytes
    need_storage bool contract need storage or not
    name string contract name
    version string contract version
    author string contract author
    email string contract email
    desc string contract description

    Return Value

    Contract

    Contract.Destroy()

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Contract.Destroy();
            }
        }
    }
    
    from ontology.interop.System.Contract import Destroy
    from ontology.interop.System.Runtime import Notify
    
    
    def Main(operation, args):
        if operation == "destroy_contract":
            return destroy_contract()
    
        return False
    
    
    def destroy_contract():
        Destroy()
        Notify(["The contract has been destoryed"])
        return True
    
    

    This interface is used to destroy a contract.script and storage of contract will be removed.

    Parameters

    Parameter Type Description

    Return Value

    void

    Contract.StorageContext

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
              byte[] script_hash = new byte[] {206, 240, 165, 25, 76, 228, 58, 100, 117, 184, 213, 171, 61, 96, 34, 234, 129, 116, 60, 71, 11, 231, 143, 195, 123, 5, 190, 250, 182, 14, 152};
             Contract contract = Blockchain.GetContract(script_hash);
             StorageContext sc = contract.StorageContext;
         }
    }
    
    

    This interface is used to get the contract's storage context

    Parameters

    Parameter Type Description

    Return Value

    StorageContext

    Storage

    Storage.CurrentContext

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             StorageContext sc = Storage.CurrentContext;
         }
    }
    
    
    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()
        return False
    
    
    def get_sc():
        return GetContext()
    
    

    This interface is used to get current storage context

    Parameters

    Parameter Type Description

    Return Value

    StorageContext

    Storage.Get(StorageContext,byte[])

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             StorageContext sc = Storage.CurrentContext;
             byte[] key = new byte[] {97, 98,99};
             byte[] val = Storage.Get(sc,key);
         }
    }
    
    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_data':
            key = args[0]
            return get_data(key)
        return False
    
    
    def get_data(key):
        sc = GetContext()
        data = Get(sc, key)  # key is a bytearray
        return data
    
    

    This interface is used to get value by key in current smart contract.

    Parameters

    Parameter Type Description
    sc StorageContext storage context
    key byte[] storage key

    Return Value

    byte[]

    Storage.Get(StorageContext,string)

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
    {
         public static void Main()
         {
             StorageContext sc = Storage.CurrentContext;
             string key = "key";
             byte[] val = Storage.Get(sc,key);
         }
    }
    
    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_data':
            key = args[0]
            return get_data(key)
        return False
    
    
    def get_data(key):
        sc = GetContext()
        data = Get(sc, key)  # key is a string
        return data
    
    

    This interface is used to get value by key in current smart contract.

    Parameters

    Parameter Type Description
    sc StorageContext storage context
    key string storage key

    Return Value

    byte[]

    Storage.Put(StorageContext,string/byte[],string/byte[])

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                StorageContext sc = Storage.CurrentContext;
                string key = "key";
                string val = "value";
                Storage.Put(sc,key,val);
            }
        }
    }
    
    
    from ontology.interop.System.Storage import GetContext, Get, Put, Delete, GetReadOnlyContext
    from ontology.interop.System.Runtime import Notify
    
    
    def Main(operation, args):
        if operation == 'save_data':
            key = args[0]
            value = args[1]
            return save_data(key, value)
        return False
    
    
    def save_data(key, value):
        sc = GetContext()
        Put(sc, key, value)
    
    

    This interface is used to insert operation, inserting data as key-value format into a persistent storage area

    Parameters

    Parameter Type Description
    sc StorageContext storage context
    key string/byte[] storage key
    val string/byte[] storage value

    Return Value

    void

    Storage.Delete(StorageContext,string/byte[])

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                StorageContext sc = Storage.CurrentContext;
                string key = "key";
                Storage.Delete(sc,key);
            }
        }
    }
    
    
    from ontology.interop.System.Storage import GetContext, Get, Put, Delete, GetReadOnlyContext
    from ontology.interop.System.Runtime import Notify
    
    
    def Main(operation, args):
        if operation == 'delete_data':
            key = args[0]
            return delete_data(key)
        return False
    
    
    def delete_data(key):
        sc = GetContext()
        Delete(sc, key)
    
    

    This interface is used to delete operation, delete the corresponding value by key in the persistent storage area

    Parameters

    Parameter Type Description
    sc StorageContext storage context
    key string/byte[] storage key

    Return Value

    void

    Runtime

    Runtime.Time

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                uint rtime = Runtime.Time;
            }
        }
    }
    
    from ontology.interop.System.Runtime import GetTime
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        time = GetTime()
        return time  # return a uint num
    
    

    This interface is used to get current block time

    Parameters

    Parameter Type Description

    Return Value

    uint

    Runtime.CheckWitness(byte[])

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] pubKey = { 2, 123, 48, 51, 62, 13, 14, 101, 82, 174, 109, 29, 169, 249, 64, 159, 85, 30, 53, 238, 151, 25, 48, 94, 148, 93, 196, 220, 186, 153, 132, 86, 202 };
                bool result = Runtime.CheckWitness(pubKey);
            }
        }
    }
    
    
    from ontology.interop.System.Runtime import CheckWitness, Notify
    from ontology.interop.Ontology.Runtime import Base58ToAddress
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        addr = Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
        res = CheckWitness(addr)
        return res
    
    

    This interface is used to verify operational permissions of account or contract

    Parameters

    Parameter Type Description
    pubkey byte[] address of people or contract

    Return Value

    bool

    Runtime.Notify(object[])

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Runtime.Notify("Hello", "World", Blockchain.GetHeight());
            }
        }
    }
    
    
    from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        Notify("hello world")
    
    

    In smart contract, sending notifications (including socket notifications or rpc queries) to clients that are executing this smart contract.

    Parameters

    Parameter Type Description
    message object[] notify messages

    Return Value

    void

    Runtime.Log(string)

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                Runtime.Log("Hello world");
            }
         }
    }
    
    
    from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        Log("hello world")
    
    

    In smart contract, sending logs ( including socket notifications) to clients that are executing this smart contract.

    Parameters

    Parameter Type Description
    message string log messages

    Return Value

    void

    ExecutionEngine

    ExecutionEngine.ScriptContainer

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                IScriptContainer isc = ExecutionEngine.ScriptContainer;
            }
         }
    }
    
    

    This method is to get script container of a smart contract

    Parameters

    Parameter Type Description

    Return Value

    IScriptContainer

    ExecutionEngine.ExecutingScriptHash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] scriptHash = ExecutionEngine.ExecutingScriptHash;
            }
        }
    }
    
    
    from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash
    
    
    def Main(operation, args):
        if operation == "get_contract_hash":
            return get_contract_hash()
    
        return False
    
    
    def get_contract_hash():
        return GetExecutingScriptHash()
    
    

    This interface is used to get current contract hash

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    ExecutionEngine.CallingScriptHash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] callingScriptHash = ExecutionEngine.CallingScriptHash;
            }
        }
    }
    
    
    from ontology.interop.System.ExecutionEngine import GetExecutingScriptHash, GetCallingScriptHash
    
    
    def Main(operation, args):
        if operation == "GetCallingScriptHash_test1":
            return GetCallingScriptHash_test1()
        return False
    
    
    def GetCallingScriptHash_test1():
        return GetCallingScriptHash()
    
    

    This interface is used to get script hash of the invoker of a smart contract

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    ExecutionEngine.EntryScriptHash

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static void Main()
            {
                byte[] entryScriptHash = ExecutionEngine.EntryScriptHash;
            }
        }
    }
    
    
    from ontology.interop.System.ExecutionEngine import GetEntryScriptHash
    
    
    def Main(operation, args):
        if operation == "invoke":
            opt = args[0]
            return invoke()
        return False
    
    
    def invoke():
        entryHash = GetEntryScriptHash()
        return entryHash
    
    

    This interface is used to get script hash of entry point (start of the contract invocation chain) of a smart contract

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Native

    Native.Invoke(byte version, byte[] address, byte[] method, object args)

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    
    
    namespace Contract1
    {
        public class Contract1: SmartContract
        {
            public static readonly byte[] ontAddr = "AFmseVrdL9f9oyCzZefL9tG6UbvhUMqNMV".ToScriptHash();
            struct Transfer
            {
                public byte[] From;
                public byte[] To;
                public ulong Value;
            }
            public static bool Main()
            {
    
                byte[] from = { 2, 123, 48, 51, 62, 13, 14, 101, 82, 174, 109, 29, 169, 249, 64, 159, 85, 30, 53, 238, 151, 25, 48, 94, 148, 93, 196, 220, 186, 153, 132, 86, 202 };
                byte[] to = { 2, 123, 48, 51, 62, 13, 14, 101, 82, 174, 109, 29, 169, 249, 64, 159, 85, 30, 53, 238, 151, 25, 48, 94, 148, 93, 196, 220, 186, 153, 132, 86, 202 };
                ulong value = 100;
                Transfer param = new Transfer { From = from, To = to, Value = value };
                object[] p = new object[1];
                p[0] = param;
                byte[] ret = Native.Invoke(0, ontAddr, "transfer", p);
                if (ret[0] != 1)
                {
                    return false;
                };
                return true;
            }
        }
    }
    
    
    from ontology.interop.System.Runtime import Notify, CheckWitness
    from ontology.interop.Ontology.Runtime import Base58ToAddress
    from ontology.interop.Ontology.Native import Invoke
    from ontology.builtins import state, bytearray
    
    # contract address 
    contract_address_ONT = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
    contract_address_ONG = bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02')
    
    
    def Main(operation, args):
        if operation == 'transfer':
            from_acct = args[0]
            to_acct = args[1]
            ont_amount = args[2]
            ong_amount = args[3]
            return transfer(from_acct, to_acct, ont_amount, ong_amount)
    
        return False
    
    
    def transfer(from_acct, to_acct, ont_amount, ong_amount):
        # transfer base58 address to address in the form of byte array 
        # check whether the sender is the payer
        if CheckWitness(from_acct):
            # transfer ONT
            if ont_amount > 0:
                param = state(from_acct, to_acct, ont_amount)
                res = Invoke(1, contract_address_ONT, 'transfer', [param])
                if res and res == b'\x01':
                    Notify('transfer succeed')
                else:
                    Notify('transfer failed')
            # transfer ONG
            if ong_amount > 0:
                param = state(from_acct, to_acct, ong_amount)
                res = Invoke(1, contract_address_ONG, 'transfer', [param])
                if res and res == b'\x01':
                    Notify('transfer succeed')
                else:
                    Notify('transfer failed')
        else:
            Notify('CheckWitness failed')
    
    

    Invokes a native contract. 1. version - version number´╝î 2. address - contract address 3. method - contract method 4. contract parameters

    Parameters

    Parameter Type Description

    Return Value

    byte[]

    Convert byte[] and String

    //convert string to byte[]
    string a = "aaa";
    byte[] b = (byte[])(object)a;
    
    //or 
    string a = "aaa";
    byte[] b = a.AsByteArray();
    
    //convert byte[] to string
    byte[] a = {1, 2, 3};
    string b = a.AsString();
    
    
    def Main(operation):
        if operation == 'demo':
            return demo()
        return False
    
    
    def demo():
        s = '123'
        b = bytearray(b'123')
    
    

    Using Map and Struct

    You can use map and struct to store data as following

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    using System;
    using System.ComponentModel;
    using System.Numerics;
    using System.Text;
    using Helper = Ont.SmartContract.Framework.Helper;
    
    namespace DID
    {
        public class DID : SmartContract
        {
            public static Object Main(string operation, params object[] args)
            {
                if (operation == "TestMap")
                {
                    return TestMap();
                }else if (operation == "DeserializeMap")
                {
                    return DeserializeMap((byte[])args[0]);
                }
                else if (operation == "TestStruct")
                {
                    return TestStruct();
                }
                 else if(operation == "DeserializeStruct"){
                    return DeserializeStruct((byte[])args[0]);
                }
                return null;
            }
    
            public static object TestMap()
            {
                StorageContext context = Storage.CurrentContext;
                Map<string, int> m = new Map<string, int>();
                int value = 100;
                //m["hello"] = "world";
                m["key"] = value;
    
                byte[] b = Helper.Serialize(m);
                Storage.Put(context, "tx", b);
    
                byte[] v = Storage.Get(context, "tx");
    
                Map<string, int> b2 = (Map<string, int>)Helper.Deserialize(v);
    
                //&& b2["hello"] == "world"
                if ( b2 != null  && (int)b2["key"] == value )
                {
                    Storage.Put(context, "result", "true");
                    return b;
                }
    
                Storage.Put(context, "result", "false");
                return false;
            }
    
    
            public static object DeserializeMap(byte[] param)
            {
                Map<string, int> b2 = (Map<string, int>)Helper.Deserialize(param);
                StorageContext context = Storage.CurrentContext;
    
                int value = 100;
                //&& b2["hello"] == "world"
                if (b2 != null  && (int)b2["key"] == value )
                {
                    Storage.Put(context, "result", "true");
                    return true;
                }
    
                Storage.Put(context, "result", "false");
                return (int)b2["key"];
            }
    
            public static object TestStruct()
            {
                StorageContext context = Storage.CurrentContext;
                ClaimTx m = new ClaimTx();
                m.name = "claimid";
                m.claimId = 100;
    
                byte[] b = Helper.Serialize(m);
                Storage.Put(context, "tx", b);
    
                byte[] v = Storage.Get(context, "tx");
    
                if(b != null){
                  return b;
                }
    
    
                Storage.Put(context, "result", "false");
                return false;
            }
            public static object DeserializeStruct(byte[] param)
            {
                ClaimTx b2 = (ClaimTx)Helper.Deserialize(param);
                StorageContext context = Storage.CurrentContext;
    
                int value = 100;
                //&& b2["hello"] == "world"
                if (b2 != null  && (int)b2.claimId == value )
                {
                    Storage.Put(context, "result", "true");
                    return true;
                }
    
                Storage.Put(context, "result", "false");
                return (int)b2.claimId;
            }
            public class ClaimTx
            {
                public int claimId;
                public string name;
            }
        }
    }
    
    from ontology.interop.System.Storage import Put, Get, GetContext
    from ontology.interop.System.Runtime import Notify, Serialize, Deserialize
    from ontology.builtins import append, remove
    
    MAPKEY = "Map"
    
    
    def Main(operation, args):
        if operation == "init":
            return init()
        if operation == "add_map":
            key = args[0]
            value = args[1]
            return add_map(key, value)
        if operation == "remove_map":
            key = args[0]
            return remove_map(key)
        return False
    
    
    def init():
        # init map
        map1 = {
            "key1": 1,
            "key2": 2
        }
        map1Info = Serialize(map1)
        Put(GetContext(), MAPKEY, map1Info)
        # return result
        Notify(["init map is ", map1["key1"], map1["key2"]])
    
        return True
    
    
    def add_map(key, value):
        map1Info = Get(GetContext(), MAPKEY)
        map1 = Deserialize(map1Info)
    
        Notify(["before add, map is ", map1["key1"], map1["key2"]])
        # add data 
        map1[key] = value
        map1Info = Serialize(map1)
        Put(GetContext(), MAPKEY, map1Info)
        Notify(["after add, map is ", map1["key1"], map1["key2"], map1[key]])
    
        return True
    
    
    def remove_map(key):
        map1Info = Get(GetContext(), MAPKEY)
        map1 = Deserialize(map1Info)
        Notify(["before remove, map is ", map1["key1"], map1["key2"], map1[key]])
        map1.remove(key)
        map1Info = Serialize(map1)
        Put(GetContext(), MAPKEY, map1Info)
        Notify(["after remove, map is ", map1["key1"], map1["key2"]])
    
        return True
    
    

    Action and Notify

    Currently action is not supported ,so you need to replace action with following event codes:

    using Ont.SmartContract.Framework;
    using Ont.SmartContract.Framework.Services.Ont;
    using Ont.SmartContract.Framework.Services.System;
    using System;
    using System.ComponentModel;
    using System.Numerics;
    
    namespace NeoContract3
    {
        public class Contract1 : SmartContract
        {
            public delegate void TransferDel(byte[] from, byte[] to, BigInteger value);
    
            [DisplayName("transfer")]
            public static event TransferDel Transferred;
    
            public static Object Main(string operation, params object[] args)
            {
                if (operation == "Hello")
                {
                    return Hello();
                }
                return false;
    
            }
    
            public static string Hello()
            {
                Storage.Put(Storage.CurrentContext, "Hello", "World");
                Transferred("hello".AsByteArray(), "world".AsByteArray(), 123);
                return "123";
            }
        }
    }
    
    from ontology.interop.System.Action import RegisterAction
    
    Transfer = RegisterAction('transfer_test', 'a', 'b', 'c')
    Refund = RegisterAction('refund_test', 'to', 'amount')
    
    
    def Main(operation, args):
        if operation == "test1":
            return test1()
        if operation == "test2":
            return test2()
        return False
    
    
    def test1():
        a = 3
        b = 8
        c = a + b
        Transfer(a, b, c)
        return True
    
    
    def test2():
        to = 'somebody'
        amount = 52
        Refund(to, amount)
        return True
    
    

    Calling other Smart Contract

    You can call other smart contracts from your contract:

    [Appcall("XXXXXXXXXX")]//ScriptHash
    public static extern int AnotherContract(string arg);
    
    public static void Main()
    {
        AnotherContract("Hello");    
    }
    
    
    from ontology.interop.System.App import RegisterAppCall
    from ontology.interop.System.Runtime import Notify
    
    HelloWorld = RegisterAppCall('5e8d6fddb980bd5b118c45d3ebff5c5f220fa2de', 'operation', 'args')
    
    
    def Main(operation, args):
        if operation == "CallHello":
            msg = args[0]
            return CallHello(msg)
        return False
    
    
    def CallHello(msg):
        return HelloWorld("Hello", [msg])