Skip to content

Commit

Permalink
Prevent Re-Charge of IP Registration Fee for Idempotent Registration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster-will authored Dec 12, 2024
1 parent d2281f5 commit 35a8301
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
17 changes: 8 additions & 9 deletions contracts/registries/IPAssetRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -112,14 +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;
ipAccount.setString("NAME", name);
Expand Down
53 changes: 53 additions & 0 deletions test/foundry/registries/IPAssetRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 35a8301

Please sign in to comment.