Skip to main content

Querying Oracle Prices

Overview​

PCL and stableswap pools record the prices of recent swaps and expose them with the observe query. observe returns the price a given number of seconds ago. If there is no observation at that exact time, the pool interpolates between the observations around it, so it can answer for any point within the range of stored observations. For times after the newest observation it returns the moving average stored with the newest one.

NOTE

Only current pool versions have observe. Older pools (for example PCL 1.x pools on Terra) answer with an unknown variant observe error. Constant product (xyk) pools don't record observations.

The query takes the number of seconds to look back:


_5
{
_5
"observe": {
_5
"seconds_ago": 3600
_5
}
_5
}

It returns an OracleObservation:


_4
{
_4
"timestamp": 1793795229,
_4
"price": "1.000060638347382797"
_4
}

FieldTypeDescription
timestampu64The time the price refers to (Unix seconds)
priceDecimalThe amount of the pool's first asset traded per unit of its second asset, in base units

The asset order is the order in the pool's pair query (asset_infos). The price is a ratio of base units: if the two assets have different decimals, scale it before comparing it with a display price. Very small swaps are not recorded, and providing or withdrawing liquidity doesn't change the observations.

Executing the "Observe" Query in a Script​

The script below uses CosmJS's read-only CosmWasmClient: it connects to the chain, queries the pool and logs the response. No wallet is needed for queries.

Here's a complete script for calling the observe query:

js
Copy

_30
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_30
_30
const start = async () => {
_30
try {
_30
_30
// ** CONSTANTS ** //
_30
_30
const rpc = "https://neutron-rpc.polkachu.com"; // or https://terra-rpc.publicnode.com
_30
const client = await CosmWasmClient.connect(rpc);
_30
const pairContract = "<Astroport-Pair-Contract-Address>"
_30
_30
// ** QUERY ** //
_30
_30
const observeQuery = await client.queryContractSmart(
_30
pairContract,
_30
{
_30
"observe": {
_30
"seconds_ago": 3600
_30
}
_30
}
_30
);
_30
_30
console.log(observeQuery);
_30
_30
} catch (err) {
_30
console.error(err);
_30
}
_30
};
_30
_30
start();

Replace <Astroport-Pair-Contract-Address> with the address of the pool you want to query. For Terra, use a Terra RPC endpoint (see Network).

Scheduling the Script​

For obtaining price data regularly, we can schedule this script to run at fixed intervals. This can be achieved using two main options: setInterval in Node.js or setting up a cron job on Unix-like systems. Both methods have their advantages and disadvantages and can be chosen based on the specific requirements of the application.

Option 1: setInterval​

This option uses Node.js' built-in setInterval function to run the start function every hour (3600000 milliseconds). This is the simplest option and doesn't require any extra setup, but it's also less robust because it relies on the Node.js process staying up indefinitely.

Here's the code using setInterval:

js
Copy

_10
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_10
_10
const start = async () => {
_10
// (code omitted for brevity)
_10
};
_10
_10
start();
_10
_10
// Run start every hour
_10
setInterval(start, 3600000);

In this example, the function runs every hour. This could be changed to every 24 hours (86400000 milliseconds) or every second (1000 milliseconds).

Option 2: Cron job​

A more robust option for Unix-like systems is to set up a cron job that runs the script. Cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.

Step 1: Make your Node.js script executable​

To start, ensure your script is executable. The first line of your script should contain the following shebang:

js
Copy

_1
#!/usr/bin/env node

So, your script would look like this:

js
Copy

_9
#!/usr/bin/env node
_9
_9
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_9
_9
const start = async () => {
_9
// (code omitted for brevity)
_9
};
_9
_9
start();

Step 2: Set the correct permissions​

Next, navigate to your script's directory in the terminal and run the following command to make it executable:

bash
Copy

_1
chmod +x your-script.js

Replace your-script.js with the name of your script.

Step 3: Create a cron job​

To create a cron job, open the crontab editor by running the following command in your terminal:

bash
Copy

_1
crontab -e

This will open the cron table file where you can set the scheduled jobs. Cron syntax allows you to specify the minute, hour, day of the month, month, and day of the week for the command to be run.

For example, to run your script at the beginning of every hour, add the following line to the file:

lua
Copy

_1
0 * * * * /usr/local/bin/node /path-to-your-script/script.js >> /path-to-log-file/log.txt 2>&1

In this case, 0 * * * * means the cron job will run at the beginning of every hour. Here's the breakdown of the 5 asterisks:

  • Minute: 0
  • Hour: * (any)
  • Day of month: * (any)
  • Month: * (any)
  • Day of week: * (any)

Replace /path-to-your-script/script.js with the path to your script, and /path-to-log-file/log.txt with the path where you want the output log file to be written.

After adding the line, save and close the file. The cron job now runs your script at the start of every hour and writes its output to the log file.