From 87fa21960e3f8ad8917f431f12c21ff8ae367f27 Mon Sep 17 00:00:00 2001 From: Mahmoud Aboelenein Date: Thu, 7 Apr 2022 16:38:50 +0200 Subject: [PATCH 1/4] project-reorg --- .env.example | 9 ++ .gitignore | 6 +- src/common/constants.ts | 10 +- src/connections/binance.ts | 25 ++++ src/connections/blockchainInfo.ts | 6 + src/connections/btcRpc.ts | 34 +++++ src/index.ts | 38 +++++ src/script/{test.ts => block.ts} | 130 ++---------------- src/script/config.ts | 52 +++++++ src/script/{mining-test.ts => mining.ts} | 53 +++---- src/script/test.js | 82 ----------- src/services/database/burnchain.ts | 1 + src/services/database/header.ts | 3 + src/services/database/index.ts | 3 + src/services/database/sortition.ts | 5 + .../graphql}/fetch-burnchain-ops-rowid.ts | 2 +- .../graphql}/fetch-latest-block-height.ts | 2 +- .../graphql}/fetch-latest-txid.ts | 2 +- .../graphql}/import-config-item.ts | 2 +- .../graphql}/importer-block-info.ts | 2 +- 20 files changed, 220 insertions(+), 247 deletions(-) create mode 100644 .env.example create mode 100644 src/connections/binance.ts create mode 100644 src/connections/blockchainInfo.ts create mode 100644 src/connections/btcRpc.ts create mode 100644 src/index.ts rename src/script/{test.ts => block.ts} (56%) create mode 100644 src/script/config.ts rename src/script/{mining-test.ts => mining.ts} (92%) delete mode 100644 src/script/test.js create mode 100644 src/services/database/burnchain.ts create mode 100644 src/services/database/header.ts create mode 100644 src/services/database/index.ts create mode 100644 src/services/database/sortition.ts rename src/{script/common => services/graphql}/fetch-burnchain-ops-rowid.ts (99%) rename src/{script/common => services/graphql}/fetch-latest-block-height.ts (98%) rename src/{script/common => services/graphql}/fetch-latest-txid.ts (98%) rename src/{script/common => services/graphql}/import-config-item.ts (99%) rename src/{script/common => services/graphql}/importer-block-info.ts (99%) diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..73503f4 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +DATA_ROOT_PATH= +HASURA_URL= +HASURA_ADMIN_KEY= +IMPORT_ENABLED= +DELTA_HEIGHT= +BTC_RPC_ENDPOINT= +INTERVAL_TIME_CONFIG= +INTERVAL_TIME_MINING= + diff --git a/.gitignore b/.gitignore index 1c4ffd9..429933e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ - .env - .idea - node_modules +.env +.idea +node_modules .DS_Store */.DS_Store diff --git a/src/common/constants.ts b/src/common/constants.ts index 956c2f6..788438e 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -4,7 +4,9 @@ dotenv.config(); export const HASURA_URL = process.env.HASURA_URL; export const HASURA_ADMIN_KEY = process.env.HASURA_ADMIN_KEY; export const STACKS_NODE_SQLITE_PATH = process.env.DATA_ROOT_PATH; -export const DELTA_HEIGHT = process.env.DELTA_HEIGHT -export const BTC_RPC_ENDPOINT = process.env.BTC_RPC_ENDPOINT -export const INTERVAL_TIME_CONFIG = process.env.INTERVAL_TIME_CONFIG -export const INTERVAL_TIME_MINING= process.env.INTERVAL_TIME_MINING +export const DELTA_HEIGHT = process.env.DELTA_HEIGHT; +export const BTC_RPC_ENDPOINT = process.env.BTC_RPC_ENDPOINT; +export const INTERVAL_TIME_CONFIG = process.env.INTERVAL_TIME_CONFIG; +export const INTERVAL_TIME_MINING= process.env.INTERVAL_TIME_MINING; +export const BINANCE_BASE_URL= 'https://api.binance.com/api/v3'; +export const BLOCKCHAIN_INFO_BASE_URL= 'https://api.blockchain.info'; diff --git a/src/connections/binance.ts b/src/connections/binance.ts new file mode 100644 index 0000000..83f3da6 --- /dev/null +++ b/src/connections/binance.ts @@ -0,0 +1,25 @@ +import axios from "axios"; +import {BINANCE_BASE_URL} from "../common/constants"; + +export const binanceApi = axios.create({ + baseURL: BINANCE_BASE_URL, +}); + +const tickerPriceUrl = (coinPair: string) => `/ticker/price?symbol=${coinPair}`; + +export const getStxPrice = async () => { + try { + const response = await binanceApi.post(tickerPriceUrl('STXUSDT')); + return response.data?.price; + } catch (e) { + throw Error(`Cant Fetch latest Stx Price ${e}`); + } +} +export const getBtcPrice = async () => { + try { + const response = await binanceApi.post(tickerPriceUrl('BTCUSDT')); + return response.data?.price; + } catch (e) { + throw Error(`Cant Fetch latest BTC Price ${e}`); + } +} diff --git a/src/connections/blockchainInfo.ts b/src/connections/blockchainInfo.ts new file mode 100644 index 0000000..bdecfa3 --- /dev/null +++ b/src/connections/blockchainInfo.ts @@ -0,0 +1,6 @@ +import axios from "axios"; +import {BLOCKCHAIN_INFO_BASE_URL} from "../common/constants"; + +export const blockchainInfo = axios.create({ + baseURL: BLOCKCHAIN_INFO_BASE_URL, +}); diff --git a/src/connections/btcRpc.ts b/src/connections/btcRpc.ts new file mode 100644 index 0000000..ea25c55 --- /dev/null +++ b/src/connections/btcRpc.ts @@ -0,0 +1,34 @@ +import axios from "axios"; +import {BTC_RPC_ENDPOINT} from "../common/constants"; + +export const btcRpc = axios.create({ + baseURL: BTC_RPC_ENDPOINT, +}); + +enum BTC_RPC_METHODS { + GET_RAW_TRANSACTION= 'getrawtransaction' +} + +export const fetchBtcRpcData = async (method: BTC_RPC_METHODS, params: any[]) => { + try { + return await btcRpc.post(BTC_RPC_ENDPOINT, { + jsonrpc: "1.0", + id: "curltest", + method, + params, + }) + } catch (e) { + console.error(e); + } +} + + +export const getRawTransaction = async (transactionId: string) => { + try { + const response = await fetchBtcRpcData(BTC_RPC_METHODS.GET_RAW_TRANSACTION, [transactionId, true]); + return {response, error: undefined} + } catch (e) { + return {response: undefined, error: e} + } +} + diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..a0d1487 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,38 @@ +import {INTERVAL_TIME_CONFIG, INTERVAL_TIME_MINING} from "./common/constants"; +import {importMiningData } from "./script/block"; +import {updateHashPower, updateTokenPrice} from "./script/config"; + +async function sleep(ms) { + console.log("sleeping") + return new Promise((resolve, reject) => { + setTimeout( + () => { + console.log("sleep time out") + resolve() + }, ms) + }) +} + +/* + Main Function + */ + +(async () => { + setInterval( + async function (){ + await updateTokenPrice() + await updateHashPower() + }, + parseInt(INTERVAL_TIME_CONFIG) * 1000 + ) + while (true){ + try { + await sleep(parseInt(INTERVAL_TIME_MINING) * 1000) + await importMiningData() + } catch (e) { + console.log(e) + } + } +}) () + + diff --git a/src/script/test.ts b/src/script/block.ts similarity index 56% rename from src/script/test.ts rename to src/script/block.ts index f9c4c15..583d372 100644 --- a/src/script/test.ts +++ b/src/script/block.ts @@ -1,20 +1,13 @@ -// import {importData} from "./mining-importer"; -import {getMinerInfo, getTransactionFromBtcRpc} from "./mining-test"; -import axios from "axios"; -import {graphClient} from "../common/graphql-client"; -import {importBlockInfos} from "./common/importer-block-info"; +import {getMinerInfo, getTransactionFromBtcRpc} from "./mining"; +import {importBlockInfos} from "../services/graphql/importer-block-info"; import { Block_Info_Insert_Input, Commit_Gas_Info_Insert_Input, - Commit_Info_Arr_Rel_Insert_Input, - Commit_Info_Insert_Input, Config_Insert_Input + Commit_Info_Insert_Input, } from "../../graphql/node-types"; -import {block} from "@graphql-codegen/visitor-plugin-common"; import c32 from "c32check"; -import {fetchLatestBlock} from "./common/fetch-latest-block-height"; -import {fetchBurnchainOpsRowid} from "./common/fetch-burnchain-ops-rowid"; -import {fetchLatestTxId} from "./common/fetch-latest-txid"; -import {DELTA_HEIGHT, INTERVAL_TIME_CONFIG, INTERVAL_TIME_MINING} from "../common/constants"; -import {importConfigItem} from "./common/import-config-item"; +import {fetchLatestBlock} from "../services/graphql/fetch-latest-block-height"; +import {fetchBurnchainOpsRowid} from "../services/graphql/fetch-burnchain-ops-rowid"; +import {DELTA_HEIGHT} from "../common/constants"; import fs from "fs" const file = fs.createWriteStream('./log.txt'); let logger = new console.Console(file, file); @@ -23,16 +16,16 @@ let logger = new console.Console(file, file); -async function importMiningData() { +export async function importMiningData() { // find the start_stacks_block_height and start_burn_block_height - let latest_block = await fetchLatestBlock() + const latest_block = await fetchLatestBlock() //console.log(latest_block.block_info[0].stacks_block_height, latest_block.block_info[0].btc_block_height) //let latest_txid_ob = await fetchLatestTxId() //let latest_txid = latest_txid_ob.commit_gas_info.length == 0? "": latest_txid_ob.commit_gas_info[0].commit_btc_tx_id - let latest_txid = "" + const latest_txid = "" // find latest burnchain ops rowid - let latest_rowid_ob = await fetchBurnchainOpsRowid() - let latest_rowid = latest_rowid_ob.config.length === 0 ? 0 : parseInt(latest_rowid_ob.config[0].value) + const latest_rowid_ob = await fetchBurnchainOpsRowid() + const latest_rowid = latest_rowid_ob.config.length === 0 ? 0 : parseInt(latest_rowid_ob.config[0].value) let start_stacks_block_height = latest_block.block_info.length === 0? 0 : latest_block.block_info[0].stacks_block_height let start_btc_block_height = latest_block.block_info.length === 0? 666050 : latest_block.block_info[0].btc_block_height @@ -54,7 +47,6 @@ async function importMiningData() { but block_commits has ${Object.getOwnPropertyNames(miningInfo.block_commits).length} items`); return } else { - // handle blockInfo and commitInfo let rowsToImport : Block_Info_Insert_Input[] = [] for (let k of Object.keys(miningInfo.winner_info)){ @@ -62,7 +54,6 @@ async function importMiningData() { let block_info = miningInfo.winner_info[k] let commit_info = miningInfo.block_commits[k] let blockInfoItem: Block_Info_Insert_Input = {} - // blockInfo // force check to confirm if stacks_block_height is 0 if (block_info.stacks_block_height < start_stacks_block_height || block_info.burn_chain_height < start_btc_block_height){ @@ -116,112 +107,13 @@ async function importMiningData() { } rowsToImport.push(blockInfoItem) } - await importBlockInfos(rowsToImport) } - - - // handle graphql insert - -} - - -async function updateTokenPrice(){ - return new Promise(async function (resolve, reject) { - try { - let stx_usdt_url = "https://api.binance.com/api/v3/ticker/price?symbol=STXUSDT"; - let btc_usdt_url = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"; - const stx_resp = await axios(stx_usdt_url) - const btc_resp = await axios(btc_usdt_url) - console.log(stx_resp.data) - console.log(btc_resp.data) - if (stx_resp.data != undefined && btc_resp.data != undefined){ - let rows :Config_Insert_Input[] = [] - let stx_row : Config_Insert_Input = { - name: "stx_price", - value: stx_resp.data.price, - comment: "stx price" - } - let btc_row : Config_Insert_Input = { - name: "btc_price", - value: btc_resp.data.price, - comment: "btc price" - } - rows.push(stx_row) - rows.push(btc_row) - await importConfigItem(rows) - resolve("finished") - } - } catch (error){ - reject(error) - } - }).catch((error) => { - console.log(error) - }) -} - -async function updateHashPower(){ - return new Promise(async function (resolve, reject) { - try { - let url = "https://api.blockchain.info/stats"; - let res = await axios((url).toString()) - - if (res.data != undefined) { - let hash_rate = (res.data.hash_rate / 1E9).toFixed(2) - console.log(`hash_rate: ${hash_rate}`) - let rows: Config_Insert_Input[] = [] - let hash_rate_row: Config_Insert_Input = { - name: "btc_hashrate", - value: String(hash_rate), - comment: "btc hash rate" - } - rows.push(hash_rate_row) - await importConfigItem(rows) - resolve("finished") - } - } catch (error) { - reject(error) - } - }).catch((error) => { - console.log(error) - }) - } -async function sleep(ms) { - console.log("sleeping") - return new Promise((resolve, reject) => { - setTimeout( - () => { - console.log("sleep time out") - resolve() - }, ms) - }) -} -/* - Main Function - */ - -(async () => { - setInterval( - async function (){ - await updateTokenPrice() - await updateHashPower() - }, - parseInt(INTERVAL_TIME_CONFIG) * 1000 - ) - while (true){ - try { - await sleep(parseInt(INTERVAL_TIME_MINING) * 1000) - await importMiningData() - } catch (e) { - console.log(e) - } - } -}) () diff --git a/src/script/config.ts b/src/script/config.ts new file mode 100644 index 0000000..6cf24f9 --- /dev/null +++ b/src/script/config.ts @@ -0,0 +1,52 @@ +import {getBtcPrice, getStxPrice} from "../connections/binance"; +import {Config_Insert_Input} from "../../graphql/node-types"; +import {importConfigItem} from "../services/graphql/import-config-item"; +import {blockchainInfo} from "../connections/blockchainInfo"; + +export async function updateTokenPrice(){ + try { + const stxPrice = await getStxPrice(); + const btcPrice = await getBtcPrice(); + console.log(stxPrice) + console.log(btcPrice) + if (stxPrice != undefined && btcPrice != undefined){ + let rows :Config_Insert_Input[] = [] + let stx_row : Config_Insert_Input = { + name: "stx_price", + value: stxPrice, + comment: "stx price" + } + let btc_row : Config_Insert_Input = { + name: "btc_price", + value: btcPrice, + comment: "btc price" + } + rows.push(stx_row) + rows.push(btc_row) + await importConfigItem(rows) + } + } catch (error){ + throw (error); + } +} + +export const updateHashPower = async() => { + try { + const res = await blockchainInfo.get('/stats'); + if (res.data != undefined) { + let hash_rate = (res.data.hash_rate / 1E9).toFixed(2) + console.log(`hash_rate: ${hash_rate}`) + let rows: Config_Insert_Input[] = [] + let hash_rate_row: Config_Insert_Input = { + name: "btc_hashrate", + value: String(hash_rate), + comment: "btc hash rate" + } + rows.push(hash_rate_row) + await importConfigItem(rows) + } + } catch (error) { + console.log(error); + throw (error); + } +} diff --git a/src/script/mining-test.ts b/src/script/mining.ts similarity index 92% rename from src/script/mining-test.ts rename to src/script/mining.ts index 3de633b..ed98359 100644 --- a/src/script/mining-test.ts +++ b/src/script/mining.ts @@ -4,32 +4,17 @@ import stacks_transactions from '@blockstack/stacks-transactions' const { getAddressFromPublicKey, TransactionVersion } = stacks_transactions import secp256k1 from 'secp256k1' import c32 from 'c32check' -import {BTC_RPC_ENDPOINT, STACKS_NODE_SQLITE_PATH} from "../common/constants"; -import axios from "axios"; - -async function getRawTransaction(txid){ - let url = BTC_RPC_ENDPOINT - try { - const response = await axios({ - method: 'post', - url: url, - data: { - - "jsonrpc": "1.0", - "id": "curltest", - "method": "getrawtransaction", - "params": [txid, true] +import { STACKS_NODE_SQLITE_PATH} from "../common/constants"; +import {getRawTransaction} from "../connections/btcRpc"; +import { + getBurnChainOps, + getAllBlockCommits, + getAllBlocks, + getLeaderKeys, + getAllBlockHeaders, + getAllPayments +} from "../services/database"; - } - }) - - return {response: response, error: undefined} - } catch (e) { - - return {response: undefined, error: e} - } - -} function getVOUTValue(vout){ let UTXOOutput = 0 @@ -72,9 +57,9 @@ export async function getTransactionFromBtcRpc(txid, btc_address) { UTXOInput += getVOutValueByBtcAddress(UTXOInputOb.response.data.result.vout, btc_address) } - - console.log(`UTXO Gas is ${Math.round(UTXOInput * 1e8 - UTXOOutput * 1e8)}`) - return Math.round(UTXOInput * 1e8 - UTXOOutput * 1e8) + const utxoGas = Math.round(UTXOInput * 1e8 - UTXOOutput * 1e8) + console.log(`UTXO Gas is ${utxoGas}`) + return utxoGas } @@ -112,18 +97,18 @@ export async function getMinerInfo(stacks_block_height, burn_block_height, burnc }) //console.log(`SELECT * FROM burnchain_db_block_ops WHERE rowid > (SELECT rowid FROM burnchain_db_block_ops WHERE txid = ${latest_txid})`) // burnchain queries - const stmt_all_burnchain_ops = burnchain_db.prepare(`SELECT * FROM burnchain_db_block_ops WHERE rowid > (SELECT rowid FROM burnchain_db_block_ops WHERE txid = '')`) + const stmt_all_burnchain_ops = burnchain_db.prepare(getBurnChainOps) // sortition queries - const stmt_all_blocks = sortition_db.prepare(`SELECT * FROM snapshots WHERE block_height > ${burn_block_height} AND block_height <= ${burn_block_height+delta_height} order by block_height desc `) + const stmt_all_blocks = sortition_db.prepare(getAllBlocks(burn_block_height, delta_height)) - const stmt_all_block_commits = sortition_db.prepare(`SELECT * FROM block_commits WHERE block_height > ${burn_block_height} AND block_height <= ${burn_block_height + delta_height} order by block_height`) + const stmt_all_block_commits = sortition_db.prepare(getAllBlockCommits(burn_block_height, delta_height)) - const stmt_all_leader_keys = sortition_db.prepare(`SELECT * FROM leader_keys WHERE block_height > ${burn_block_height} AND block_height <= ${burn_block_height+delta_height}`) + const stmt_all_leader_keys = sortition_db.prepare(getLeaderKeys(burn_block_height, delta_height)) // header queries - const stmt_all_payments = headers_db.prepare(`SELECT * FROM payments WHERE stacks_block_height > ${stacks_block_height} AND stacks_block_height <= ${stacks_block_height+delta_height} order by stacks_block_height`) - const stmt_all_block_headers = headers_db.prepare(`SELECT * FROM block_headers WHERE block_height > ${stacks_block_height} AND block_height <= ${stacks_block_height+delta_height} order by block_height`) + const stmt_all_payments = headers_db.prepare(getAllPayments(stacks_block_height, delta_height)) + const stmt_all_block_headers = headers_db.prepare(getAllBlockHeaders(stacks_block_height, delta_height)) // staging queries const stmt_all_staging_blocks = staging_db.prepare(`SELECT * FROM staging_blocks WHERE height > ${stacks_block_height} AND height <= ${stacks_block_height+delta_height} order by height`) diff --git a/src/script/test.js b/src/script/test.js deleted file mode 100644 index 52a2b28..0000000 --- a/src/script/test.js +++ /dev/null @@ -1,82 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [op[0] & 2, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -exports.__esModule = true; -// import {importData} from "./mining-importer"; -// import {getMinerInfo} from "./mining-test"; -var got = require("got"); -(function () { return __awaiter(void 0, void 0, void 0, function () { - var url, response; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - url = 'http://daemontech2:daemontech2@47.242.123.146:8332/'; - return [4 /*yield*/, got.got.post(url, { - json: { - "jsonrpc": "1.0", - "id": "curltest", - "method": "gettransaction", - "params": ["593c45f20f60b3eabd58303d844fc4c468a1f5393d1a15d1d379a2e3c1cf3693", true] - } - })]; - case 1: - response = _a.sent(); - console.log(response); - return [2 /*return*/]; - } - }); -}); })(); -// interface testInter { -// name : string -// value : string -// } -// -// function hi(param: testInter){ -// console.log(param.name) -// } -//let a: testInter = {name: "gavin", value: "hello"} -// @ts-ignore -//hi(a) -/* -(async function b(){ - await afunc() -} ()) -∂ - */ -// getMinerInfo(44980, 718537).then(r => { -// console.log(r) -// }) diff --git a/src/services/database/burnchain.ts b/src/services/database/burnchain.ts new file mode 100644 index 0000000..42e2451 --- /dev/null +++ b/src/services/database/burnchain.ts @@ -0,0 +1 @@ +export const getBurnChainOps = `SELECT * FROM burnchain_db_block_ops WHERE rowid > (SELECT rowid FROM burnchain_db_block_ops WHERE txid = '')` diff --git a/src/services/database/header.ts b/src/services/database/header.ts new file mode 100644 index 0000000..602b076 --- /dev/null +++ b/src/services/database/header.ts @@ -0,0 +1,3 @@ +export const getAllPayments = (stacksBlockHeight: number, deltaHeight: number) => `SELECT * FROM payments WHERE stacks_block_height > ${stacksBlockHeight} AND stacks_block_height <= ${stacksBlockHeight+deltaHeight} order by stacks_block_height`; + +export const getAllBlockHeaders = (stacksBlockHeight: number, deltaHeight: number) => `SELECT * FROM block_headers WHERE block_height > ${stacksBlockHeight} AND block_height <= ${stacksBlockHeight+deltaHeight} order by block_height`; diff --git a/src/services/database/index.ts b/src/services/database/index.ts new file mode 100644 index 0000000..fcd53e9 --- /dev/null +++ b/src/services/database/index.ts @@ -0,0 +1,3 @@ +export * from './burnchain'; +export * from './header'; +export * from './sortition'; diff --git a/src/services/database/sortition.ts b/src/services/database/sortition.ts new file mode 100644 index 0000000..0ce25e1 --- /dev/null +++ b/src/services/database/sortition.ts @@ -0,0 +1,5 @@ +export const getAllBlocks = (burnBlockHeight: number, deltaHeight: number) => `SELECT * FROM snapshots WHERE block_height > ${burnBlockHeight} AND block_height <= ${burnBlockHeight+deltaHeight} order by block_height desc`; + +export const getLeaderKeys = (burnBlockHeight: number, deltaHeight: number) => `SELECT * FROM leader_keys WHERE block_height > ${burnBlockHeight} AND block_height <= ${burnBlockHeight+deltaHeight}` + +export const getAllBlockCommits= (burnBlockHeight: number, deltaHeight: number) => `SELECT * FROM block_commits WHERE block_height > ${burnBlockHeight} AND block_height <= ${burnBlockHeight + deltaHeight} order by block_height`; diff --git a/src/script/common/fetch-burnchain-ops-rowid.ts b/src/services/graphql/fetch-burnchain-ops-rowid.ts similarity index 99% rename from src/script/common/fetch-burnchain-ops-rowid.ts rename to src/services/graphql/fetch-burnchain-ops-rowid.ts index 8566f77..64c71c7 100644 --- a/src/script/common/fetch-burnchain-ops-rowid.ts +++ b/src/services/graphql/fetch-burnchain-ops-rowid.ts @@ -3,4 +3,4 @@ import {graphClient} from "../../common/graphql-client"; export async function fetchBurnchainOpsRowid() { return await graphClient.queryBurnchainOpsRowid(); -} \ No newline at end of file +} diff --git a/src/script/common/fetch-latest-block-height.ts b/src/services/graphql/fetch-latest-block-height.ts similarity index 98% rename from src/script/common/fetch-latest-block-height.ts rename to src/services/graphql/fetch-latest-block-height.ts index 1c5eeaf..0fedc1f 100644 --- a/src/script/common/fetch-latest-block-height.ts +++ b/src/services/graphql/fetch-latest-block-height.ts @@ -4,4 +4,4 @@ import {graphClient} from "../../common/graphql-client"; export async function fetchLatestBlock() { return await graphClient.queryLatestBlockHeight(); -} \ No newline at end of file +} diff --git a/src/script/common/fetch-latest-txid.ts b/src/services/graphql/fetch-latest-txid.ts similarity index 98% rename from src/script/common/fetch-latest-txid.ts rename to src/services/graphql/fetch-latest-txid.ts index 7426706..a5813af 100644 --- a/src/script/common/fetch-latest-txid.ts +++ b/src/services/graphql/fetch-latest-txid.ts @@ -4,4 +4,4 @@ import {graphClient} from "../../common/graphql-client"; export async function fetchLatestTxId() { return await graphClient.queryLatestTxId(); -} \ No newline at end of file +} diff --git a/src/script/common/import-config-item.ts b/src/services/graphql/import-config-item.ts similarity index 99% rename from src/script/common/import-config-item.ts rename to src/services/graphql/import-config-item.ts index a916000..86550d4 100644 --- a/src/script/common/import-config-item.ts +++ b/src/services/graphql/import-config-item.ts @@ -8,4 +8,4 @@ export async function importConfigItem(rows: Config_Insert_Input[]) { console.log(`inserting ${rows.length} config item`); await graphClient.updateConfigItem({ rows }); } -} \ No newline at end of file +} diff --git a/src/script/common/importer-block-info.ts b/src/services/graphql/importer-block-info.ts similarity index 99% rename from src/script/common/importer-block-info.ts rename to src/services/graphql/importer-block-info.ts index e8f7934..2b6d23a 100644 --- a/src/script/common/importer-block-info.ts +++ b/src/services/graphql/importer-block-info.ts @@ -11,4 +11,4 @@ export async function importBlockInfos(rows: Block_Info_Insert_Input[]) { console.log(`inserting ${rows.length} block info items`); await graphClient.insertBlockInfos({ rows }); } -} \ No newline at end of file +} From 0ab0533f8fbab0c98e908b6e74fbe72743adb351 Mon Sep 17 00:00:00 2001 From: Mahmoud Aboelenein Date: Mon, 9 May 2022 18:39:58 +0200 Subject: [PATCH 2/4] fix: Change price fetching requests to get instead of post --- src/connections/binance.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connections/binance.ts b/src/connections/binance.ts index 83f3da6..e3bc0d9 100644 --- a/src/connections/binance.ts +++ b/src/connections/binance.ts @@ -9,7 +9,7 @@ const tickerPriceUrl = (coinPair: string) => `/ticker/price?symbol=${coinPair}`; export const getStxPrice = async () => { try { - const response = await binanceApi.post(tickerPriceUrl('STXUSDT')); + const response = await binanceApi.get(tickerPriceUrl('STXUSDT')); return response.data?.price; } catch (e) { throw Error(`Cant Fetch latest Stx Price ${e}`); @@ -17,7 +17,7 @@ export const getStxPrice = async () => { } export const getBtcPrice = async () => { try { - const response = await binanceApi.post(tickerPriceUrl('BTCUSDT')); + const response = await binanceApi.get(tickerPriceUrl('BTCUSDT')); return response.data?.price; } catch (e) { throw Error(`Cant Fetch latest BTC Price ${e}`); From 1695824bb496f30c7d37b0995053f6bb316fc77e Mon Sep 17 00:00:00 2001 From: Mahmoud Aboelenein Date: Mon, 9 May 2022 18:40:42 +0200 Subject: [PATCH 3/4] Replace Interval functions with cron jobs for running the script --- src/index.ts | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/index.ts b/src/index.ts index a0d1487..244954c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,38 +1,19 @@ -import {INTERVAL_TIME_CONFIG, INTERVAL_TIME_MINING} from "./common/constants"; import {importMiningData } from "./script/block"; import {updateHashPower, updateTokenPrice} from "./script/config"; +import cron from 'node-cron'; -async function sleep(ms) { - console.log("sleeping") - return new Promise((resolve, reject) => { - setTimeout( - () => { - console.log("sleep time out") - resolve() - }, ms) - }) -} - -/* - Main Function - */ - -(async () => { - setInterval( - async function (){ - await updateTokenPrice() - await updateHashPower() - }, - parseInt(INTERVAL_TIME_CONFIG) * 1000 - ) - while (true){ - try { - await sleep(parseInt(INTERVAL_TIME_MINING) * 1000) - await importMiningData() - } catch (e) { - console.log(e) - } - } -}) () + +cron.schedule('* * * * *', async function() { + console.log('here'); + await updateTokenPrice() + await updateHashPower(); +}); + + + + +cron.schedule('0 */5 * * *', async function() { + await importMiningData(); +}); From 712519bd28610860f4bdee3dd509a0c5baae8e69 Mon Sep 17 00:00:00 2001 From: Mahmoud Aboelenein Date: Mon, 20 Jun 2022 11:18:13 +0200 Subject: [PATCH 4/4] script runner upgrade src --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a63790c..af0533b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "", "scripts": { - "test": "node --max_old_space_size=4096 -r ts-eager/register ./src/script/test.ts", + "start": "node --max_old_space_size=4096 -r ts-eager/register ./src/index.ts", "generate-graphql": "graphql-codegen --config codegen.js" }, "author": "", @@ -34,6 +34,7 @@ "got": "^12.0.1", "graphql": "^16.2.0", "js-sha512": "^0.8.0", + "node-cron": "^3.0.0", "secp256k1": "^4.0.3" } }