Status DataClose notification

Rain Disclosed Report

Audit Report

When a disputed pool isn't finalized after timeExtended is set to 7, the pool collects the `resolverShare` and `oracleFixedFee` twice.

Target

https://github.com/hackenproof-public/rain-contracts

Vulnerability details

The closePool() function is implemented as follows.

    function closePool() public saleEnded nonReentrant {
        if (poolFinalized) {
            _revert(PoolClosed.selector);
        }
        poolFinalized = true;
        endTime = block.timestamp;
        uint256 totalBaseTokens = allFunds;
        if (isPublic) {
            liquidityShare =
                (totalBaseTokens * liquidityFee) /
                FEE_MAGNIFICATION;
            creatorShare = (totalBaseTokens * creatorFee) / FEE_MAGNIFICATION;
			// @audit the resolverShare is calculated as a fraction of the allFunds variable and is deducted from the allFunds variable
            resolverShare =
                (totalBaseTokens * resultResolverFee) /
                FEE_MAGNIFICATION;
            winningPoolShare =
                totalBaseTokens -
                platformShare -
                liquidityShare -
                creatorShare -
                resolverShare;
        } else {
            liquidityShare =
                (totalBaseTokens * liquidityFee) /
                FEE_MAGNIFICATION;
            resolverShare =
                (totalBaseTokens * resultResolverFee) /
                FEE_MAGNIFICATION;
            creatorShare = (totalBaseTokens * creatorFee) / FEE_MAGNIFICATION;
			// @audit the resolverShare is calculated as a fraction of the allFunds variable and is deducted from the allFunds variable
            resolverShare =
                (totalBaseTokens * resultResolverFee) /
                FEE_MAGNIFICATION;
            winningPoolShare =
                totalBaseTokens -
                platformShare -
                liquidityShare -
                creatorShare -
                resolverShare;
        }

        _swapAndBurn(platformShare);
        IERC20(baseToken).safeTransfer(poolOwner, creatorShare);
		// commented out for brevity
    }

Let's note that, the resolverShare fee is calculated as a percentage of the allFunds variable which is then deducted from the variable and left in the contract. The resolverShare fee is then collected in the openDispute() method as indicated next.

The openDispute() method is implemented as follows.

    function openDispute() external nonReentrant {
        uint256 disputeFee = (allFunds * 10) / FEE_MAGNIFICATION;
		
		// commented out for brevity

        bool isParticipant;

        uint256 i = 1;
        for (; i <= numberOfOptions; ) {
            if (userVotes[i][msg.sender] > 0) {
                isParticipant = true;
                break;
            }
            unchecked {
                ++i;
            }
        }
        if (!isParticipant) {
            _revert(InvalidCaller.selector);
        }

        IERC20(baseToken).safeTransferFrom(
            msg.sender,
            address(this),
            dispute.disputeFee
        );

		// @audit: the fees are sent to the factory
        IERC20(baseToken).safeTransfer(FACTORY, resolverShare + oracleFixedFee);

        dispute.resolver = resolver;

        resolver = IRainDeployer(FACTORY).createOracle(
            _calculateNumberOfOracles(resolverShare),
            resolverShare,
            oracleFixedFee,
            address(this),
            (block.timestamp + oracleEndTime),
            numberOfOptions,
            ipfsUri
        );

        // commented out for brevity

        dispute.disputedWinner = winner;
        dispute.disputer = msg.sender;
        winner = 0;
        isDisputed = true;
    }

When the openDispute() method is called, the pool sends the resolverShare + oracleFixedFee to the FACTORY.

