Skip to content

Commit 53b1e87

Browse files
authored
opt(licensing): rm hasIpAttachedLicenseTerms check (#84)
1 parent d7a4f19 commit 53b1e87

File tree

2 files changed

+126
-5
lines changed

2 files changed

+126
-5
lines changed

contracts/lib/LicensingHelper.sol

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.26;
33

4-
import { ILicenseRegistry } from "@storyprotocol/core/interfaces/registries/ILicenseRegistry.sol";
4+
import { Errors as CoreErrors } from "@storyprotocol/core/lib/Errors.sol";
55
import { ILicensingModule } from "@storyprotocol/core/interfaces/modules/licensing/ILicensingModule.sol";
66
import { IPILicenseTemplate, PILTerms } from "@storyprotocol/core/interfaces/modules/licensing/IPILicenseTemplate.sol";
77

@@ -40,9 +40,15 @@ library LicensingHelper {
4040
address licenseTemplate,
4141
uint256 licenseTermsId
4242
) internal {
43-
// Returns if license terms are already attached.
44-
if (ILicenseRegistry(licenseRegistry).hasIpAttachedLicenseTerms(ipId, licenseTemplate, licenseTermsId)) return;
45-
46-
ILicensingModule(licensingModule).attachLicenseTerms(ipId, licenseTemplate, licenseTermsId);
43+
try ILicensingModule(licensingModule).attachLicenseTerms(ipId, licenseTemplate, licenseTermsId) {
44+
return; // license terms are attached successfully
45+
} catch (bytes memory reason) {
46+
// if the error is not that the license terms are already attached, revert with the original error
47+
if (CoreErrors.LicenseRegistry__LicenseTermsAlreadyAttached.selector != bytes4(reason)) {
48+
assembly {
49+
revert(add(reason, 32), mload(reason))
50+
}
51+
}
52+
}
4753
}
4854
}

test/workflows/LicenseAttachmentWorkflows.t.sol

+115
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity 0.8.26;
44

55
// external
66
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
7+
import { Errors as CoreErrors } from "@storyprotocol/core/lib/Errors.sol";
78
import { ICoreMetadataModule } from "@storyprotocol/core/interfaces/modules/metadata/ICoreMetadataModule.sol";
89
import { IIPAccount } from "@storyprotocol/core/interfaces/IIPAccount.sol";
910
import { ILicensingModule } from "@storyprotocol/core/interfaces/modules/licensing/ILicensingModule.sol";
@@ -158,4 +159,118 @@ contract LicenseAttachmentWorkflowsTest is BaseTest {
158159
sigAttach: WorkflowStructs.SignatureData({ signer: u.alice, deadline: deadline, signature: sigAttach })
159160
});
160161
}
162+
163+
function test_LicenseAttachmentWorkflows_registerPILTermsAndAttach_idempotency()
164+
public
165+
withCollection
166+
withIp(u.alice)
167+
{
168+
address payable ipId = ipAsset[1].ipId;
169+
uint256 deadline = block.timestamp + 1000;
170+
171+
(bytes memory signature, , bytes memory data) = _getSetPermissionSigForPeriphery({
172+
ipId: ipId,
173+
to: address(licenseAttachmentWorkflows),
174+
module: address(licensingModule),
175+
selector: ILicensingModule.attachLicenseTerms.selector,
176+
deadline: deadline,
177+
state: IIPAccount(ipId).state(),
178+
signerSk: sk.alice
179+
});
180+
181+
IIPAccount(ipId).executeWithSig({
182+
to: address(accessController),
183+
value: 0,
184+
data: data,
185+
signer: u.alice,
186+
deadline: deadline,
187+
signature: signature
188+
});
189+
190+
uint256 licenseTermsId1 = licenseAttachmentWorkflows.registerPILTermsAndAttach({
191+
ipId: ipId,
192+
terms: PILFlavors.commercialUse({
193+
mintingFee: 100,
194+
currencyToken: address(mockToken),
195+
royaltyPolicy: address(royaltyPolicyLAP)
196+
})
197+
});
198+
199+
// attach the same license terms to the IP again, but it shouldn't revert
200+
uint256 licenseTermsId2 = licenseAttachmentWorkflows.registerPILTermsAndAttach({
201+
ipId: ipId,
202+
terms: PILFlavors.commercialUse({
203+
mintingFee: 100,
204+
currencyToken: address(mockToken),
205+
royaltyPolicy: address(royaltyPolicyLAP)
206+
})
207+
});
208+
209+
assertEq(licenseTermsId1, licenseTermsId2);
210+
}
211+
212+
function test_revert_registerPILTermsAndAttach_DerivativesCannotAddLicenseTerms()
213+
public
214+
withCollection
215+
whenCallerHasMinterRole
216+
withEnoughTokens(address(licenseAttachmentWorkflows))
217+
{
218+
(address ipIdParent, , uint256 licenseTermsIdParent) = licenseAttachmentWorkflows
219+
.mintAndRegisterIpAndAttachPILTerms({
220+
spgNftContract: address(nftContract),
221+
recipient: caller,
222+
ipMetadata: ipMetadataDefault,
223+
terms: PILFlavors.nonCommercialSocialRemixing()
224+
});
225+
226+
address[] memory parentIpIds = new address[](1);
227+
parentIpIds[0] = ipIdParent;
228+
229+
uint256[] memory licenseTermsIds = new uint256[](1);
230+
licenseTermsIds[0] = licenseTermsIdParent;
231+
232+
(address ipIdChild, uint256 tokenIdChild) = derivativeWorkflows.mintAndRegisterIpAndMakeDerivative({
233+
spgNftContract: address(nftContract),
234+
derivData: WorkflowStructs.MakeDerivative({
235+
parentIpIds: parentIpIds,
236+
licenseTemplate: address(pilTemplate),
237+
licenseTermsIds: licenseTermsIds,
238+
royaltyContext: ""
239+
}),
240+
ipMetadata: ipMetadataDefault,
241+
recipient: caller
242+
});
243+
244+
uint256 deadline = block.timestamp + 1000;
245+
246+
(bytes memory signature, , bytes memory data) = _getSetPermissionSigForPeriphery({
247+
ipId: ipIdChild,
248+
to: address(licenseAttachmentWorkflows),
249+
module: address(licensingModule),
250+
selector: ILicensingModule.attachLicenseTerms.selector,
251+
deadline: deadline,
252+
state: IIPAccount(payable(ipIdChild)).state(),
253+
signerSk: sk.alice
254+
});
255+
256+
IIPAccount(payable(ipIdChild)).executeWithSig({
257+
to: address(accessController),
258+
value: 0,
259+
data: data,
260+
signer: u.alice,
261+
deadline: deadline,
262+
signature: signature
263+
});
264+
265+
// attach a different license terms to the child ip, should revert with the correct error
266+
vm.expectRevert(CoreErrors.LicensingModule__DerivativesCannotAddLicenseTerms.selector);
267+
licenseAttachmentWorkflows.registerPILTermsAndAttach({
268+
ipId: ipIdChild,
269+
terms: PILFlavors.commercialUse({
270+
mintingFee: 100,
271+
currencyToken: address(mockToken),
272+
royaltyPolicy: address(royaltyPolicyLAP)
273+
})
274+
});
275+
}
161276
}

0 commit comments

Comments
 (0)