re-al-Foundation / rwa-contracts

0 stars 0 forks source link

[CCM-01C] Code Repetition #72

Closed chasebrownn closed 5 months ago

chasebrownn commented 5 months ago

CCM-01C: Code Repetition

Type Severity Location
Gas Optimization CrossChainMigrator.sol:L195-L196, L200-L226, L270-L300

Description:

The referenced code segments perform identical statements and can be merged into a single function.

Example:

function migrateNFT(
    uint256 _tokenId,
    address to,
    address payable refundAddress,
    address zroPaymentAddress,
    bytes calldata adapterParams
) payable external {
    require(migrationActive, "CrossChainMigrator: Migration is not active");

    // Transfer NFT into this contract. Keep custody
    passiveIncomeNFT.transferFrom(msg.sender, address(this), _tokenId);

    _checkAdapterParams(remoteChainId, SEND_NFT, adapterParams, 0);

    (uint256 startTime,
    uint256 endTime,
    uint256 lockedAmount,
    uint256 multiplier,
    uint256 claimed,
    uint256 maxPayout) = passiveIncomeNFT.locks(_tokenId);

    uint256 amountTokens;
    if (multiplier != piCalculator.determineMultiplier(BOOST_START, BOOST_END, startTime, uint8((endTime - startTime) / 30 days))) {
        // if early claim, just mint them remaining in `maxPayout`.
        amountTokens = maxPayout;
    }
    else {
        // otherwise, just calculate amount to mint/lock as normal.
        amountTokens = lockedAmount + ((lockedAmount * (multiplier - 1e18)) / 1e18) - claimed;
    }

    // if lock is expired -> just mint them RWA tokens
    if (block.timestamp >= endTime) {
        revert ExpiredNFT(_tokenId);
    }

    uint256 duration = endTime - block.timestamp;

    if (duration > MAX_DURATION) {
        duration = MAX_DURATION;
    }

    _lzSend(
        remoteChainId,
        abi.encode(SEND_NFT, abi.encodePacked(to), amountTokens, duration),
        refundAddress,
        zroPaymentAddress,
        adapterParams,
        msg.value
    );

    emit MigrationMessageSent_PINFT(
        msg.sender,
        _tokenId,
        amountTokens,
        SafeCast.toUint208(amountTokens.calculateVotingPower(duration))
    );
}

Recommendation:

We advise this to be done so, optimizing the bytecode size of the contract as well as increasing its maintainability greatly.

chasebrownn commented 5 months ago

Acknowledged