Details: Starcoin’s Standard Oracle Protocol

Starcoin
7 min readOct 28, 2021

--

Why do we need Oracle?

Blockchain development has encountered many difficulties so far, and behind these difficulties often spawn more innovation. How does the chain access real-world data? How do contracts capture the price fluctuations of the market? How can chain travel ensure unpredictability? Similarly many questions plagued us, Oracle (prophecy machine) was born. Oracle not only bridges the gap between on-chain and off-chain, but also opens a door between public chains, greatly expanding the application scenarios of blockchain. So Oracle is like a bridge that allows data from different places to be connected with each other.

Oracle has become a necessary basic component in the public chain ecosystem. starcoin also defines a standard set of Oracle protocols, which are like satellites that collect data from the planet and distribute it to other places through Move contracts to create rich value.

Starcoin’s Standard Oracle Protocol

Starcoin has designed a standard Oracle protocol in Stdlib through the smart contract language Move, with the following features.

●Scalability
●Open data source
●Secure and reliable
●Simple and efficient
The protocol is fully functional and designed to be refined and clever, let’s take a deeper look through the source code at.

1.Flexible OracleInfo

struct OracleInfo<OracleT: copy+store+drop, Info: copy+store+drop> has key {
///The datasource counter
counter: u64,
///Ext info
info: Info,
}

public fun register_oracle<OracleT: copy+store+drop, Info: copy+store+drop>(_sender: &signer, info: Info) acquires GenesisSignerCapability

●OracleInfo is oriented to generic programming, through the generic parameters to support any type of Info, there is very good scalability.
●OracleInfo has only the ability of key, neither can copy generate duplicate types of OracleInfo instances, nor let the existing OracleInfo instances drop out of thin air, safe and reliable.
●No use of array storage, and any account can register OracleInfo by register_oracle function.

2.Enriched DataSource

struct DataSource<OracleT: copy+store+drop, ValueT: copy+store+drop> has key {
/// the id of data source of ValueT
id: u64,
/// the data version counter.
counter: u64,
update_events: Event::EventHandle<OracleUpdateEvent<OracleT, ValueT>>,
}

public fun init_data_source<OracleT: copy+store+drop, Info: copy+store+drop, ValueT: copy+store+drop>(sender: &signer, init_value: ValueT) acquires OracleInfo

●Any account can call the init_data_source function to become a DataSource.
●Data such as update credentialsUpdateCapability is stored under the current account with clear ownership.

3.Reasonable permission management

struct UpdateCapability<OracleT: copy+store+drop> has store, key {
account: address,
}

struct GenesisSignerCapability has key{
cap: Account::SignerCapability,
}

●Two different kinds of permissions are defined: GenesisSignerCapability for OracleInfo registration, and UpdateCapability for OracleFeed updates.
●Only GENESIS_ADDRESS has GenesisSignerCapability, which allows everyone to register OracleInfo by lending it out.
●Any account can have UpdateCapability, and any account with UpdateCapability can update the corresponding type of OracleFeed data.

4.Secure OracleFeed

struct DataRecord<ValueT: copy+store+drop> has copy, store, drop {
///The data version
version: u64,
///The record value
value: ValueT,
///Update timestamp millisecond
updated_at: u64,
}

struct OracleFeed<OracleT: copy+store+drop, ValueT: copy+store+drop> has key {
record: DataRecord<ValueT>,
}

public fun update<OracleT: copy+store+drop, ValueT: copy+store+drop>(sender: &signer, value: ValueT) acquires UpdateCapability, DataSource, OracleFeed

public fun update_with_cap<OracleT: copy+store+drop, ValueT: copy+store+drop>(cap: &mut UpdateCapability<OracleT>, value: ValueT) acquires DataSource,OracleFeed

●DataRecord stores real data with strict version version control and updated_at chain timestamp.
●OracleFeed only has the ability of key, can’t copy and drop, safe and reliable.
●OracleFeed<OracleT: copy+store+drop, ValueT: copy+store+drop> is unique under the current account, only the latest data is kept to avoid reading the expired data.
●OracleFeed is updated by the update function and update_with_cap function, regardless of which function, UpdateCapability is required to avoid being updated by others by mistake and to secure the data.
Starcoin’s standard Oracle protocol keeps the structure simple and efficient, applies the advantages of Move well, and has very good security and scalability.

Starcoin’s Oracle Scenario Discussion

We focused on the implementation details of the standard Oracle protocol earlier, and then we’ll throw in a discussion of possible Oracle scenarios for Starcoin.

1.Real-time prices

