From c9f33f12c1525890bd2808a443e2e7dd91a87f8f Mon Sep 17 00:00:00 2001 From: Nico Arqueros Date: Wed, 24 Feb 2021 16:35:57 -0600 Subject: [PATCH] working --- src/services/cardanoWallet.ts | 20 +++++++++++-- src/services/poolInfo.ts | 56 +++++++++++++++++++++++++---------- tests/getPoolInfo.test.ts | 4 +-- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/services/cardanoWallet.ts b/src/services/cardanoWallet.ts index fec392f0..e5306d51 100644 --- a/src/services/cardanoWallet.ts +++ b/src/services/cardanoWallet.ts @@ -3,9 +3,14 @@ import { UtilEither } from "../utils"; import { Pool } from "pg"; import { Request, Response } from "express"; import * as utils from "../utils"; +import axios from "axios"; +import config from "config"; +import {latestMetadataQuery, smashPoolLookUp} from "./poolInfo"; + +const smashEndpoint: string = config.get("server.smashEndpoint"); const poolByRewards = ` - select * + select pool_id, cost, margin, pledge, saturation, non_myopic_member_rewards::int, produced_blocks::int, relative_stake from cardano_wallet order by non_myopic_member_rewards::int desc limit $1 @@ -14,6 +19,7 @@ const poolByRewards = ` export interface CardanoWalletPool { pool_id: string, + pool_info: any, cost: string, margin: string, pledge: string, @@ -50,7 +56,17 @@ export const handleGetCardanoWalletPools = (pool: Pool) => async (req: Request, switch (result.kind) { case "ok": { - res.send(result); + const promisesWithPoolInfo = result.value.map(async (walletPoolInfo) => { + const pool_info = await smashPoolLookUp(pool, walletPoolInfo.pool_id) + return { + ...walletPoolInfo, + pool_info: pool_info.smashInfo, + } + }) + + const respWithPoolInfo = await Promise.all(promisesWithPoolInfo); + + res.send(respWithPoolInfo); break; } case "error": { diff --git a/src/services/poolInfo.ts b/src/services/poolInfo.ts index e2d5093b..7a9132c3 100644 --- a/src/services/poolInfo.ts +++ b/src/services/poolInfo.ts @@ -51,6 +51,42 @@ export const poolHistoryQuery = ` and ("jsType" = 'PoolRegistration' or "jsType" = 'PoolRetirement'); `; +export interface SmashLookUpResponse { + metadataHash: string | null, + smashInfo: any, +} + +export const smashPoolLookUp = async (p: Pool, hash: string): Promise => { + const metadataHashResp = await p.query(latestMetadataQuery, [hash]); + if (metadataHashResp.rows.length === 0) { + return { + metadataHash: null, + smashInfo: null, + }; + } + + const metadataHash = metadataHashResp.rows[0].metadata_hash; + + try { + const endpointResponse = await axios.get(`${smashEndpoint}${hash}/${metadataHash}`); + if(endpointResponse.status === 200) { + return { + metadataHash: metadataHash, + smashInfo: endpointResponse.data, + }; + } else { + console.log(`SMASH did not respond to user submitted hash: ${hash}`); + } + } catch(e) { + console.log(`SMASH did not respond with hash ${hash}, giving error ${e}`); + } + + return { + metadataHash: metadataHash, + smashInfo: {}, + }; +} + export const handlePoolInfo = (p: Pool) => async (req: Request, res: Response): Promise => { if(!req.body.poolIds) throw new Error ("No poolIds in body"); @@ -65,27 +101,15 @@ export const handlePoolInfo = (p: Pool) => async (req: Request, res: Response): if(hash.length !== 56){ throw new Error(`Received invalid pool id: ${hash}`); } - const metadataHashResp = await p.query(latestMetadataQuery, [hash]); - if (metadataHashResp.rows.length === 0) { + + const smashPoolResponse = await smashPoolLookUp(p, hash) + if (smashPoolResponse.metadataHash == null) { ret[hash] = null; continue; } - const metadataHash = metadataHashResp.rows[0].metadata_hash; - - let info = {}; - try { - const endpointResponse = await axios.get(`${smashEndpoint}${hash}/${metadataHash}`); - if(endpointResponse.status === 200){ - info = endpointResponse.data; - }else{ - console.log(`SMASH did not respond to user submitted hash: ${hash}`); - } - } catch(e) { - console.log(`SMASH did not respond with hash ${hash}, giving error ${e}`); - } + const info = smashPoolResponse.smashInfo; const dbHistory = await p.query(poolHistoryQuery, [hash]); - const history = dbHistory.rows.map( (row: any) => ({ epoch: row.epoch_no , slot: row.epoch_slot_no diff --git a/tests/getPoolInfo.test.ts b/tests/getPoolInfo.test.ts index 679f9484..4b31823c 100644 --- a/tests/getPoolInfo.test.ts +++ b/tests/getPoolInfo.test.ts @@ -66,7 +66,7 @@ const stakhanoviteHistorySuffix = [{ "cost": "340000000", "margin": 0.019, "rewardAccount": "e10af10f4a5d365af01f0ca7651713a8a073263b61bbd1f69623097bd7", - "poolOwners": ["0a03ee791abf663e98b81661dadd72420f29bb6960ca0a676e75dd70", "7dfe98a743499f7a67ab2f9771e683d2e9fa1a53b4632aa7e1df339f", "e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1"], + "poolOwners": ["e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1", "7dfe98a743499f7a67ab2f9771e683d2e9fa1a53b4632aa7e1df339f", "0a03ee791abf663e98b81661dadd72420f29bb6960ca0a676e75dd70"], "relays": [{ "ipv4": null, "ipv6": null, @@ -95,7 +95,7 @@ const stakhanoviteHistorySuffix = [{ "cost": "340000000", "margin": 0.019, "rewardAccount": "e10af10f4a5d365af01f0ca7651713a8a073263b61bbd1f69623097bd7", - "poolOwners": ["3e04ddd9d0a3b383ff5ee2e813060b337ad2228bb51bab6dc6d843fa", "e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1"], + "poolOwners": ["e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1", "3e04ddd9d0a3b383ff5ee2e813060b337ad2228bb51bab6dc6d843fa"], "relays": [{ "ipv4": null, "ipv6": null,