Skip to content

Commit

Permalink
Removing Ogmios
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Nov 30, 2021
1 parent 743cec1 commit e56683f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 173 deletions.
2 changes: 0 additions & 2 deletions config/default.ts
Expand Up @@ -13,8 +13,6 @@ export default {
smashEndpoint: process.env.SMASH_ENDPOINT || "https://smash.yoroiwallet.com/api/v1/metadata/",
port: process.env.PORT || 8082,
txsHashesRequestLimit: 150,
ogmiosAddress: process.env.OGMIOS_ADDRESS || "ogmios.waw.emurgo-rnd.com",
ogmiosPort: process.env.OGMIOS_PORT || 1338
},
safeBlockDifference: process.env.SAFE_BLOCK_DIFFERENCE || "10"
};
88 changes: 1 addition & 87 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -20,8 +20,6 @@
}
},
"dependencies": {
"@cardano-ogmios/client": "^4.0.0",
"@cardano-ogmios/schema": "^4.0.0",
"@emurgo/cardano-serialization-lib-nodejs": "^4.2.0",
"axios": "^0.21.1",
"bech32": "^1.1.4",
Expand Down
86 changes: 4 additions & 82 deletions src/services/accountState.ts
@@ -1,15 +1,9 @@
import { assertNever, validateAddressesReq } from "../utils";
import {createInteractionContext, InteractionContext} from "@cardano-ogmios/client";
import {assertNever, validateAddressesReq} from "../utils";
import config from "config";
import { Pool } from "pg";
import { Request, Response } from "express";
import {DelegationsAndRewardsByAccounts} from "@cardano-ogmios/schema";
import { Query } from "@cardano-ogmios/client/dist/StateQuery";
import { Ogmios } from "@cardano-ogmios/schema";
import {Pool} from "pg";
import {Request, Response} from "express";

const addrReqLimit:number = config.get("server.addressRequestLimit");
const ogmiosAddress:string = config.get("server.ogmiosAddress");
const ogmiosPort:number = config.get("server.ogmiosPort");

const accountRewardsQuery = `
select stake_address.hash_raw as "stakeAddress"
Expand Down Expand Up @@ -60,49 +54,6 @@ interface Dictionary<T> {
[key: string]: T;
}

type OgmiosRes = { [k: string]: number };
type OgmiosErr = { err: string };
type OgmiosReturn = OgmiosRes | OgmiosErr;

const OGMIOS_CONTEXT: InteractionContext[] = [];

const getRewardStateFromOgmios = async (addresses: string[]): Promise<OgmiosReturn> => {
try {
const delegationsAndRewards = addresses.map(x => x.substring(2));
if (OGMIOS_CONTEXT[0] == null) {
OGMIOS_CONTEXT[0] = await createInteractionContext(
console.error,
() => console.log("closed."),
{connection: {host: ogmiosAddress, port: ogmiosPort}}
);
}
const ogmiosResult = await Query<
Ogmios["Query"],
Ogmios["QueryResponse[delegationsAndRewards]"],
DelegationsAndRewardsByAccounts
>({
methodName: "Query",
args: { query: { delegationsAndRewards } }
}, {
handler: (response, resolve, reject) => {
const result = response.result;
console.log(`[getRewardStateFromOgmios][Query] Result: ${JSON.stringify(result)}`);
if (typeof result === "object" && result.eraMismatch == null) {
resolve(result as DelegationsAndRewardsByAccounts);
}
reject(`Unexpected Ogmios result: ${JSON.stringify(result)}`);
}
}, OGMIOS_CONTEXT[0]);
return addresses.reduce((res: OgmiosRes, addr) => {
res[addr] = ogmiosResult[addr.substring(2)]?.rewards ?? 0;
return res;
}, {});
} catch (e) {
console.error("Failed to getRewardStateFromOgmios2!", e);
return { err: String(e) };
}
};

const getAccountStateFromDB = async (pool: Pool, addresses: string[]): Promise<Dictionary<RewardInfo|null>> => {
const ret : Dictionary<RewardInfo|null> = {};
const rewards = await pool.query(accountRewardsQuery, [addresses]);
Expand All @@ -122,35 +73,6 @@ const getAccountStateFromDB = async (pool: Pool, addresses: string[]): Promise<D
return ret;
};

const askAccountRewards = async (pool: Pool, addresses: string[]): Promise<Dictionary<RewardInfo|null>> => {
const ogmiosPromise = getRewardStateFromOgmios(addresses)
.catch(e => ({ err: String(e) }));
const dbPromise = getAccountStateFromDB(pool, addresses);

const [ogmiosResult, dbResult]: [OgmiosReturn, Dictionary<RewardInfo|null>]
= await Promise.all([ogmiosPromise, dbPromise]);

console.log("dbResult:", dbResult);
console.log("ogmiosResult:", ogmiosResult);

if (ogmiosResult.err == null) {
for (const a of addresses) {
const dbResultElement = dbResult[a];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const ogResultNumber = ogmiosResult[a] ?? 0;
if (dbResultElement != null) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
dbResultElement.remainingAmountDB = dbResultElement.remainingAmount;
dbResultElement.remainingAmount = String(ogResultNumber);
}
}
}

return dbResult;
};

export const handleGetAccountState = (pool: Pool) => async (req: Request, res:Response): Promise<void> => {
if(!req.body || !(req.body.addresses?.length > 0)) {
throw new Error("no addresses.");
Expand All @@ -159,7 +81,7 @@ export const handleGetAccountState = (pool: Pool) => async (req: Request, res:Re
const verifiedAddrs = validateAddressesReq(addrReqLimit, req.body.addresses);
switch(verifiedAddrs.kind){
case "ok": {
const accountState = await askAccountRewards(pool, verifiedAddrs.value);
const accountState = await getAccountStateFromDB(pool, verifiedAddrs.value);
res.send(accountState);
return;
}
Expand Down

0 comments on commit e56683f

Please sign in to comment.