Skip to content

Add real-time MEV refund metrics to navbar #623

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,25 @@ This command generates static content into the `build` directory and can be serv
Create a PR and once merged, Github actions automatically deploy it.

The docs use Vercel for hosting, and deployment is done by Vercel on any merge into the master branch.

## Refund Metrics Widget

This site displays MEV and gas refund metrics in the navbar, fetched from the [Flashbots Refund Metrics API](https://github.com/flashbots/refund-metrics-dune-api).

### Configuration

To configure the widget, edit `docusaurus.config.js`:

```js
customFields: {
refundMetricsApiUrl: 'https://refund-metrics-dune-api.vercel.app',
refundMetricsRedirectUrl: 'https://protect.flashbots.net/',
},
```

- `refundMetricsApiUrl`: The API endpoint for fetching metrics
- `refundMetricsRedirectUrl`: Where to redirect when users click on MEV refunds

The widget implementation is in `src/components/MevMetrics.tsx`. For Flashbots docs, it:
- Shows both MEV and gas refunds
- Clicking on MEV refunds redirects to the configured URL (default: [Flashbots Protect](https://protect.flashbots.net/))
8 changes: 8 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ module.exports = async function createConfigAsync() {
sidebarId: 'api',
position: 'left',
},
{
type: 'custom-mevMetrics',
position: 'right',
},
{
href: 'https://github.com/flashbots/docs',
label: 'GitHub',
Expand Down Expand Up @@ -116,5 +120,9 @@ module.exports = async function createConfigAsync() {
},
'docusaurus-plugin-sass'
],
customFields: {
refundMetricsApiUrl: 'https://refund-metrics-dune-api.vercel.app',
refundMetricsRedirectUrl: 'https://protect.flashbots.net/',
},
}
}
52 changes: 52 additions & 0 deletions src/components/MevMetrics.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.container {
display: flex;
align-items: center;
gap: 0.75rem;
font-size: 0.875rem;
color: var(--ifm-navbar-link-color);
margin-right: 0.75rem;
}

.metric {
display: flex;
align-items: center;
gap: 0.25rem;
}

.label {
font-weight: 400;
}

.value {
font-family: monospace;
font-weight: 600;
transition: opacity 0.3s ease;
}

.loading {
opacity: 0.5;
}

.separator {
color: var(--ifm-navbar-link-color);
opacity: 0.3;
}

.clickable {
cursor: pointer;
transition: opacity 0.2s ease;
}

.clickable:hover {
opacity: 0.8;
}

.clickable:active {
opacity: 0.6;
}

@media (max-width: 996px) {
.container {
display: none !important;
}
}
82 changes: 82 additions & 0 deletions src/components/MevMetrics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { useEffect, useState } from 'react';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import styles from './MevMetrics.module.css';

interface MetricsResponse {
totalMevRefund: number;
totalGasRefund: number;
fetchedAt: string;
stale: boolean;
}

export default function MevMetrics(): JSX.Element {
const { siteConfig } = useDocusaurusContext();
const [data, setData] = useState<MetricsResponse | null>(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchMetrics = async () => {
try {
const apiUrl = siteConfig.customFields?.refundMetricsApiUrl as string;
const response = await fetch(`${apiUrl}/api/metrics`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const metrics: MetricsResponse = await response.json();
setData(metrics);
} catch (error) {
console.error('Error fetching MEV metrics:', error);
// Mock data as fallback
setData({
totalMevRefund: 380.29,
totalGasRefund: 444.24,
fetchedAt: new Date().toISOString(),
stale: true
});
} finally {
setLoading(false);
}
};

fetchMetrics();
}, []);

const formatValue = (value: number): string => {
return `${value.toFixed(2)} ETH`;
};

const handleMevClick = () => {
const redirectUrl = siteConfig.customFields?.refundMetricsRedirectUrl as string;
window.open(redirectUrl, '_blank', 'noopener,noreferrer');
};

return (
<div className={styles.container}>
<span className={styles.label}>Refund</span>
<span className={styles.separator}>|</span>
<div
className={`${styles.metric} ${styles.clickable}`}
onClick={handleMevClick}
role="button"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
handleMevClick();
}
}}
>
<span className={styles.label}>MEV:</span>
<span className={`${styles.value} ${loading ? styles.loading : ''}`}>
{loading ? '...' : data && formatValue(data.totalMevRefund)}
</span>
</div>
<span className={styles.separator}>|</span>
<div className={styles.metric}>
<span className={styles.label}>Gas:</span>
<span className={`${styles.value} ${loading ? styles.loading : ''}`}>
{loading ? '...' : data && formatValue(data.totalGasRefund)}
</span>
</div>
</div>
);
}
27 changes: 27 additions & 0 deletions src/theme/NavbarItem/ComponentTypes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
import LocaleDropdownNavbarItem from '@theme/NavbarItem/LocaleDropdownNavbarItem';
import SearchNavbarItem from '@theme/NavbarItem/SearchNavbarItem';
import HtmlNavbarItem from '@theme/NavbarItem/HtmlNavbarItem';
import DocNavbarItem from '@theme/NavbarItem/DocNavbarItem';
import DocSidebarNavbarItem from '@theme/NavbarItem/DocSidebarNavbarItem';
import DocsVersionNavbarItem from '@theme/NavbarItem/DocsVersionNavbarItem';
import DocsVersionDropdownNavbarItem from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
import MevMetrics from '@site/src/components/MevMetrics';

import type {ComponentTypesObject} from '@theme/NavbarItem/ComponentTypes';

const ComponentTypes: ComponentTypesObject = {
default: DefaultNavbarItem,
localeDropdown: LocaleDropdownNavbarItem,
search: SearchNavbarItem,
dropdown: DropdownNavbarItem,
html: HtmlNavbarItem,
doc: DocNavbarItem,
docSidebar: DocSidebarNavbarItem,
docsVersion: DocsVersionNavbarItem,
docsVersionDropdown: DocsVersionDropdownNavbarItem,
'custom-mevMetrics': MevMetrics,
};

export default ComponentTypes;