Skip to content

Commit

Permalink
[alpha / breaking] - pass chain objects around everywhere, expose m…
Browse files Browse the repository at this point in the history
…ore chains (#2321)
  • Loading branch information
jnsdls authored Feb 17, 2024
1 parent 774f65c commit a2b5cc0
Show file tree
Hide file tree
Showing 98 changed files with 776 additions and 464 deletions.
1 change: 1 addition & 0 deletions packages/thirdweb/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = {
],
...jsdocRuleOverrides,
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/no-import-type-side-effects": "error",
},
overrides: [
{
Expand Down
8 changes: 8 additions & 0 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"import": "./dist/esm/storage/index.js",
"default": "./dist/cjs/storage/index.js"
},
"./chains": {
"types": "./dist/types/chains/index.d.ts",
"import": "./dist/esm/chains/index.js",
"default": "./dist/cjs/chains/index.js"
},
"./wallets": {
"types": "./dist/types/wallets/index.d.ts",
"import": "./dist/esm/wallets/index.js",
Expand Down Expand Up @@ -103,6 +108,9 @@
"storage": [
"./dist/types/storage/index.d.ts"
],
"chains": [
"./dist/types/chains/index.d.ts"
],
"wallets": [
"./dist/types/wallets/index.d.ts"
],
Expand Down
3 changes: 2 additions & 1 deletion packages/thirdweb/src/adapters/ethers5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import * as universalethers from "ethers";
import type { Abi } from "abitype";
import type { Hex, TransactionSerializable } from "viem";
import type { ThirdwebClient } from "../client/client.js";
import { getRpcUrlForChain, type Chain } from "../chain/index.js";
import type { Chain } from "../chains/index.js";
import { getContract, type ThirdwebContract } from "../contract/index.js";
import type { Account } from "../wallets/interfaces/wallet.js";
import { getRpcUrlForChain } from "../chains/utils.js";

type Ethers5 = typeof ethers5;

Expand Down
17 changes: 7 additions & 10 deletions packages/thirdweb/src/adapters/ethers6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import * as universalethers from "ethers";
import type { Abi } from "abitype";
import type { AccessList, Hex, TransactionSerializable } from "viem";
import type { ThirdwebClient } from "../client/client.js";
import {
getRpcUrlForChain,
type Chain,
getChainIdFromChain,
} from "../chain/index.js";
import type { Chain } from "../chains/index.js";
import { getContract, type ThirdwebContract } from "../contract/index.js";
import type { Account, Wallet } from "../wallets/interfaces/wallet.js";
import { normalizeChainId } from "../wallets/utils/normalizeChainId.js";
import { resolvePromisedValue } from "../utils/promise/resolve-promised-value.js";
import { uint8ArrayToHex } from "../utils/uint8-array.js";
import { getRpcUrlForChain } from "../chains/utils.js";

type Ethers6 = typeof ethers6;

Expand Down Expand Up @@ -147,7 +144,7 @@ function toEthersProvider(
fetchRequest.setHeader("Content-Type", "application/json");
}

return new ethers.JsonRpcProvider(fetchRequest, getChainIdFromChain(chain), {
return new ethers.JsonRpcProvider(fetchRequest, chain.id, {
staticNetwork: true,
});
}
Expand Down Expand Up @@ -262,9 +259,9 @@ async function toEthersSigner(
wallet: Wallet,
): Promise<ethers6.Signer> {
const account = wallet.getAccount();
const chainId = wallet.getChainId();
if (!chainId) {
throw new Error("Chain ID not found");
const chain = wallet.getChain();
if (!chain) {
throw new Error("Chain not found");
}
if (!account) {
throw new Error("Account not found");
Expand Down Expand Up @@ -354,7 +351,7 @@ async function toEthersSigner(
}
}
return new ThirdwebAdapterSigner(
toEthersProvider(ethers, client, chainId),
toEthersProvider(ethers, client, chain),
account.address,
);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/arbitrum-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineChain } from "../utils.js";

export const arbitrumSepolia = /* @__PURE__ */ defineChain({
id: 421_614,
name: "Arbitrum Sepolia",
nativeCurrency: {
name: "Arbitrum Sepolia Ether",
symbol: "ETH",
decimals: 18,
},
blockExplorers: [
{
name: "Arbiscan",
url: "https://sepolia.arbiscan.io",
apiUrl: "https://sepolia.arbiscan.io/api",
},
],
testnet: true,
});
14 changes: 14 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/arbitrum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineChain } from "../utils.js";

export const arbitrum = /* @__PURE__ */ defineChain({
id: 42161,
name: "Arbitrum One",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
blockExplorers: [
{
name: "Arbiscan",
url: "https://arbiscan.io",
apiUrl: "https://api.arbiscan.io/api",
},
],
});
19 changes: 19 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/avalanche-fuji.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineChain } from "../utils.js";

export const avalancheFuji = /* @__PURE__ */ defineChain({
id: 43113,
name: "Avalanche Fuji",
nativeCurrency: {
decimals: 18,
name: "Avalanche Fuji",
symbol: "AVAX",
},
blockExplorers: [
{
name: "SnowTrace",
url: "https://testnet.snowtrace.io",
apiUrl: "https://api-testnet.snowtrace.io/api",
},
],
testnet: true,
});
18 changes: 18 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/avalanche.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineChain } from "../utils.js";

export const avalanche = /* @__PURE__ */ defineChain({
id: 43114,
name: "Avalanche",
nativeCurrency: {
decimals: 18,
name: "Avalanche",
symbol: "AVAX",
},
blockExplorers: [
{
name: "SnowTrace",
url: "https://snowtrace.io",
apiUrl: "https://api.snowtrace.io/api",
},
],
});
15 changes: 15 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/base-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineChain } from "../utils.js";

export const baseSepolia = /* @__PURE__ */ defineChain({
id: 84532,
name: "Base Sepolia",
nativeCurrency: { name: "Sepolia Ether", symbol: "ETH", decimals: 18 },
blockExplorers: [
{
name: "Basescan",
url: "https://sepolia.basescan.org",
apiUrl: "https://api-sepolia.basescan.org/api",
},
],
testnet: true,
});
14 changes: 14 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineChain } from "../utils.js";

export const base = /* @__PURE__ */ defineChain({
id: 8453,
name: "Base",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
blockExplorers: [
{
name: "Basescan",
url: "https://basescan.org",
apiUrl: "https://api.basescan.org/api",
},
],
});
22 changes: 22 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/ethereum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineChain } from "../utils.js";

export const ethereum = /* @__PURE__ */ defineChain({
id: 1,
name: "Ethereum",
nativeCurrency: {
name: "Ether",
symbol: "ETH",
decimals: 18,
},
blockExplorers: [
{
name: "Etherscan",
url: "https://etherscan.io",
},
],
});

/**
* @alias ethereum
*/
export const mainnet = ethereum;
15 changes: 15 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/optimism-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineChain } from "../utils.js";

export const optimismSepolia = /* @__PURE__ */ defineChain({
id: 11155420,
name: "OP Sepolia",
nativeCurrency: { name: "Sepolia Ether", symbol: "ETH", decimals: 18 },
blockExplorers: [
{
name: "Blockscout",
url: "https://optimism-sepolia.blockscout.com",
apiUrl: "https://optimism-sepolia.blockscout.com/api",
},
],
testnet: true,
});
14 changes: 14 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/optimism.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineChain } from "../utils.js";

export const optimism = /* @__PURE__ */ defineChain({
id: 10,
name: "OP Mainnet",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
blockExplorers: [
{
name: "Optimism Explorer",
url: "https://optimistic.etherscan.io",
apiUrl: "https://api-optimistic.etherscan.io",
},
],
});
20 changes: 20 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/polygon-mumbai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineChain } from "../utils.js";

