diff --git a/packages/core-sdk/src/client.ts b/packages/core-sdk/src/client.ts index 272fae48..005c9e6e 100644 --- a/packages/core-sdk/src/client.ts +++ b/packages/core-sdk/src/client.ts @@ -1,6 +1,5 @@ import axios, { AxiosInstance } from "axios"; import { createPublicClient, createWalletClient, http, PublicClient, WalletClient } from "viem"; -import { sepolia } from "viem/chains"; import * as dotenv from "dotenv"; import { StoryConfig, StoryReadOnlyConfig } from "./types/config"; @@ -23,6 +22,7 @@ import { LicenseClient } from "./resources/license"; import { RelationshipClient } from "./resources/relationship"; import { RelationshipTypeClient } from "./resources/relationshipType"; import { RelationshipTypeReadOnlyClient } from "./resources/relationshipTypeReadOnly"; +import { chainStringToViemChain } from "./utils/utils"; if (typeof process !== "undefined") { dotenv.config(); @@ -56,7 +56,7 @@ export class StoryClient { this.isReadOnly = isReadOnly; const clientConfig = { - chain: this.config.chain || sepolia, + chain: chainStringToViemChain(this.config.chainId || "sepolia"), transport: this.config.transport || http(process.env.RPC_PROVIDER_URL), }; diff --git a/packages/core-sdk/src/index.ts b/packages/core-sdk/src/index.ts index bedf17da..0e7a3356 100644 --- a/packages/core-sdk/src/index.ts +++ b/packages/core-sdk/src/index.ts @@ -16,7 +16,7 @@ export { TransactionClient } from "./resources/transaction"; export { PlatformClient } from "./utils/platform"; export { AddressZero, HashZero } from "./constants/common"; -export type { StoryConfig, StoryReadOnlyConfig } from "./types/config"; +export type { StoryConfig, StoryReadOnlyConfig, SupportedChainIds } from "./types/config"; export type { Client, ReadOnlyClient } from "./types/client"; export type { Hex, TypedData } from "./types/common"; diff --git a/packages/core-sdk/src/types/config.ts b/packages/core-sdk/src/types/config.ts index c196bffe..89164462 100644 --- a/packages/core-sdk/src/types/config.ts +++ b/packages/core-sdk/src/types/config.ts @@ -1,4 +1,11 @@ -import { Account, Chain, Transport } from "viem"; +import { Account, Transport } from "viem"; + +/** + * Supported chains. For convenience, both name or chain ID are supported. + * + * @public + */ +export type SupportedChainIds = "11155111" | "sepolia" | "1" | "mainnet"; /** * Configuration for the SDK Client. @@ -15,6 +22,6 @@ export interface StoryConfig extends StoryReadOnlyConfig { * @public */ export interface StoryReadOnlyConfig { - readonly chain?: Chain; + readonly chainId?: SupportedChainIds; readonly transport?: Transport; } diff --git a/packages/core-sdk/src/utils/utils.ts b/packages/core-sdk/src/utils/utils.ts index f7e57aa5..91d0cc66 100644 --- a/packages/core-sdk/src/utils/utils.ts +++ b/packages/core-sdk/src/utils/utils.ts @@ -1,10 +1,19 @@ import { Hash } from "viem/types/misc"; import { DecodeEventLogReturnType } from "viem/_types/utils/abi/decodeEventLog"; -import { Abi, decodeEventLog, PublicClient, encodeAbiParameters, parseAbiParameters } from "viem"; +import { + Abi, + decodeEventLog, + PublicClient, + encodeAbiParameters, + parseAbiParameters, + Chain, +} from "viem"; import { InferEventName } from "viem/types/contract"; +import { mainnet, sepolia } from "viem/chains"; import { Hex, TypedData } from "../types/common"; import { DERIVATIVES_ALLOWED_OPTIONS, PARAMS_TAG } from "../constants/license"; +import { SupportedChainIds } from "../types/config"; export function isIntegerString(s: string): boolean { const num = Number(s); @@ -177,3 +186,14 @@ export function paramsTagValueDecoder(paramTag: Hex, paramValue: unknown) { return { tag: parsedTag, value, type }; } + +export function chainStringToViemChain(chainId: SupportedChainIds): Chain { + switch (chainId) { + case "1" || "mainnet": + return mainnet; + case "11155111" || "sepolia": + return sepolia; + default: + throw new Error(`chainId ${chainId} not supported`); + } +}