# Pythia: automation

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.

### **Frontend Subscribe**

{% tabs %}
{% tab title="Frontend" %}
For frontend subscription, follow these steps:

1. Visit the Pythia application page at [Pythia DApp](https://kclew-uiaaa-aaaal-qbuiq-cai.ic0.app/pythia).
2. Set up your own subscription by choosing the chain where your contract resides, specifying your contract address, and defining the method to be executed.
3. Define the automation frequency, and if desired, add payload to execution transactions as randomness or price feed from Sybil.
4. Authenticate with Self Identity Wallet Extension (SIWE), top up your execution wallet, and submit your subscription.
   {% endtab %}

{% tab title="Solidity Call Subscribe \[under construction]" %}
You can also create a subscription through Solidity using the [`OrallyPythiaSubscriptionsRegistry`](https://github.com/orally-network/evm-oracle/blob/main/contracts/OrallyPythiaSubscriptionsRegistry.sol) contract. Here's the contract's interface:

```solidity
interface IOrallyPythiaSubscriptionsRegistry {
    function subscribe(
        address target,
        string calldata method,
        uint256 frequency,
        bool is_random,
        string calldata pair_id
    ) external;

    function unsubscribe(uint256 subscription_id) external;
}
```

{% endtab %}
{% endtabs %}

### Subscription Usage Examples

Here are a few examples demonstrating how to use the Pythia subscription in different scenarios:

{% tabs %}
{% tab title="Basic Execution" %}

1. Basic Execution: An example where the Pythia oracle executes a simple update to a value.&#x20;

```solidity
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;
    }
}
```

{% endtab %}

{% tab title="Raffle" %}
2\. Raffle: In this scenario, Pythia is used to randomly pick a raffle winner.

```solidity
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}("");
    }
}
```

{% endtab %}

{% tab title="Price Feed" %}
3\. Price Feed: This example demonstrates how Pythia can be used to regularly update a price feed using data from Sybil.

```solidity
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;
    }
}
```

{% endtab %}
{% endtabs %}

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.orally.network/quick-start/pythia-automation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
