Skip to content

Commit

Permalink
Merge pull request #37 from supriyaamisshra/home-page-caching
Browse files Browse the repository at this point in the history
Home page component caching
  • Loading branch information
codenamejason authored Mar 2, 2022
2 parents 430e35c + 64f2fef commit cbe5123
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/react-app/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ function App(props) {
</Menu.Item>
</Menu>


<Switch>
<Route exact path="/">
<Home
Expand Down
2 changes: 1 addition & 1 deletion packages/react-app/src/contracts/hardhat_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,4 @@
}
}
}
}
}
60 changes: 38 additions & 22 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, mainnetProvider) {
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,28 +75,9 @@ 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 = await resolveStreamSummary(streams[b].stream, mainnetProvider);
copy[b].push(summary.cap);
copy[b].percent = summary.percent;
}
setData(copy);

Expand Down

0 comments on commit cbe5123

Please sign in to comment.