Skip to content

Commit

Permalink
revert royalty related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Spablob committed Apr 12, 2024
1 parent 929e14f commit 1520309
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ interface IIpRoyaltyVault {

/// @notice Allows token holders to claim revenue token based on the token balance at certain snapshot
/// @param snapshotId The snapshot id
/// @param tokenList The list of revenue tokens to claim
function claimRevenueByTokenBatch(uint256 snapshotId, address[] calldata tokenList) external;
/// @param tokens The list of revenue tokens to claim
function claimRevenueByTokenBatch(uint256 snapshotId, address[] calldata tokens) external;

/// @notice Allows token holders to claim by a list of snapshot ids based on the token balance at certain snapshot
/// @param snapshotIds The list of snapshot ids
Expand Down Expand Up @@ -103,4 +103,4 @@ interface IIpRoyaltyVault {

/// @notice The list of revenue tokens in the vault
function tokens() external view returns (address[] memory);
}
}
1 change: 1 addition & 0 deletions contracts/modules/royalty/RoyaltyModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ contract RoyaltyModule is
RoyaltyModuleStorage storage $ = _getRoyaltyModuleStorage();
if (!$.isWhitelistedRoyaltyToken[token]) revert Errors.RoyaltyModule__NotWhitelistedRoyaltyToken();
if (IDisputeModule($.disputeModule).isIpTagged(receiverIpId)) revert Errors.RoyaltyModule__IpIsTagged();
if (licenseRoyaltyPolicy == address(0)) revert Errors.RoyaltyModule__NoRoyaltyPolicySet();
if (!$.isWhitelistedRoyaltyPolicy[licenseRoyaltyPolicy])
revert Errors.RoyaltyModule__NotWhitelistedRoyaltyPolicy();

Expand Down
51 changes: 23 additions & 28 deletions contracts/modules/royalty/policies/IpRoyaltyVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ contract IpRoyaltyVault is IIpRoyaltyVault, ERC20SnapshotUpgradeable, Reentrancy
__ERC20_init(name, symbol);
}

/// @notice Returns the number royalty token decimals
function decimals() public view override returns (uint8) {
return 6;
}

/// @notice Adds a new revenue token to the vault
/// @param token The address of the revenue token
/// @dev Only callable by the royalty policy LAP
Expand All @@ -128,24 +123,24 @@ contract IpRoyaltyVault is IIpRoyaltyVault, ERC20SnapshotUpgradeable, Reentrancy
uint32 unclaimedTokens = $.unclaimedRoyaltyTokens;
$.unclaimedAtSnapshot[snapshotId] = unclaimedTokens;

address[] memory tokenList = $.tokens.values();
address[] memory tokens = $.tokens.values();

