Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rpc list #68

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6b2ed4d
Revert "Shutdown notice."
0xngmi Mar 6, 2022
6dd9534
remove multichain & add sorting by chain
0xngmi Mar 6, 2022
f3120c6
add SSR and optimize
0xngmi Mar 6, 2022
c2ab136
change favicon
0xngmi Mar 6, 2022
d063d33
move chains.js
0xngmi Mar 6, 2022
211de94
add yarn.lock
0xngmi Mar 6, 2022
83e4cca
remove unused code
0xngmi Mar 6, 2022
0f96aba
add fathom
0xngmi Mar 6, 2022
4ba7128
add testnets switch
mintdart Mar 6, 2022
d89f901
add chainlist.org domain
0xngmi Mar 7, 2022
ac9d711
add chain icons
mintdart Mar 7, 2022
b5be804
rework chain icons
0xngmi Mar 8, 2022
ea313b4
move testnet toggle to header
mintdart Mar 8, 2022
15a06d1
add rpc list url table
mintdart Mar 12, 2022
48183db
fix sorting url list by height and latency
mintdart Mar 12, 2022
7124483
add loading indicator
mintdart Mar 13, 2022
f43f870
try using axios to calc latency
mintdart Mar 14, 2022
ee04ded
add extra rpcs
0xngmi Mar 14, 2022
2cbf2f1
update rpcs
0xngmi Mar 14, 2022
d00a250
update header styles
mintdart Mar 15, 2022
5bc4549
include option to add network with different rpc urls
mintdart Mar 15, 2022
e0fdea5
cleanup
mintdart Mar 15, 2022
426c57a
show data of websocket server nodes
mintdart Mar 16, 2022
a4b3957
add trust score sto rpc urls
mintdart Mar 17, 2022
3b31001
Update index.js
0xngmi Mar 17, 2022
a6b1b75
smol fix
0xngmi Mar 18, 2022
c4a998b
update table header
mintdart Mar 18, 2022
3e1bce4
fix harmony rpcs
0xngmi Mar 18, 2022
001eaf5
remove add to wallet buttons for eth mainnet urls
mintdart Mar 18, 2022
2646662
add link to docs on how to add endpoints manually
mintdart Mar 18, 2022
49e4131
cleanup
mintdart Mar 18, 2022
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
154 changes: 154 additions & 0 deletions components/RPCList/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Button, Paper } from '@material-ui/core';
import { useEffect, useMemo } from 'react';
import useRPCData from '../../hooks/useRPCData';
import { useAccount, useRpcStore } from '../../stores';
import { addToNetwork, renderProviderText } from '../../utils/utils';
import classes from './index.module.css';
import FiberManualRecordIcon from '@material-ui/icons/FiberManualRecord';

export default function RPCList({ chain }) {
const chains = useRPCData(chain.rpc);

const data = useMemo(() => {
const sortedData = chains?.sort((a, b) => {
if (a.isLoading) {
return 1;
}

const h1 = a?.data?.height;
const h2 = b?.data?.height;
const l1 = a?.data?.latency;
const l2 = b?.data?.latency;

if (!h2) {
return -1;
}

if (h2 - h1 > 0) {
return 1;
}
if (h2 - h1 < 0) {
return -1;
}
if (h1 === h2) {
if (l1 - l2 < 0) {
return -1;
} else {
return 1;
}
}
});

const topRpc = sortedData[0]?.data ?? {};

return sortedData.map(({ data, ...rest }) => {
const { height = null, latency = null, url = '' } = data || {};

let trust = 'transparent';
let disableConnect = false;

if (!height || !latency || topRpc.height - height > 3 || topRpc.latency - latency > 5000) {
trust = 'red';
} else if (topRpc.height - height < 2 && topRpc.latency - latency > -600) {
trust = 'green';
} else {
trust = 'orange';
}

if (url.includes('wss://') || url.includes('API_KEY')) disableConnect = true;

const lat = latency ? (latency / 1000).toFixed(3) + 's' : null;

return { ...rest, data: { ...data, height, latency: lat, trust, disableConnect } };
});
}, [chains]);

const darkMode = window.localStorage.getItem('yearn.finance-dark-mode') === 'dark';

const isEthMainnet = chain?.name === 'Ethereum Mainnet';

return (
<Paper elevation={1} className={classes.disclosure}>
<table
className={classes.table}
style={{ '--border-color': darkMode ? 'hsl(0deg 0% 39% / 33%)' : 'hsl(0deg 0% 17% / 4%)' }}
>
<caption>{`${chain.name} RPC URL List`}</caption>
<thead>
<tr>
<th>RPC Server Address</th>
<th>Height</th>
<th>Latency</th>
<th>Score</th>
<th></th>
</tr>
</thead>
<tbody>
{data.map((item, index) => (
<Row values={item} chain={chain} isEthMainnet={isEthMainnet} key={index} />
))}
</tbody>
</table>
</Paper>
);
}
Copy link

