Skip to content

Commit

Permalink
refactor client config to use chainId string
Browse files Browse the repository at this point in the history
  • Loading branch information
DonFungible committed Jan 4, 2024
1 parent ec055df commit 2b6b0b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/core-sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
Expand Down Expand Up @@ -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),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
11 changes: 9 additions & 2 deletions packages/core-sdk/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,6 +22,6 @@ export interface StoryConfig extends StoryReadOnlyConfig {
* @public
*/
export interface StoryReadOnlyConfig {
readonly chain?: Chain;
readonly chainId?: SupportedChainIds;
readonly transport?: Transport;
}
22 changes: 21 additions & 1 deletion packages/core-sdk/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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`);
}
}

0 comments on commit 2b6b0b6

Please sign in to comment.