Skip to content
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
15 changes: 1 addition & 14 deletions hooks/useFilteredChains.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import React, { useMemo } from 'react';
import { useRouter } from 'next/router';

const TESTNET_KEYWORDS = ['test', 'devnet'];
import { isTestnet } from '../utils';

const createSearchMatcher = (searchTerm) => {
const normalized = searchTerm.toLowerCase();
return (value) => value?.toLowerCase().includes(normalized);
};

const isTestnet = (chain) => {
const lowercaseValues = [
chain.name,
chain.title,
chain.network
].map((val) => val?.toLowerCase());

return TESTNET_KEYWORDS.some((keyword) =>
lowercaseValues.some((val) => val?.includes(keyword))
);
};

const matchesSearchTerm = (chain, matcher) => {
return (
matcher(chain.chain) ||
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "chainlist",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "./scripts/build.sh",
Expand Down
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Home({ chains }) {
})}
</div>
</React.Suspense>
{end - 1 < finalChains.length ? (
{end < finalChains.length ? (
<button
onClick={() => setEnd(finalChains.length)}
className="w-full border dark:border-[#171717] border-[#EAEAEA] px-4 py-2 rounded-[50px] mb-auto text-white bg-[#2F80ED] mx-auto"
Expand Down
14 changes: 12 additions & 2 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ fi
# find the parent directory name of the file _buildManifest.js within the .next/static directory
BUILD_ID=$(find .next -name _buildManifest.js | sed 's/\/_buildManifest.js//g' | sed 's/\.next\/static\///g')

./scripts/post-export.sh
POST_EXPORT_STATUS=$?

# If post-export failed, update build status to reflect the failure
if [ $POST_EXPORT_STATUS -ne 0 ]; then
echo ""
echo "⚠️ Post-export script failed with status $POST_EXPORT_STATUS"
if [ $BUILD_STATUS -eq 0 ]; then
BUILD_STATUS=$POST_EXPORT_STATUS
fi
fi

echo ""
echo "======================="
if [ $BUILD_STATUS -eq 0 ]; then
Expand All @@ -98,7 +110,5 @@ echo ""

node ./scripts/build-msg.js $BUILD_STATUS "$BUILD_TIME_STR" "$START_TIME" "$BUILD_ID" "$COMMIT_COMMENT" "$COMMIT_AUTHOR" "$COMMIT_HASH"

./scripts/post-export.sh

# exit with the build status
exit $BUILD_STATUS
19 changes: 17 additions & 2 deletions scripts/post-export.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
#!/bin/sh
set -e

# Detect OS for sed compatibility (macOS uses BSD sed, Linux uses GNU sed)
if [ "$(uname)" = "Darwin" ]; then
# macOS (BSD sed)
sed -i '' '4 i\
"type": "module",' package.json
else
# Linux (GNU sed)
sed -i '4 i "type": "module",' package.json
fi

sed -i '4 i "type": "module",' package.json
node generate-sitemap.js
node generate-json.js
sed -i "4 d" package.json

if [ "$(uname)" = "Darwin" ]; then
sed -i '' "4 d" package.json
else
sed -i "4 d" package.json
fi
rm out/404.html
mv out/error.html out/404.html
cp serve.json out/serve.json
2 changes: 1 addition & 1 deletion stores/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import create from "zustand";
import { create } from "zustand";

export const useChain = create((set) => ({
id: null,
Expand Down
80 changes: 68 additions & 12 deletions utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import allExtraRpcs from "../constants/extraRpcs.js";
import chainIds from "../constants/chainIds.js";
import fetch from "node-fetch";
import { overwrittenChains } from "../constants/additionalChainRegistry/list.js";
import { isTestnet } from "./index.js";

export const fetcher = (...args) => fetch(...args).then((res) => res.json());

const cache = {}
const cache = {};
export const fetchWithCache = async (url) => {
if(cache[url]){
return cache[url]
if (cache[url]) {
return cache[url];
}
const data = await fetch(url).then((res) => res.json());
cache[url] = data
return data
}
cache[url] = data;
return data;
};

function removeEndingSlashObject(rpc) {
if (typeof rpc === "string") {
Expand Down Expand Up @@ -100,24 +101,79 @@ export function arrayMove(array, fromIndex, toIndex) {
return newArray;
}

function getBaseName(name) {
if (!name) return "";

return name
.replace(/\s+(Sepolia|Goerli|Testnet|Mumbai|Fuji|Amoy|Hoodi)(\s+.*)?$/i, "")
.replace(/\s+Test\s+Network$/i, "")
.replace(/\s+Mainnet$/i, "")
.replace(/\s+(One|C-Chain)$/i, "")
.trim();
}

function handleTestnets(activeChains) {
const parentChainTvls = {};

// map testnets to their parent's TVL
activeChains.forEach((chain) => {
if (chain.tvl && !isTestnet(chain)) {
const baseName = getBaseName(chain.name);
if (!parentChainTvls[baseName] || parentChainTvls[baseName] < chain.tvl) {
parentChainTvls[baseName] = chain.tvl;
}
}
});

return activeChains.map((chain) => {
const isTestnetChain = isTestnet(chain);

if (isTestnetChain && !chain.tvl) {
const baseName = getBaseName(chain.name);
const parentTvl = parentChainTvls[baseName] || 0;

return {
...chain,
isTestnet: true,
tvl: parentTvl,
};
}

return {
...chain,
isTestnet: isTestnetChain,
};
});
}

export async function generateChainData() {
const [chains, chainTvls] = await Promise.all([
fetchWithCache("https://chainid.network/chains.json"),
fetchWithCache("https://api.llama.fi/chains")
fetchWithCache("https://api.llama.fi/chains"),
]);

const overwrittenIds = overwrittenChains.reduce((acc, curr) => {
acc[curr.chainId] = true;
return acc;
}, {});

const sortedChains = chains
const activeChains = chains
.filter((c) => c.status !== "deprecated" && !overwrittenIds[c.chainId])
.concat(overwrittenChains)
.map((chain) => populateChain(chain, chainTvls))
.sort((a, b) => {
return (b.tvl ?? 0) - (a.tvl ?? 0);
});
.map((chain) => populateChain(chain, chainTvls));

const chainsWithTestnetTvls = handleTestnets(activeChains);

const sortedChains = chainsWithTestnetTvls.sort((a, b) => {
// First: separate mainnets and testnets (mainnets first)
if (!a.isTestnet && b.isTestnet) return -1;
if (a.isTestnet && !b.isTestnet) return 1;

// Second: within same type (mainnet or testnet), sort by TVL (descending)
const aTvl = a.tvl ?? 0;
const bTvl = b.tvl ?? 0;
return bTvl - aTvl;
});

return sortedChains;
}
18 changes: 16 additions & 2 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import en from "../translations/en.json";
import zh from "../translations/zh.json";
import en from "../translations/en.json" with { type: "json" };
import zh from "../translations/zh.json" with { type: "json" };

export function formatCurrency(amount, decimals = 2) {
if (!isNaN(amount)) {
Expand Down Expand Up @@ -94,3 +94,17 @@ export const notTranslation =
return en[ns][key];
}
};

const TESTNET_KEYWORDS = ['test', 'devnet', 'sepolia', 'goerli', 'mumbai', 'fuji', 'amoy', 'hoodi'];

export const isTestnet = (chain) => {
const lowercaseValues = [
chain.name,
chain.title,
chain.network
].map((val) => val?.toLowerCase());

return TESTNET_KEYWORDS.some((keyword) =>
lowercaseValues.some((val) => val?.includes(keyword))
);
};