Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
nicarq committed Feb 24, 2021
1 parent c9384fd commit c9f33f1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
20 changes: 18 additions & 2 deletions src/services/cardanoWallet.ts
Expand Up @@ -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
Expand All @@ -14,6 +19,7 @@ const poolByRewards = `

export interface CardanoWalletPool {
pool_id: string,
pool_info: any,
cost: string,
margin: string,
pledge: string,
Expand Down Expand Up @@ -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": {
Expand Down
56 changes: 40 additions & 16 deletions src/services/poolInfo.ts
Expand Up @@ -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<SmashLookUpResponse> => {
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<void> => {
if(!req.body.poolIds)
throw new Error ("No poolIds in body");
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/getPoolInfo.test.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -95,7 +95,7 @@ const stakhanoviteHistorySuffix = [{
"cost": "340000000",
"margin": 0.019,
"rewardAccount": "e10af10f4a5d365af01f0ca7651713a8a073263b61bbd1f69623097bd7",
"poolOwners": ["3e04ddd9d0a3b383ff5ee2e813060b337ad2228bb51bab6dc6d843fa", "e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1"],
"poolOwners": ["e9aba14ed15240e855f5b62d28f0e5f913cf9c289d3cbc5d6016c1b1", "3e04ddd9d0a3b383ff5ee2e813060b337ad2228bb51bab6dc6d843fa"],
"relays": [{
"ipv4": null,
"ipv6": null,
Expand Down

0 comments on commit c9f33f1

Please sign in to comment.