Last active
January 15, 2022 04:35
-
-
Save anonymoussprocket/09e54c5f6bd7fca208cc59a0274c51c4 to your computer and use it in GitHub Desktop.
Querying Tezos historical data with Conseil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"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