Ontology Developer Center
DISCOVERCOMMUNITYSUPPORT
  • Introduction
  • Discover Ontology
  • Getting Started
  • Glossary
  • Decentralized Identity and Data
    • ONT ID
      • Decentralized Identifiers
        • Method Specification for Ontology
        • Method Specification for Ethereum
        • Method Specification for BSC
      • Verifiable Credentials
        • Anonymous Credentials
      • ONT Login
        • Scenarios
        • Protocol Specification
        • Front-end JavaScript SDK
          • Integration and Usage
          • API Reference
        • Front-end UI SDK
          • Integration and Usage
          • API Reference
        • Back-end Go SDK
          • Integration and Usage
          • API Reference
        • Back-end Java SDK
          • Integration and Usage
          • API Reference
      • ONT TAG
        • Workflow
        • API Reference
      • Mercury
      • OScore
    • DDXF
      • Components and Interfaces
      • GREP
      • Overall Scheme
      • Solutions
        • Marketplace
          • Deployment
          • Scenarios
          • SaaS Tenant
          • Java SDK
        • Data Storage
          • Deployment
          • Java SDK
        • Resource Auditor
        • Offline Judge
      • Use Cases
        • E-Shops
  • ONTOLOGY ELEMENTS
    • Smart Contracts
      • Types of smart contracts
    • Token Protocols
    • Consensus Mechanism
    • Ontology Oracle
      • Oracle Process Flow
  • GUIDES & TUTORIALS
    • Development Guides
      • dApp Development
        • Using the dAPI
        • Data Synchronization
      • Smart Contract Development
        • EVM Contract
          • Development Environment and Tools
          • Wallet Setup
          • Contract Development
          • How to Deploy a Smart Contract with GetBlock
        • NeoVM Contract
          • Development tools and environment
          • Launching the IDE
          • Writing and editing program logic
          • Deploying and testing on private net
        • WASM Contract
          • Development Environment
          • Project Initiation - Hello World
          • Creating your own project
          • Development using SmartX
          • Runtime API
          • Contract Fundamentals
          • Inter-contract Interaction
          • Developing Contracts in C++
        • Publish Contract Source Code
    • Integration Guides
      • dApp Integration
        • dAPI Integration
          • Chrome Plugin
          • Mobile wallet dApp
          • QR code mechanism
          • Wake call mechanism
        • Cocos 2D-x
        • Unity 3D applications
      • Mobile Wallet Integration
        • SDK integration
        • dAPI Integration
          • In-wallet applications
          • QR code mechanism
          • Wake call mechanism
        • Stake
      • Using ONT ID
      • Exchange Integration
        • Exchange Docking Guide
        • Exchange API
      • Ontology for dApp Stores
    • EVM & Token Decimals Upgrade
  • ONTOLOGY NODE
    • Abstract
    • Node Deployment
      • Standard Node
      • Rosetta Node
    • Interacting with a Public Node
  • DEVELOPER TOOLS
    • dApp Development Framework
      • Punica CLI
      • Punica boxes
      • Solo Chain
    • IDE
    • APIs
      • HTTP API
        • Restful
        • WebSocket
        • Remote Procedure Call (RPC)
      • Explorer v2 API
        • Block
        • Address
        • Contract
        • Token
        • Transactions
        • ONT ID
        • Summary
        • Node
      • Native Token API
        • ONT Contract API
        • ONG Contract API
      • ONT ID Contract API
      • Web3 API
      • OScore Open API
      • Rosetta Node API
        • Data API
        • Construction API
      • DToken Contract API
      • DDXF
        • Marketplace Contract API
        • Storage API
      • Governance API
    • Digital Wallet
      • Chrome Plugin provider
      • Chrome Plugin dAPI
      • Mobile version provider
      • Mobile version dAPI
    • SDKs
    • Signing Server
      • Installation
      • API reference
  • COMMUNITY
    • Ecosystem Programs
    • Community Libraries
    • Community Events
    • Community Channels
    • Core Contributors
  • SUPPORT
    • FAQ
      • Basic blockchain concepts
      • Ontology Nodes
      • Ontology token protocols
      • Smart contracts
      • SDKs and APIs
    • Contact Us
Powered by GitBook
On this page
  • API Method Usage Specifications
  • timestamp()
  • block_height()
  • address()
  • caller()
  • entry_address()
  • current_blockhash()
  • current_txhash()
  • sha256()
  • check_witness()
  • notify()
  • panic()
  • storage_write()
  • storage_read()
  • storage_delete()

Was this helpful?

  1. GUIDES & TUTORIALS
  2. Development Guides
  3. Smart Contract Development
  4. WASM Contract

Runtime API

WASM Contract Runtime API Specifications

The runtime module of the ontology-wasm-cdt-rust Ontology WASM contract development toolkit includes APIs that enable communication between the contract and Ontology blockchain. The API methods can be used to fetch on-chain data and store the contract data on the chain. The API methods have been listed below:

API Methods

Response Value

Description

timestamp

u64

Fetch current timestamp

block_height

u32

Fetch current block height

address

Address

Fetch address of the contract that is run

caller

Address

Fetch the address of the party invoking the contract, mainly used in certain cross contract scenarios

entry_address

Address

Fetch the entry address

current_blockhash

H256

Fetch current block's hash

current_txhash

H256

Fetch current transaction's hash

sha256(data: impl AsRef<[u8]>)

H256

Calculate the SHA256 encryption of the input parameter

check_witness(addr: &Address)

bool

Check whether the specified address's signature exists

input

Vec

Fetch the parameters passed when the contract was invoked

ret(data: &[u8])

!

Returns the result of contract execution

notify(data: &[u8])

!

Save the contract's notify content on the blockchain

panic(msg: &str)

!

Contract's panic message

storage_write(key: &[u8], val: &[u8])

Transmits data to the blockchain

storage_read(key: &[u8])

Option<Vec>

Fetch on-chain data

storage_delete(key: &[u8])

Deleting on-chain data

Next, we will describe the available API methods in detail. Developers are advised to first clone our smart contract template from Github and then add the contract logic in lib.rs file.

API Method Usage Specifications

Developers can use the following command to import the runtime module into the contract.

use ontio_std::runtime;

All the API methods can be called using the runtime module. The available methods are:

timestamp()

The timestamp() method can be used to fetch the current timestamp. The value returns the UNIX timestamp in seconds. Example:

let t = runtime::timestamp();

block_height()

The block_height method can be used to fetch the current height of the blockchain. Example:

let t = runtime::block_height();

address()

The address() method can be used to fetch a contract's address. Example:

let t = runtime::address();

caller()

The caller() method can be used to fetch the address of the party calling the contract. This finds application in cross-contract scenarios, for example if a contract A calls another contract B, contract B can use this method to find out the address of contract A. Example:

let t = runtime::caller();

entry_address()

The entry_address() can be used to fetch the entry address of a contract. A sample application could be where a contract A calls a contract C through contract B, and the contract C uses this method to fetch the address of contract A. Example:

let t = runtime::entry_address();

current_blockhash()

The current_blockhash() method can be used to fetch the hash of the current block. Example:

let t = runtime::current_blockhash();

current_txhash()

The current_txhash() method can be used to fetch the hash of the current transaction. Example:

let t = runtime::current_txhash();

sha256()

This sha256() method can be used to fetch the SHA256 encryption of the input parameter. Example:

let h = runtime::sha256("test");

check_witness()

The check_witness(from) verifies whether the signature of the passed address exists.

  • The method checks whether the party invoking the method contains the signature of from. If true (and signature verification is successful), the method returns true.

  • The method checks whether the invoking party is a contract. If it is, and the method is invoked from this contract, it returns true. It also checks if the from is the returned value from caller(). Here, the caller() method returns the contract hash of the contract that invokes the method.

assert!(runtime::check_witness(from));

notify()

The notify method can be used to pass contract event information to the network along with transmitting it to the blockchain. Example:

runtime::notify("notify".as_bytes())

An event function can be defined when sending a message from the contract using the #[event] annotation. The toolkit provided includes the necessary macros which can be imported using use ostd::macros::event;. Example:

use ostd::macros::event;
mod notify {
    use super::*;
    #[event]
    pub fn transfer(from: &Address, to: &Address, amount: U128) {}
}
fn transfer(from: &Address, to: &Address, amount: U128) -> bool {
    ...
    notify::transfer(from, to, amount);
}

panic()

The panic method stops a transaction when a critical error occurs and then rolls back the current transaction. This method can prove to be very useful in a cross-contract scenario.

For example, before contract A's method calls contract B's method, it transmits and stores certain data to the blockchain, but before contract B's method can be executed a critical error occurs. At this point, the action performed by contract A and the data stored on the chain need to be rolled back. This is carried out by using the panic function in contract B's method. Example:

runtime::panic("test");

storage_write()

This method is used to transmit and store data on the blockchain in the form of key-value pairs. The key and value are both of the bytearray data type. Example:

runtime::storage_write("key".as_bytes(), "value".as_bytes())

storage_read()

The method is used to fetch data from the blockchain using the key. The response is also of the bytearray data type . Example:

runtime::storage_read("key".as_bytes())

storage_delete()

This method is used to delete the on-chain data using the key. Example:

runtime::storage_delete("key".as_bytes())
PreviousDevelopment using SmartXNextContract Fundamentals

Last updated 4 years ago

Was this helpful?