Skip to content

Commit

Permalink
Cache Pools Query to New File (osmosis-labs#1030)
Browse files Browse the repository at this point in the history
* save pools query to dedicated file

* re add queryPool()
  • Loading branch information
JeremyParish69 authored Jan 29, 2024
1 parent 3524e0e commit cd3f094
Show file tree
Hide file tree
Showing 7 changed files with 50,915 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/utility/assetlist_functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function writeToFile(chainName, directoryName, fileName, value) {
if (err) throw err;
}
);
//console.log("Write successful!");
} catch (err) {
console.log(err);
}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/utility/generate_assetlist_new.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as chain_reg from './chain_registry.mjs';
import * as zone from './assetlist_functions.mjs';
import { returnAssets } from './getPools.mjs';
import { getAssetsPricing } from './getPools.mjs';
import { getAllRelatedAssets } from './getRelatedAssets.mjs';


Expand Down Expand Up @@ -45,7 +45,7 @@ async function asyncForEach(array, callback) {
const generateAssets = async (chainName, zoneConfig, zone_assets, zone_config_assets, chain_reg_assets) => {

let pool_assets;
pool_assets = await returnAssets(chainName);
pool_assets = await getAssetsPricing(chainName);
if (!pool_assets) { return; }

await asyncForEach(zone_assets, async (zone_asset) => {
Expand Down
65 changes: 31 additions & 34 deletions .github/workflows/utility/getPools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
// to identify the pools and liquidity of an asset. To price it and determine whether it goes on info site



//-- Imports --

import * as fs from 'fs';
import fetch from 'node-fetch';
import * as queryApi from './queryApi.mjs';
import * as zone from './assetlist_functions.mjs';






//-- Globals --

let pools = new Map();
export let assets = new Map();
Expand All @@ -14,6 +26,12 @@ const num_largest_pools = 5;

let ticks = 0;





//-- Functions --

function get_base_url(domain) {
let baseUrl;
if (domain == "osmosis") {
Expand All @@ -27,6 +45,7 @@ function get_base_url(domain) {
}
}


export async function queryPool(domain, pool_id) {

//--QUERY API--
Expand All @@ -49,45 +68,23 @@ export async function queryPool(domain, pool_id) {
}
}

async function queryPools(domain) {

async function queryPools(domain) {

//--QUERY API--

let baseUrl = get_base_url(domain);
let params;
let fileName = "all-pools.json";

console.log(domain);
console.log(baseUrl);
const options = {
method: 'GET',
accept: 'application/json'
}
//console.log(baseUrl);

let url = baseUrl;
let response;
let result;
let param = null;
let paginationKey = "";
const paginationLimit = 2000;

param = `?pagination.limit=${paginationLimit}`
if(param) {
url = baseUrl + param;
}
response = await fetch(url,options);
result = await response.json();
if (!result.pools) { return; }
getPools(result.pools);


// -- For testing locally --
//fs.writeFile('pools.json', JSON.stringify(pools,null,2), (err) => {
//if (err) throw err;
//});
//getPools(JSON.parse(fs.readFileSync('pools.json')));

queryApi.queryAPI(baseUrl, params, domain, fileName);

console.log("Pools: ");
console.log(pools.size);
//console.log(pools);
let storedResult = queryApi.readQueryResponse(domain, fileName);
if (!storedResult?.pools) { return; }
getPools(storedResult.pools);

}

Expand Down Expand Up @@ -412,7 +409,7 @@ function getLargestRoute(routes, i){



export async function returnAssets(chain){
export async function getAssetsPricing(chain){
getAssets(chain);
await queryPools(chain);
if (pools.size == 0) { return; }
Expand All @@ -425,7 +422,7 @@ async function main() {
let domain = "osmosis";
//let domain = "osmosistestnet4";
//let domain = "osmosistestnet";
returnAssets(domain);
getAssetsPricing(domain);
}

//main(); //TURN THIS BACK OFF
60 changes: 60 additions & 0 deletions .github/workflows/utility/queryApi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//Purpose:
// to query APIs and record ('cache') the results for each result to a file.


//-- Imports --

import * as fs from 'fs';
import * as zone from './assetlist_functions.mjs';





//-- Global Variables --

const queryResponsesDirectoryName = "query_responses";






//-- Functions --

//--Query URL--
export async function queryAPI(baseUrl, params, chainName, fileName) {

console.log(baseUrl);
const options = {
method: 'GET',
accept: 'application/json'
}
let url;
let response;
let result;
let param = null;
let paginationKey = "";
const paginationLimit = 2000;

param = `?pagination.limit=${paginationLimit}`
if(param) {
url = baseUrl + param;
}
response = await fetch(url,options);
result = await response.json();
if (!result) {
console.log("No result to: ${url}");
return;
}
//console.log(result);

await zone.writeToFile(chainName, queryResponsesDirectoryName, fileName, result);
console.log("Saved API Query Response!");

}

//--Read Query Response--
export function readQueryResponse(chainName, fileName) {
return zone.readFromFile(chainName, queryResponsesDirectoryName, fileName);
}
Loading

0 comments on commit cd3f094

Please sign in to comment.