Skip to content

Commit

Permalink
refactor(utils): Add support for converting legacy chains
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 24, 2024
1 parent 93f2a80 commit 1f0e771
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
34 changes: 34 additions & 0 deletions packages/thirdweb/src/chains/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,37 @@ export type ApiChain = {
bridges?: Readonly<Array<{ url: string }>>;
};
};

// legacy chain type
export type LegacyChain = {
name: string;
title?: string;
chain: string;
icon?: Icon;
rpc: readonly string[];
features?: Readonly<Array<{ name: string }>>;
faucets?: readonly string[];
nativeCurrency: {
name: string;
symbol: string;
decimals: number;
};
infoURL?: string;
shortName: string;
chainId: number;
networkId?: number;
ens?: {
registry: string;
};
explorers?: Readonly<Array<ChainExplorer>>;
testnet: boolean;
slug: string;
slip44?: number;
status?: string;
redFlags?: readonly string[];
parent?: {
chain: string;
type: string;
bridges?: Readonly<Array<{ url: string }>>;
};
};

Check warning on line 104 in packages/thirdweb/src/chains/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/chains/types.ts#L1-L104

Added lines #L1 - L104 were not covered by tests
41 changes: 38 additions & 3 deletions packages/thirdweb/src/chains/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ThirdwebClient } from "../client/client.js";
import { isThirdwebUrl } from "../utils/fetch.js";
import { withCache } from "../utils/promise/withCache.js";
import type { ApiChain, Chain, ChainOptions } from "./types.js";
import type { ApiChain, Chain, ChainOptions, LegacyChain } from "./types.js";
import type { Chain as ViemChain } from "viem";

/**
Expand All @@ -20,12 +20,16 @@ import type { Chain as ViemChain } from "viem";
* });
* ```
*/
export function defineChain(options: number | ChainOptions | ViemChain): Chain {
export function defineChain(
options: number | ChainOptions | ViemChain | LegacyChain,
): Chain {
if (typeof options === "number") {
return { id: options, rpc: `https://${options}.rpc.thirdweb.com` } as const;
}

Check warning on line 28 in packages/thirdweb/src/chains/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/chains/utils.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
if (isViemChain(options)) {
return convertViemChain(options);

Check warning on line 30 in packages/thirdweb/src/chains/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/chains/utils.ts#L30

Added line #L30 was not covered by tests
} else if (isLegacyChain(options)) {
return convertLegacyChain(options);
}

Check warning on line 33 in packages/thirdweb/src/chains/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/chains/utils.ts#L32-L33

Added lines #L32 - L33 were not covered by tests
// otherwise if it's not a viem chain, continue
let rpc = options.rpc;
Expand All @@ -35,7 +39,38 @@ export function defineChain(options: number | ChainOptions | ViemChain): Chain {
return { ...options, rpc } as const;
}

function isViemChain(chain: ChainOptions | ViemChain): chain is ViemChain {
function isLegacyChain(
chain: ChainOptions | ViemChain | LegacyChain,
): chain is LegacyChain {
return "rpc" in chain && Array.isArray(chain.rpc) && "slug" in chain;
}

function convertLegacyChain(legacyChain: LegacyChain): Chain {
const c: Chain = {
id: legacyChain.chainId,
name: legacyChain.name,
rpc:
legacyChain.rpc[0] ?? `https://${legacyChain.chainId}.rpc.thirdweb.com`,
blockExplorers: legacyChain?.explorers?.map((explorer) => ({
name: explorer.name,
url: explorer.url,
apiUrl: explorer.url,
})),
nativeCurrency: {
name: legacyChain.nativeCurrency.name,
symbol: legacyChain.nativeCurrency.symbol,
decimals: legacyChain.nativeCurrency.decimals,
},
};
if (legacyChain.testnet) {
return { ...c, testnet: true };
}
return c;
}

Check warning on line 69 in packages/thirdweb/src/chains/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/chains/utils.ts#L48-L69

Added lines #L48 - L69 were not covered by tests

function isViemChain(
chain: ChainOptions | ViemChain | LegacyChain,
): chain is ViemChain {
return "rpcUrls" in chain && !("rpc" in chain);
}

Expand Down

0 comments on commit 1f0e771

Please sign in to comment.