Shibaswap v1 integrates with Unification’s OoO (Oracle of Oracles) to provide secure, reliable price feeds and data for the decentralized exchange. This partnership ensures that Shibaswap has access to high-quality oracle data for various use cases including price validation, liquidation mechanisms, and advanced trading features.
Overview
The Oracle of Oracles (OoO) is a decentralized oracle network that aggregates data from multiple sources to provide reliable price feeds and market data. Shibaswap v1 leverages this system to enhance security and provide accurate pricing information for traders and liquidity providers.
Shibaswap v1 is partnered with Unification’s OoO to provide secure oracle data for price feeds and market information.
Key Components
xFUND Router Network
The xFUND Router network is the core infrastructure that enables communication between smart contracts (Consumers) and data providers. It handles:
Data Request Routing : Routes requests from consumers to appropriate providers
Fee Management : Handles payment for oracle services
Gas Refunds : Manages gas cost reimbursements
Request Fulfillment : Ensures data is delivered to requesting contracts
Consumer Smart Contracts
Shibaswap v1 implements consumer smart contracts that can request and receive data from the OoO network:
ConsumerBase.sol
Price Feed Integration
// Import the ConsumerBase contract
import "@unification/ooo/contracts/ConsumerBase.sol" ;
contract ShibaswapOracle is ConsumerBase {
// Oracle request functions
function requestPriceData (
string memory _symbol ,
uint256 _timestamp
) external returns ( bytes32 requestId ) {
// Request price data from OoO
return _requestData (_symbol, _timestamp);
}
// Callback function to receive oracle data
function _fulfillData (
bytes32 _requestId ,
bytes memory _data
) internal override {
// Process received oracle data
( uint256 price, uint256 timestamp) = abi . decode (_data, ( uint256 , uint256 ));
// Update internal price feeds
_updatePriceFeed (_requestId, price, timestamp);
}
}
Data Flow
Request Initiation : Shibaswap smart contracts request price data
Router Processing : xFUND Router routes requests to appropriate providers
Data Aggregation : OoO aggregates data from multiple sources
Response Delivery : Processed data is delivered back to requesting contracts
Price Updates : Shibaswap updates internal price feeds
Supported Data Types
Shibaswap v1 can request various types of data through the OoO network:
Price Feeds : Real-time token prices
Market Data : Trading volume, market cap, etc.
Cross-Chain Data : Prices from different blockchain networks
Historical Data : Price history and analytics
Volatility Metrics : Market volatility indicators
Implementation Guide
Setting Up Oracle Integration
Install Dependencies
Install the Unification OoO consumer library: npm install @unification/ooo
Import ConsumerBase
Import the ConsumerBase contract into your Shibaswap contract: import "@unification/ooo/contracts/ConsumerBase.sol" ;
Configure Router Address
Set the xFUND Router address for your network: address public constant ROUTER_ADDRESS = 0 x...; // Network-specific address
Implement Callback Functions
Override the _fulfillData
function to handle received oracle data: function _fulfillData ( bytes32 _requestId , bytes memory _data ) internal override {
// Process oracle data
}
Requesting Oracle Data
Basic Request
Advanced Request
// Request price data for a specific token
function requestTokenPrice ( string memory _symbol ) external {
bytes32 requestId = keccak256 ( abi . encodePacked (_symbol, block .timestamp));
// Request data through the router
_requestData (_symbol, block .timestamp);
emit OracleRequested (requestId, _symbol);
}
Processing Oracle Responses
Data Processing
Price Validation
// Process received oracle data
function _fulfillData ( bytes32 _requestId , bytes memory _data ) internal override {
// Decode the received data
( uint256 price, uint256 timestamp, uint256 confidence) = abi . decode (
_data,
( uint256 , uint256 , uint256 )
);
// Validate the data
require (timestamp > lastUpdateTime[_requestId], "Stale data" );
require (confidence >= MIN_CONFIDENCE, "Low confidence" );
// Update price feed
priceFeeds[_requestId] = price;
lastUpdateTime[_requestId] = timestamp;
emit PriceUpdated (_requestId, price, timestamp, confidence);
}
Use Cases in Shibaswap v1
Price Validation
Oracle data is used to validate prices and prevent manipulation:
Price Validation
Manipulation Protection
// Validate swap price against oracle
function validateSwapPrice (
address _tokenIn ,
address _tokenOut ,
uint256 _amountIn ,
uint256 _amountOut
) internal view returns ( bool ) {
// Get oracle price
uint256 oraclePrice = getOraclePrice (_tokenIn, _tokenOut);
// Calculate swap price
uint256 swapPrice = (_amountOut * 1e18 ) / _amountIn;
// Check if price deviation is acceptable
uint256 deviation = calculateDeviation (swapPrice, oraclePrice);
return deviation <= MAX_DEVIATION;
}
Liquidation Mechanisms
Oracle data enables automated liquidation for lending protocols:
// Check if position should be liquidated
function checkLiquidation (
address _user ,
address _collateral ,
address _debt
) external view returns ( bool ) {
uint256 collateralPrice = getOraclePrice (_collateral, WETH);
uint256 debtPrice = getOraclePrice (_debt, WETH);
uint256 collateralValue = userCollateral[_user] * collateralPrice;
uint256 debtValue = userDebt[_user] * debtPrice;
// Check if collateral ratio is below threshold
return (collateralValue * 100 ) / debtValue < LIQUIDATION_THRESHOLD;
}
Advanced Trading Features
Oracle data powers advanced trading features:
// Execute limit orders based on oracle prices
function executeLimitOrder (
address _tokenIn ,
address _tokenOut ,
uint256 _amountIn ,
uint256 _limitPrice
) external {
uint256 currentPrice = getOraclePrice (_tokenIn, _tokenOut);
require (currentPrice >= _limitPrice, "Price not met" );
// Execute the swap
_executeSwap (_tokenIn, _tokenOut, _amountIn);
}
Security Considerations
Data Validation
Timestamp Validation
Confidence Check
// Ensure data is fresh
function validateTimestamp ( uint256 _timestamp ) internal view returns ( bool ) {
return block .timestamp - _timestamp <= MAX_AGE;
}
Fallback Mechanisms
// Fallback to internal pricing if oracle fails
function getPriceWithFallback ( address _token ) external view returns ( uint256 ) {
uint256 oraclePrice = getOraclePrice (_token, WETH);
if (oraclePrice == 0 || ! isOracleDataValid (_token)) {
// Fallback to internal pool pricing
return getPoolPrice (_token, WETH);
}
return oraclePrice;
}
Best Practices
For Developers
Always validate oracle data before using it
Implement fallback mechanisms for oracle failures
Use multiple data sources when possible
Monitor oracle health and data quality
Set appropriate timeouts for oracle requests
For Users
Understand oracle dependencies in trading strategies
Monitor oracle data quality and confidence levels
Be aware of potential delays in oracle updates
Consider fallback scenarios in risk management
Oracle data is critical for security. Always implement proper validation and fallback mechanisms to ensure system reliability.
Resources
For detailed integration guides and API documentation, refer to the official Unification OoO documentation linked above.