Users can collect time and price data of cryptocurrencies from different data sources and submit them to the chain in near real-time via Oracle. Other contracts can use this data directly by filtering the data sources, or use this data after aggregation processing, which is very convenient and flexible.

2.Random numbers and chain tour

We all know that in the case of input determination, smart contracts must ensure that the execution results of all people are consistent, so it is difficult to provide a mechanism similar to random numbers on the chain. And through Oracle, users can submit VRF random data to the chain, which can verify the correctness of random numbers and add richer scenarios by giving the contract the ability to be random.

Take games as an example, unpredictability is one of the important factors for games to be attractive. The game gets random numbers through Oracle, which can guarantee the unpredictability of the game.

Of course, there are more application scenarios for random numbers, such as red packets, simulating uncertain environments, etc.

3.Complex computation under the chain

Due to Gas, single-threaded and other restrictions, smart contracts cannot have complex computational logic in them, which can make the scenario of smart contracts very limited. If the complex computation is executed under the chain by some Proof-like scheme, and then the result and Proof are submitted to the Oracle on the chain and provided to other contracts, it will greatly reduce the difficulty of smart contract design and enrich the application scenarios of smart contracts.

4.Insurance Claims

Insurance is an important financial scenario, but there have been various irregularities, such as fraudulent insurance and so on. Smart contract is the law, and it will be a good combination if we use Oracle to put the event on the chain, and then identify and execute it through the contract. For example, common flight delay insurance, education benefit insurance, etc., can be used in this way to regulate the market and improve efficiency.

5.Predict the market

The market is fast-changing, and any event generated under the chain can be quickly mapped to the chain through Oracle. Smart contracts trigger prediction markets to perform operations such as settlement by accessing the data provided by Oracle. For example, sports results, gold price changes, e-sports, etc.

Starcoin’s PriceOracle Implementation

We dive into the design of Starcoin’s standard Oracle protocol, and then explore its rich application scenarios. Next, we take the market price scenario as an example to understand how to add scenario-specific business logic on top of the standard Oracle protocol to implement a complete Oracle application.

Starcoin wraps a PriceOracle module on top of the standard Oracle protocol to make an official implementation for the price scenario. priceOracle is also a generic contract that can register any type of digital asset. Starcoin then implements an STCUSDOracle contract on top of PriceOracle, registering a pair of currencies STC and USDT, and then registers the price of STC corresponding to USD into the Oracle on the chain via the PriceOracleScript contract. Finally, anyone can filter and filter the prices through the PriceOracleAggregator aggregator and apply the aggregated prices to their own products. Next, let’s take a deeper look at the source code of PriceOracle, STCUSDOracle, and PriceOracleAggregator.

1.PriceOracle

PriceOracle is built on top of the standard Oracle protocol and is a common contract implemented for price scenarios. In other words, any data in the form of Price can be chained through PriceOracle.

struct PriceOracleInfo has copy,store,drop{
scaling_factor: u128,
}

In the standard Oracle protocol, OracleInfo has a generic parameter Info: copy+store+drop. in PriceOracle, Info corresponds to the specific implementation of PriceOracleInfo, which contains only the calculation factor scaling_factor, so PriceOracleInfo must have copy, store, drop these three capabilities, which is the requirement of the generic parameter Info.

After clarifying the data definition of PriceOracleInfo, you can call the register_oracle function of the Oracle contract to register PriceOracleInfo, you can also call Oracle’s init_data_source and update to chain the data.

PriceOracle is an application on top of the standard Oracle protocol, so it inherits all the features of the Oracle protocol and is very easy to use.

2.STCUSDTOracle

If PriceOracle is a generic contract for prices, then the STCUSDTOracle contract is a product for specific implementations of price scenarios.

struct STCUSD has copy,store,drop {}

We see that in the standard Oracle protocol, OracleInfo has another generic parameter OracleT: copy+store+drop in addition to Info, OracleT means a pair of currency combinations, Info means information. priceOracle only implements PriceOracleInfo, OracleT is also a generic parameter in PriceOracle. Of course, anyone can implement their own pair of currency combination contracts to define their own products.

3.PriceOracleScript

PriceOracleScript is a number of scripts defined for transactions, which is an important user entry point, mainly for updating Oracle data, including

Registering PriceOracleInfo for any pair of currency pairs OracleT
Update the latest data of any pair of currency combinations OracleT

4.PriceOracleAggregator

The PriceOracleAggregator contract is an aggregator that filters and filters the data in Oracle and then returns the aggregated prices to the user, applying real-time prices on the chain to wherever they are needed.

--

--

Starcoin
Starcoin

Written by Starcoin

Starcoin is a proof-of-work blockchain that enables secure smart contracts based on Move to power services in Web 3.0

No responses yet