Skip to content

Commit

Permalink
- Explicitly caching streams data (cap, balance).
Browse files Browse the repository at this point in the history
- Working on the right way to invalidate it
  • Loading branch information
supriyaamisshra committed Feb 26, 2022
1 parent 7fe20ab commit 65d4ef2
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions packages/react-app/src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,41 @@ import { SimpleStreamABI } from "../contracts/external_ABI";
import { useHistory } from "react-router";
import { Link } from "react-router-dom";

const streamsCache = {};

async function resolveStreamSummary(streamAddress) {
if (streamsCache[streamAddress]) {
return streamsCache[streamAddress];
}

var contract = new ethers.Contract(
streamAddress,
SimpleStreamABI,
mainnetProvider
);

var data = {};

// Call it's cap function
await contract
.cap()
.then((result) =>
data.cap = Number(result._hex) * 0.000000000000000001
);

// Call it's Balance function, calculate the current percentage
await contract
.streamBalance()
.then(
(result) =>
(data.percent =
((Number(result._hex) * 0.000000000000000001) / data.cap) * 100)
);

streamsCache[streamAddress] = data;
return data;
}

export default function Home({
mainnetProvider,
tx,
Expand All @@ -40,32 +75,13 @@ export default function Home({
useEffect(async () => {
// Get an instance for each Stream contract
for (let b in streams) {
if (streams)
var contract = new ethers.Contract(
streams[b].stream,
SimpleStreamABI,
mainnetProvider
);

// Call it's cap function
const cap = await contract
.cap()
.then((result) =>
copy[b].push(Number(result._hex) * 0.000000000000000001)
);

// Call it's Balance function, calculate the current percentage
const balance = await contract
.streamBalance()
.then(
(result) =>
(copy[b].percent =
((Number(result._hex) * 0.000000000000000001) / copy[b][3]) * 100)
);
const summary = resolveStreamSummary(streams[b].stream);
copy[b].push(summary.cap);
copy[b].percent = summary.percent;
}
setData(copy);

// Wait until list is almost fully loaded to render
// Wait until list is almost fully loaded to renderø
if (copy.length >= 18) setReady(true);
}, [streams]);

Expand Down

0 comments on commit 65d4ef2

Please sign in to comment.