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

feat(hyperliquid): UI integration #1360

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions scripts/extern-configs/hyper-liquid/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions scripts/extern-configs/hyper-liquid/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CHAIN_IDs } from "@across-protocol/constants";
import { ExternalProjectConfig } from "../types";

export default {
name: "Hyperliquid",
projectId: "hyper-liquid",
explorer: "https://arbiscan.io",
logoPath: "./assets/logo.svg",
grayscaleLogoPath: "./assets/grayscale-logo.svg",
publicRpcUrl: "https://arbitrum.publicnode.com",
intermediaryChain: CHAIN_IDs.ARBITRUM,
tokens: ["USDC"],
} as ExternalProjectConfig;
1 change: 1 addition & 0 deletions scripts/extern-configs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as HYPER_LIQUID } from "./hyper-liquid";
13 changes: 13 additions & 0 deletions scripts/extern-configs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Destination only projects that are supported through a message bridge
// at a known supported intermediary chain
export type ExternalProjectConfig = {
projectId: string;
name: string;
fullName?: string;
explorer: string;
publicRpcUrl: string;
logoPath: string;
grayscaleLogoPath: string;
intermediaryChain: number;
tokens: string[];
};
288 changes: 176 additions & 112 deletions scripts/generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { writeFileSync } from "fs";
import * as prettier from "prettier";
import path from "path";
import * as chainConfigs from "./chain-configs";
import * as externConfigs from "./extern-configs";
import assert from "assert";

function getTokenSymbolForLogo(tokenSymbol: string): string {
switch (tokenSymbol) {
Expand All @@ -29,6 +31,8 @@ type ToToken = ToChain["tokens"][number];
type SwapToken = ToChain["swapTokens"][number];
type ValidTokenSymbol = string;

const enabledMainnetExternalProjects = [externConfigs.HYPER_LIQUID];

const enabledMainnetChainConfigs = [
chainConfigs.MAINNET,
chainConfigs.OPTIMISM,
Expand Down Expand Up @@ -107,7 +111,10 @@ const enabledRoutes = {
// [CHAIN_IDs.BASE]: "0xbcfbCE9D92A516e3e7b0762AE218B4194adE34b4",
},
},
routes: transformChainConfigs(enabledMainnetChainConfigs),
routes: transformChainConfigs(
enabledMainnetChainConfigs,
enabledMainnetExternalProjects
),
},
[CHAIN_IDs.SEPOLIA]: {
hubPoolChain: CHAIN_IDs.SEPOLIA,
Expand All @@ -134,18 +141,21 @@ const enabledRoutes = {
"0x17496824Ba574A4e9De80110A91207c4c63e552a", // Mocked
},
},
routes: transformChainConfigs(enabledSepoliaChainConfigs),
routes: transformChainConfigs(enabledSepoliaChainConfigs, []),
},
} as const;

function transformChainConfigs(
enabledChainConfigs: typeof enabledMainnetChainConfigs
enabledChainConfigs: typeof enabledMainnetChainConfigs,
enabledExternalProjects: typeof enabledMainnetExternalProjects
) {
const transformedChainConfigs: {
fromChain: number;
fromSpokeAddress: string;
externalProjectId?: string;
toChains: {
chainId: number;
externalProjectId?: string;
tokens: (
| string
| {
Expand Down Expand Up @@ -193,115 +203,7 @@ function transformChainConfigs(
throw new Error(`No config found for chain ${toChainId}`);
}

const tokens = chainConfig.tokens.flatMap((token) => {
const tokenSymbol = typeof token === "string" ? token : token.symbol;

// Handle native USDC -> bridged USDC routes
if (tokenSymbol === "USDC") {
if (toChainConfig.enableCCTP) {
return [
"USDC",
{
inputTokenSymbol: "USDC",
outputTokenSymbol: getBridgedUsdcSymbol(toChainConfig.chainId),
},
];
} else if (
toChainConfig.tokens.find(
(token) =>
typeof token === "string" && sdkUtils.isBridgedUsdc(token)
)
) {
return [
{
inputTokenSymbol: "USDC",
outputTokenSymbol: getBridgedUsdcSymbol(toChainConfig.chainId),
},
];
}
}

// Handle bridged USDC -> native/bridged USDC routes
if (sdkUtils.isBridgedUsdc(tokenSymbol)) {
if (toChainConfig.enableCCTP) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: "USDC",
},
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: getBridgedUsdcSymbol(toChainConfig.chainId),
},
];
} else if (toChainConfig.tokens.includes("USDC")) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: "USDC",
},
];
} else if (
toChainConfig.tokens.find(
(token) =>
typeof token === "string" && sdkUtils.isBridgedUsdc(token)
)
) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: getBridgedUsdcSymbol(toChainConfig.chainId),
},
];
}
}

// Handle USDB -> DAI
if (tokenSymbol === "USDB" && toChainConfig.tokens.includes("DAI")) {
return [
{
inputTokenSymbol: "USDB",
outputTokenSymbol: "DAI",
},
];
}
if (tokenSymbol === "DAI" && toChainConfig.tokens.includes("USDB")) {
return [
{
inputTokenSymbol: "DAI",
outputTokenSymbol: "USDB",
},
];
}

// Handle WETH Polygon & other non-eth chains
if (
tokenSymbol === "WETH" &&
!toChainConfig.tokens.includes("ETH") &&
chainConfig.tokens.includes("ETH")
) {
return ["WETH", "ETH"];
}

