Skip to content

Instantly share code, notes, and snippets.

@anonymoussprocket
Last active January 15, 2022 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymoussprocket/09e54c5f6bd7fca208cc59a0274c51c4 to your computer and use it in GitHub Desktop.
Save anonymoussprocket/09e54c5f6bd7fca208cc59a0274c51c4 to your computer and use it in GitHub Desktop.
Querying Tezos historical data with Conseil

Querying Tezos historical data with Conseil

import * as fs from 'fs';
import { JSONPath } from 'jsonpath-plus';
import * as log from 'loglevel';
import fetch from 'node-fetch';
import { registerFetch, registerLogger } from 'conseiljs';
import { TezosParameterFormat, TezosMessageUtils, TezosLanguageUtil } from 'conseiljs';
function initConseil() {
const logger = log.getLogger('conseiljs');
logger.setLevel('debug', false);
registerLogger(logger);
registerFetch(fetch);
}
function bareFetch(conseilUrl, entity, query) {
return fetch(`${conseilUrl}/v2/data/tezos/mainnet/${entity}`,
{ method: 'post',
headers: { apiKey: 'KEY', 'Content-Type': 'application/json' }, // get a key from nautilus.cloud
body: query })
.then(response => { return response.json(); });
}
async function run() {
initConseil();
const lbAMMAddress = 'KT1TxqZ8QtKvLu3V3JH7Gx58n7Co8pgtpQU5'
const michelsonKey = `(Pair "ledger" 0x${TezosMessageUtils.writeAddress(lbAMMAddress)})`;
const packedkey = Buffer.from(TezosMessageUtils.writePackedData(michelsonKey, '', TezosParameterFormat.Michelson), 'hex');
const rePackedKey = TezosMessageUtils.writePackedData(packedkey, 'bytes');
const encodedKey = TezosMessageUtils.encodeBigMapKey(Buffer.from(rePackedKey, 'hex'));
const query = `{
"fields": [ "period", "cycle", "block_level", "timestamp", "value" ],
"predicates": [
{ "field": "big_map_id", "operation": "eq", "set": [ "31" ], "inverse": false },
{ "field": "key_hash", "operation": "eq", "set": [ "${encodedKey}" ], "inverse": false }
],
"orderBy": [ { "field": "block_level", "direction": "asc" } ],
"aggregation": [],
"limit": 50000
}`;
const historicalBalances = await bareFetch('CONSEIL_URL', 'big_map_contents_history', query); // get the URL from nautilus.cloud
let data = '';
historicalBalances.forEach(r => {
const v = JSON.parse(TezosLanguageUtil.hexToMicheline(r['value'].slice(4)).code);
const n = Number(JSONPath({ path: '$.args[0].int', json: v })[0]);
data += `"${new Date(r['timestamp']).toLocaleString()}", ${(n / 100_000_000).toFixed(4)}\n`;
});
fs.writeFileSync('./log.csv', data);
}
run();
{
"name": "tezos-workflow-test",
"version": "8.0.0",
"description": "Integrated Tezos Workflow Test",
"main": "index.ts",
"scripts": {
"start": "tsc lb.ts && node lb.js"
},
"author": "anonymoussprocket",
"license": "Apache2",
"engines": {
"node": "12.21.0",
"npm": "6.14.11"
},
"homepage": "https://gist.github.com/anonymoussprocket",
"dependencies": {
"@types/node": "14.14.35",
"conseiljs": "5.0.9-3",
"jsonpath-plus": "5.0.4",
"loglevel": "1.7.1",
"node-fetch": "2.6.1"
},
"devDependencies": {
"typescript": "3.8.3"
}
}
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"outDir": "dist",
"removeComments": true,
"strict": true,
"noImplicitAny": false,
"baseUrl": ".",
"esModuleInterop": true,
"resolveJsonModule": true
},
"exclude": [ "node_modules" ],
"include": [ "." ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment