Pythia is a flexible automation oracle capable of executing transactions at scheduled intervals, providing randomness, or delivering price feeds from Sybil. This guide will walk you through the process of creating a subscription using both the front-end application and Solidity.
Set up your own subscription by choosing the chain where your contract resides, specifying your contract address, and defining the method to be executed.
Define the automation frequency, and if desired, add payload to execution transactions as randomness or price feed from Sybil.
Authenticate with Self Identity Wallet Extension (SIWE), top up your execution wallet, and submit your subscription.
You can also create a subscription through Solidity using the OrallyPythiaSubscriptionsRegistry contract. Here's the contract's interface:
Here are a few examples demonstrating how to use the Pythia subscription in different scenarios:
Basic Execution: An example where the Pythia oracle executes a simple update to a value.
import {OrallyPythiaConsumer} from "../consumers/OrallyPythiaConsumer.sol";
contract PythiaExecutionExample is OrallyPythiaConsumer {
uint256 public value;
constructor(
address _pythiaRegistry
) OrallyPythiaConsumer(_pythiaRegistry) {}
function updateValue(uint256 _value) external onlyExecutor {
value = _value;
}
}
Raffle: In this scenario, Pythia is used to randomly pick a raffle winner.
import {OrallyPythiaConsumer} from "../consumers/OrallyPythiaConsumer.sol";
contract RaffleExample is OrallyPythiaConsumer {
uint256 maxNumberOfTickets;
uint256 ticketPrice;
address[] entries;
constructor(
address _pythiaRegistry,
uint256 _maxNumberOfTickets,
uint256 _ticketPrice
) OrallyPythiaConsumer(_pythiaRegistry) {
maxNumberOfTickets = _maxNumberOfTickets;
ticketPrice = _ticketPrice;
}
function enterRaffle() external payable {
require(
entries.length < maxNumberOfTickets,
"RaffleExample: Raffle is full"
);
require(
msg.value == ticketPrice,
"RaffleExample: Ticket price is not correct"
);
entries.push(msg.sender);
}
function pickWinner(uint256 _randomNumber) external onlyExecutor {
require(
entries.length == maxNumberOfTickets,
"RaffleExample: Raffle is not full"
);
uint256 winnerIndex = _randomNumber % entries.length;
payable(entries[winnerIndex]).call{value: address(this).balance}("");
}
}
Price Feed: This example demonstrates how Pythia can be used to regularly update a price feed using data from Sybil.
import {OrallyPythiaConsumer} from "../consumers/OrallyPythiaConsumer.sol";
interface IFxPriceFeedExample {
function pair() external view returns (string memory);
function baseTokenAddr() external view returns (address);
function decimalPlaces() external view returns (uint256);
}
contract FxPriceFeedExample is OrallyPythiaConsumer, IFxPriceFeedExample {
uint256 public rate;
uint256 public lastUpdate;
string public pair;
address public baseTokenAddr;
uint256 public decimalPlaces;
constructor(
address _pythiaRegistry,
string memory _pair,
address _baseTokenAddr,
uint256 _decimalPlaces
) OrallyPythiaConsumer(_pythiaRegistry) {
pair = _pair;
baseTokenAddr = _baseTokenAddr;
decimalPlaces = _decimalPlaces;
}
function updateRate(
string memory _pairId,
uint256 _rate,
uint256 _decimals,
uint256 _timestamp
) external onlyExecutor {
rate = (_rate * (10 ** decimalPlaces)) / (10 ** _decimals); // normalise rate
lastUpdate = _timestamp;
}
function updateTime() external view returns (uint256) {
return lastUpdate;
}
function exchangeRate() external view returns (uint256) {
return rate;
}
}
These examples should help you get started with using Pythia. With its robust features, Pythia offers great flexibility in automating transactions and retrieving real-time data, making it a vital tool for any decentralized application.