Skip to content

Commit

Permalink
Adds decoding example to Oracle Data.
Browse files Browse the repository at this point in the history
- Adds decoding example.
- Improves readbility.
- Splits section into sub-headers.
  • Loading branch information
johnnymatthews committed Dec 19, 2024
1 parent 662ab56 commit 8811d2c
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions content/concepts/programs.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,43 @@ Programs can require users to include auxiliary data, separate from the message,

### Oracle Data

Oracle data is information that can be grabbed by the chain and passed through to the program. This information comes from the pallet `Oracle` and storage slot `OracleData`. `OracleData` takes in a key which can denote any oracle data you want. Oracle data needs to come from the chain first as all programs need to be deterministic.

All oracle data is scale encode to chain

Current headings
Oracle data is information that can be grabbed by the chain and passed through to Entropy programs. The programs can then do whatever they want with this data. The data contained by the oracles comes from the `Oracle` pallet and `OracleData` storage slot `OracleData` takes in a key and outputs the value associated with that key. Since all programs must be deterministic, oracle data needs to come from the chain.

#### Available oracles

The following table lists the currently available oracles:

| Key | Value | Type |
| ----------------------- | -------------------------------------------- | --------- |
| `block_number_entropy` | Stores the current block number of entropy. | `u32` |

#### Encoding and decoding

All oracle data is [SCALE](https://docs.substrate.io/reference/scale-codec/) encoded. To decode oracle data use:

```rust {hl_lines=[8,8],linenostart=1}
impl Program for OracleExample {
fn evaluate(
_signature_request: SignatureRequest,
_config: Option<Vec<u8>>,
oracle_data: Option<Vec<Vec<u8>>>,
) -> Result<(), Error> {
let data = oracle_data.ok_or(Error::Evaluation("No oracle data provided.".to_string()))?;
let block_number = u32::decode(&mut data[0].as_ref())
.map_err(|_| Error::Evaluation("Unable to decode oracle data".to_string()))?;
if block_number > 100 {
return Err(Error::Evaluation("Block Number too large".to_string()));
}
Ok(())
}

fn custom_hash(_data: Vec<u8>) -> Option<Vec<u8>> {
None
}
}
```

| Key | Value | Type |
| --------------------- | ------------------------------------------- | ------- |
| block_number_entropy | Stores the current block number of entropy | u32 |
A complete copy of the above example can be found in the [Entropy Core GitHub repository](https://github.com/entropyxyz/programs/blob/de27c61a91b716b84696f25949cbf9843920002d/examples/oracle-example/src/lib.rs).

### Custom Hashing

Expand Down

0 comments on commit 8811d2c

Please sign in to comment.