const chainIds =
typeof token === "string" ? [toChainId] : token.chainIds;

const toToken = toChainConfig.tokens.find((token) =>
typeof token === "string"
? token === tokenSymbol
: token.symbol === tokenSymbol
);
if (
!toToken ||
(typeof toToken === "object" &&
!toToken.chainIds.includes(fromChainId)) ||
!chainIds.includes(toChainId)
) {
return [];
}

return tokenSymbol;
});
const tokens = processTokenRoutes(chainConfig, toChainConfig);

// Handle USDC swap tokens
const usdcSwapTokens = chainConfig.enableCCTP
Expand All @@ -328,6 +230,47 @@ function transformChainConfigs(
toChains.push(toChain);
}

for (const externalProject of enabledExternalProjects) {
if (externalProject.intermediaryChain === fromChainId) {
continue;
}
const associatedChain = enabledChainConfigs.find(
(config) => config.chainId === externalProject.intermediaryChain
);
assert(associatedChain, "Associated chain not found");

let associatedRoutes = processTokenRoutes(
chainConfig,
{ ...associatedChain, enableCCTP: false },
externalProject.tokens
);

const externalProjectId = externalProject.projectId;

// Handle USDC swap tokens
const usdcSwapTokens = [];

const toChain = {
chainId: externalProject.intermediaryChain,
externalProjectId,
tokens: associatedRoutes,
swapTokens: usdcSwapTokens.filter(
({ acrossInputTokenSymbol, acrossOutputTokenSymbol }) =>
associatedRoutes.some((token) =>
typeof token === "string"
? token === acrossInputTokenSymbol
: token.inputTokenSymbol === acrossInputTokenSymbol
) &&
associatedRoutes.some((token) =>
typeof token === "string"
? token === acrossOutputTokenSymbol
: token.outputTokenSymbol === acrossOutputTokenSymbol
)
),
};
toChains.push(toChain);
}

transformedChainConfigs.push({
fromChain: fromChainId,
fromSpokeAddress,
Expand All @@ -338,6 +281,126 @@ function transformChainConfigs(
return transformedChainConfigs;
}

function processTokenRoutes(
fromConfig: typeof chainConfigs.MAINNET,
toConfig: typeof chainConfigs.MAINNET,
tokensToProcess?: string[]
) {
const toChainId = toConfig.chainId;
const tokens = tokensToProcess ?? fromConfig.tokens;
return tokens.flatMap((token) => {
const tokenSymbol = typeof token === "string" ? token : token.symbol;

// If the fromConfig does not support the token, return an empty array
if (!fromConfig.tokens.includes(tokenSymbol)) {
return [];
}

// Handle native USDC -> bridged USDC routes
if (tokenSymbol === "USDC") {
if (toConfig.enableCCTP) {
return [
"USDC",
{
inputTokenSymbol: "USDC",
outputTokenSymbol: getBridgedUsdcSymbol(toChainId),
},
];
} else if (
toConfig.tokens.find(
(token) => typeof token === "string" && sdkUtils.isBridgedUsdc(token)
)
) {
return [
{
inputTokenSymbol: "USDC",
outputTokenSymbol: getBridgedUsdcSymbol(toChainId),
},
];
}
}

// Handle bridged USDC -> native/bridged USDC routes
if (sdkUtils.isBridgedUsdc(tokenSymbol)) {
if (toConfig.enableCCTP) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: "USDC",
},
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: getBridgedUsdcSymbol(toChainId),
},
];
} else if (toConfig.tokens.includes("USDC")) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: "USDC",
},
];
} else if (
toConfig.tokens.find(
(token) => typeof token === "string" && sdkUtils.isBridgedUsdc(token)
)
) {
return [
{
inputTokenSymbol: tokenSymbol,
outputTokenSymbol: getBridgedUsdcSymbol(toChainId),
},
];
}
}

// Handle USDB -> DAI
if (tokenSymbol === "USDB" && toConfig.tokens.includes("DAI")) {
return [
{
inputTokenSymbol: "USDB",
outputTokenSymbol: "DAI",
},
];
}
if (tokenSymbol === "DAI" && toConfig.tokens.includes("USDB")) {
return [
{
inputTokenSymbol: "DAI",
outputTokenSymbol: "USDB",
},
];
}

// Handle WETH Polygon & other non-eth chains
if (
tokenSymbol === "WETH" &&
!toConfig.tokens.includes("ETH") &&
fromConfig.tokens.includes("ETH")
) {
return ["WETH", "ETH"];
}

const chainIds = typeof token === "string" ? [toChainId] : token.chainIds;

const toToken = toConfig.tokens.find((token) =>
typeof token === "string"
? token === tokenSymbol
: token.symbol === tokenSymbol
);
if (
!toToken ||
(typeof toToken === "object" &&
!toToken.chainIds.includes(fromConfig.chainId)) ||
!chainIds.includes(toChainId)
) {
return [];
}

return tokenSymbol;
});
}

async function generateRoutes(hubPoolChainId = 1) {
const config = enabledRoutes[hubPoolChainId];

Expand Down Expand Up @@ -559,6 +622,7 @@ function transformToRoute(
toTokenSymbol: outputTokenSymbol,
isNative: inputTokenSymbol === TOKEN_SYMBOLS_MAP.ETH.symbol,
l1TokenAddress: inputToken.l1TokenAddress,
externalProjectId: toChain.externalProjectId,
};
}

Expand Down
Loading
Loading