Skip to content

Commit 80d3f11

Browse files
Lukasz2891bohmac
andauthored
Radix connector (#131)
* feat: Radix docs * Update docs/get-started/supported-chains/radix/index.mdx Co-authored-by: Maciej Bohusz <93550769+bohmac@users.noreply.github.com> * Update typescript-tutorial.md * 0.8.0 * Use Rust SDK 2.0 * Fix lin * Fix links * Fix links * Fix links * radix moved * radix moved * radix moved * radix moved * radix moved * dots --------- Co-authored-by: Maciej Bohusz <93550769+bohmac@users.noreply.github.com>
1 parent f51fc6e commit 80d3f11

File tree

7 files changed

+318
-2
lines changed

7 files changed

+318
-2
lines changed

docs/dapps/non-evm/non-evm.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
sidebar_position: 4
3+
sidebar_label: "Non-EVM chains"
4+
---
5+
6+
# Non-EVM chains
7+
8+
We also support a few non-EVM-compatible chains
9+
10+
import DocCardList from "@theme/DocCardList";
11+
12+
<DocCardList />

docs/dapps/non-evm/radix/index.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
sidebar_position: 1
3+
sidebar_label: "Radix — how to start"
4+
---
5+
6+
import DocCardList from "@theme/DocCardList";
7+
8+
# Radix — how to start
9+
10+
1. Read about RedStone models in [Get Started](../../../category/getting-started/) section
11+
2. Follow the [@redstone-finance/radix-connector](https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/radix-connector/README.md) landing page
12+
13+
Then, see:
14+
15+
<DocCardList />
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
sidebar_position: 3
3+
sidebar_label: "How to use in TypeScript"
4+
---
5+
6+
# How to use in TypeScript
7+
8+
1. Read firstly the docs from [How to start](../) section
9+
2. See how to [connect to the contract/component](https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/radix-connector#-connecting-to-the-contract) in the TypeScript layer
10+
3. The full source of the package is available [here](https://github.com/redstone-finance/redstone-oracles-monorepo/tree/main/packages/radix-connector/src)
11+
12+
The **info described there** is mostly **NOT REPEATED below**.
13+
14+
## Dependencies
15+
16+
1. Use the following dependencies to embed _RedStone Radix Connector_ into TypeScript.
17+
18+
```json
19+
{
20+
dependencies: {
21+
...
22+
"@redstone-finance/radix-connector": "0.8.0",
23+
...
24+
}
25+
}
26+
```
27+
28+
## Using the PriceAdapter package with `@redstone-finance/radix-connector`
29+
30+
### Deploying package
31+
32+
```shell
33+
yarn sample-deploy
34+
```
35+
36+
1. The package can be deployed by using the above command, defined [here](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scripts/sample_deploy.ts).
37+
2. The deployed `package.stokenet.addr` address can be found in the `scrypto/price_adapter` directory.
38+
3. The script uses [`RadixPackageDeployer`](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scripts/RadixPackageDeployer.ts)
39+
with the [`@atlantis-l/radix-tool`](https://github.com/atlantis-l/Radix-Desktop-Tool) package under the hood,
40+
as the Radix Desktop Tool uses.
41+
42+
### Instantiating component
43+
44+
```shell
45+
yarn sample-instantiate
46+
```
47+
48+
1. The component can be instantiated by using the above command, defined [here](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scripts/sample_instantiate_price_adapter.ts).
49+
2. The deployed `component.stokenet.addr` address can be found in the `scrypto/price_adapter` directory.
50+
3. The script uses [`PriceAdapterRadixContractDeployer`](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/src/contracts/price_adapter/PriceAdapterRadixContractDeployer.ts).
51+
52+
### Sample run
53+
54+
```shell
55+
yarn sample-run
56+
```
57+
58+
1. The sample scenario can be run by using the above command, defined [here](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector/scripts/sample_run.ts).
59+
2. The script uses [`PriceAdapterRadixContractConnector`](https://github.com/redstone-finance/redstone-oracles-monorepo/blob/main/packages/radix-connector//src/contracts/price_adapter/PriceAdapterRadixContractConnector.ts).

docs/dapps/redstone-push.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ sidebar_position: 1
33
sidebar_label: "Push model"
44
---
55

6+
import DocCardList from "@theme/DocCardList";
7+
68
# Standardized Access for DeFi Interoperability
79

810
Ideal for dApps deeply integrated with existing protocols and traditional data consumption patterns

docs/data/3-lombard.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To burn LBTC and unstake BTC, a user initiates the burn by sending LBTC back to
2525

2626
### Overview
2727

28-
RedStone delivers LBTC/BTC fundamental price using both [pull](/docs/dapps/redstone-pull.mdx) and [push](/docs/dapps/redstone-push.md) models. The price is calculated as a ratio between BTC controlled by the Lombard protocol and the total supply of LBTC tokens across all supported chains. There are also adjustments for **total_unclaimed_lbtc** (LBTC tokens that are not yet minted but already have correlated BTC tokens deposited to the Lombard protocol) and **total_btc_unstakes_pending** (BTC tokens in the 7-day withdrawal period).
28+
RedStone delivers LBTC/BTC fundamental price using both [pull](/docs/dapps/redstone-pull.mdx) and [push](/docs/dapps/redstone-push) models. The price is calculated as a ratio between BTC controlled by the Lombard protocol and the total supply of LBTC tokens across all supported chains. There are also adjustments for **total_unclaimed_lbtc** (LBTC tokens that are not yet minted but already have correlated BTC tokens deposited to the Lombard protocol) and **total_btc_unstakes_pending** (BTC tokens in the 7-day withdrawal period).
2929

3030
Currently, the value has an upper cap of 1, meaning a healthy value is 1, indicating the protocol’s stability. A value of 1 also signifies that there are as many or more BTC held by the protocol than there are LBTC tokens in circulation, ensuring full or over-collateralization. For example, if half of the BTC tokens disappear from wallets controlled by Lombard, but the LBTC supply remains the same, the ratio would drop to 0.5.
3131

docusaurus.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const config = {
118118
prism: {
119119
theme: lightCodeTheme,
120120
darkTheme: darkCodeTheme,
121-
additionalLanguages: ["bash", "solidity"],
121+
additionalLanguages: ["bash", "solidity", "rust", "toml"],
122122
},
123123
}),
124124
plugins: [

0 commit comments

Comments
 (0)