From 2b6b0b656530c29a1613c7467361d74c066ca3af Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Thu, 4 Jan 2024 15:18:20 -0500 Subject: [PATCH 1/7] refactor client config to use chainId string --- packages/core-sdk/src/client.ts | 4 ++-- packages/core-sdk/src/index.ts | 2 +- packages/core-sdk/src/types/config.ts | 11 +++++++++-- packages/core-sdk/src/utils/utils.ts | 22 +++++++++++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) 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`); + } +} From 092bface1285427c809c97f3790b4b181246c91b Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:26:07 -0500 Subject: [PATCH 2/7] update tests --- packages/core-sdk/src/utils/utils.ts | 8 ++- packages/core-sdk/test/unit/client.test.ts | 22 +++--- .../core-sdk/test/unit/clientReadOnly.test.ts | 70 +++++++------------ 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/packages/core-sdk/src/utils/utils.ts b/packages/core-sdk/src/utils/utils.ts index 91d0cc66..6a69ba80 100644 --- a/packages/core-sdk/src/utils/utils.ts +++ b/packages/core-sdk/src/utils/utils.ts @@ -189,11 +189,13 @@ export function paramsTagValueDecoder(paramTag: Hex, paramValue: unknown) { export function chainStringToViemChain(chainId: SupportedChainIds): Chain { switch (chainId) { - case "1" || "mainnet": + case "1": + case "mainnet": return mainnet; - case "11155111" || "sepolia": + case "11155111": + case "sepolia": return sepolia; default: - throw new Error(`chainId ${chainId} not supported`); + throw new Error(`chainId ${chainId as string} not supported`); } } diff --git a/packages/core-sdk/test/unit/client.test.ts b/packages/core-sdk/test/unit/client.test.ts index 2453a001..a07d0d42 100644 --- a/packages/core-sdk/test/unit/client.test.ts +++ b/packages/core-sdk/test/unit/client.test.ts @@ -5,23 +5,19 @@ import { Account } from "viem"; describe("Test StoryClient", function () { describe("Test constructor", function () { - it("should succeed when passing in valid params", function () { - try { - StoryClient.newClient({ - account: privateKeyToAccount(generatePrivateKey()), - }); - } catch (error) { - expect.fail(`Function should not have thrown any error, but it threw: ${error}`); - } + it("should succeed when passing in default params", function () { + const client = StoryClient.newClient({ + account: privateKeyToAccount(generatePrivateKey()), + }); + expect(client).to.be.instanceOf(StoryClient); }); - it("throw error when wallet account is null", function () { - try { - StoryClient.newClient({ + it("should throw error when wallet account is null", function () { + expect(() => { + const client = StoryClient.newClient({ account: null as any as Account, }); - expect.fail(`Function should not get here, it should throw an error `); - } catch (error) {} + }).to.throw("account is null"); }); }); diff --git a/packages/core-sdk/test/unit/clientReadOnly.test.ts b/packages/core-sdk/test/unit/clientReadOnly.test.ts index 26f4f534..3aaa32c8 100644 --- a/packages/core-sdk/test/unit/clientReadOnly.test.ts +++ b/packages/core-sdk/test/unit/clientReadOnly.test.ts @@ -1,27 +1,38 @@ import { expect } from "chai"; import { StoryClient, ReadOnlyClient } from "../../src"; -import { fantom } from "viem/chains"; import { http } from "viem"; describe("Test StoryReadOnlyClient", function () { describe("Test constructor", function () { - it("should succeed when passing in valid params", function () { - try { - StoryClient.newReadOnlyClient({}); - } catch (error) { - expect.fail(`Function should not have thrown any error, but it threw: ${error}`); - } + it("should succeed when passing in default params", function () { + const client = StoryClient.newReadOnlyClient({}); + expect(client).to.be.instanceOf(StoryClient); + }); + it("should succeed when passing in chainId = '1' ", function () { + const client = StoryClient.newReadOnlyClient({ chainId: "1" }); + expect(client).to.be.instanceOf(StoryClient); + }); + it("should succeed when passing in chainId = 'mainnet' ", function () { + const client = StoryClient.newReadOnlyClient({ chainId: "mainnet" }); + expect(client).to.be.instanceOf(StoryClient); + }); + it("should succeed when passing in chainId = '11155111' ", function () { + const client = StoryClient.newReadOnlyClient({ chainId: "11155111" }); + expect(client).to.be.instanceOf(StoryClient); + }); + it("should succeed when passing in chainId = 'sepolia' ", function () { + const client = StoryClient.newReadOnlyClient({ chainId: "sepolia" }); + expect(client).to.be.instanceOf(StoryClient); }); - it("should succeed when passing in valid params w/ provider", function () { - try { + it("should fail when passing in unsupported chain ID", function () { + expect(() => StoryClient.newReadOnlyClient({ - chain: fantom, + //@ts-ignore + chainId: "fantom", transport: http(process.env.RPC_PROVIDER_URL), - }); - } catch (error) { - expect.fail(`Function should not have thrown any error, but it threw: ${error}`); - } + }), + ).to.throw(`chainId fantom not supported`); }); }); @@ -80,35 +91,4 @@ describe("Test StoryReadOnlyClient", function () { }); }); }); - - // describe("Test getters w/ provider", function () { - // let client: ReadOnlyClient; - - // beforeEach(function () { - // client = StoryClient.newReadOnlyClient({ - // environment: Environment.TEST, - // provider: new providers.JsonRpcProvider(), - // }); - // }); - - // describe("Test franchise getter w/ provider", function () { - // it("should return the same franchise when every time it's called", function () { - // const franchise1 = client.franchise; - // const franchise2 = client.franchise; - // expect(franchise1).to.be.equal(franchise2); - // }); - // }); - // }); - - // describe("Test franchise getter w/o creating a client", function () { - // let client: ReadOnlyClient; - - // it("should throw error when a client hasn't been created", function () { - // try { - // client.franchise; - - // expect.fail(`You haven't created a client yet.`); - // } catch (error) {} - // }); - // }); }); From 2b3e921528ad1c0705c790777db458bef52e04cc Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:30:21 -0500 Subject: [PATCH 3/7] change chain to chainId --- README.md | 58 +++++++++++-------- .../core-sdk/test/integration/ipAsset.test.ts | 3 +- .../core-sdk/test/integration/ipOrg.test.ts | 3 +- .../core-sdk/test/integration/license.test.ts | 3 +- .../test/integration/relationship.test.ts | 3 +- .../test/integration/relationshipType.test.ts | 3 +- 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 4954ab4a..4abbf7ad 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,23 @@ Welcome to the documents for Story Protocol SDK. The SDK provides the APIs for d ## How to use Story Protocol SDK in Your Project ### Install Story Protocol core SDK + Suppose you already have a node project or created a new node project. First, you need to install `@story-protocol/core-sdk` in your project. You can use one of the following command to install the package: Use `npm`: + ``` npm install --save @story-protocol/core-sdk viem ``` Use `pnpm`: + ``` pnpm install @story-protocol/core-sdk viem ``` Use `yarn`: + ``` yarn add @story-protocol/core-sdk viem ``` @@ -57,39 +61,38 @@ Next, we need create a client to access Story Protocol by using private key or f Here is the way to create a Story Protocol client with the private key: ```typescript -import { privateKeyToAccount } from "viem/accounts"; +import { privateKeyToAccount } from 'viem/accounts'; -const PRIVATE_KEY = process.env.PRIVATE_KEY || "0x"; +const PRIVATE_KEY = process.env.PRIVATE_KEY || '0x'; const account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`); -// Instantiate the Story Client for readonly operations, using default +// Instantiate the Story Client for readonly operations, using default export const realonlyClient = StoryClient.newReadOnlyClient({}); // Instantiate the Story Client, test environment required for alpha release. // The private key is also required for written operations. -export const client = StoryClient.newClient({account}); +export const client = StoryClient.newClient({ account }); ``` Here is the way to create a Story Protocol with wallet app: ```typescript -import { createWalletClient, custom } from 'viem' -import { sepolia } from 'viem/chains' +import { createWalletClient, custom } from 'viem'; const walletClient = createWalletClient({ - chain: sepolia, - transport: custom(window.ethereum) -}) + chainId: 'sepolia', + transport: custom(window.ethereum), +}); // Retrieve the first account for eth_requestAccounts method -const account = await walletClient.requestAddresses()[0] +const account = await walletClient.requestAddresses()[0]; -// Instantiate the Story Client for readonly operations, using default +// Instantiate the Story Client for readonly operations, using default export const realonlyClient = StoryClient.newReadOnlyClient({}); // Instantiate the Story Client, test environment required for alpha release. // The private key is also required for written operations. -export const client = StoryClient.newClient({account}); +export const client = StoryClient.newClient({ account }); ``` ### Use `Client` or `ReadOnlyClient` to access Story Protocol @@ -104,32 +107,40 @@ This section provides the instructions on how to build Story Protocol SDK from s ### Prerequisite -* Install PNPM: Execute `npm install -g pnpm` -* Install TypeScript: Run `pnpm add typescript -D` -* Install Yalc: Use `npm install -g yalc` +- Install PNPM: Execute `npm install -g pnpm` +- Install TypeScript: Run `pnpm add typescript -D` +- Install Yalc: Use `npm install -g yalc` ### Steps for Using Yalc for Local Testing of Core-SDK + For manual testing of the core-sdk, set up a separate web project. The guide below uses `yalc` to link the `core-sdk` locally, enabling its installation and import for testing. Under the `typescript-sdk/packages/core-sdk` directory: -* Navigate to the `core-sdk` directory. -* Execute `npm run build` to build your latest code. -* Run `yalc publish`. You should see a message like `@story-protocol/core-sdk@ published in store.` (Note: The version number may vary). + +- Navigate to the `core-sdk` directory. +- Execute `npm run build` to build your latest code. +- Run `yalc publish`. You should see a message like `@story-protocol/core-sdk@ published in store.` (Note: The version number may vary). To set up your testing environment (e.g., a new Next.js project), use `yalc add @story-protocol/core-sdk@` (ensure the version number is updated accordingly). -* Run `pnpm install`. This installs `@story-protocol/core-sdk@` with your local changes. + +- Run `pnpm install`. This installs `@story-protocol/core-sdk@` with your local changes. ### Steps to Refresh the Changes + Under the `typescript-sdk/packages/core-sdk` directory: -* Execute `npm run build` to build your latest code. -* Run `yalc push`. + +- Execute `npm run build` to build your latest code. +- Run `yalc push`. In your testing environment: -* Run `yalc update` to pull the latest changes. + +- Run `yalc update` to pull the latest changes. ## Steps to pull and compile latest smart contract ABIs (Currently pulls from the protocol-contracts `dev` branch) + Must have `solc` installed (https://docs.soliditylang.org/en/v0.8.9/installing-solidity.html) -* run `make compile_contracts` + +- run `make compile_contracts` ## Release @@ -149,4 +160,3 @@ Please make sure to update tests as appropriate. [MIT License](/LICENSE.md) ## Contact Us - diff --git a/packages/core-sdk/test/integration/ipAsset.test.ts b/packages/core-sdk/test/integration/ipAsset.test.ts index 3748a644..929db0b3 100644 --- a/packages/core-sdk/test/integration/ipAsset.test.ts +++ b/packages/core-sdk/test/integration/ipAsset.test.ts @@ -1,6 +1,5 @@ import { expect } from "chai"; import { StoryClient, StoryConfig, Client } from "../../src"; -import { sepolia } from "viem/chains"; import { Hex, http } from "viem"; import { privateKeyToAccount } from "viem/accounts"; @@ -10,7 +9,7 @@ describe("IP Asset Functions", () => { before(function () { const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; diff --git a/packages/core-sdk/test/integration/ipOrg.test.ts b/packages/core-sdk/test/integration/ipOrg.test.ts index 68e9ebec..80d3adfb 100644 --- a/packages/core-sdk/test/integration/ipOrg.test.ts +++ b/packages/core-sdk/test/integration/ipOrg.test.ts @@ -1,6 +1,5 @@ import { expect } from "chai"; import { StoryClient, StoryConfig, Client } from "../../src"; -import { sepolia } from "viem/chains"; import { Hex, http } from "viem"; import { privateKeyToAccount } from "viem/accounts"; @@ -10,7 +9,7 @@ describe("IPOrg Functions", () => { before(function () { const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; diff --git a/packages/core-sdk/test/integration/license.test.ts b/packages/core-sdk/test/integration/license.test.ts index 8e672fd1..e62662d6 100644 --- a/packages/core-sdk/test/integration/license.test.ts +++ b/packages/core-sdk/test/integration/license.test.ts @@ -1,6 +1,5 @@ import { expect } from "chai"; import { StoryClient, StoryConfig, Client } from "../../src"; -import { sepolia } from "viem/chains"; import { http, parseGwei, PrivateKeyAccount, stringToHex } from "viem"; import { privateKeyToAccount } from "viem/accounts"; @@ -19,7 +18,7 @@ describe("License Functions", () => { wallet = privateKeyToAccount(process.env.WALLET_PRIVATE_KEY as `0x${string}`); const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: wallet, }; diff --git a/packages/core-sdk/test/integration/relationship.test.ts b/packages/core-sdk/test/integration/relationship.test.ts index aa251c43..26f36749 100644 --- a/packages/core-sdk/test/integration/relationship.test.ts +++ b/packages/core-sdk/test/integration/relationship.test.ts @@ -2,14 +2,13 @@ import { expect } from "chai"; import { StoryClient, StoryConfig, Client, RegisterRelationshipRequest } from "../../src"; import { privateKeyToAccount } from "viem/accounts"; import { Hex, http } from "viem"; -import { sepolia } from "viem/chains"; describe("Relationship Functions", function () { let client: Client; before(function () { const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; diff --git a/packages/core-sdk/test/integration/relationshipType.test.ts b/packages/core-sdk/test/integration/relationshipType.test.ts index 19a2437f..29e4718d 100644 --- a/packages/core-sdk/test/integration/relationshipType.test.ts +++ b/packages/core-sdk/test/integration/relationshipType.test.ts @@ -8,14 +8,13 @@ import { } from "../../src"; import { privateKeyToAccount } from "viem/accounts"; import { Hex, http } from "viem"; -import { sepolia } from "viem/chains"; describe("Relationship Type Functions", function () { let client: Client; before(function () { const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; From 79f02911437bae8e10cbb5cac07cbbf72be7952a Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:32:52 -0500 Subject: [PATCH 4/7] replace goerli with sepolia --- packages/core-sdk/test/integration/platform.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core-sdk/test/integration/platform.test.ts b/packages/core-sdk/test/integration/platform.test.ts index 0d235c8a..321de66e 100644 --- a/packages/core-sdk/test/integration/platform.test.ts +++ b/packages/core-sdk/test/integration/platform.test.ts @@ -1,7 +1,6 @@ import { expect } from "chai"; import { StoryClient, StoryConfig, Client } from "../../src"; import { createFileReaderMock } from "../unit/testUtils"; -import { goerli } from "viem/chains"; import { Hex, http } from "viem"; import { privateKeyToAccount } from "viem/accounts"; @@ -16,7 +15,7 @@ describe("Platform client integration tests", () => { beforeEach(function () { const config: StoryConfig = { - chain: goerli, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; From 652d52e2a842eec66084a1e05e567c2298477e02 Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:28:23 -0800 Subject: [PATCH 5/7] add support for mumbai --- packages/core-sdk/src/types/config.ts | 9 ++++++++- packages/core-sdk/src/utils/utils.ts | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/core-sdk/src/types/config.ts b/packages/core-sdk/src/types/config.ts index 89164462..b57cff3d 100644 --- a/packages/core-sdk/src/types/config.ts +++ b/packages/core-sdk/src/types/config.ts @@ -5,7 +5,14 @@ import { Account, Transport } from "viem"; * * @public */ -export type SupportedChainIds = "11155111" | "sepolia" | "1" | "mainnet"; +export type SupportedChainIds = + | "11155111" + | "sepolia" + | "1" + | "mainnet" + | "80001" + | "mumbai" + | "polygonMumbai"; /** * Configuration for the SDK Client. diff --git a/packages/core-sdk/src/utils/utils.ts b/packages/core-sdk/src/utils/utils.ts index 6a69ba80..22f5dd88 100644 --- a/packages/core-sdk/src/utils/utils.ts +++ b/packages/core-sdk/src/utils/utils.ts @@ -9,7 +9,7 @@ import { Chain, } from "viem"; import { InferEventName } from "viem/types/contract"; -import { mainnet, sepolia } from "viem/chains"; +import { mainnet, polygonMumbai, sepolia } from "viem/chains"; import { Hex, TypedData } from "../types/common"; import { DERIVATIVES_ALLOWED_OPTIONS, PARAMS_TAG } from "../constants/license"; @@ -195,6 +195,10 @@ export function chainStringToViemChain(chainId: SupportedChainIds): Chain { case "11155111": case "sepolia": return sepolia; + case "80001": + case "mumbai": + case "polygonMumbai": + return polygonMumbai; default: throw new Error(`chainId ${chainId as string} not supported`); } From f7d26af9856180133ea85b852ed9934ab48a980c Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:31:15 -0800 Subject: [PATCH 6/7] remove old tests --- .../core-sdk/test/integration/ipOrg.test.ts | 62 --- .../core-sdk/test/integration/license.test.ts | 442 ------------------ .../test/integration/relationship.test.ts | 45 -- .../test/integration/relationshipType.test.ts | 50 -- 4 files changed, 599 deletions(-) delete mode 100644 packages/core-sdk/test/integration/ipOrg.test.ts delete mode 100644 packages/core-sdk/test/integration/license.test.ts delete mode 100644 packages/core-sdk/test/integration/relationship.test.ts delete mode 100644 packages/core-sdk/test/integration/relationshipType.test.ts diff --git a/packages/core-sdk/test/integration/ipOrg.test.ts b/packages/core-sdk/test/integration/ipOrg.test.ts deleted file mode 100644 index 80d3adfb..00000000 --- a/packages/core-sdk/test/integration/ipOrg.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { expect } from "chai"; -import { StoryClient, StoryConfig, Client } from "../../src"; -import { Hex, http } from "viem"; -import { privateKeyToAccount } from "viem/accounts"; - -describe("IPOrg Functions", () => { - let client: Client; - let senderAddress: string; - - before(function () { - const config: StoryConfig = { - chainId: "sepolia", - transport: http(process.env.RPC_PROVIDER_URL), - account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), - }; - senderAddress = config.account.address; - client = StoryClient.newClient(config); - }); - - describe("Create IPOrg", async function () { - it("should not throw error when creating a ipOrg", async () => { - const waitForTransaction: boolean = true; - const response = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: waitForTransaction, - }, - }), - ).to.not.be.rejected; - - expect(response.txHash).to.exist.and.be.a("string").and.not.be.empty; - - if (waitForTransaction) { - expect(response.ipOrgId).to.exist.and.be.a("string").and.not.be.empty; - } - }); - - it("should not throw error when creating a ipOrg without the owner field", async () => { - const waitForTransaction: boolean = true; - const response = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: waitForTransaction, - }, - }), - ).to.not.be.rejected; - - expect(response.txHash).to.exist.and.be.a("string").and.not.be.empty; - - if (waitForTransaction) { - expect(response.ipOrgId).to.exist.and.be.a("string").and.not.be.empty; - } - }); - }); -}); diff --git a/packages/core-sdk/test/integration/license.test.ts b/packages/core-sdk/test/integration/license.test.ts deleted file mode 100644 index e62662d6..00000000 --- a/packages/core-sdk/test/integration/license.test.ts +++ /dev/null @@ -1,442 +0,0 @@ -import { expect } from "chai"; -import { StoryClient, StoryConfig, Client } from "../../src"; -import { http, parseGwei, PrivateKeyAccount, stringToHex } from "viem"; -import { privateKeyToAccount } from "viem/accounts"; - -import { - ConfigureLicenseRequest, - CreateLicenseRequest, - LicensorConfigEnum, -} from "../../src/types/resources/license"; - -describe("License Functions", () => { - let client: Client; - let senderAddress: string; - let wallet: PrivateKeyAccount; - - before(function () { - wallet = privateKeyToAccount(process.env.WALLET_PRIVATE_KEY as `0x${string}`); - - const config: StoryConfig = { - chainId: "sepolia", - transport: http(process.env.RPC_PROVIDER_URL), - account: wallet, - }; - - senderAddress = config.account.address; - client = StoryClient.newClient(config); - }); - - describe("Configuring license", async function () { - it("should be able to configure license with default values", async () => { - const waitForTransaction: boolean = true; - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: waitForTransaction, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: [], - licensor: 1, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - }, - }; - - const response = await client.license.configure(configureLicenseRequest); - - expect(response.txHash).to.be.a("string"); - expect(response.txHash).not.be.undefined; - - expect(response.success).to.be.true; - }); - it("should be able to configure with Attribution=true", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - const attributionParam = { - tag: stringToHex("Attribution", { size: 32 }), - value: { - interface: "bool", - data: [true], - }, - }; - - const licenseParameters = [attributionParam]; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: licenseParameters, - licensor: LicensorConfigEnum.Source, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - - expect(configureResponse.success).to.be.true; - }); - it("should be able to configure with Attribution=false", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - const attributionParam = { - tag: stringToHex("Attribution", { size: 32 }), - value: { - interface: "bool", - data: [false], - }, - }; - - const licenseParameters = [attributionParam]; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: licenseParameters, - licensor: LicensorConfigEnum.Source, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - - expect(configureResponse.success).to.be.true; - }); - it("should be able get txHash after configuring with Derivatives-Allowed=false", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - const attributionParam = { - tag: stringToHex("Derivatives-Allowed", { size: 32 }), - value: { - interface: "bool", - data: [false], - }, - }; - - const licenseParameters = [attributionParam]; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: licenseParameters, - licensor: LicensorConfigEnum.Source, - txOptions: { - waitForTransaction: false, - gasPrice: parseGwei("250"), - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - }); - }); - - describe("License creation", async function () { - it("should be able to create an NFT with empty/default values", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "", - params: [], - licensor: 1, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - // numBlockConfirmations: 1, - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - - expect(configureResponse.success).to.be.true; - - const createLicenseRequest: CreateLicenseRequest = { - ipOrgId: createIpoResponse.ipOrgId, - params: [], - parentLicenseId: "0", - ipaId: "0", - preHookData: [], - postHookData: [], - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - // numBlockConfirmations: 1, - }, - }; - - const response = await expect(client.license.create(createLicenseRequest)).to.not.be.rejected; - - expect(response.txHash).to.be.a("string"); - expect(response.txHash).not.be.undefined; - - expect(response.licenseId).to.be.a("string"); - expect(response.licenseId).not.be.undefined; - }); - it("should be able to create an NFT with non-default license parameters", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - // const attributionParam = { - // tag: "Attribution", - // value: { - // interface: "bool", - // data: [true], - // }, - // }; - const derivativesParam = { - tag: stringToHex("Derivatives-Allowed", { size: 32 }), - value: { - interface: "bool", - data: [false], - }, - }; - // const licenseParameters = [attributionParam]; - const licenseParameters = [derivativesParam]; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: licenseParameters, - licensor: LicensorConfigEnum.Source, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - numBlockConfirmations: 2, - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - - expect(configureResponse.success).to.be.true; - - const createLicenseRequest: CreateLicenseRequest = { - ipOrgId: createIpoResponse.ipOrgId, - params: [], - parentLicenseId: "0", - ipaId: "0", - preHookData: [], - postHookData: [], - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - numBlockConfirmations: 2, - }, - }; - - const response = await expect(client.license.create(createLicenseRequest)).to.not.be.rejected; - - expect(response.txHash).to.be.a("string"); - expect(response.txHash).not.be.undefined; - - expect(response.licenseId).to.be.a("string"); - expect(response.licenseId).not.be.undefined; - }); - it.skip("should be able to create an NFT with 'Attribution'=true", async () => { - // 1. Create IPO first - // 2. Configure framework - // 3. Create license - - const createIpoResponse = await expect( - client.ipOrg.create({ - name: "Alice In Wonderland", - symbol: "AIW", - owner: senderAddress, - ipAssetTypes: ["Story", "Character"], - txOptions: { - waitForTransaction: true, - }, - }), - ).to.not.be.rejected; - expect(createIpoResponse.txHash).to.be.a("string"); - expect(createIpoResponse.txHash).not.empty; - expect(createIpoResponse.ipOrgId).to.be.a("string"); - expect(createIpoResponse.ipOrgId).not.empty; - - // const attributionParam = { - // tag: "Attribution", - // value: { - // interface: "bool", - // data: [true], - // }, - // }; - const derivativesParam = { - tag: stringToHex("Derivatives-Allowed", { size: 32 }), - value: { - interface: "bool", - data: [false], - }, - }; - // const licenseParameters = [attributionParam, derivativesParam]; - const licenseParameters = [derivativesParam]; - - // Configure license - const configureLicenseRequest: ConfigureLicenseRequest = { - ipOrg: createIpoResponse.ipOrgId, - frameworkId: "SPUML-1.0", - params: licenseParameters, - licensor: LicensorConfigEnum.Source, - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - }, - }; - - const configureResponse = await client.license.configure(configureLicenseRequest); - - expect(configureResponse.txHash).to.be.a("string"); - expect(configureResponse.txHash).not.be.undefined; - - expect(configureResponse.success).to.be.true; - - const createLicenseRequest: CreateLicenseRequest = { - ipOrgId: createIpoResponse.ipOrgId, - params: [], - parentLicenseId: "0", - ipaId: "0", - preHookData: [], - postHookData: [], - txOptions: { - waitForTransaction: true, - gasPrice: parseGwei("250"), - numBlockConfirmations: 2, - }, - }; - - const response = await expect(client.license.create(createLicenseRequest)).to.not.be.rejected; - - expect(response.txHash).to.be.a("string"); - expect(response.txHash).not.be.undefined; - - expect(response.licenseId).to.be.a("string"); - expect(response.licenseId).not.be.undefined; - }); - }); -}); diff --git a/packages/core-sdk/test/integration/relationship.test.ts b/packages/core-sdk/test/integration/relationship.test.ts deleted file mode 100644 index 26f36749..00000000 --- a/packages/core-sdk/test/integration/relationship.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { expect } from "chai"; -import { StoryClient, StoryConfig, Client, RegisterRelationshipRequest } from "../../src"; -import { privateKeyToAccount } from "viem/accounts"; -import { Hex, http } from "viem"; - -describe("Relationship Functions", function () { - let client: Client; - - before(function () { - const config: StoryConfig = { - chainId: "sepolia", - transport: http(process.env.RPC_PROVIDER_URL), - account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), - }; - - client = StoryClient.newClient(config); - }); - - describe("Register", async function () { - it("should create a relationship and return txHash", async function () { - const waitForTransaction: boolean = true; - const mockRegisterRequest: RegisterRelationshipRequest = { - ipOrgId: process.env.TEST_IPORG_ID as string, - relType: process.env.TEST_RELATIONSHIP_TYPE as string, - srcContract: process.env.NEXT_PUBLIC_IP_ASSET_REGISTRY_CONTRACT as string, - srcTokenId: process.env.TEST_IPASSET_ID1 as string, - dstContract: process.env.NEXT_PUBLIC_IP_ASSET_REGISTRY_CONTRACT as string, - dstTokenId: process.env.TEST_IPASSET_ID2 as string, - preHookData: [], - postHookData: [], - txOptions: { - waitForTransaction: waitForTransaction, - }, - }; - - const response = await expect(client.relationship.register(mockRegisterRequest)).to.not.be - .rejected; - - expect(response.txHash).to.exist.and.be.a("string").and.not.be.empty; - if (waitForTransaction) { - expect(response.relationshipId).to.exist.and.be.a("string"); - } - }); - }); -}); diff --git a/packages/core-sdk/test/integration/relationshipType.test.ts b/packages/core-sdk/test/integration/relationshipType.test.ts deleted file mode 100644 index 29e4718d..00000000 --- a/packages/core-sdk/test/integration/relationshipType.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { expect } from "chai"; -import { - StoryClient, - StoryConfig, - Client, - RegisterRelationshipTypeRequest, - Relatables, -} from "../../src"; -import { privateKeyToAccount } from "viem/accounts"; -import { Hex, http } from "viem"; - -describe("Relationship Type Functions", function () { - let client: Client; - - before(function () { - const config: StoryConfig = { - chainId: "sepolia", - transport: http(process.env.RPC_PROVIDER_URL), - account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), - }; - - client = StoryClient.newClient(config); - }); - - describe("RegisterType", async function () { - it("should create a relationship type and return txHash", async function () { - const waitForTransaction: boolean = true; - const mockRegisterTypeRequest: RegisterRelationshipTypeRequest = { - ipOrgId: process.env.TEST_IPORG_ID as string, - relType: process.env.TEST_RELATIONSHIP_TYPE as string, - relatedElements: { - src: Relatables.IPA, - dst: Relatables.IPA, - }, - allowedSrcIpAssetTypes: [1], - allowedDstIpAssetTypes: [1], - txOptions: { - waitForTransaction: waitForTransaction, - }, - }; - const response = await expect(client.relationshipType.register(mockRegisterTypeRequest)).to - .not.be.rejected; - - expect(response.txHash).to.exist.and.be.a("string").and.not.be.empty; - if (waitForTransaction) { - expect(response.success).to.exist.and.be.true; - } - }); - }); -}); From 6fd8d19fe66c6da45eb689a22eea7ba14de930a2 Mon Sep 17 00:00:00 2001 From: Don <100505855+DonFungible@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:34:34 -0800 Subject: [PATCH 7/7] fix chainId --- packages/core-sdk/test/integration/permission.test.ts | 2 +- packages/core-sdk/test/integration/tagging.test.ts | 1 + packages/core-sdk/test/integration/taggingReadOnly.test.ts | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core-sdk/test/integration/permission.test.ts b/packages/core-sdk/test/integration/permission.test.ts index 606517f9..a11b9b64 100644 --- a/packages/core-sdk/test/integration/permission.test.ts +++ b/packages/core-sdk/test/integration/permission.test.ts @@ -10,7 +10,7 @@ describe("Permission Functions", () => { before(function () { const config: StoryConfig = { - chain: sepolia, + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; diff --git a/packages/core-sdk/test/integration/tagging.test.ts b/packages/core-sdk/test/integration/tagging.test.ts index 9c34895d..f5c2af6f 100644 --- a/packages/core-sdk/test/integration/tagging.test.ts +++ b/packages/core-sdk/test/integration/tagging.test.ts @@ -9,6 +9,7 @@ describe("Tagging Functions", () => { before(function () { const config: StoryConfig = { + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), }; diff --git a/packages/core-sdk/test/integration/taggingReadOnly.test.ts b/packages/core-sdk/test/integration/taggingReadOnly.test.ts index 56256083..56c47f07 100644 --- a/packages/core-sdk/test/integration/taggingReadOnly.test.ts +++ b/packages/core-sdk/test/integration/taggingReadOnly.test.ts @@ -10,6 +10,7 @@ describe("Tagging Indexer Functions", () => { before(function () { const config: StoryConfig = { + chainId: "sepolia", transport: http(process.env.RPC_PROVIDER_URL), account: privateKeyToAccount((process.env.WALLET_PRIVATE_KEY || "0x") as Hex), };