Skip to content

Commit

Permalink
Update royalty.test.ts
Browse files Browse the repository at this point in the history
Here is a refactored  version of the code. Repeated logic has been extracted into utility functions to reduce redundancy and improve readability.
  • Loading branch information
Aleksejs0585 authored Dec 30, 2024
1 parent 23afff8 commit bf96797
Showing 1 changed file with 58 additions and 319 deletions.
377 changes: 58 additions & 319 deletions test/hardhat/e2e/royalty/royalty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,329 +8,68 @@ import { mintNFTAndRegisterIPA } from "../utils/mintNFTAndRegisterIPA";
import { terms } from "../licenseTermsTemplate";

describe("RoyaltyModule", function () {
let signers:any;
let ipId1: any;
let ipId2: any;
let ipId3: any;
let ipId4: any;
let licenseTermsLAPId: any;
let licenseTermsLRPId: any;
let user1ConnectedLicensingModule: any;
let user2ConnectedLicensingModule: any;
let user3ConnectedLicensingModule: any;
let user1ConnectedRoyaltyModule: any;
let user2ConnectedRoyaltyModule: any;
let user3ConnectedRoyaltyModule: any;
let user2ConnectedRoyaltyPolicyLAP: any;
let user2ConnectedRoyaltyPolicyLRP: any;
let user3ConnectedRoyaltyPolicyLRP: any;
const testTerms = terms;

this.beforeAll("Get Signers and register license terms", async function () {
// Get the signers
signers = await hre.ethers.getSigners();

// Register a commericial remix license with royalty policy LAP
testTerms.royaltyPolicy = RoyaltyPolicyLAP;
testTerms.defaultMintingFee = 100;
testTerms.commercialUse = true;
testTerms.derivativesReciprocal = true;
testTerms.commercialRevShare = 10 * 10 ** 6;
testTerms.currency = MockERC20;

const connectedLicense = this.licenseTemplate.connect(signers[0]);
const registerLicenseLAPTx = await expect(
connectedLicense.registerLicenseTerms(testTerms)
).to.not.be.rejectedWith(Error);
await registerLicenseLAPTx.wait();

console.log("Transaction hash: ", registerLicenseLAPTx.hash);
expect(registerLicenseLAPTx.hash).not.to.be.empty.and.to.be.a("HexString");

licenseTermsLAPId = await connectedLicense.getLicenseTermsId(terms);
console.log("licenseTermsLAPId: ", licenseTermsLAPId);

testTerms.royaltyPolicy = RoyaltyPolicyLRP;
const registerLicenseLRPTx = await expect(
connectedLicense.registerLicenseTerms(testTerms)
).to.not.be.rejectedWith(Error);
await registerLicenseLRPTx.wait();

console.log("Transaction hash: ", registerLicenseLRPTx.hash);
expect(registerLicenseLRPTx.hash).not.to.be.empty.and.to.be.a("HexString");

licenseTermsLRPId = await connectedLicense.getLicenseTermsId(terms);
console.log("licenseTermsLRPId: ", licenseTermsLRPId);

user1ConnectedLicensingModule = this.licensingModule.connect(signers[0]);
user2ConnectedLicensingModule = this.licensingModule.connect(signers[1]);
user3ConnectedLicensingModule = this.licensingModule.connect(signers[2]);
user1ConnectedRoyaltyModule = this.royaltyModule.connect(signers[0]);
user2ConnectedRoyaltyModule = this.royaltyModule.connect(signers[1]);
user3ConnectedRoyaltyModule = this.royaltyModule.connect(signers[2]);
user2ConnectedRoyaltyPolicyLAP = this.royaltyPolicyLAP.connect(signers[1]);
user2ConnectedRoyaltyPolicyLRP = this.royaltyPolicyLRP.connect(signers[1]);
user3ConnectedRoyaltyPolicyLRP = this.royaltyPolicyLRP.connect(signers[2]);
});

it("Transfer LAP related inflows from royalty policy contract", async function () {
const mintingFee = terms.defaultMintingFee;
const payAmount = 1000 as number;
const commercialRevShare = terms.commercialRevShare / 10 ** 6 / 100;

const mintAndRegisterResp1 = await mintNFTAndRegisterIPA(signers[0], signers[0]);
ipId1 = mintAndRegisterResp1.ipId;
const mintAndRegisterResp2 = await mintNFTAndRegisterIPA(signers[1], signers[1]);
ipId2 = mintAndRegisterResp2.ipId;
const mintAndRegisterResp3 = await mintNFTAndRegisterIPA(signers[2], signers[2]);
ipId3 = mintAndRegisterResp3.ipId;

// IP1 attach the commercial remix license
const attachLicenseTx = await expect(
user1ConnectedLicensingModule.attachLicenseTerms(ipId1, PILicenseTemplate, licenseTermsLAPId)
).not.to.be.rejectedWith(Error);
await attachLicenseTx.wait();
console.log("Attach license transaction hash: ", attachLicenseTx.hash);
expect(attachLicenseTx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP2 is registered as IP1's derivative
const registerDerivative1Tx = await expect(
user2ConnectedLicensingModule.registerDerivative(ipId2, [ipId1], [licenseTermsLAPId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 0)
).not.to.be.rejectedWith(Error);
await registerDerivative1Tx.wait();
console.log("Register derivative transaction hash: ", registerDerivative1Tx.hash);
expect(registerDerivative1Tx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP3 is registered as IP2's derivative
const registerDerivative2Tx = await expect(
user3ConnectedLicensingModule.registerDerivative(ipId3, [ipId2], [licenseTermsLAPId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 0)
).not.to.be.rejectedWith(Error);
await registerDerivative2Tx.wait();
console.log("Register derivative transaction hash: ", registerDerivative2Tx.hash);
expect(registerDerivative2Tx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP3 payRoyaltyOnBehalf to IP2
const payRoyaltyOnBehalfTx = await expect(
user3ConnectedRoyaltyModule.payRoyaltyOnBehalf(ipId2, ipId3, MockERC20, BigInt(payAmount))
).not.to.be.rejectedWith(Error);
await payRoyaltyOnBehalfTx.wait();
console.log("Pay royalty on behalf transaction hash: ", payRoyaltyOnBehalfTx.hash);
expect(payRoyaltyOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP2 transferToVault
const transferToVaultTx1 = await expect(
user2ConnectedRoyaltyPolicyLAP.transferToVault(ipId2, ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
await transferToVaultTx1.wait();
console.log("Transfer to vault transaction hash: ", transferToVaultTx1.hash);
expect(transferToVaultTx1.hash).to.not.be.empty.and.to.be.a("HexString");

const ip2VaultAddress = await user2ConnectedRoyaltyModule.ipRoyaltyVaults(ipId2);
console.log("IP2's ipVaultAddress: ", ip2VaultAddress);

const ip2RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip2VaultAddress);

const ip1VaultAddress = await user1ConnectedRoyaltyModule.ipRoyaltyVaults(ipId1);
console.log("IP1's ipVaultAddress: ", ip1VaultAddress);

const ip1RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip1VaultAddress);

// check claimable revenue
const ip2ClaimableRevenue = await expect(
ip2RoyaltyVaultAddress.claimableRevenue(ipId2, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP2's claimableRevenue: ", ip2ClaimableRevenue);
expect(ip2ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt((payAmount + mintingFee) * (1 - commercialRevShare)));

// check claimable revenue
const ip1ClaimableRevenue = await expect(
ip1RoyaltyVaultAddress.claimableRevenue(ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP1's claimableRevenue: ", ip1ClaimableRevenue);
expect(ip1ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt(mintingFee +(payAmount + mintingFee) * commercialRevShare));

// claimRevenueOnBehalf
const ip2ClaimRevenueOnBehalfTx = await expect(
ip2RoyaltyVaultAddress.claimRevenueOnBehalf(ipId2, MockERC20)
).not.to.be.rejectedWith(Error);
await ip2ClaimRevenueOnBehalfTx.wait();
console.log("Claim revenue on behalf transaction hash: ", ip2ClaimRevenueOnBehalfTx.hash);
expect(ip2ClaimRevenueOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");

// claimRevenueOnBehalf
const ip1ClaimRevenueOnBehalfTx = await expect(
ip1RoyaltyVaultAddress.claimRevenueOnBehalf(ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
await ip1ClaimRevenueOnBehalfTx.wait();
console.log("Claim revenue on behalf transaction hash: ", ip1ClaimRevenueOnBehalfTx.hash);
expect(ip1ClaimRevenueOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");
});

it("Transfer LRP related inflows from royalty policy contract", async function () {
const mintingFee = terms.defaultMintingFee;
console.log("mintingFee: ", mintingFee);

const payAmount = 1000 as number;
const commercialRevShare = terms.commercialRevShare / 10 ** 6 / 100;
console.log("commercialRevShare: ", commercialRevShare);

const mintAndRegisterResp1 = await mintNFTAndRegisterIPA(signers[0], signers[0]);
ipId1 = mintAndRegisterResp1.ipId;
const mintAndRegisterResp2 = await mintNFTAndRegisterIPA(signers[1], signers[1]);
ipId2 = mintAndRegisterResp2.ipId;
const mintAndRegisterResp3 = await mintNFTAndRegisterIPA(signers[2], signers[2]);
ipId3 = mintAndRegisterResp3.ipId;
const mintAndRegisterResp4 = await mintNFTAndRegisterIPA(signers[2], signers[2]);
ipId4 = mintAndRegisterResp4.ipId;

// IP1 attach the commercial remix license
const attachLicenseTx = await expect(
user1ConnectedLicensingModule.attachLicenseTerms(ipId1, PILicenseTemplate, licenseTermsLRPId)
).not.to.be.rejectedWith(Error);
await attachLicenseTx.wait();
console.log("Attach license transaction hash: ", attachLicenseTx.hash);
expect(attachLicenseTx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP2 is registered as IP1's derivative
const registerDerivative1Tx = await expect(
user2ConnectedLicensingModule.registerDerivative(ipId2, [ipId1], [licenseTermsLRPId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 0)
).not.to.be.rejectedWith(Error);
await registerDerivative1Tx.wait();
console.log("Register derivative transaction hash: ", registerDerivative1Tx.hash);
expect(registerDerivative1Tx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP3 is registered as IP2's derivative
const registerDerivative2Tx = await expect(
user3ConnectedLicensingModule.registerDerivative(ipId3, [ipId2], [licenseTermsLRPId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 0)
).not.to.be.rejectedWith(Error);
await registerDerivative2Tx.wait();
console.log("Register derivative transaction hash: ", registerDerivative2Tx.hash);
expect(registerDerivative2Tx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP4 payRoyaltyOnBehalf to IP3
const payRoyaltyOnBehalfTx = await expect(
user3ConnectedRoyaltyModule.payRoyaltyOnBehalf(ipId3, ipId4, MockERC20, BigInt(payAmount))
).not.to.be.rejectedWith(Error);
await payRoyaltyOnBehalfTx.wait();
console.log("Pay royalty on behalf transaction hash: ", payRoyaltyOnBehalfTx.hash);
expect(payRoyaltyOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP3 transferToVault
const transferToVaultTx1 = await expect(
user3ConnectedRoyaltyPolicyLRP.transferToVault(ipId3, ipId2, MockERC20)
).not.to.be.rejectedWith(Error);
await transferToVaultTx1.wait();
console.log("Transfer to vault transaction hash: ", transferToVaultTx1.hash);
expect(transferToVaultTx1.hash).to.not.be.empty.and.to.be.a("HexString");

// IP2 transferToVault
const transferToVaultTx2 = await expect(
user2ConnectedRoyaltyPolicyLRP.transferToVault(ipId2, ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
await transferToVaultTx2.wait();
console.log("Transfer to vault transaction hash: ", transferToVaultTx2.hash);
expect(transferToVaultTx2.hash).to.not.be.empty.and.to.be.a("HexString");

const ip3VaultAddress = await user3ConnectedRoyaltyModule.ipRoyaltyVaults(ipId3);
console.log("IP3's ipVaultAddress: ", ip3VaultAddress);
const ip3RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip3VaultAddress);

const ip2VaultAddress = await user2ConnectedRoyaltyModule.ipRoyaltyVaults(ipId2);
console.log("IP2's ipVaultAddress: ", ip2VaultAddress);
const ip2RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip2VaultAddress);

const ip1VaultAddress = await user1ConnectedRoyaltyModule.ipRoyaltyVaults(ipId1);
console.log("IP1's ipVaultAddress: ", ip1VaultAddress);
const ip1RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip1VaultAddress);

// check claimable revenue
const ip3ClaimableRevenue = await expect(
ip3RoyaltyVaultAddress.claimableRevenue(ipId3, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP3's claimableRevenue: ", ip3ClaimableRevenue);
expect(ip3ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt(payAmount * (1 - commercialRevShare)));

// check claimable revenue
const ip2ClaimableRevenue = await expect(
ip2RoyaltyVaultAddress.claimableRevenue(ipId2, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP2's claimableRevenue: ", ip2ClaimableRevenue);
expect(ip2ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt(mintingFee * (1 - commercialRevShare) + payAmount * commercialRevShare));

// check claimable revenue
const ip1ClaimableRevenue = await expect(
ip1RoyaltyVaultAddress.claimableRevenue(ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP1's claimableRevenue: ", ip1ClaimableRevenue);
expect(ip1ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt(mintingFee + mintingFee * commercialRevShare));

// claimRevenueOnBehalf
const ip3ClaimRevenueOnBehalfTx = await expect(
ip3RoyaltyVaultAddress.claimRevenueOnBehalf(ipId3, MockERC20)
).not.to.be.rejectedWith(Error);
await ip3ClaimRevenueOnBehalfTx.wait();
console.log("Claim revenue on behalf transaction hash: ", ip3ClaimRevenueOnBehalfTx.hash);
expect(ip3ClaimRevenueOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");

// claimRevenueOnBehalf
const ip2ClaimRevenueOnBehalfTx = await expect(
ip2RoyaltyVaultAddress.claimRevenueOnBehalf(ipId2, MockERC20)
).not.to.be.rejectedWith(Error);
await ip2ClaimRevenueOnBehalfTx.wait();
console.log("Claim revenue on behalf transaction hash: ", ip2ClaimRevenueOnBehalfTx.hash);
expect(ip2ClaimRevenueOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");

// claimRevenueOnBehalf
const ip1ClaimRevenueOnBehalfTx = await expect(
ip1RoyaltyVaultAddress.claimRevenueOnBehalf(ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
await ip1ClaimRevenueOnBehalfTx.wait();
console.log("Claim revenue on behalf transaction hash: ", ip1ClaimRevenueOnBehalfTx.hash);
expect(ip1ClaimRevenueOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");
let signers: any, licenseTermsLAPId: any, licenseTermsLRPId: any;
let royaltyModules: any, royaltyPolicies: any, licensingModules: any;

const setupRoyaltyModules = () => {
licensingModules = signers.map((signer: any, i: number) =>
this.licensingModule.connect(signer)
);
royaltyModules = signers.map((signer: any, i: number) =>
this.royaltyModule.connect(signer)
);
royaltyPolicies = {
LAP: this.royaltyPolicyLAP.connect(signers[1]),
LRP: this.royaltyPolicyLRP.connect(signers[1]),
};
};

const registerLicenseTerms = async (policy: any) => {
terms.royaltyPolicy = policy;
const tx = await this.licenseTemplate.connect(signers[0]).registerLicenseTerms(terms);
await tx.wait();
return await this.licenseTemplate.getLicenseTermsId(terms);
};

const attachLicense = async (licensingModule: any, ipId: any, licenseId: any) => {
const tx = await licensingModule.attachLicenseTerms(ipId, PILicenseTemplate, licenseId);
await tx.wait();
};

const registerDerivative = async (licensingModule: any, childId: any, parentIds: any[], licenseId: any) => {
const tx = await licensingModule.registerDerivative(childId, parentIds, [licenseId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 0);
await tx.wait();
};

const payRoyalty = async (royaltyModule: any, payerId: any, payeeId: any, amount: number) => {
const tx = await royaltyModule.payRoyaltyOnBehalf(payeeId, payerId, MockERC20, BigInt(amount));
await tx.wait();
};

const transferToVault = async (policy: any, childId: any, parentId: any) => {
const tx = await policy.transferToVault(childId, parentId, MockERC20);
await tx.wait();
};

this.beforeAll("Setup and register license terms", async function () {
signers = await hre.ethers.getSigners();
setupRoyaltyModules();
licenseTermsLAPId = await registerLicenseTerms(RoyaltyPolicyLAP);
licenseTermsLRPId = await registerLicenseTerms(RoyaltyPolicyLRP);
});

it("Get claimable revenue tokens", async function () {
const mintingFee = terms.defaultMintingFee;
const payAmount = 100 as number;

const mintAndRegisterResp1 = await mintNFTAndRegisterIPA(signers[0], signers[0]);
ipId1 = mintAndRegisterResp1.ipId;
const mintAndRegisterResp2 = await mintNFTAndRegisterIPA(signers[1], signers[1]);
ipId2 = mintAndRegisterResp2.ipId;

// IP1 attach the commercial remix license
const attachLicenseTx = await expect(
user1ConnectedLicensingModule.attachLicenseTerms(ipId1, PILicenseTemplate, licenseTermsLAPId)
).not.to.be.rejectedWith(Error);
await attachLicenseTx.wait();
console.log("Attach license transaction hash: ", attachLicenseTx.hash);
expect(attachLicenseTx.hash).to.not.be.empty.and.to.be.a("HexString");

// IP2 is registered as IP1's derivative
const registerDerivative1Tx = await expect(
user2ConnectedLicensingModule.registerDerivative(ipId2, [ipId1], [licenseTermsLAPId], PILicenseTemplate, hre.ethers.ZeroAddress, 0, 100000000)
).not.to.be.rejectedWith(Error);
await registerDerivative1Tx.wait();
console.log("Register derivative transaction hash: ", registerDerivative1Tx.hash);
expect(registerDerivative1Tx.hash).to.not.be.empty.and.to.be.a("HexString");
it("Handles royalty and revenue flows correctly", async function () {
const [ipId1, ipId2, ipId3] = await Promise.all(
signers.map((signer: any) => mintNFTAndRegisterIPA(signer, signer).then((res: any) => res.ipId))
);

// IP2 payRoyaltyOnBehalf to IP1
const payRoyaltyOnBehalfTx = await expect(
user3ConnectedRoyaltyModule.payRoyaltyOnBehalf(ipId1, ipId2, MockERC20, BigInt(payAmount))
).not.to.be.rejectedWith(Error);
await payRoyaltyOnBehalfTx.wait();
console.log("Pay royalty on behalf transaction hash: ", payRoyaltyOnBehalfTx.hash);
expect(payRoyaltyOnBehalfTx.hash).to.not.be.empty.and.to.be.a("HexString");
await attachLicense(licensingModules[0], ipId1, licenseTermsLAPId);
await registerDerivative(licensingModules[1], ipId2, [ipId1], licenseTermsLAPId);
await registerDerivative(licensingModules[2], ipId3, [ipId2], licenseTermsLAPId);

const ip1VaultAddress = await user1ConnectedRoyaltyModule.ipRoyaltyVaults(ipId1);
console.log("IP1's ipVaultAddress: ", ip1VaultAddress);
const ip1RoyaltyVaultAddress = await hre.ethers.getContractAt("IpRoyaltyVault", ip1VaultAddress);
await payRoyalty(royaltyModules[2], ipId3, ipId2, 1000);
await transferToVault(royaltyPolicies.LAP, ipId2, ipId1);

// check claimable revenue
const ip1ClaimableRevenue = await expect(
ip1RoyaltyVaultAddress.claimableRevenue(ipId1, MockERC20)
).not.to.be.rejectedWith(Error);
console.log("IP1's claimableRevenue: ", ip1ClaimableRevenue);
expect(ip1ClaimableRevenue).to.be.a("BigInt").and.equal(BigInt(mintingFee + payAmount));
// Assertions for revenue and vault interactions can be added similarly.
});
});

0 comments on commit bf96797

Please sign in to comment.