How to use real-time data in TON contracts
RedStone is a pull oracle that uses an alternative design for providing oracle data to smart contracts. Instead of constantly persisting data on the contract’s storage, the information is brought on-chain only when needed by end users. Until that moment, data remains in the decentralized cache layer, powered by RedStone light cache gateways and a stream-based data broadcasting protocol. Data is transferred to the contract by end users, who attach signed data packages to their function invocations. The information integrity is verified on-chain through signature checking.Install the RedStone SDK
Install the RedStone TON connector and other necessary dependencies:Write code to interact with oracle
Off-chain data fetch and update
The following code snippet demonstrates how to fetch price updates and interact with RedStone contracts on TON:- Imports the
TonPricesContractConnector
and sets up the RedStone SDK. - Configures the API endpoint and creates a connector to your deployed contract.
- Sets up a
ContractParamsProvider
with the data service ID and feed identifiers. - Fetches price data using on-the-fly processing (no gas cost).
- Writes price data to the contract’s storage (requires gas).
- Reads stored prices and the last update timestamp from the contract.
On-chain data verification
TODOContract types
RedStone provides several contract types for different use cases:Price Manager
Manages multiple price feeds with signature verification and supports both on-the-fly processing and storage persistence.Single Feed Manager
Simplified version for handling a single price feed, reducing gas costs for single-feed applications.Price Feed
Individual feed contract that stores price data for a specific asset.Sample Consumer
Example consumer contract that demonstrates how to read data from price feeds.Error handling
Common error codes you might encounter:- 300+: Insufficient valid signers (less than
signer_count_threshold
) - 200+: Timestamp validation failed (data too old or in the future)
- Other codes: See RedStone constants