Skip to content

Commit

Permalink
Add more validation for getRevenueShare
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnie57 committed Jan 9, 2025
1 parent e43c4ce commit ac78ad6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/core-sdk/src/utils/licenseTermsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
29 changes: 28 additions & 1 deletion packages/core-sdk/test/unit/resources/ipAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion packages/core-sdk/test/unit/utils/licenseTermsHelper.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit ac78ad6

Please sign in to comment.