From 289661e42eaad39c88f7fecc8b358b3bba27f299 Mon Sep 17 00:00:00 2001 From: Bonnie57 <146059114+bonnie57@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:07:18 +0800 Subject: [PATCH] Refactor the related unit tests (#213) * Modify calling signTypedData by wallet client * Upload pnpm-lock.yaml * Remove useless type * Delete StoryAPI and related tests * Remove utils useless function * Refactor client unit test * Refactor dispute unit tests * Refactor ipAccount tests * Refactor nftClient tests * Add address validation for signature * Export PermissionSignatureResponse type * bump up package version to 1.0.0-rc.15 --- packages/core-sdk/package.json | 2 +- packages/core-sdk/src/clients/storyAPI.ts | 28 -- packages/core-sdk/src/index.ts | 9 +- packages/core-sdk/src/resources/dispute.ts | 6 +- packages/core-sdk/src/resources/ipAccount.ts | 4 +- packages/core-sdk/src/types/common.ts | 10 +- .../core-sdk/src/types/resources/dispute.ts | 23 -- packages/core-sdk/src/utils/sign.ts | 26 +- packages/core-sdk/src/utils/utils.ts | 66 ----- packages/core-sdk/test/unit/client.test.ts | 59 +--- .../test/unit/clients/storyAPI.test.ts | 82 ----- .../test/unit/resources/dispute.test.ts | 280 ++++++------------ .../test/unit/resources/ipAccount.test.ts | 181 ++--------- .../test/unit/resources/nftClient.test.ts | 106 +++---- .../test/unit/resources/permission.test.ts | 2 +- .../core-sdk/test/unit/utils/utils.test.ts | 103 ------- 16 files changed, 190 insertions(+), 797 deletions(-) delete mode 100644 packages/core-sdk/src/clients/storyAPI.ts delete mode 100644 packages/core-sdk/test/unit/clients/storyAPI.test.ts diff --git a/packages/core-sdk/package.json b/packages/core-sdk/package.json index 2151ab1b..08a0bbea 100644 --- a/packages/core-sdk/package.json +++ b/packages/core-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@story-protocol/core-sdk", - "version": "1.0.0-rc.14", + "version": "1.0.0-rc.15", "description": "Story Protocol Core SDK", "main": "dist/story-protocol-core-sdk.cjs.js", "module": "dist/story-protocol-core-sdk.esm.js", diff --git a/packages/core-sdk/src/clients/storyAPI.ts b/packages/core-sdk/src/clients/storyAPI.ts deleted file mode 100644 index d1ca4470..00000000 --- a/packages/core-sdk/src/clients/storyAPI.ts +++ /dev/null @@ -1,28 +0,0 @@ -import axios, { AxiosInstance } from "axios"; - -import { RoyaltyPolicy, RoyaltyPolicyApiResponse } from "../types/resources/royalty"; -import { License, LicenseApiResponse } from "../types/resources/license"; - -export class StoryAPIClient { - private readonly httpClient: AxiosInstance; - - constructor() { - this.httpClient = axios.create({ - baseURL: "https://api.storyprotocol.net", - timeout: 60000, - headers: { - "x-api-key": "U3RvcnlQcm90b2NvbFRlc3RBUElLRVk=", - }, - }); - } - - public async getRoyaltyPolicy(ipId: string): Promise { - const royaltyPolicyResp = await this.httpClient.get(`/api/v1/royalties/policies/${ipId}`); - return (royaltyPolicyResp.data as RoyaltyPolicyApiResponse).data; - } - - public async getLicense(licenseId: string): Promise { - const licenseResp = await this.httpClient.get(`/api/v1/licenses/${licenseId}`); - return (licenseResp.data as LicenseApiResponse).data; - } -} diff --git a/packages/core-sdk/src/index.ts b/packages/core-sdk/src/index.ts index 9c51cd78..e32f9257 100644 --- a/packages/core-sdk/src/index.ts +++ b/packages/core-sdk/src/index.ts @@ -10,11 +10,10 @@ export { IPAccountClient } from "./resources/ipAccount"; export { RoyaltyClient } from "./resources/royalty"; export type { StoryConfig, SupportedChainIds } from "./types/config"; -export type { TypedData } from "./types/common"; export type { - RegisterIpResponse, RegisterRequest, + RegisterIpResponse, RegisterDerivativeResponse, RegisterDerivativeRequest, RegisterDerivativeWithLicenseTokensRequest, @@ -34,7 +33,6 @@ export type { RegisterPILResponse, AttachLicenseTermsRequest, AttachLicenseTermsResponse, - LicenseTermsIdResponse, MintLicenseTokensRequest, MintLicenseTokensResponse, LicenseTermsId, @@ -64,11 +62,8 @@ export type { } from "./types/resources/permission"; export { AccessPermission } from "./types/resources/permission"; export type { - Dispute, RaiseDisputeRequest, RaiseDisputeResponse, - SetDisputeJudgementRequest, - SetDisputeJudgementResponse, CancelDisputeRequest, CancelDisputeResponse, ResolveDisputeRequest, @@ -93,4 +88,4 @@ export type { } from "./abi/generated"; export { getPermissionSignature } from "./utils/sign"; -export type { PermissionSignatureRequest } from "./types/common"; +export type { PermissionSignatureRequest, PermissionSignatureResponse } from "./types/common"; diff --git a/packages/core-sdk/src/resources/dispute.ts b/packages/core-sdk/src/resources/dispute.ts index 2cfd30fe..4ff41e9d 100644 --- a/packages/core-sdk/src/resources/dispute.ts +++ b/packages/core-sdk/src/resources/dispute.ts @@ -10,6 +10,7 @@ import { ResolveDisputeResponse, } from "../types/resources/dispute"; import { DisputeModuleClient, SimpleWalletClient } from "../abi/generated"; +import { getAddress } from "../utils/utils"; export class DisputeClient { private readonly rpcClient: PublicClient; @@ -39,7 +40,7 @@ export class DisputeClient { public async raiseDispute(request: RaiseDisputeRequest): Promise { try { const txHash = await this.disputeModuleClient.raiseDispute({ - targetIpId: request.targetIpId, + targetIpId: getAddress(request.targetIpId, "request.targetIpId"), linkToDisputeEvidence: request.linkToDisputeEvidence, targetTag: stringToHex(request.targetTag, { size: 32 }), data: request.calldata || "0x", @@ -104,14 +105,13 @@ export class DisputeClient { disputeId: BigInt(request.disputeId), data: request.data, }); - if (request.txOptions?.waitForTransaction) { await this.rpcClient.waitForTransactionReceipt({ hash: txHash }); } return { txHash: txHash }; } catch (error) { - handleError(error, "Failed to cancel dispute"); + handleError(error, "Failed to resolve dispute"); } } } diff --git a/packages/core-sdk/src/resources/ipAccount.ts b/packages/core-sdk/src/resources/ipAccount.ts index 4fce31c2..5822e775 100644 --- a/packages/core-sdk/src/resources/ipAccount.ts +++ b/packages/core-sdk/src/resources/ipAccount.ts @@ -77,10 +77,10 @@ export class IPAccountClient { ); const txHash = await ipAccountClient.executeWithSig({ - to: request.to, + to: getAddress(request.to, "request.to"), value: BigInt(0), data: request.data, - signer: request.signer, + signer: getAddress(request.signer, "request.signer"), deadline: BigInt(request.deadline), signature: request.signature, }); diff --git a/packages/core-sdk/src/types/common.ts b/packages/core-sdk/src/types/common.ts index a517766c..6ba78e4f 100644 --- a/packages/core-sdk/src/types/common.ts +++ b/packages/core-sdk/src/types/common.ts @@ -1,4 +1,4 @@ -import { Address, WalletClient } from "viem"; +import { Address, Hex, WalletClient } from "viem"; import { SetPermissionsRequest } from "./resources/permission"; @@ -9,10 +9,12 @@ export type TypedData = { export type PermissionSignatureRequest = { ipId: Address; - nonce: number | bigint; - deadline: bigint; + nonce: string | number | bigint; + deadline: string | number | bigint; wallet: WalletClient; - chainId: bigint; + chainId: string | number | bigint; permissions: Omit[]; permissionFunc?: "setPermission" | "setBatchPermissions"; }; + +export type PermissionSignatureResponse = Hex; diff --git a/packages/core-sdk/src/types/resources/dispute.ts b/packages/core-sdk/src/types/resources/dispute.ts index caaf7883..a650b561 100644 --- a/packages/core-sdk/src/types/resources/dispute.ts +++ b/packages/core-sdk/src/types/resources/dispute.ts @@ -2,15 +2,6 @@ import { Address } from "viem"; import { TxOptions } from "../options"; -export type Dispute = { - targetIpId: Address; // The ipId that is the target of the dispute - disputeInitiator: Address; // The address of the dispute initiator - arbitrationPolicy: Address; // The address of the arbitration policy - linkToDisputeEvidence: string; // The link of the dispute evidence - targetTag: string; // The target tag of the dispute - currentTag: string; // The current tag of the dispute -}; - export type RaiseDisputeRequest = { targetIpId: Address; arbitrationPolicy: Address; @@ -26,20 +17,6 @@ export type RaiseDisputeResponse = { arbitrationPolicy?: Address; }; -export type SetDisputeJudgementRequest = { - disputeId: number; - decision: boolean; - calldata?: Address; - txOptions?: TxOptions; -}; - -export type SetDisputeJudgementResponse = { - txHash: string; - disputeId?: bigint; - decision?: boolean; - data?: Address; -}; - export type CancelDisputeRequest = { disputeId: number | string | bigint; calldata?: Address; diff --git a/packages/core-sdk/src/utils/sign.ts b/packages/core-sdk/src/utils/sign.ts index 3763c682..6eb98106 100644 --- a/packages/core-sdk/src/utils/sign.ts +++ b/packages/core-sdk/src/utils/sign.ts @@ -1,9 +1,9 @@ -import { Hex, encodeFunctionData, toFunctionSelector } from "viem"; +import { encodeFunctionData, toFunctionSelector } from "viem"; import { accessControllerAbi, accessControllerAddress } from "../abi/generated"; import { getAddress } from "./utils"; import { defaultFunctionSelector } from "../constants/common"; -import { PermissionSignatureRequest } from "../types/common"; +import { PermissionSignatureRequest, PermissionSignatureResponse } from "../types/common"; /** * Get the signature for setting permissions. @@ -17,7 +17,9 @@ import { PermissionSignatureRequest } from "../types/common"; * @param param.permissionFunc - The permission function,default function is setPermission. * @returns A Promise that resolves to the signature. */ -export const getPermissionSignature = async (param: PermissionSignatureRequest): Promise => { +export const getPermissionSignature = async ( + param: PermissionSignatureRequest, +): Promise => { const { ipId, deadline, nonce, wallet, chainId, permissions, permissionFunc } = param; if (!wallet.signTypedData) { throw new Error("The wallet client does not support signTypedData, please try again."); @@ -32,17 +34,17 @@ export const getPermissionSignature = async (param: PermissionSignatureRequest): args: permissionFunction === "setPermission" ? [ - permissions[0].ipId, - permissions[0].signer, - permissions[0].to, + getAddress(permissions[0].ipId, "permissions[0].ipId"), + getAddress(permissions[0].signer, "permissions[0].signer"), + getAddress(permissions[0].to, "permissions[0].to"), permissions[0].func ? toFunctionSelector(permissions[0].func) : defaultFunctionSelector, permissions[0].permission, ] : [ - permissions.map((item) => ({ - ipAccount: item.ipId, - signer: item.signer, - to: item.to, + permissions.map((item, index) => ({ + ipAccount: getAddress(item.ipId, `permissions[${index}].ipId`), + signer: getAddress(item.signer, `permissions[${index}].signer`), + to: getAddress(item.to, `permissions[${index}].to`), func: item.func ? toFunctionSelector(item.func) : defaultFunctionSelector, permission: item.permission, })), @@ -54,7 +56,7 @@ export const getPermissionSignature = async (param: PermissionSignatureRequest): name: "Story Protocol IP Account", version: "1", chainId: Number(chainId), - verifyingContract: ipId, + verifyingContract: getAddress(ipId, "ipId"), }, types: { Execute: [ @@ -74,7 +76,7 @@ export const getPermissionSignature = async (param: PermissionSignatureRequest): value: BigInt(0), data, nonce: BigInt(nonce), - deadline, + deadline: BigInt(deadline), }, }); }; diff --git a/packages/core-sdk/src/utils/utils.ts b/packages/core-sdk/src/utils/utils.ts index 8c7009ae..20885244 100644 --- a/packages/core-sdk/src/utils/utils.ts +++ b/packages/core-sdk/src/utils/utils.ts @@ -4,8 +4,6 @@ import { Abi, decodeEventLog, PublicClient, - encodeAbiParameters, - parseAbiParameters, Chain, ContractEventName, Hex, @@ -15,14 +13,8 @@ import { } from "viem"; import { sepolia } from "viem/chains"; -import { TypedData } from "../types/common"; import { SupportedChainIds } from "../types/config"; -export function isIntegerString(s: string): boolean { - const num = Number(s); - return !isNaN(num) && parseInt(s, 10) === num; -} - export async function waitTxAndFilterLog< const TAbi extends Abi | readonly unknown[], TEventName extends ContractEventName | undefined = ContractEventName, @@ -85,64 +77,6 @@ export async function waitTx( }); } -export function dictToQueryParams(params: Record): string { - const queryParamList: string[] = []; - for (const key in params) { - const value = params[key]; - queryParamList.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); - } - - return queryParamList.join("&"); -} - -export function typedDataArrayToBytesArray(typedDataArray: Array): Array { - const result: Array = []; - typedDataArray.forEach(function (typedData: TypedData) { - result.push(typedDataToBytes(typedData)); - }); - return result; -} - -export function typedDataToBytes(typedData: TypedData): Hex { - return encodeAbiParameters(parseAbiParameters(typedData.interface), typedData.data); -} - -export function decodeShortstring(hexString: string): string { - if (hexString.startsWith("0x")) { - hexString = hexString.slice(2); - } - - // Replace the last two hexadecimal digits with zero - hexString = hexString.substring(0, hexString.length - 2) + "00"; - - let asciiString = ""; - for (let i = 0; i < hexString.length; i += 2) { - const hexByte = hexString.substring(i, i + 2); - if (hexByte === "00") { - break; // Stop if padding (00) is found - } - asciiString += String.fromCharCode(parseInt(hexByte, 16)); - } - return asciiString; -} - -export function splitIntoBytes32(hexString: string): Hex[] { - if (hexString.startsWith("0x")) { - hexString = hexString.slice(2); // Remove the '0x' prefix - } - - if (hexString.length % 64 !== 0) { - throw new Error("Hex string length must be a multiple of 64."); - } - - const bytes32Array: string[] = []; - for (let i = 0; i < hexString.length; i += 64) { - bytes32Array.push("0x" + hexString.substring(i, i + 64)); - } - - return bytes32Array as Hex[]; -} - export function chainStringToViemChain(chainId: SupportedChainIds): Chain { switch (chainId) { case "11155111": diff --git a/packages/core-sdk/test/unit/client.test.ts b/packages/core-sdk/test/unit/client.test.ts index 6f80c610..10882ff8 100644 --- a/packages/core-sdk/test/unit/client.test.ts +++ b/packages/core-sdk/test/unit/client.test.ts @@ -2,17 +2,7 @@ import { expect } from "chai"; import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; import { sepolia } from "viem/chains"; import { createPublicClient, createWalletClient, http, Transport } from "viem"; -import { - DisputeClient, - LicenseClient, - NftClient, - PermissionClient, - StoryClient, - StoryConfig, -} from "../../src"; -import { StoryAPIClient } from "../../src/clients/storyAPI"; -import { RoyaltyClient } from "../../src/resources/royalty"; -import { sepoliaChainId } from "../integration/utils/util"; +import { StoryClient, StoryConfig } from "../../src"; const rpc = "http://127.0.0.1:8545"; describe("Test StoryClient", () => { @@ -95,45 +85,14 @@ describe("Test StoryClient", () => { }); const client: StoryClient = StoryClient.newClient(config); - const storyAPIClient = new StoryAPIClient(); - it("client ipAsset should not be empty", () => { - expect(client.ipAsset).to.not.equal(null); - expect(client.ipAsset).to.not.equal(undefined); - }); - - it("should return client permission", () => { - const permission = new PermissionClient(rpcClient, wallet, "sepolia"); - expect(client.permission).to.not.equal(null); - expect(client.permission).to.not.equal(undefined); - }); - - it("should return client license", () => { - const license = new LicenseClient(rpcClient, wallet); - expect(client.license).to.not.equal(null); - expect(client.license).to.not.equal(undefined); - }); - - it("should return client account", () => { - expect(client.ipAccount).to.not.equal(null); - expect(client.ipAccount).to.not.equal(undefined); - }); - - it("should return client dispute", () => { - const dispute = new DisputeClient(rpcClient, wallet); - expect(client.dispute).to.not.equal(null); - expect(client.dispute).to.not.equal(undefined); - }); - - it("should return client royalty", () => { - const royalty = new RoyaltyClient(rpcClient, wallet); - expect(client.royalty).to.not.equal(null); - expect(client.royalty).to.not.equal(undefined); - }); - - it("should return client nft", () => { - const royalty = new NftClient(rpcClient, wallet); - expect(client.nftClient).to.not.equal(null); - expect(client.nftClient).to.not.equal(undefined); + it("client modules should not be empty", () => { + expect(client.ipAsset).to.not.equal(null).and.to.not.equal(undefined); + expect(client.permission).to.not.equal(null).and.to.not.equal(undefined); + expect(client.license).to.not.equal(null).and.to.not.equal(undefined); + expect(client.ipAccount).to.not.equal(null).and.to.not.equal(undefined); + expect(client.dispute).to.not.equal(null).and.to.not.equal(undefined); + expect(client.royalty).to.not.equal(null).and.to.not.equal(undefined); + expect(client.nftClient).to.not.equal(null).and.to.not.equal(undefined); }); }); }); diff --git a/packages/core-sdk/test/unit/clients/storyAPI.test.ts b/packages/core-sdk/test/unit/clients/storyAPI.test.ts deleted file mode 100644 index d93ff996..00000000 --- a/packages/core-sdk/test/unit/clients/storyAPI.test.ts +++ /dev/null @@ -1,82 +0,0 @@ -import sinon from "sinon"; -import chai from "chai"; -import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; -import { StoryAPIClient } from "../../../src/clients/storyAPI"; -import chaiAsPromised from "chai-as-promised"; -chai.use(chaiAsPromised); -const expect = chai.expect; -describe("Test StoryAPIClient", () => { - afterEach(() => { - sinon.restore(); - }); - - describe("test for getRoyaltyPolicy", () => { - afterEach(() => { - sinon.restore(); - }); - it("should resolve error if network error occurs", async () => { - sinon.stub(axios, "create").returns({ - get: (url: string, config?: AxiosRequestConfig): Promise => - Promise.reject(new Error("http 500")), - } as AxiosInstance); - const storyClient: StoryAPIClient = new StoryAPIClient(); - await expect(storyClient.getRoyaltyPolicy("1")).to.be.rejectedWith("http 500"); - }); - - it("should resolve royalty policy when request succeeded", async () => { - sinon.stub(axios, "create").returns({ - get: (url: string, config?: AxiosRequestConfig): Promise => - Promise.resolve({ - status: 200, - data: { - data: { - id: "3", - targetAncestors: ["0x222222222222222222222222222222222"], - targetRoyaltyAmount: ["1"], - }, - }, - } as AxiosResponse), - } as AxiosInstance); - const storyClient: StoryAPIClient = new StoryAPIClient(); - const { id, targetAncestors, targetRoyaltyAmount } = await storyClient.getRoyaltyPolicy("1"); - expect(id).to.equal("3"); - expect(targetAncestors[0]).to.equal("0x222222222222222222222222222222222"); - expect(targetRoyaltyAmount[0]).to.equal("1"); - }); - }); - - describe("test for getLicense", () => { - afterEach(() => { - sinon.restore(); - }); - it("should resolve error if network error occurs", async () => { - sinon.stub(axios, "create").returns({ - get: (url: string, config?: AxiosRequestConfig): Promise => - Promise.reject(new Error("http 500")), - } as AxiosInstance); - const storyClient: StoryAPIClient = new StoryAPIClient(); - await expect(storyClient.getLicense("1")).to.be.rejectedWith("http 500"); - }); - - it("should resolve license when request succeeded", async () => { - sinon.stub(axios, "create").returns({ - get: (url: string, config?: AxiosRequestConfig): Promise => - Promise.resolve({ - status: 200, - data: { - data: { - id: "3", - policyId: "2", - licensorIpId: "1", - }, - }, - } as AxiosResponse), - } as AxiosInstance); - const storyClient: StoryAPIClient = new StoryAPIClient(); - const { id, policyId, licensorIpId } = await storyClient.getLicense("1"); - expect(id).to.equal("3"); - expect(policyId).to.equal("2"); - expect(licensorIpId).to.equal("1"); - }); - }); -}); diff --git a/packages/core-sdk/test/unit/resources/dispute.test.ts b/packages/core-sdk/test/unit/resources/dispute.test.ts index a4419750..a28b60c1 100644 --- a/packages/core-sdk/test/unit/resources/dispute.test.ts +++ b/packages/core-sdk/test/unit/resources/dispute.test.ts @@ -3,8 +3,7 @@ import { createMock } from "../testUtils"; import chai, { expect } from "chai"; import chaiAsPromised from "chai-as-promised"; import { PublicClient, WalletClient } from "viem"; -import { CancelDisputeRequest, DisputeClient, ResolveDisputeRequest } from "../../../src"; -import { DisputeModuleClient } from "../../../src/abi/generated"; +import { DisputeClient } from "../../../src"; chai.use(chaiAsPromised); @@ -13,9 +12,7 @@ describe("Test DisputeClient", () => { let rpcMock: PublicClient; let walletMock: WalletClient; - const mock = { - txHash: "0x063834efe214f4199b1ad7181ce8c5ced3e15d271c8e866da7c89e86ee629cfb", - }; + const txHash = "0x063834efe214f4199b1ad7181ce8c5ced3e15d271c8e866da7c89e86ee629cfb"; beforeEach(() => { rpcMock = createMock(); @@ -27,228 +24,125 @@ describe("Test DisputeClient", () => { sinon.restore(); }); - describe("Should be able to", async () => { - const targetIpId = - "0xa3028b46ff4aeba585ebfa1c241ad4a453b6f10dc7bc3d3ebaa9cecc680a6f71" as `0x${string}`; - const arbitrationPolicy = "0xC6A1c49BCeeE2E512167d5c03e4753776477730b" as `0x${string}`; - const linkToDisputeEvidence = "foo"; - const targetTag = "bar"; - - it("should throw simulateContract error if simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().throws(new Error("simulateContract error")); - const raiseDisputeRequest = { - targetIpId, - arbitrationPolicy, - linkToDisputeEvidence, - targetTag, - txOptions: { - waitForTransaction: true, - }, - }; + describe("Test raiseDispute", () => { + it("throw address error when call raiseDispute with invalid targetIpId", async () => { try { - await disputeClient.raiseDispute(raiseDisputeRequest); - } catch (err) { - expect((err as Error).message).includes("simulateContract error"); + await disputeClient.raiseDispute({ + targetIpId: "0x", + arbitrationPolicy: "0x", + linkToDisputeEvidence: "link", + targetTag: "tag", + }); + } catch (e) { + expect((e as Error).message).equal( + "Failed to raise dispute: request.targetIpId address is invalid: 0x, Address must be a hex value of 20 bytes (40 hex characters) and match its checksum counterpart.", + ); } }); - it("should throw writeContract error if writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().throws(new Error("writeContract error")); - const raiseDisputeRequest = { - targetIpId, - arbitrationPolicy, - linkToDisputeEvidence, - targetTag, - txOptions: { - waitForTransaction: true, - }, - }; - try { - await disputeClient.raiseDispute(raiseDisputeRequest); - } catch (err) { - expect((err as Error).message).includes("writeContract error"); - } - }); + it("should return txHash when call raiseDispute successfully", async () => { + sinon.stub(disputeClient.disputeModuleClient, "raiseDispute").resolves(txHash); + const result = await disputeClient.raiseDispute({ + targetIpId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", + arbitrationPolicy: "0x", + linkToDisputeEvidence: "link", + targetTag: "tag", + }); - it("should return txHash only if request.txOptions is missing", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - disputeClient.disputeModuleClient.parseTxDisputeRaisedEvent = sinon.stub().resolves([ - { - disputeId: "7", - }, - ]); - const raiseDisputeRequest = { - targetIpId, - arbitrationPolicy, - linkToDisputeEvidence, - targetTag, - }; - - const result = await disputeClient.raiseDispute(raiseDisputeRequest); - expect(Object.keys(result).length).to.equal(1); - expect(Object.keys(result)[0]).to.equal("txHash"); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); }); - it("should return txHash and disputeId if request.txOptions is present", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves({}); - - disputeClient.disputeModuleClient = createMock(); - disputeClient.disputeModuleClient.raiseDispute = sinon.stub().resolves(mock.txHash); - disputeClient.disputeModuleClient.parseTxDisputeRaisedEvent = sinon.stub().returns([ + it("should return txHash and disputeId when call raiseDispute successfully with waitForTransaction", async () => { + sinon.stub(disputeClient.disputeModuleClient, "raiseDispute").resolves(txHash); + sinon.stub(disputeClient.disputeModuleClient, "parseTxDisputeRaisedEvent").returns([ { - disputeId: "7", + disputeId: 1n, + targetIpId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", + disputeInitiator: "0x", + arbitrationPolicy: "0x", + linkToDisputeEvidence: "0x", + targetTag: "0x", + data: "0x", }, ]); - const raiseDisputeRequest = { - targetIpId, - arbitrationPolicy, - linkToDisputeEvidence, - targetTag, - txOptions: { - waitForTransaction: true, - }, - }; - - const result = await disputeClient.raiseDispute(raiseDisputeRequest); - expect(result.txHash).to.equal(mock.txHash); - expect(result.disputeId).to.equal(BigInt(7).toString()); + const result = await disputeClient.raiseDispute({ + targetIpId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", + arbitrationPolicy: "0x", + linkToDisputeEvidence: "link", + targetTag: "tag", + txOptions: { waitForTransaction: true }, + }); + + expect(result.txHash).equal(txHash); + expect(result.disputeId).equal(1n); }); }); - describe("test for cancelDispute", () => { - it("should throw simulateContract error if simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().throws(new Error("simulateContract error")); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - - const cancelDisputeRequest: CancelDisputeRequest = { - disputeId: 1, - txOptions: { - waitForTransaction: true, - }, - }; + describe("Test cancelDispute", () => { + it("should throw error when call cancelDispute failed", async () => { + sinon.stub(disputeClient.disputeModuleClient, "cancelDispute").rejects(new Error("500")); try { - await disputeClient.cancelDispute(cancelDisputeRequest); - } catch (err) { - expect((err as Error).message.includes("simulateContract error")); + await disputeClient.cancelDispute({ + disputeId: 1, + }); + } catch (e) { + expect((e as Error).message).equal("Failed to cancel dispute: 500"); } }); - it("should throw writeContract error if writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().throws(new Error("writeContract error")); - const cancelDisputeRequest: CancelDisputeRequest = { + it("should return txHash when call cancelDispute successfully", async () => { + sinon.stub(disputeClient.disputeModuleClient, "cancelDispute").resolves(txHash); + const result = await disputeClient.cancelDispute({ disputeId: 1, - txOptions: { - waitForTransaction: true, - }, - }; - try { - await disputeClient.cancelDispute(cancelDisputeRequest); - } catch (err) { - expect((err as Error).message.includes("writeContract error")); - } - }); + calldata: "0x", + }); - it("should return txHash", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - const cancelDisputeRequest: CancelDisputeRequest = { - disputeId: 1, - txOptions: { - waitForTransaction: true, - }, - }; - - const result = await disputeClient.cancelDispute(cancelDisputeRequest); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); }); - it("should return txHash if txOptions.waitForTransaction is falsy", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - const cancelDisputeRequest: CancelDisputeRequest = { + + it("should return txHash when call cancelDispute successfully with waitForTransaction", async () => { + sinon.stub(disputeClient.disputeModuleClient, "cancelDispute").resolves(txHash); + const result = await disputeClient.cancelDispute({ disputeId: 1, - calldata: "0x1111", - txOptions: { - waitForTransaction: false, - }, - }; + txOptions: { waitForTransaction: true }, + }); - const result = await disputeClient.cancelDispute(cancelDisputeRequest); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); }); }); - describe("test for resolveDispute", () => { - it("should throw simulateContract error if simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().throws(new Error("simulateContract error")); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - - const resolveDisputeRequest: ResolveDisputeRequest = { - disputeId: 1, - data: "0x1111", - txOptions: { - waitForTransaction: true, - }, - }; + describe("Test resolveDispute", () => { + it("should throw error when call resolveDispute failed", async () => { + sinon.stub(disputeClient.disputeModuleClient, "resolveDispute").rejects(new Error("500")); try { - await disputeClient.resolveDispute(resolveDisputeRequest); - } catch (err) { - expect((err as Error).message.includes("simulateContract error")); + await disputeClient.resolveDispute({ + disputeId: 1, + data: "0x", + }); + } catch (e) { + expect((e as Error).message).equal("Failed to resolve dispute: 500"); } }); - it("should throw writeContract error if writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().throws(new Error("writeContract error")); - const resolveDisputeRequest: ResolveDisputeRequest = { + it("should return txHash when call resolveDispute successfully", async () => { + sinon.stub(disputeClient.disputeModuleClient, "resolveDispute").resolves(txHash); + const result = await disputeClient.resolveDispute({ disputeId: 1, - data: "0x1111", - txOptions: { - waitForTransaction: true, - }, - }; - try { - await disputeClient.resolveDispute(resolveDisputeRequest); - } catch (err) { - expect((err as Error).message.includes("writeContract error")); - } - }); - - it("should return txHash", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - const resolveDisputeRequest: ResolveDisputeRequest = { - disputeId: 1, - data: "0x1111", - txOptions: { - waitForTransaction: true, - }, - }; + data: "0x", + }); - const result = await disputeClient.resolveDispute(resolveDisputeRequest); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); }); - it("should return txHash if txOptions.waitForTransaction is falsy", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - const resolveDisputeRequest: ResolveDisputeRequest = { + + it("should return txHash when call resolveDispute successfully with waitForTransaction", async () => { + sinon.stub(disputeClient.disputeModuleClient, "resolveDispute").resolves(txHash); + const result = await disputeClient.resolveDispute({ disputeId: 1, - data: "0x1111", - txOptions: { - waitForTransaction: false, - }, - }; + data: "0x", + txOptions: { waitForTransaction: true }, + }); - const result = await disputeClient.resolveDispute(resolveDisputeRequest); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); }); }); }); diff --git a/packages/core-sdk/test/unit/resources/ipAccount.test.ts b/packages/core-sdk/test/unit/resources/ipAccount.test.ts index 77b3dc0c..dda58f98 100644 --- a/packages/core-sdk/test/unit/resources/ipAccount.test.ts +++ b/packages/core-sdk/test/unit/resources/ipAccount.test.ts @@ -11,7 +11,7 @@ describe("Test IPAccountClient", () => { let ipAccountClient: IPAccountClient; let rpcMock: PublicClient; let walletMock: WalletClient; - + const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; beforeEach(() => { rpcMock = createMock(); walletMock = createMock(); @@ -40,95 +40,32 @@ describe("Test IPAccountClient", () => { ); } }); - it("should throw simulateContract error when simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().rejects(new Error("simulateContract error")); - const request: IPAccountExecuteRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - }; - try { - await ipAccountClient.execute(request); - } catch (err) { - expect((err as Error).message).includes("simulateContract error"); - } - }); - it("should throw writeContract error when writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().rejects(new Error("writeContract error")); - const request: IPAccountExecuteRequest = { - ipId: zeroAddress, + it("should return txHash when call execute successfully", async () => { + IpAccountImplClient.prototype.execute = sinon.stub().resolves(txHash); + const result = await ipAccountClient.execute({ + ipId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", to: zeroAddress, value: 2, data: "0x11111111111111111111111111111", - }; - try { - await ipAccountClient.execute(request); - } catch (err) { - expect((err as Error).message).includes("writeContract error"); - } - }); + }); - it("should throw waitTx error when waitTx throws an error", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves({}); - walletMock.writeContract = sinon.stub().resolves(txHash); - sinon.stub(utils, "waitTx").rejects(new Error("waitTx error")); - const request: IPAccountExecuteRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - txOptions: { - waitForTransaction: true, - }, - }; - try { - await ipAccountClient.execute(request); - } catch (err) { - expect((err as Error).message).includes("waitTx error"); - } + expect(result.txHash).to.equal(txHash); }); - it("should return txHash if waitTx succeeds", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves({}); - walletMock.writeContract = sinon.stub().resolves(txHash); + it("should return txHash when call execute successfully with waitForTransaction", async () => { + IpAccountImplClient.prototype.execute = sinon.stub().resolves(txHash); sinon.stub(utils, "waitTx").resolves(); - const request: IPAccountExecuteRequest = { - ipId: zeroAddress, + const result = await ipAccountClient.execute({ + ipId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", to: zeroAddress, value: 2, data: "0x11111111111111111111111111111", txOptions: { waitForTransaction: true, }, - }; - - const result = await ipAccountClient.execute(request); - expect(result.txHash).to.equal(txHash); - }); - - it("should return txHash if txOptions is falsy", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(txHash); - sinon.stub(utils, "waitTx").rejects(new Error("waitTx error")); - const request: IPAccountExecuteRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - txOptions: { - waitForTransaction: false, - }, - }; + }); - const result = await ipAccountClient.execute(request); expect(result.txHash).to.equal(txHash); }); }); @@ -153,76 +90,26 @@ describe("Test IPAccountClient", () => { } }); - it("should throw simulateContract error when simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().rejects(new Error("simulateContract error")); - const request: IPAccountExecuteWithSigRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - signer: zeroAddress, - deadline: 20, - signature: zeroAddress, - }; - try { - await ipAccountClient.executeWithSig(request); - } catch (err) { - expect((err as Error).message).includes("simulateContract error"); - } - }); - - it("should throw writeContract error when writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().rejects(new Error("writeContract error")); - const request: IPAccountExecuteWithSigRequest = { - ipId: zeroAddress, + it("should return txHash when call executeWithSig successfully", async () => { + IpAccountImplClient.prototype.executeWithSig = sinon.stub().resolves(txHash); + const result = await ipAccountClient.executeWithSig({ + ipId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", to: zeroAddress, value: 2, data: "0x11111111111111111111111111111", signer: zeroAddress, deadline: 20, signature: zeroAddress, - }; - try { - await ipAccountClient.executeWithSig(request); - } catch (err) { - expect((err as Error).message).includes("writeContract error"); - } - }); + }); - it("should throw waitTx error when waitTx throws an error", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves({}); - walletMock.writeContract = sinon.stub().resolves(txHash); - sinon.stub(utils, "waitTx").rejects(new Error("waitTx error")); - const request: IPAccountExecuteWithSigRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - signer: zeroAddress, - deadline: 20, - signature: zeroAddress, - txOptions: { - waitForTransaction: true, - }, - }; - try { - await ipAccountClient.executeWithSig(request); - } catch (err) { - expect((err as Error).message).includes("waitTx error"); - } + expect(result.txHash).to.equal(txHash); }); - it("should return txHash when waitTx succeeds", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves({}); - walletMock.writeContract = sinon.stub().resolves(txHash); + it("should return txHash when call executeWithSig successfully with waitForTransaction", async () => { + IpAccountImplClient.prototype.executeWithSig = sinon.stub().resolves(txHash); sinon.stub(utils, "waitTx").resolves(); - const request: IPAccountExecuteWithSigRequest = { - ipId: zeroAddress, + const result = await ipAccountClient.executeWithSig({ + ipId: "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c", to: zeroAddress, value: 2, data: "0x11111111111111111111111111111", @@ -232,30 +119,8 @@ describe("Test IPAccountClient", () => { txOptions: { waitForTransaction: true, }, - }; - const result = await ipAccountClient.executeWithSig(request); - expect(result.txHash).to.equal(txHash); - }); - - it("should return txHash if txOptions is falsy", async () => { - const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(txHash); - sinon.stub(utils, "waitTx").rejects(new Error("waitTx error")); - const request: IPAccountExecuteWithSigRequest = { - ipId: zeroAddress, - to: zeroAddress, - value: 2, - data: "0x11111111111111111111111111111", - signer: zeroAddress, - deadline: 20, - signature: zeroAddress, - txOptions: { - waitForTransaction: false, - }, - }; + }); - const result = await ipAccountClient.executeWithSig(request); expect(result.txHash).to.equal(txHash); }); }); diff --git a/packages/core-sdk/test/unit/resources/nftClient.test.ts b/packages/core-sdk/test/unit/resources/nftClient.test.ts index 2c9809e2..3698bc23 100644 --- a/packages/core-sdk/test/unit/resources/nftClient.test.ts +++ b/packages/core-sdk/test/unit/resources/nftClient.test.ts @@ -12,11 +12,8 @@ describe("Test NftClient", () => { let nftClient: NftClient; let rpcMock: PublicClient; let walletMock: WalletClient; - - const mock = { - txHash: "0x063834efe214f4199b1ad7181ce8c5ced3e15d271c8e866da7c89e86ee629cfb", - nftContract: "0x73fcb515cee99e4991465ef586cfe2b072ebb512", - }; + const txHash = "0x063834efe214f4199b1ad7181ce8c5ced3e15d271c8e866da7c89e86ee629cfb"; + const mintFeeToken = "0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c"; beforeEach(() => { rpcMock = createMock(); @@ -29,83 +26,64 @@ describe("Test NftClient", () => { }); describe("test for CreateNFTCollection", () => { - const reqBody: CreateNFTCollectionRequest = { - name: "test-collection", - symbol: "TEST", - maxSupply: 100, - owner: "0x0000000000000000000000000000000000000001" as Hex, - txOptions: { - waitForTransaction: false, - }, - }; - - it("should throw simulateContract error if simulateContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().throws(new Error("simulateContract error")); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - + it("should throw mint fee error when call createNFTCollection given mintFee less than 0", async () => { try { - await nftClient.createNFTCollection(reqBody); - } catch (err) { - expect((err as Error).message.includes("simulateContract error")); - } - }); - - it("should throw writeContract error if writeContract throws an error", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().throws(new Error("writeContract error")); - - try { - await nftClient.createNFTCollection(reqBody); - } catch (err) { - expect((err as Error).message.includes("writeContract error")); + await nftClient.createNFTCollection({ + name: "name", + symbol: "symbol", + maxSupply: 1, + mintFee: -1n, + }); + } catch (e) { + expect((e as Error).message).equal( + "Failed to create a SPG NFT collection: Invalid mint fee token address, mint fee is greater than 0.", + ); } }); - it("should throw Invalid mintFee and mintFeeToken error if mintFee is 0", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); + it("should throw mint fee error when call createNFTCollection given mintFeeToken is invalid", async () => { try { - await nftClient.createNFTCollection({ ...reqBody, mintFee: 0n }); - } catch (err) { - expect((err as Error).message).equal( + await nftClient.createNFTCollection({ + name: "name", + symbol: "symbol", + maxSupply: 1, + mintFee: 1n, + }); + } catch (e) { + expect((e as Error).message).equal( "Failed to create a SPG NFT collection: Invalid mint fee token address, mint fee is greater than 0.", ); } }); - it("should return txHash and nftContract if txOptions.waitForTransaction is truthy", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - rpcMock.waitForTransactionReceipt = sinon.stub().resolves(); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); + it("should return txHash when call createNFTCollection successfully", async () => { + sinon.stub(nftClient.spgClient, "createCollection").resolves(txHash); + const result = await nftClient.createNFTCollection({ + name: "name", + symbol: "symbol", + maxSupply: 1, + mintFee: 1n, + mintFeeToken: mintFeeToken, + }); - sinon.stub(nftClient.spgClient, "parseTxCollectionCreatedEvent").returns([ - { - nftContract: "0x73fcb515cee99e4991465ef586cfe2b072ebb512", - }, - ]); + expect(result.txHash).equal(txHash); + }); + it("should return txHash when call createNFTCollection successfully with waitForTransaction", async () => { + const nftContract = "0x73fcb515cee99e4991465ef586cfe2b072ebb512"; + sinon.stub(nftClient.spgClient, "createCollection").resolves(txHash); + sinon.stub(nftClient.spgClient, "parseTxCollectionCreatedEvent").returns([{ nftContract }]); const result = await nftClient.createNFTCollection({ - ...reqBody, - owner: undefined, + name: "name", + symbol: "symbol", + owner: "0x73fcb515cee99e4991465ef586cfe2b072ebb512", txOptions: { waitForTransaction: true, }, }); - expect(result.txHash).to.equal(mock.txHash); - expect(result.nftContract).to.equal(mock.nftContract); - }); - - it("should return txHash if txOptions.waitForTransaction is falsy", async () => { - rpcMock.simulateContract = sinon.stub().resolves({ request: null }); - walletMock.writeContract = sinon.stub().resolves(mock.txHash); - const result = await nftClient.createNFTCollection({ - ...reqBody, - maxSupply: undefined, - mintFee: undefined, - mintFeeToken: undefined, - }); - expect(result.txHash).to.equal(mock.txHash); + expect(result.txHash).equal(txHash); + expect(result.nftContract).equal(nftContract); }); }); }); diff --git a/packages/core-sdk/test/unit/resources/permission.test.ts b/packages/core-sdk/test/unit/resources/permission.test.ts index 130ff20b..7b482ff3 100644 --- a/packages/core-sdk/test/unit/resources/permission.test.ts +++ b/packages/core-sdk/test/unit/resources/permission.test.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { createMock } from "../testUtils"; import * as sinon from "sinon"; import { PermissionClient, AddressZero } from "../../../src"; -import { PublicClient, WalletClient, Account, LocalAccount } from "viem"; +import { PublicClient, WalletClient, LocalAccount } from "viem"; import { AccessPermission } from "../../../src/types/resources/permission"; const { IpAccountImplClient } = require("../../../src/abi/generated"); diff --git a/packages/core-sdk/test/unit/utils/utils.test.ts b/packages/core-sdk/test/unit/utils/utils.test.ts index a69eda28..7825a741 100644 --- a/packages/core-sdk/test/unit/utils/utils.test.ts +++ b/packages/core-sdk/test/unit/utils/utils.test.ts @@ -5,12 +5,7 @@ import { sepolia } from "viem/chains"; import { TypedData } from "../../../src/types/common"; import { SupportedChainIds } from "../../../src/types/config"; import { - isIntegerString, - decodeShortstring, - splitIntoBytes32, waitTxAndFilterLog, - dictToQueryParams, - typedDataArrayToBytesArray, chainStringToViemChain, waitTx, getAddress, @@ -18,57 +13,6 @@ import { import { createMock } from "../testUtils"; import { licensingModuleAbi } from "../../../src/abi/generated"; -describe("Test isIntegerString", () => { - it("should return true when passing in an integer string", () => { - expect(isIntegerString("7")).to.be.true; - }); - - it("should return false when passing in a non-integer string", () => { - expect(isIntegerString("a")).to.be.false; - }); -}); - -describe("Test decodeShortstring", () => { - it("should decode a short string", () => { - const encodedString = "0x4368616e6e656c732d4f662d446973747269627574696f6e0000000000000018"; // "Hello World" in hex - const decodedString = decodeShortstring(encodedString); - expect(decodedString).to.equal("Channels-Of-Distribution"); - }); - - it("should return an empty string for an empty input", () => { - const encodedString = "0x"; - const decodedString = decodeShortstring(encodedString); - expect(decodedString).to.equal(""); - }); -}); - -describe("splitIntoBytes32", () => { - it("splits a valid hex string into bytes32 chunks", () => { - const hexString = "a".repeat(64); - const result = splitIntoBytes32(hexString); - expect(result).to.deep.equal(["0x" + "a".repeat(64)]); - }); - - it("handles hex strings with 0x prefix correctly", () => { - const hexString = "0x" + "b".repeat(64); - const result = splitIntoBytes32(hexString); - expect(result).to.deep.equal(["0x" + "b".repeat(64)]); - }); - - it("throws an error if hex string length is not a multiple of 64", () => { - const hexString = "c".repeat(63); - expect(() => splitIntoBytes32(hexString)).to.throw( - "Hex string length must be a multiple of 64.", - ); - }); - - it("handles empty hex string", () => { - const hexString = ""; - const result = splitIntoBytes32(hexString); - expect(result).to.deep.equal([]); - }); -}); - describe("Test waitTxAndFilterLog", () => { const txHash = "0x129f7dd802200f096221dd89d5b086e4bd3ad6eafb378a0c75e3b04fc375f997"; let rpcMock: viem.PublicClient = createMock(); @@ -200,17 +144,6 @@ describe("Test waitTxAndFilterLog", () => { }); }); -describe("Test dictToQueryParams", () => { - it("should return expected query string", () => { - const query = { - package: "sdk", - test: "dictToQueryParams", - }; - const queryStr = dictToQueryParams(query); - expect(queryStr).to.equal("package=sdk&test=dictToQueryParams"); - }); -}); - describe("Test chainStringToViemChain", () => { it("should throw error if chainId is not supported", () => { try { @@ -220,42 +153,6 @@ describe("Test chainStringToViemChain", () => { } }); - describe("Test typedDataArrayToBytesArray", () => { - it("should return expected bytes array", () => { - const data = [ - [ - [viem.zeroAddress], - [20, 2, 4], - [viem.zeroAddress], - [viem.zeroAddress], - [1, 2, 3], - [1, 2, 3], - ], - ]; - const typedDataArray: Array = [ - { - interface: "(address[], uint32[], address[], address[], uint32[], uint32[])", - data, - }, - ]; - const result = typedDataArrayToBytesArray(typedDataArray); - const decodedResult = viem.decodeAbiParameters( - viem.parseAbiParameters("(address[], uint32[], address[], address[], uint32[], uint32[])"), - result[0], - ); - expect(result.length).to.equal(1); - expect(result[0].startsWith("0x")).to.equal(true); - expect(decodedResult.length).to.equal(1); - expect(decodedResult[0].length).to.equal(6); - expect(decodedResult[0][0]).includes.all.members(data[0][0]); - expect(decodedResult[0][1]).includes.all.members(data[0][1]); - expect(decodedResult[0][2]).includes.all.members(data[0][2]); - expect(decodedResult[0][3]).includes.all.members(data[0][3]); - expect(decodedResult[0][4]).includes.all.members(data[0][4]); - expect(decodedResult[0][5]).includes.all.members(data[0][5]); - }); - }); - it("should return sepolia if id is 11155111", () => { const chain = chainStringToViemChain("11155111"); expect(chain).to.equal(sepolia);