Serving TWAPs for every AMM pool.
A time weighted average price is a function that takes a sequence of (time, price)
pairs and returns a price representing an 'average' over the entire time period. Currently, only the arithmetic mean method is implemented.
URL: https://lcd-osmosis.blockapsis.com/osmosis/twap/v1beta1/ArithmeticTwap
Full endpoint URL with parameters: https://lcd-osmosis.blockapsis.com/osmosis/twap/v1beta1/ArithmeticTwap?pool_id=604&base_asset=ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4"e_asset=uosmo&start_time=2023-06-05T16:36:10.000000000Z&end_time=2023-06-05T16:56:10.000000000Z
Query Parameters:
pool_id=604
: Specifies the ID of the pool for which you want to calculate the TWAP.base_asset=ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4
: Specifies the base asset used in the TWAP calculation. You can see all assets here: https://docs.osmosis.zone/osmosis-core/asset-infoquote_asset=uosmo
: Specifies the quote asset used in the TWAP calculation.start_time=2023-06-05T16:36:10.000000000Z
: Indicates the start time for the TWAP calculation.end_time=2023-06-05T16:56:10.000000000Z
: Indicates the end time for the TWAP calculation.
This query requests the API to calculate the TWAP for a specific pool on the Osmosis blockchain. It provides the pool ID, base asset, quote asset, start time, and end time as parameters. The API will respond with the calculated TWAP for the given parameters.
This function fetches data from the LCD api. It constructs the URL with query parameters and returns a promise that resolves to the response.
poolId
(string): The ID of the pool.baseAsset
(string): The base asset.quoteAsset
(string): The quote asset.startTime
(string): The start time for the data.endTime
(string): The end time for the data.
A promise that resolves to the response from the URL.
const utcTime = new Date().toISOString();
console.log('UTC Time:', utcTime);
async function ArithmeticTwap(poolId, baseAsset, quoteAsset, seconds) {
const baseUrl = "https://lcd-osmosis.blockapsis.com/osmosis/twap/v1beta1/ArithmeticTwap";
// Calculate the end
// TODO: Removing 100 mili seconds else the API cries that this is a date in the future. Seems a litle behind. Need to fix this.
const endTime = new Date();
endTime.setSeconds(endTime.getSeconds() - 100);
// Calculate the start time
const startTime = new Date(endTime.getTime() - seconds * 1000).toISOString();
// Construct the query parameters
const params = new URLSearchParams();
params.append("pool_id", poolId);
params.append("base_asset", baseAsset);
params.append("quote_asset", quoteAsset);
params.append("start_time", startTime);
params.append("end_time", endTime.toISOString());
// Generate the full URL with query parameters
const url = `${baseUrl}?${params.toString()}`;
return fetch(url)
.then(response => {
console.log('URL:', url);
console.log('Response:', response);
return response;
})
.catch(error => {
console.error('Error:', error);
throw new Error('Failed to fetch URL');
});
}
// Example usage
const poolId = "604";
const baseAsset = "ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4";
const quoteAsset = 'uosmo';
const seconds = 60; // Fetch data for the last x seconds
ArithmeticTwap(poolId, baseAsset, quoteAsset, seconds)
.then(response => response.json())
.then(data => {
console.log('Data:', data);
})
.catch(error => {
console.error('Error:', error);
});