From ac78ad661628c70c98089569db56dde837ddec47 Mon Sep 17 00:00:00 2001 From: Bonnie Date: Thu, 9 Jan 2025 11:10:42 +0800 Subject: [PATCH] Add more validation for getRevenueShare --- .../core-sdk/src/utils/licenseTermsHelper.ts | 3 ++ .../test/unit/resources/ipAsset.test.ts | 29 ++++++++++++++++++- .../unit/utils/licenseTermsHelper.test.ts | 28 +++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/core-sdk/src/utils/licenseTermsHelper.ts b/packages/core-sdk/src/utils/licenseTermsHelper.ts index 5925fef4..b5c496fb 100644 --- a/packages/core-sdk/src/utils/licenseTermsHelper.ts +++ b/packages/core-sdk/src/utils/licenseTermsHelper.ts @@ -165,6 +165,9 @@ const verifyDerivatives = (terms: LicenseTerms) => { export const getRevenueShare = (revShare: number | string) => { const revShareNumber = Number(revShare); + if (isNaN(revShareNumber)) { + throw new Error("CommercialRevShare must be a valid number."); + } if (revShareNumber < 0 || revShareNumber > 100) { throw new Error("CommercialRevShare should be between 0 and 100."); } diff --git a/packages/core-sdk/test/unit/resources/ipAsset.test.ts b/packages/core-sdk/test/unit/resources/ipAsset.test.ts index 62d0bca2..d3472c89 100644 --- a/packages/core-sdk/test/unit/resources/ipAsset.test.ts +++ b/packages/core-sdk/test/unit/resources/ipAsset.test.ts @@ -3202,7 +3202,34 @@ describe("Test IpAssetClient", () => { ); } }); - + it("should throw commercial terms error when mintAndRegisterIpAndMakeDerivativeAndDistributeRoyaltyTokens given license terms id is not commercial", async () => { + try { + sinon.stub(ipAssetClient.licenseTemplateClient, "getLicenseTerms").resolves({ + terms: { + ...licenseTerms, + commercialUse: false, + }, + }); + await ipAssetClient.mintAndRegisterIpAndMakeDerivativeAndDistributeRoyaltyTokens({ + spgNftContract, + royaltyShares: [ + { recipient: "0x73fcb515cee99e4991465ef586cfe2b072ebb512", percentage: 100 }, + ], + derivData: { + parentIpIds: ["0x1daAE3197Bc469Cb97B917aa460a12dD95c6627c"], + licenseTermsIds: [1n], + maxRevenueShare: 100, + maxMintingFee: 100, + maxRts: 100, + }, + allowDuplicates: false, + }); + } catch (err) { + expect((err as Error).message).equal( + "Failed to mint and register IP and make derivative and distribute royalty tokens: The license terms attached to the IP must be a commercial license to distribute royalty tokens.", + ); + } + }); it("should return txHash when mintAndRegisterIpAndMakeDerivativeAndDistributeRoyaltyTokens given correct args", async () => { sinon.stub(ipAssetClient.licenseRegistryReadOnlyClient, "getRoyaltyPercent").resolves({ royaltyPercent: 100, diff --git a/packages/core-sdk/test/unit/utils/licenseTermsHelper.test.ts b/packages/core-sdk/test/unit/utils/licenseTermsHelper.test.ts index aba0899e..7f34601a 100644 --- a/packages/core-sdk/test/unit/utils/licenseTermsHelper.test.ts +++ b/packages/core-sdk/test/unit/utils/licenseTermsHelper.test.ts @@ -1,6 +1,10 @@ import { Hex, PublicClient, zeroAddress } from "viem"; import { LicenseTerms, PIL_TYPE } from "../../../src/types/resources/license"; -import { getLicenseTermByType, validateLicenseTerms } from "../../../src/utils/licenseTermsHelper"; +import { + getLicenseTermByType, + getRevenueShare, + validateLicenseTerms, +} from "../../../src/utils/licenseTermsHelper"; import { expect } from "chai"; import { MockERC20 } from "../../integration/utils/mockERC20"; import sinon from "sinon"; @@ -506,4 +510,26 @@ describe("License Terms Helper", () => { }); }); }); + + describe("getRevenueShare", () => { + it("should throw error when call getRevenueShare given revShare is not a number", async () => { + expect(() => getRevenueShare("not a number")).to.throw( + "CommercialRevShare must be a valid number.", + ); + }); + + it("should throw error when call getRevenueShare given revShare is less than 0", async () => { + expect(() => getRevenueShare(-1)).to.throw("CommercialRevShare should be between 0 and 100."); + }); + + it("should throw error when call getRevenueShare given revShare is greater than 100", async () => { + expect(() => getRevenueShare(101)).to.throw( + "CommercialRevShare should be between 0 and 100.", + ); + }); + + it("should return correct value when call getRevenueShare given revShare is 10", async () => { + expect(getRevenueShare(10)).to.equal(10000000); + }); + }); });