Choose a reason for hiding this comment

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


const Shimmer = () => {
const darkMode = window.localStorage.getItem('yearn.finance-dark-mode') === 'dark';
const linearGradient = darkMode
? 'linear-gradient(90deg, rgb(255 247 247 / 7%) 0px, rgb(85 85 85 / 80%) 40px, rgb(255 247 247 / 7%) 80px)'
: 'linear-gradient(90deg, #f4f4f4 0px, rgba(229, 229, 229, 0.8) 40px, #f4f4f4 80px)';
return <div className={classes.shimmer} style={{ '--linear-gradient': linearGradient }}></div>;
};

const Row = ({ values, chain, isEthMainnet }) => {
const { data, isLoading, refetch } = values;

const rpcs = useRpcStore((state) => state.rpcs);
const addRpc = useRpcStore((state) => state.addRpc);
const account = useAccount((state) => state.account);

useEffect(() => {
// ignore first request to a url and refetch to calculate latency which doesn't include DNS lookup
if (data && !rpcs.includes(data.url)) {
refetch();
addRpc(data.url);
}
}, [data, rpcs, addRpc, refetch]);

return (
<tr>
<td>{isLoading ? <Shimmer /> : data?.url}</td>
<td>{isLoading ? <Shimmer /> : data?.height}</td>
<td>{isLoading ? <Shimmer /> : data?.latency}</td>
<td className={classes.trustScore} style={{ '--trust-color': data?.trust ?? 'transparent' }}>
{isLoading ? <Shimmer /> : <FiberManualRecordIcon />}
</td>
<td>
{isLoading ? (
<Shimmer />
) : (
<>
{isEthMainnet ? (
<CopyUrl url={data?.url} />
) : (
!data.disableConnect && (
<Button style={{ padding: '0 8px' }} onClick={() => addToNetwork(account, chain, data?.url)}>
{renderProviderText(account)}
</Button>
)
)}
</>
)}
</td>
</tr>
);
};

const CopyUrl = ({ url = '' }) => {
return (
<Button style={{ padding: '0 8px' }} onClick={() => navigator.clipboard.writeText(url)}>
Copy URL
</Button>
);
};
65 changes: 65 additions & 0 deletions components/RPCList/index.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.disclosure {
grid-column: 1 / -1;
position: relative;
padding: 30px;
overflow-x: auto;
}

.table {
border-collapse: collapse;
margin: 0 auto;
}

.table {
white-space: nowrap;
}

.table caption,
.table th,
.table td {
padding: 4px 12px;
border: 1px solid var(--border-color);
}

.table caption {
font-size: 1rem;
font-weight: 500;
border-bottom: 0;
}

.table th {
font-weight: 500;
}

.shimmer {
border-radius: 4px;
height: 20px;
width: 100%;
min-width: 40px;
background-image: var(--linear-gradient);
background-size: 600px;
animation: loading 2s infinite;
}

.trustScore {
text-align: center;
color: var(--trust-color);
}

.helperText {
text-align: center;
}

.helperText a {
text-decoration: underline;
}

@keyframes loading {
0% {
background-position: -100px;
}
40%,
100% {
background-position: 140px;
}
}
Loading