export const polygonMumbai = /*@__PURE__*/ defineChain({
id: 80001,
name: "Polygon Mumbai",
nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
blockExplorers: [
{
name: "PolygonScan",
url: "https://mumbai.polygonscan.com",
apiUrl: "https://mumbai.polygonscan.com/api",
},
],
testnet: true,
});

/**
* @alias polygonMumbai
*/
export const mumbai = polygonMumbai;
14 changes: 14 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/polygon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineChain } from "../utils.js";

export const polygon = /*@__PURE__*/ defineChain({
id: 137,
name: "Polygon",
nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
blockExplorers: [
{
name: "PolygonScan",
url: "https://polygonscan.com",
apiUrl: "https://api.polygonscan.com/api",
},
],
});
15 changes: 15 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineChain } from "../utils.js";

export const sepolia = /*@__PURE__*/ defineChain({
id: 11155111,
name: "Sepolia",
nativeCurrency: { name: "Sepolia Ether", symbol: "SEP", decimals: 18 },
blockExplorers: [
{
name: "Etherscan",
url: "https://sepolia.etherscan.io",
apiUrl: "https://api-sepolia.etherscan.io/api",
},
],
testnet: true,
});
20 changes: 20 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/zora-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineChain } from "../utils.js";

export const zoraSepolia = /*@__PURE__*/ defineChain({
id: 999999999,
name: "Zora Sepolia",

nativeCurrency: {
decimals: 18,
name: "Zora Sepolia",
symbol: "ETH",
},
blockExplorers: [
{
name: "Zora Sepolia Explorer",
url: "https://sepolia.explorer.zora.energy/",
apiUrl: "https://sepolia.explorer.zora.energy/api",
},
],
testnet: true,
});
18 changes: 18 additions & 0 deletions packages/thirdweb/src/chains/chain-definitions/zora.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineChain } from "../utils.js";

export const zora = /*@__PURE__*/ defineChain({
id: 7777777,
name: "Zora",
nativeCurrency: {
decimals: 18,
name: "Ether",
symbol: "ETH",
},
blockExplorers: [
{
name: "Explorer",
url: "https://explorer.zora.energy",
apiUrl: "https://explorer.zora.energy/api",
},
],
});
24 changes: 24 additions & 0 deletions packages/thirdweb/src/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// chain type
export type { Chain } from "./types.js";
// define chain
export { defineChain } from "./utils.js";

/**
* PRE_DEFINED CHAINS
*/
export { arbitrumSepolia } from "./chain-definitions/arbitrum-sepolia.js";
export { arbitrum } from "./chain-definitions/arbitrum.js";
export { avalancheFuji } from "./chain-definitions/avalanche-fuji.js";
export { avalanche } from "./chain-definitions/avalanche.js";
export { baseSepolia } from "./chain-definitions/base-sepolia.js";
export { base } from "./chain-definitions/base.js";
// mainnet = alias for ethereum
export { ethereum, mainnet } from "./chain-definitions/ethereum.js";
export { optimismSepolia } from "./chain-definitions/optimism-sepolia.js";
export { optimism } from "./chain-definitions/optimism.js";
// mumbai = alias for polygonMumbai
export { polygonMumbai, mumbai } from "./chain-definitions/polygon-mumbai.js";
export { polygon } from "./chain-definitions/polygon.js";
export { sepolia } from "./chain-definitions/sepolia.js";
export { zoraSepolia } from "./chain-definitions/zora-sepolia.js";
export { zora } from "./chain-definitions/zora.js";
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import type { Prettify } from "../utils/type-utils.js";

export type Chain = Prettify<Readonly<ChainOptions & { rpc: string }>>;

export type ChainOptions = {
id: number;
name?: string;
rpc?: string;
nativeCurrency?: {
name?: string;
symbol?: string;
decimals?: number;
};
blockExplorers?: Array<{
name: string;
url: string;
apiUrl?: string;
}>;
testnet?: true;
};

type Icon = {
url: string;
width: number;
Expand Down
Loading

0 comments on commit a2b5cc0

Please sign in to comment.