Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Re-Charge of IP Registration Fee for Idempotent Registration #360

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading