|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +sidebar_label: "How to use in Rust/Scrypto" |
| 4 | +--- |
| 5 | + |
| 6 | +# Scrypto component—how it is built |
| 7 | + |
| 8 | +1. Read firstly the docs from [How to start](../) section |
| 9 | +2. See how to [set up local scrypto environment](https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/radix-connector/scrypto/README.md) |
| 10 | +3. Read the [general philosophy of the on-ledger component](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scrypto/contracts/price_adapter/README.md) |
| 11 | +4. The full source of the component is available [here](https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/radix-connector/scrypto/contracts/price_adapter) |
| 12 | +5. See the [docs of the _RedStone Rust SDK_](https://docs.redstone.finance/rust/redstone/rust_sdk_2/redstone/index.html)—the component is built on |
| 13 | + |
| 14 | +The **info described there** is mostly **NOT REPEATED below**. |
| 15 | + |
| 16 | +## Dependencies |
| 17 | + |
| 18 | +1. Use the following dependencies to embed _RedStone Rust SDK_ into Scrypto. |
| 19 | + |
| 20 | +```toml |
| 21 | +[dependencies] |
| 22 | +scrypto = { version = "1.3.0" } |
| 23 | +redstone = { git = "https://github.com/redstone-finance/rust-sdk", tag = "2.0.0", default-features = false, features = ["radix"] } |
| 24 | +``` |
| 25 | + |
| 26 | +## Instantiating & Storage |
| 27 | + |
| 28 | +Some of the values are necessary to be stored inside the component during its instantiating. |
| 29 | +Also, for the [Push model](#push-model), the values and timestamp are stored in the component. |
| 30 | + |
| 31 | +```rust |
| 32 | +#[blueprint] |
| 33 | +mod price_adapter { |
| 34 | + struct PriceAdapter { |
| 35 | + signer_count_threshold: u8, |
| 36 | + signers: Vec<Vec<u8>>, |
| 37 | + prices: HashMap<Vec<u8>, RedStoneValue>, |
| 38 | + timestamp: u64, |
| 39 | + } |
| 40 | + |
| 41 | + impl PriceAdapter { |
| 42 | + pub fn instantiate( |
| 43 | + signer_count_threshold: u8, |
| 44 | + allowed_signer_addresses: Vec<Vec<u8>>, |
| 45 | + ) -> Global<PriceAdapter> { |
| 46 | + let addresses: Vec<SignerAddress> = allowed_signer_addresses |
| 47 | + .iter() |
| 48 | + .map(|signer| (signer.clone()).into()) |
| 49 | + .collect(); |
| 50 | + |
| 51 | + verify_signers_config(addresses.as_slice(), signer_count_threshold) |
| 52 | + .unwrap_or_else(|err| panic!("{}", err)); |
| 53 | + |
| 54 | + Self { |
| 55 | + signer_count_threshold, |
| 56 | + signers: allowed_signer_addresses, |
| 57 | + prices: hashmap!(), |
| 58 | + timestamp: 0, |
| 59 | + } |
| 60 | + .instantiate() |
| 61 | + .prepare_to_globalize(OwnerRole::None) |
| 62 | + .globalize() |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Using the _RedStone Rust SDK_ |
| 69 | + |
| 70 | +### Payload processing |
| 71 | + |
| 72 | +1. The payload bytes should be defined as described [here](https://docs.redstone.finance/img/payload.png). |
| 73 | +2. The payload can be generated as described [here](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scrypto/README.md#preparing-sample-data). |
| 74 | + |
| 75 | +To process the payload data, the following command should be used inside the `#[blueprint]`. |
| 76 | + |
| 77 | +```rust |
| 78 | +use redstone::{ |
| 79 | + core::{config::Config, ProcessorResult}, |
| 80 | + radix::RadixRedStoneConfig, |
| 81 | + Value as RedStoneValue, |
| 82 | + contract::verification::*, |
| 83 | + SignerAddress |
| 84 | +}; |
| 85 | +``` |
| 86 | + |
| 87 | +The function processes on-chain the payload passed as an argument and returns an array of aggregated values of each feed passed as an identifier inside feed_ids, and a timestamp related to the payload data packages. |
| 88 | + |
| 89 | +```rust |
| 90 | +fn process_payload( |
| 91 | + &mut self, |
| 92 | + feed_ids: Vec<Vec<u8>>, |
| 93 | + payload: Vec<u8>, |
| 94 | +) -> (u64, Vec<RedStoneValue>) { |
| 95 | + let result = Self::try_process_payload( |
| 96 | + Self::get_current_time(), |
| 97 | + self.signer_count_threshold, |
| 98 | + self.signers.clone(), |
| 99 | + feed_ids, |
| 100 | + payload, |
| 101 | + None, |
| 102 | + None, |
| 103 | + ) |
| 104 | + .unwrap_or_else(|err| panic!("{}", err)); |
| 105 | + |
| 106 | + (result.timestamp.as_millis(), result.values) |
| 107 | +} |
| 108 | + |
| 109 | +fn try_process_payload( |
| 110 | + block_timestamp: u64, |
| 111 | + signer_count_threshold: u8, |
| 112 | + signers: Vec<Vec<u8>>, |
| 113 | + feed_ids: Vec<Vec<u8>>, |
| 114 | + payload: Vec<u8>, |
| 115 | + max_timestamp_delay_ms: Option<u64>, |
| 116 | + max_timestamp_ahead_ms: Option<u64>, |
| 117 | +) -> ProcessorResult { |
| 118 | + let config: RadixRedStoneConfig = Config::try_new( |
| 119 | + signer_count_threshold, |
| 120 | + signers.into_iter().map(|id| id.into()).collect(), |
| 121 | + feed_ids.into_iter().map(|id| id.into()).collect(), |
| 122 | + block_timestamp.into(), |
| 123 | + max_timestamp_delay_ms.map(|v| v.into()), |
| 124 | + max_timestamp_ahead_ms.map(|v| v.into()), |
| 125 | + )? |
| 126 | + .into(); |
| 127 | + |
| 128 | + redstone::core::process_payload(&config, payload) |
| 129 | +} |
| 130 | + |
| 131 | +``` |
| 132 | + |
| 133 | +#### Config |
| 134 | + |
| 135 | +The `Config` structure is described [here](https://docs.redstone.finance/rust/redstone/rust_sdk_2/redstone/core/config/struct.Config.html). |
| 136 | + |
| 137 | +For safety reasons, the allowed `signers` and `signer_count_threshold` should be embedded in the component as defined above. |
| 138 | + |
| 139 | +#### Current timestamp |
| 140 | + |
| 141 | +Also, the current timestamp in milliseconds is necessary to be passed as the `block timestamp` parameter: |
| 142 | + |
| 143 | +```rust |
| 144 | +use scrypto::prelude::*; |
| 145 | + |
| 146 | +pub fn get_current_time() -> u64 { |
| 147 | + let rtn = ScryptoVmV1Api::object_call( |
| 148 | + CONSENSUS_MANAGER.as_node_id(), |
| 149 | + CONSENSUS_MANAGER_GET_CURRENT_TIME_IDENT, |
| 150 | + scrypto_encode(&ConsensusManagerGetCurrentTimeInputV2 { |
| 151 | + precision: TimePrecisionV2::Second, |
| 152 | + }) |
| 153 | + .unwrap(), |
| 154 | + ); |
| 155 | + |
| 156 | + let instant: Instant = scrypto_decode(&rtn).unwrap(); |
| 157 | + |
| 158 | + instant.seconds_since_unix_epoch as u64 |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +#### Errors |
| 163 | + |
| 164 | +The possible errors thrown during the payload processing can be found [here](https://docs.redstone.finance/rust/redstone/rust_sdk_2/redstone/network/error/enum.Error.html). |
| 165 | + |
| 166 | +## Pull model |
| 167 | + |
| 168 | +To use the pull model, just invoke the `process_payload` function and return the value. |
| 169 | + |
| 170 | +```rust |
| 171 | +pub fn get_prices( |
| 172 | + &mut self, |
| 173 | + feed_ids: Vec<Vec<u8>>, |
| 174 | + payload: Vec<u8>, |
| 175 | +) -> (u64, Vec<RedStoneValue>) { |
| 176 | + self.process_payload(feed_ids, payload) |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +## Push model |
| 181 | + |
| 182 | +For the Push model, invoke the `process_payload` function and save the value inside storage. |
| 183 | + |
| 184 | +```rust |
| 185 | +pub fn write_prices( |
| 186 | + &mut self, |
| 187 | + feed_ids: Vec<Vec<u8>>, |
| 188 | + payload: Vec<u8>, |
| 189 | +) -> (u64, Vec<RedStoneValue>) { |
| 190 | + let (payload_timestamp, values) = self.process_payload(feed_ids.clone(), payload); |
| 191 | + |
| 192 | + UpdateTimestampVerifier::Untrusted.verify_timestamp( |
| 193 | + Self::get_current_time().into(), |
| 194 | + None, |
| 195 | + 0.into(), |
| 196 | + self.timestamp.into(), |
| 197 | + payload_timestamp.into(), |
| 198 | + ).unwrap_or_else(|err| panic!("{}", err)); |
| 199 | + |
| 200 | + self.timestamp = payload_timestamp; |
| 201 | + self.prices = feed_ids |
| 202 | + .iter() |
| 203 | + .zip(values.clone()) |
| 204 | + .map(|(feed_id, value)| (feed_id.clone(), value)) |
| 205 | + .collect(); |
| 206 | + |
| 207 | + (payload_timestamp, values) |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +Then the values can be read by using |
| 212 | + |
| 213 | +```rust |
| 214 | +pub fn read_prices(&mut self, feed_ids: Vec<Vec<u8>>) -> Vec<RedStoneValue> { |
| 215 | + feed_ids |
| 216 | + .iter() |
| 217 | + .map(|feed_id| self.read_price(feed_id.clone())) |
| 218 | + .collect() |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +or |
| 223 | + |
| 224 | +```rust |
| 225 | +pub fn read_price(&mut self, feed_id: Vec<u8>) -> RedStoneValue { |
| 226 | + *self.prices.get(&feed_id).unwrap() |
| 227 | +} |
| 228 | +``` |
0 commit comments