Skip to main content

Gear Oracle

What is Oracle?​

Blockchain oracles are a combination of programs (smart contracts) and off-chain entities that connect blockchains to external systems(api, etc..), allowing other programs to execute depending on real-world inputs and outputs. Oracles give the Web3 ecosystem a method to connect to existing legacy systems, data sources and advanced calculations.

These programs can then be used to obtain external data which can't exist in blockchain space. In general, oracles are used for:

  • Fetching aggregated tokens prices in fiat (USD, EUR, etc..)
  • Quering Web2 API
  • Obtaining prices for different securities

Moreover, oracles allow the creation of lending / DEX protocols, which form an important part of DeFi.

Gear provides an example of the native implementation of randomness oracle, which provides an ability to use random numbers in programs. It can be used as is or modified to suit your own scenarios. Anyone can easily create their own oracle and run it on the Gear Network. The source code is available on GitHub.

Storage Structure​

#[derive(Debug, Default)]
pub struct RandomnessOracle {
pub owner: ActorId,
pub values: BTreeMap<u128, state::Random>,
pub last_round: u128,
pub manager: ActorId,
}

Action and Event​

Event is generated when Action is triggered. Action enum wraps various Input structs, Event wraps Reply.

#[derive(Debug, Encode, Decode, TypeInfo)]
pub enum Action {
RequestValue,
ChangeManager(ActorId),
}
#[derive(Debug, Encode, Decode, TypeInfo)]
pub enum Event {
NewValue { value: u128 },
NewManager(ActorId),
}

Conclusion​

A source code of the program example is available on GitHub: oracle/oracle/src/contract.rs.

See also an example of the program testing implementation based on gtest and gclient: oracle/oracle/tests.

For more details about testing programs written on Gear, refer to this article: Program Testing.