Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/adaptors/superstate-uscc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,33 @@ const apy = async () => {
tvlPlume = tvlPlume.output / 1e6;

const timestampNow = Math.floor(Date.now() / 1000);
const timestampYesterday = timestampNow - 86400;
const timestamp7daysAgo = timestampNow - 86400 * 7;
const timestamp30daysAgo = timestampNow - 86400 * 30;

const blockNow = (
await axios.get(`https://coins.llama.fi/block/ethereum/${timestampNow}`)
).data.height;

const blockYesterday = (
await axios.get(`https://coins.llama.fi/block/ethereum/${timestampYesterday}`)
const block7daysAgo = (
await axios.get(`https://coins.llama.fi/block/ethereum/${timestamp7daysAgo}`)
).data.height;

let exchangeRateYesterday = await sdk.api.abi.call({
const block30daysAgo = (
await axios.get(`https://coins.llama.fi/block/ethereum/${timestamp30daysAgo}`)
).data.height;

let exchangeRate7daysAgo = await sdk.api.abi.call({
target: USCC_ORACLE,
chain: 'ethereum',
abi: 'uint256:latestAnswer',
block: block7daysAgo,
});

let exchangeRate30daysAgo = await sdk.api.abi.call({
target: USCC_ORACLE,
chain: 'ethereum',
abi: 'uint256:latestAnswer',
block: blockYesterday,
block: block30daysAgo,
});

let exchangeRateToday = await sdk.api.abi.call({
Expand All @@ -46,34 +58,40 @@ const apy = async () => {
});

exchangeRateToday = exchangeRateToday.output / 1e6;
exchangeRateYesterday = exchangeRateYesterday.output / 1e6;
exchangeRate7daysAgo = exchangeRate7daysAgo.output / 1e6;
exchangeRate30daysAgo = exchangeRate30daysAgo.output / 1e6;

const apr = ((exchangeRateToday - exchangeRateYesterday) / exchangeRateYesterday) *
365 * 100;
const apr7d = ((exchangeRateToday - exchangeRate7daysAgo) / exchangeRate7daysAgo) *
(365 / 7) * 100;
const apr30d = ((exchangeRateToday - exchangeRate30daysAgo) / exchangeRate30daysAgo) *
(365 / 30) * 100;
Comment on lines +64 to +67
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Guard against zero/invalid historic rates to prevent Infinity/NaN APRs.
If the oracle returns 0 or a non-finite value (e.g., during downtime), the current math yields spikes or NaNs—exactly what this PR aims to prevent. Add a safety check and return a safe fallback (e.g., null or 0) aligned with downstream expectations.

✅ Proposed safety guard
-    const apr7d = ((exchangeRateToday - exchangeRate7daysAgo) / exchangeRate7daysAgo) *
-        (365 / 7) * 100;
-    const apr30d = ((exchangeRateToday - exchangeRate30daysAgo) / exchangeRate30daysAgo) *
-        (365 / 30) * 100;
+    const apr7d =
+        exchangeRate7daysAgo > 0
+            ? ((exchangeRateToday - exchangeRate7daysAgo) / exchangeRate7daysAgo) *
+              (365 / 7) * 100
+            : null;
+    const apr30d =
+        exchangeRate30daysAgo > 0
+            ? ((exchangeRateToday - exchangeRate30daysAgo) / exchangeRate30daysAgo) *
+              (365 / 30) * 100
+            : null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const apr7d = ((exchangeRateToday - exchangeRate7daysAgo) / exchangeRate7daysAgo) *
(365 / 7) * 100;
const apr30d = ((exchangeRateToday - exchangeRate30daysAgo) / exchangeRate30daysAgo) *
(365 / 30) * 100;
const apr7d =
exchangeRate7daysAgo > 0
? ((exchangeRateToday - exchangeRate7daysAgo) / exchangeRate7daysAgo) *
(365 / 7) * 100
: null;
const apr30d =
exchangeRate30daysAgo > 0
? ((exchangeRateToday - exchangeRate30daysAgo) / exchangeRate30daysAgo) *
(365 / 30) * 100
: null;
🤖 Prompt for AI Agents
In `@src/adaptors/superstate-uscc/index.js` around lines 64 - 67, The APR
calculations for apr7d and apr30d can produce Infinity/NaN when
exchangeRate7daysAgo or exchangeRate30daysAgo are 0 or non-finite; update the
logic in the apr7d/apr30d computation to first validate the historic rates
(exchangeRate7daysAgo and exchangeRate30daysAgo) using Number.isFinite(...) and
!== 0, and if invalid return the safe fallback expected downstream (choose null
or 0 consistently), otherwise compute the APR as before; ensure the same guard
is applied to both apr7d and apr30d so neither division can produce
Infinity/NaN.


return [
{
pool: `${USCC['ethereum']}`,
chain: 'ethereum',
project,
symbol,
apyBase: apr,
apyBase: apr30d,
apyBase7d: apr7d,
tvlUsd: tvlEth * exchangeRateToday,
},
{
pool: `${USCC['plume_mainnet']}`,
chain: 'plume_mainnet',
project,
symbol,
apyBase: apr,
apyBase: apr30d,
apyBase7d: apr7d,
tvlUsd: tvlPlume * exchangeRateToday,
},
{
pool: `${USCC['solana']}`,
chain: 'solana',
project,
symbol,
apyBase: apr,
apyBase: apr30d,
apyBase7d: apr7d,
tvlUsd: tvlSol * exchangeRateToday,
},
];
Expand Down
Loading