The claim() function is implemented as follows.

    function claim() public nonReentrant {
        if (!firstClaim) {
            if (endTime + DISPUTE_WINDOW > block.timestamp) {
                if (!isDisputed) {
                    _revert(DisputeWindowNotEnded.selector);
                }
            }

            if (isDisputed && winner == 0) {
                if (IQuestion(resolver).winnerFinalized()) {
					// commented out for brevity
                } else if (
                    IQuestion(resolver).timeExtended() == 7 &&
                    !IQuestion(resolver).winnerFinalized()
                ) {
                    try IQuestion(resolver).calculateWinnerReadOnly() {
                        _revert(VotingEnded.selector);
                    } catch {
                        IQuestion.ExternalSourceInfo
                            memory externalSourceInfo = IQuestion(resolver)
                                .getExternalSource();
                        if (block.timestamp < externalSourceInfo.endTime) {
                            _revert(OracleNotFinalized.selector);
                        }
                        winner = dispute.disputedWinner;
                    }
                    IQuestion(resolver).refund();
					// @audit when this branch executes, we collect the fees once more from the pool's balance
                    IERC20(baseToken).safeTransfer(
                        dispute.resolver,
                        resolverShare + oracleFixedFee
                    );
                    IERC20(baseToken).safeTransfer(
                        dispute.disputer,
                        dispute.disputeFee
                    );
                } else {
                    _revert(OracleNotFinalized.selector);
                }
            }
            if (!isDisputed) {
                IERC20(baseToken).safeTransfer(
                    resolver,
                    resolverShare + oracleFixedFee
                );
                emit ResolverClaim(resolver, resolverShare + oracleFixedFee);
            }
            firstClaim = true;
        }

        if (claimed[msg.sender]) {
            _revert(AlreadyClaimed.selector);
        }
        claimed[msg.sender] = true;

        if (winner == 0) {
            _revert(PoolNotClosed.selector);
        }

        if (userVotesInEscrow[winner][msg.sender] > 0) {
            _revert(UserSellOrderExist.selector);
        }

        uint256 i = 1;
        for (; i <= numberOfOptions; ) {
            if (userAmountInEscrow[i][msg.sender] > 0) {
                _revert(UserBuyOrderExist.selector);
            }
            unchecked {
                ++i;
            }
        }

        uint256 liquidityReward;
        if (totalLiquidity > 0) {
            liquidityReward =
                (liquidityShare * userLiquidity[msg.sender]) /
                totalLiquidity;
        }
        uint256 reward;
        if (totalVotes[winner] > 0) {
            reward =
                (userVotes[winner][msg.sender] * winningPoolShare) /
                totalVotes[winner];
        }
        uint256 totalReward = liquidityReward + reward;

        if (totalReward == 0) {
            _revert(IneligibleToClaim.selector);
        }

        IERC20(baseToken).safeTransfer(msg.sender, totalReward);

        emit Claim(msg.sender, winner, liquidityReward, reward, totalReward);
    }

Notice the @audit tags. We see that, the pool collects the fees for a second time if it isn't finalized by the time the timeExetended variable is set to 7 on the resolver.

Impact Explanation

Given the fees(resolverShare + oracleFixedFee) are collected for the second time directly from the pool's baseToken balance and this isn't registered in the allFunds and winningPoolShare variable. As the various winners come to claim, the pool will deliver their rewards without taking into account that, the actual baseToken balance of the pools is less than what is registered in the allFunds variable by the amount resolverShare + oracleFixedFee. As such, it'll distribute the rewards to the winners as though the fees weren't collected for the second time. Because of that, by the time the last winner comes to claim his rewards, there will be less baseToken left in the pool than is actually calculated by the pool as his reward and as such, the call to claim his rewards will revert with an insufficient balance error. This issue affects only the last winner to claim his rewards and not all the winners. Severity: High

Likelihood explanation

It is somewhat expected that, pools will get disputed as such, likelihood: high

Recommendation

Do not collect the fees for a second time given they were already collected when the pool got disputed.

Validation steps

  1. Pool gets closed
  2. Pool gets disputed ( resolverShare + oracleFixedFee is collected and sent to factory)
  3. Resolver fails to finalize pool by the time timeExtended is set to 7
  4. First winner claims
  5. Pool collects the resolverShare + oracleFixedFee for a second time and send to the resolver.
  6. All but 1 winner claim their rewards
  7. Last winner tries to claim rewards. It reverts due to insuficient balance (given the second instance of fee collection didn't update the winning share variable.)

Attachments

Attachments are hidden

This information is not publicly available.

Comments are hidden

This information is not publicly available.