Skip to content

Commit

Permalink
Revert "Merge pull request #6 from Gearbox-protocol/points"
Browse files Browse the repository at this point in the history
This reverts commit 31d1bf5, reversing
changes made to 606c37e.
  • Loading branch information
essserrr committed Jan 9, 2025
1 parent 31d1bf5 commit 25ef731
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 452 deletions.
88 changes: 30 additions & 58 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { isAddress } from "viem";

import type { GearAPY } from "./apy";
import type { ApyDetails, Fetcher } from "./fetcher";
import type { PointsInfo } from "./points/constants";
import { isSupportedNetwork, toJSONWithBigint } from "./utils";
import { isSupportedNetwork } from "./utils";

interface Response {
status: string;
Expand All @@ -18,7 +17,6 @@ interface OutputDetails {
symbol: string;
rewards: {
apy: Array<ApyDetails>;
points: Array<PointsInfo>;
};
}

Expand All @@ -33,22 +31,23 @@ export async function getByChainAndToken(req: any, res: any, fetcher: Fetcher) {
if (!checkResp(isTokenValid, res)) {
return;
}

const a = fetcher.cache[chainId]?.apyList?.[tokenAddress as Address];
const p = fetcher.cache[chainId]?.pointsList?.[tokenAddress as Address];

const data: OutputDetails = {
chainId: chainId,
address: tokenAddress.toLowerCase(),
symbol: a?.symbol || p?.symbol || "",
symbol: "",
rewards: {
apy: a.apys || [],
points: p ? [p] : [],
apy: [],
},
};

const d = fetcher.cache[chainId]?.tokens?.[tokenAddress as Address];
if (d) {
data.rewards.apy = d.apys;
data.symbol = d.symbol;
}

res.set({ "Content-Type": "application/json" });
res.send(toJSONWithBigint({ data: data, status: "ok" } as Response));
res.send(JSON.stringify({ data: data, status: "ok" } as Response));
//
}

Expand All @@ -57,45 +56,21 @@ export async function getAll(req: any, res: any, fetcher: Fetcher) {
if (!checkResp(isChainIdValid, res)) {
return;
}
const data: Array<OutputDetails> = [];

const data = Object.entries(fetcher.cache[chainId]?.apyList || {}).reduce<
Record<Address, OutputDetails>
>((acc, [t, a]) => {
acc[t as Address] = {
Object.entries(fetcher.cache[chainId]?.tokens).forEach(([token, apy]) => {
data.push({
chainId: chainId,
address: t,
symbol: a.symbol,
address: token,
symbol: apy.symbol,
rewards: {
apy: a.apys,
points: [],
apy: apy.apys,
},
};

return acc;
}, {});

Object.entries(fetcher.cache[chainId]?.pointsList || {}).forEach(([t, p]) => {
const token = t as Address;

if (data[token]) {
data[token].rewards.points.push(p);
} else {
data[token] = {
chainId: chainId,
address: t,
symbol: p.symbol,
rewards: {
apy: [],
points: [p],
},
};
}
});
});

res.set({ "Content-Type": "application/json" });
res.send(
toJSONWithBigint({ data: Object.values(data), status: "ok" } as Response),
);
res.send(JSON.stringify({ data: data, status: "ok" } as Response));
}

export async function getRewardList(req: any, res: any, fetcher: Fetcher) {
Expand All @@ -108,30 +83,27 @@ export async function getRewardList(req: any, res: any, fetcher: Fetcher) {
res,
);
}
const [isTokenList, tokenList] = checkTokenList(toJSONWithBigint(req.body));
const [isTokenList, tokenList] = checkTokenList(JSON.stringify(req.body));
if (!checkResp(isTokenList, res)) {
return;
}

const data: Array<OutputDetails> = [];
for (const t of tokenList) {
const a = fetcher.cache[t.chain_id]?.apyList?.[t.token_address as Address];
const p =
fetcher.cache[t.chain_id]?.pointsList?.[t.token_address as Address];

for (const entry of tokenList) {
const apys =
fetcher.cache[entry.chain_id]?.tokens?.[entry.token_address as Address];
data.push({
chainId: t.chain_id,
address: t.token_address.toLowerCase(),
symbol: a.symbol,
chainId: entry.chain_id,
address: entry.token_address.toLowerCase(),
symbol: apys.symbol,
rewards: {
apy: a.apys || [],
points: p ? [p] : [],
apy: apys.apys,
},
});
}

res.set({ "Content-Type": "application/json" });
res.send(toJSONWithBigint({ data: data, status: "ok" } as Response));
res.send(JSON.stringify({ data: data, status: "ok" } as Response));
}

export async function getGearAPY(req: any, res: any, fetcher: Fetcher) {
Expand All @@ -142,7 +114,7 @@ export async function getGearAPY(req: any, res: any, fetcher: Fetcher) {

res.set({ "Content-Type": "application/json" });
res.send(
toJSONWithBigint({
JSON.stringify({
data: fetcher.cache[chainId]?.gear,
status: "ok",
} as Response),
Expand Down Expand Up @@ -185,13 +157,13 @@ function checkTokenAddress(data: any): [Response, string] {
"",
];
}
return [{ status: "ok" }, notUndefined.toString()];
return [{ status: "ok" }, (notUndefined as Address).toString()];
}

export function checkResp(res: Response, out: any): boolean {
if (res.status === "error") {
out.set({ "Content-Type": "application/json" });
out.send(toJSONWithBigint(res));
out.send(JSON.stringify(res));
return false;
}
return true;
Expand Down
76 changes: 26 additions & 50 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@ import {
getAPYYearn,
getGearAPY,
} from "./apy";
import { getPoints } from "./points";
import type { PointsInfo } from "./points/constants";
import type { Apy, APYResult, NetworkType, TokenAPY } from "./utils";
import { getChainId, supportedChains } from "./utils";

export type ApyDetails = Apy & { lastUpdated: string };
type TokenDetails = TokenAPY<ApyDetails>;

interface NetworkState {
apyList: Record<Address, TokenDetails>;
pointsList: Record<Address, PointsInfo>;
tokens: Record<Address, TokenDetails>;
gear: GearAPY;
}

function log(
network: NetworkType,
allProtocolAPYs: Array<PromiseSettledResult<APYResult>>,
pointsList: PromiseSettledResult<Record<Address, PointsInfo>>,
gearAPY: PromiseSettledResult<GearAPY>,
) {
const list = allProtocolAPYs.map(apyRes => {
Expand All @@ -56,16 +52,6 @@ function log(
} else {
console.log(`Gear error: ${gearAPY.reason}`);
}

if (pointsList.status === "fulfilled") {
console.log(
`Fetched points for ${Object.values(pointsList.value)
.map(p => p.symbol)
.join(", ")} for ${network}`,
);
} else {
console.log(`Points error: ${pointsList.reason}`);
}
}

export class Fetcher {
Expand All @@ -76,11 +62,8 @@ export class Fetcher {
}

private async getNetworkState(network: NetworkType): Promise<NetworkState> {
const [gearAPY, points, ...allProtocolAPYs] = await Promise.allSettled([
const [gearAPY, ...allProtocolAPYs] = await Promise.allSettled([
getGearAPY(network),

getPoints(network),

getAPYCurve(network),
getAPYEthena(network),
getAPYLama(network),
Expand All @@ -89,46 +72,39 @@ export class Fetcher {
getAPYYearn(network),
getAPYConstant(network),
]);
log(network, allProtocolAPYs, points, gearAPY);
log(network, allProtocolAPYs, gearAPY);

const result: Record<Address, TokenDetails> = {};
const time = moment().utc().format();

const apyList = allProtocolAPYs.reduce<Record<Address, TokenDetails>>(
(acc, apyRes) => {
if (apyRes.status === "fulfilled") {
Object.entries(apyRes.value).forEach(([addr, tokenAPY]) => {
const address = addr.toLowerCase() as Address;

const apyList = tokenAPY?.apys.map(
({ reward, ...rest }): ApyDetails => ({
...rest,
lastUpdated: time,
reward: reward.toLowerCase() as Address,
}),
);

acc[address] = {
...tokenAPY,
address,
apys: [...(acc[address]?.apys || []), ...apyList],
};
});
}

return acc;
},
{},
);

const pointsList = points.status === "fulfilled" ? points.value : {};
allProtocolAPYs.forEach(apyRes => {
if (apyRes.status === "fulfilled") {
Object.entries(apyRes.value).forEach(([addr, tokenAPY]) => {
const address = addr.toLowerCase() as Address;

const apyList = tokenAPY?.apys.map(
({ reward, ...rest }): ApyDetails => ({
...rest,
lastUpdated: time,
reward: reward.toLowerCase() as Address,
}),
);

result[address] = {
...tokenAPY,
address,
apys: [...(result[address]?.apys || []), ...apyList],
};
});
}
});

return {
gear:
gearAPY.status === "fulfilled"
? gearAPY.value
: { base: 0, crv: 0, gear: 0 },
apyList,
pointsList,
tokens: result,
};
}

Expand Down
Loading

0 comments on commit 25ef731

Please sign in to comment.