diff --git a/contracts/registries/IPAssetRegistry.sol b/contracts/registries/IPAssetRegistry.sol index d66a2f3e..6147117d 100644 --- a/contracts/registries/IPAssetRegistry.sol +++ b/contracts/registries/IPAssetRegistry.sol @@ -101,8 +101,15 @@ contract IPAssetRegistry is uint256 tokenId, address registerFeePayer ) internal override returns (address id) { - IPAssetRegistryStorage storage $ = _getIPAssetRegistryStorage(); + id = _registerIpAccount(chainid, tokenContract, tokenId); + IIPAccount ipAccount = IIPAccount(payable(id)); + + // return if the IP was already registered + if (bytes(ipAccount.getString("NAME")).length != 0) { + return id; + } + IPAssetRegistryStorage storage $ = _getIPAssetRegistryStorage(); // Pay registration fee uint96 feeAmount = $.feeAmount; if (feeAmount > 0) { @@ -112,13 +119,6 @@ contract IPAssetRegistry is emit IPRegistrationFeePaid(registerFeePayer, treasury, feeToken, feeAmount); } - id = _registerIpAccount(chainid, tokenContract, tokenId); - IIPAccount ipAccount = IIPAccount(payable(id)); - - // return if the IP was already registered - if (bytes(ipAccount.getString("NAME")).length != 0) { - return id; - } (string memory name, string memory uri) = _getNameAndUri(chainid, tokenContract, tokenId); uint256 registrationDate = block.timestamp; diff --git a/test/foundry/registries/IPAssetRegistry.t.sol b/test/foundry/registries/IPAssetRegistry.t.sol index d248b1cc..a5822e32 100644 --- a/test/foundry/registries/IPAssetRegistry.t.sol +++ b/test/foundry/registries/IPAssetRegistry.t.sol @@ -169,6 +169,59 @@ contract IPAssetRegistryTest is BaseTest { assertEq(IIPAccount(payable(ipId)).getUint256(address(registry), "REGISTRATION_DATE"), block.timestamp); } + function test_IPAssetRegistry_RegisterWithPayRegisterFee_Twice() public { + address treasury = address(0x123); + vm.prank(u.admin); + vm.expectEmit(); + emit IIPAssetRegistry.RegistrationFeeSet(treasury, address(erc20), 1000); + registry.setRegistrationFee(treasury, address(erc20), 1000); + + erc20.mint(alice, 1000); + uint256 alicePreviousBalance = erc20.balanceOf(alice); + vm.prank(alice); + erc20.approve(address(registry), 1000); + + uint256 totalSupply = registry.totalSupply(); + + string memory name = string.concat(block.chainid.toString(), ": Ape #99"); + vm.expectEmit(true, true, true, true); + emit IIPAssetRegistry.IPRegistrationFeePaid(alice, treasury, address(erc20), 1000); + emit IIPAssetRegistry.IPRegistered( + ipId, + block.chainid, + tokenAddress, + tokenId, + name, + "https://storyprotocol.xyz/erc721/99", + block.timestamp + ); + vm.prank(alice); + address ipId = registry.register(block.chainid, tokenAddress, tokenId); + + assertEq(totalSupply + 1, registry.totalSupply()); + assertTrue(IPAccountChecker.isRegistered(ipAssetRegistry, block.chainid, tokenAddress, tokenId)); + assertEq(IIPAccount(payable(ipId)).getString(address(registry), "NAME"), name); + assertEq(IIPAccount(payable(ipId)).getString(address(registry), "URI"), "https://storyprotocol.xyz/erc721/99"); + assertEq(IIPAccount(payable(ipId)).getUint256(address(registry), "REGISTRATION_DATE"), block.timestamp); + assertEq(erc20.balanceOf(treasury), 1000); + assertEq(erc20.balanceOf(alice), alicePreviousBalance - 1000); + + totalSupply = registry.totalSupply(); + alicePreviousBalance = erc20.balanceOf(alice); + + vm.prank(alice); + address newIpId = registry.register(block.chainid, tokenAddress, tokenId); + + assertEq(ipId, newIpId); + assertEq(erc20.balanceOf(treasury), 1000); + assertEq(erc20.balanceOf(alice), alicePreviousBalance); + assertEq(totalSupply, registry.totalSupply()); + assertTrue(IPAccountChecker.isRegistered(ipAssetRegistry, block.chainid, tokenAddress, tokenId)); + assertEq(IIPAccount(payable(ipId)).getString(address(registry), "NAME"), name); + assertEq(IIPAccount(payable(ipId)).getString(address(registry), "URI"), "https://storyprotocol.xyz/erc721/99"); + assertEq(IIPAccount(payable(ipId)).getUint256(address(registry), "REGISTRATION_DATE"), block.timestamp); + } + /// @notice Tests registration of IP permissionlessly. function test_IPAssetRegistry_revert_RegisterNotEnoughRegisterFee() public { address treasury = address(0x123);