for (uint256 i = 0; i < tokenList.length; i++) {
uint256 tokenBalance = IERC20Upgradeable(tokenList[i]).balanceOf(address(this));
for (uint256 i = 0; i < tokens.length; i++) {
uint256 tokenBalance = IERC20Upgradeable(tokens[i]).balanceOf(address(this));
if (tokenBalance == 0) {
$.tokens.remove(tokenList[i]);
$.tokens.remove(tokens[i]);
continue;
}

uint256 newRevenue = tokenBalance - $.claimVaultAmount[tokenList[i]] - $.ancestorsVaultAmount[tokenList[i]];
uint256 newRevenue = tokenBalance - $.claimVaultAmount[tokens[i]] - $.ancestorsVaultAmount[tokens[i]];
if (newRevenue == 0) continue;

uint256 ancestorsTokens = (newRevenue * unclaimedTokens) / totalSupply();
$.ancestorsVaultAmount[tokenList[i]] += ancestorsTokens;
$.ancestorsVaultAmount[tokens[i]] += ancestorsTokens;

uint256 claimableTokens = newRevenue - ancestorsTokens;
$.claimableAtSnapshot[snapshotId][tokenList[i]] = claimableTokens;
$.claimVaultAmount[tokenList[i]] += claimableTokens;
$.claimableAtSnapshot[snapshotId][tokens[i]] = claimableTokens;
$.claimVaultAmount[tokens[i]] += claimableTokens;
}

emit SnapshotCompleted(snapshotId, block.timestamp, unclaimedTokens);
Expand All @@ -164,19 +159,19 @@ contract IpRoyaltyVault is IIpRoyaltyVault, ERC20SnapshotUpgradeable, Reentrancy

/// @notice Allows token holders to claim revenue token based on the token balance at certain snapshot
/// @param snapshotId The snapshot id
/// @param tokenList The list of revenue tokens to claim
function claimRevenueByTokenBatch(uint256 snapshotId, address[] calldata tokenList) external nonReentrant {
/// @param tokens The list of revenue tokens to claim
function claimRevenueByTokenBatch(uint256 snapshotId, address[] calldata tokens) external nonReentrant {
IpRoyaltyVaultStorage storage $ = _getIpRoyaltyVaultStorage();

for (uint256 i = 0; i < tokenList.length; i++) {
uint256 claimableToken = _claimableRevenue(msg.sender, snapshotId, tokenList[i]);
for (uint256 i = 0; i < tokens.length; i++) {
uint256 claimableToken = _claimableRevenue(msg.sender, snapshotId, tokens[i]);
if (claimableToken == 0) continue;

$.isClaimedAtSnapshot[snapshotId][msg.sender][tokenList[i]] = true;
$.claimVaultAmount[tokenList[i]] -= claimableToken;
IERC20Upgradeable(tokenList[i]).safeTransfer(msg.sender, claimableToken);
$.isClaimedAtSnapshot[snapshotId][msg.sender][tokens[i]] = true;
$.claimVaultAmount[tokens[i]] -= claimableToken;
IERC20Upgradeable(tokens[i]).safeTransfer(msg.sender, claimableToken);

emit RevenueTokenClaimed(msg.sender, tokenList[i], claimableToken);
emit RevenueTokenClaimed(msg.sender, tokens[i], claimableToken);
}
}

Expand Down Expand Up @@ -249,19 +244,19 @@ contract IpRoyaltyVault is IIpRoyaltyVault, ERC20SnapshotUpgradeable, Reentrancy
function _collectAccruedTokens(uint256 royaltyTokensToClaim, address ancestorIpId) internal {
IpRoyaltyVaultStorage storage $ = _getIpRoyaltyVaultStorage();

address[] memory tokenList = $.tokens.values();
address[] memory tokens = $.tokens.values();

for (uint256 i = 0; i < tokenList.length; ++i) {
for (uint256 i = 0; i < tokens.length; ++i) {
// the only case in which unclaimedRoyaltyTokens can be 0 is when the vault is empty and everyone claimed
// in which case the call will revert upstream with IpRoyaltyVault__AlreadyClaimed error
uint256 collectAmount = ($.ancestorsVaultAmount[tokenList[i]] * royaltyTokensToClaim) /
uint256 collectAmount = ($.ancestorsVaultAmount[tokens[i]] * royaltyTokensToClaim) /
$.unclaimedRoyaltyTokens;
if (collectAmount == 0) continue;

$.ancestorsVaultAmount[tokenList[i]] -= collectAmount;
IERC20Upgradeable(tokenList[i]).safeTransfer(ancestorIpId, collectAmount);
$.ancestorsVaultAmount[tokens[i]] -= collectAmount;
IERC20Upgradeable(tokens[i]).safeTransfer(ancestorIpId, collectAmount);

emit RevenueTokenClaimed(ancestorIpId, tokenList[i], collectAmount);
emit RevenueTokenClaimed(ancestorIpId, tokens[i], collectAmount);
}
}

Expand Down Expand Up @@ -331,4 +326,4 @@ contract IpRoyaltyVault is IIpRoyaltyVault, ERC20SnapshotUpgradeable, Reentrancy
$.slot := IpRoyaltyVaultStorageLocation
}
}
}
}
9 changes: 2 additions & 7 deletions test/foundry/modules/royalty/IpRoyaltyVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ contract TestIpRoyaltyVault is BaseTest {

(, address IpRoyaltyVault2, , , ) = royaltyPolicyLAP.getRoyaltyData(address(2));
ipRoyaltyVault = IpRoyaltyVault(IpRoyaltyVault2);

assertEq(ipRoyaltyVault.ipId(), address(2));
}

function _setupMaxUniqueTree() internal {
Expand Down Expand Up @@ -131,17 +129,14 @@ contract TestIpRoyaltyVault is BaseTest {

vm.startPrank(address(licensingModule));
royaltyModule.onLinkToParents(address(100), address(royaltyPolicyLAP), parents, encodedLicenseData, "");
//royaltyPolicyLAP.onLinkToParents(address(100), parents, encodedLicenseData, "");
}

function test_IpRoyaltyVault_AddIpRoyaltyVaultTokens_NotRoyaltyPolicyLAP() public {
vm.expectRevert(Errors.IpRoyaltyVault__NotRoyaltyPolicyLAP.selector);
ipRoyaltyVault.addIpRoyaltyVaultTokens(address(0));
}

function test_IpRoyaltyVault_decimals() public {
assertEq(ipRoyaltyVault.decimals(), 6);
}

function test_IpRoyaltyVault_AddIpRoyaltyVaultTokens() public {
vm.startPrank(address(royaltyPolicyLAP));
ipRoyaltyVault.addIpRoyaltyVaultTokens(address(1));
Expand Down Expand Up @@ -414,4 +409,4 @@ contract TestIpRoyaltyVault is BaseTest {
accruedCollectableRevenue
);
}
}
}
Loading

0 comments on commit 1520309

Please sign in to comment.