lohoz/Smart-Contract-MultiTask-Dataset huggingface dataset:
// Solidity 0.6.12
/**
 * @dev Transfer tokens to a specified address.
 * @param to The address to transfer to.
 * @param value The amount to be transferred.
 * @return True on success, false otherwise.
 */
function transfer(address to, uint256 value)
        public
        override
        validRecipient(to)
        returns (bool)
    {
        uint256 gonValue = value.mul(_gonsPerFragment);
        _gonBalances[_currentBalances][msg.sender] = _gonBalances[_currentBalances][msg.sender].sub(gonValue);
        _gonBalances[_currentBalances][to] = _gonBalances[_currentBalances][to].add(gonValue);
        emit Transfer(msg.sender, to, value);
        // console.log("transfer", symbol(), msg.sender);
        // console.log(to, value.div(10**18));
        return true;
    }
// Solidity 0.6.12
/**
 * @dev Decrease the amount of tokens that an owner has allowed to a spender.
 *
 * @param spender The address which will spend the funds.
 * @param subtractedValue The amount of tokens to decrease the allowance by.
 */
function decreaseAllowance(address spender, uint256 subtractedValue)
        public
        returns (bool)
    {
        uint256 oldValue = _allowedFragments[msg.sender][spender];
        if (subtractedValue >= oldValue) {
            _allowedFragments[msg.sender][spender] = 0;
        } else {
            _allowedFragments[msg.sender][spender] = oldValue.sub(subtractedValue);
        }
        emit Approval(msg.sender, spender, _allowedFragments[msg.sender][spender]);
        return true;
    }
// Solidity 0.6.12
// following the pattern of uniswap so there can be a pool factory
function initialize(
        address owner_,
        address payoutToken_,
        uint256 epochLength_,
        uint8 multiplier_,
        address oracle_,
        address underlying_
    ) public isOwner {
        owner = owner_;
        payoutToken = IERC20(payoutToken_);
        oracle = IPriceOracleGetter(oracle_);
        epochStart = block.number;
        epochLength = epochLength_;
        multiplier = multiplier_;
        underlying = IERC20(underlying_);
        epochStartPrice = oracle.getAssetPrice(underlying_);
        createUpAndDown();
    }
// Solidity 0.6.12
/**
 calculate the percent change defined as an 18 precision decimal (1e18 is 100%)
 */
function percentChangeMax100(uint256 diff, uint256 base)
        internal
        view
        returns (uint256)
    {
        if (base == 0) {
            return 0; // ignore zero price
        }
        uint256 percent = (diff * 10**18).mul(10**18).div(base.mul(10**18)).mul(uint256(multiplier));
        if (percent >= 10**18) {
            percent = uint256(10**18).sub(1);
        }
        return percent;
    }
// Solidity 0.6.12
// TODO: this provides an arbitrage opportunity to withdraw
// before the end of a period. This should be a lockup.
function liquidate() public checkEpoch {
        // TODO: only allow this after the lockout period

        address user_ = msg.sender;
        uint256 upBal = up.balanceOf(user_);
        uint256 downBal = down.balanceOf(user_);

        up.liquidate(address(user_));
        down.liquidate(address(user_));

        upsLiquidated += upBal;
        downsLiquidated += downBal;
        // console.log(
        //     "liquidating (ups/downs): ",
        //     upBal.div(10**18),
        //     downBal.div(10**18)
        // );
        uint256 totalPayout = upBal.add(downBal).div(200);
        // console.log(
        //     "liquidate payout to ",
        //     msg.sender,
        //     totalPayout.div(10**18)
        // );

        uint256 fee = totalPayout.div(1000); // 10 basis points fee
        uint256 toUser = totalPayout.sub(fee);
        require(payoutToken.transfer(user_, toUser));
        uint poolBal = payoutToken.balanceOf(address(this));
        if (fee > poolBal) {
            payoutToken.transfer(owner, poolBal);
            return;
        }
        require(payoutToken.transfer(owner, fee));
    }
// Solidity 0.6.12
/// Returns an estimation of the fees and bootstrap a guardian will be entitled to for a duration of time
/// The estimation is based on the current system state and there for only provides an estimation
/// @param guardian is the guardian address
/// @param duration is the amount of time in seconds for which the estimation is calculated
/// @return estimatedFees is the estimated received fees for the duration
/// @return estimatedBootstrapRewards is the estimated received bootstrap for the duration
function estimateFutureFeesAndBootstrapRewards(address guardian, uint256 duration) external override view returns (uint256 estimatedFees, uint256 estimatedBootstrapRewards) {
        (FeesAndBootstrap memory guardianFeesAndBootstrapNow,) = getGuardianFeesAndBootstrap(guardian, block.timestamp);
        (FeesAndBootstrap memory guardianFeesAndBootstrapFuture,) = getGuardianFeesAndBootstrap(guardian, block.timestamp.add(duration));
        estimatedFees = guardianFeesAndBootstrapFuture.feeBalance.sub(guardianFeesAndBootstrapNow.feeBalance);
        estimatedBootstrapRewards = guardianFeesAndBootstrapFuture.bootstrapBalance.sub(guardianFeesAndBootstrapNow.bootstrapBalance);
    }
// Solidity 0.6.12
/// Transfers the guardian Fees balance to their account
/// @dev One may withdraw for another guardian
/// @param guardian is the guardian address
function withdrawFees(address guardian) external override onlyWhenActive {
        updateGuardianFeesAndBootstrap(guardian);

        uint256 amount = feesAndBootstrap[guardian].feeBalance;
        feesAndBootstrap[guardian].feeBalance = 0;
        uint96 withdrawnFees = feesAndBootstrap[guardian].withdrawnFees.add(amount);
        feesAndBootstrap[guardian].withdrawnFees = withdrawnFees;

        emit FeesWithdrawn(guardian, amount, withdrawnFees);
        require(feesToken.transfer(guardian, amount), "Rewards::withdrawFees - insufficient funds");
    }
// Solidity 0.6.12
/// Returns the current global Fees and Bootstrap rewards state 
/// @dev calculated to the latest block, may differ from the state read
/// @return certifiedFeesPerMember represents the fees a certified committee member from day 0 would have receive
/// @return generalFeesPerMember represents the fees a non-certified committee member from day 0 would have receive
/// @return certifiedBootstrapPerMember represents the bootstrap fund a certified committee member from day 0 would have receive
/// @return generalBootstrapPerMember represents the bootstrap fund a non-certified committee member from day 0 would have receive
/// @return lastAssigned is the time the calculation was done to (typically the latest block time)
function getFeesAndBootstrapState() external override view returns (
        uint256 certifiedFeesPerMember,
        uint256 generalFeesPerMember,
        uint256 certifiedBootstrapPerMember,
        uint256 generalBootstrapPerMember,
        uint256 lastAssigned
    ) {
        (uint generalCommitteeSize, uint certifiedCommitteeSize, ) = committeeContract.getCommitteeStats();
        (FeesAndBootstrapState memory _feesAndBootstrapState,,) = _getFeesAndBootstrapState(generalCommitteeSize, certifiedCommitteeSize, generalFeesWallet.getOutstandingFees(block.timestamp), certifiedFeesWallet.getOutstandingFees(block.timestamp), block.timestamp, settings);
        certifiedFeesPerMember = _feesAndBootstrapState.certifiedFeesPerMember;
        generalFeesPerMember = _feesAndBootstrapState.generalFeesPerMember;
        certifiedBootstrapPerMember = _feesAndBootstrapState.certifiedBootstrapPerMember;
        generalBootstrapPerMember = _feesAndBootstrapState.generalBootstrapPerMember;
        lastAssigned = _feesAndBootstrapState.lastAssigned;
    }
// Solidity 0.6.12
/// Returns the current guardian Fees and Bootstrap rewards state 
/// @dev calculated to the latest block, may differ from the state read
/// @return feeBalance is the guardian fees balance 
/// @return lastFeesPerMember is the FeesPerMember on the last update based on the guardian certification state
/// @return bootstrapBalance is the guardian bootstrap balance 
/// @return lastBootstrapPerMember is the FeesPerMember on the last BootstrapPerMember based on the guardian certification state
function getFeesAndBootstrapData(address guardian) external override view returns (
        uint256 feeBalance,
        uint256 lastFeesPerMember,
        uint256 bootstrapBalance,
        uint256 lastBootstrapPerMember,
        uint256 withdrawnFees,
        uint256 withdrawnBootstrap,
        bool certified
    ) {
        FeesAndBootstrap memory guardianFeesAndBootstrap;
        (guardianFeesAndBootstrap, certified) = getGuardianFeesAndBootstrap(guardian, block.timestamp);
        return (
            guardianFeesAndBootstrap.feeBalance,
            guardianFeesAndBootstrap.lastFeesPerMember,
            guardianFeesAndBootstrap.bootstrapBalance,
            guardianFeesAndBootstrap.lastBootstrapPerMember,
            guardianFeesAndBootstrap.withdrawnFees,
            guardianFeesAndBootstrap.withdrawnBootstrap,
            certified
        );
    }
// Solidity 0.6.12
/// Migrates the rewards balance to a new FeesAndBootstrap contract
/// @dev The new rewards contract is determined according to the contracts registry
/// @dev No impact of the calling contract if the currently configured contract in the registry
/// @dev may be called also while the contract is locked
/// @param guardians is the list of guardians to migrate
function migrateRewardsBalance(address[] calldata guardians) external override {
        require(!settings.rewardAllocationActive, "Reward distribution must be deactivated for migration");

        IFeesAndBootstrapRewards currentRewardsContract = IFeesAndBootstrapRewards(getFeesAndBootstrapRewardsContract());
        require(address(currentRewardsContract) != address(this), "New rewards contract is not set");

        uint256 totalFees = 0;
        uint256 totalBootstrap = 0;
        uint256[] memory fees = new uint256[](guardians.length);
        uint256[] memory bootstrap = new uint256[](guardians.length);

        for (uint i = 0; i < guardians.length; i++) {
            updateGuardianFeesAndBootstrap(guardians[i]);

            FeesAndBootstrap memory guardianFeesAndBootstrap = feesAndBootstrap[guardians[i]];
            fees[i] = guardianFeesAndBootstrap.feeBalance;
            totalFees = totalFees.add(fees[i]);
            bootstrap[i] = guardianFeesAndBootstrap.bootstrapBalance;
            totalBootstrap = totalBootstrap.add(bootstrap[i]);

            guardianFeesAndBootstrap.feeBalance = 0;
            guardianFeesAndBootstrap.bootstrapBalance = 0;
            feesAndBootstrap[guardians[i]] = guardianFeesAndBootstrap;
        }

        require(feesToken.approve(address(currentRewardsContract), totalFees), "migrateRewardsBalance: approve failed");
        require(bootstrapToken.approve(address(currentRewardsContract), totalBootstrap), "migrateRewardsBalance: approve failed");
        currentRewardsContract.acceptRewardsBalanceMigration(guardians, fees, totalFees, bootstrap, totalBootstrap);

        for (uint i = 0; i < guardians.length; i++) {
            emit FeesAndBootstrapRewardsBalanceMigrated(guardians[i], fees[i], bootstrap[i], address(currentRewardsContract));
        }
    }
// Solidity 0.6.12
/// Accepts guardian's balance migration from a previous rewards contract
/// @dev the function may be called by any caller that approves the amounts provided for transfer
/// @param guardians is the list of migrated guardians
/// @param fees is the list of received guardian fees balance
/// @param totalFees is the total amount of fees migrated for all guardians in the list. Must match the sum of the fees list.
/// @param bootstrap is the list of received guardian bootstrap balance.
/// @param totalBootstrap is the total amount of bootstrap rewards migrated for all guardians in the list. Must match the sum of the bootstrap list.
function acceptRewardsBalanceMigration(address[] memory guardians, uint256[] memory fees, uint256 totalFees, uint256[] memory bootstrap, uint256 totalBootstrap) external override {
        uint256 _totalFees = 0;
        uint256 _totalBootstrap = 0;

        for (uint i = 0; i < guardians.length; i++) {
            _totalFees = _totalFees.add(fees[i]);
            _totalBootstrap = _totalBootstrap.add(bootstrap[i]);
        }

        require(totalFees == _totalFees, "totalFees does not match fees sum");
        require(totalBootstrap == _totalBootstrap, "totalBootstrap does not match bootstrap sum");

        if (totalFees > 0) {
            require(feesToken.transferFrom(msg.sender, address(this), totalFees), "acceptRewardBalanceMigration: transfer failed");
        }
        if (totalBootstrap > 0) {
            require(bootstrapToken.transferFrom(msg.sender, address(this), totalBootstrap), "acceptRewardBalanceMigration: transfer failed");
        }

        FeesAndBootstrap memory guardianFeesAndBootstrap;
        for (uint i = 0; i < guardians.length; i++) {
            guardianFeesAndBootstrap = feesAndBootstrap[guardians[i]];
            guardianFeesAndBootstrap.feeBalance = guardianFeesAndBootstrap.feeBalance.add(fees[i]);
            guardianFeesAndBootstrap.bootstrapBalance = guardianFeesAndBootstrap.bootstrapBalance.add(bootstrap[i]);
            feesAndBootstrap[guardians[i]] = guardianFeesAndBootstrap;

            emit FeesAndBootstrapRewardsBalanceMigrationAccepted(msg.sender, guardians[i], fees[i], bootstrap[i]);
        }
    }
// Solidity 0.6.12
/// Returns the current global Fees and Bootstrap rewards state 
/// @dev receives the relevant committee and general state data
/// @param generalCommitteeSize is the current number of members in the certified committee
/// @param certifiedCommitteeSize is the current number of members in the general committee
/// @param collectedGeneralFees is the amount of fees collected from general virtual chains for the calculated period
/// @param collectedCertifiedFees is the amount of fees collected from general virtual chains for the calculated period
/// @param currentTime is the time to calculate the fees and bootstrap for
/// @param _settings is the contract settings
function _getFeesAndBootstrapState(uint generalCommitteeSize, uint certifiedCommitteeSize, uint256 collectedGeneralFees, uint256 collectedCertifiedFees, uint256 currentTime, Settings memory _settings) private view returns (FeesAndBootstrapState memory _feesAndBootstrapState, uint256 allocatedGeneralBootstrap, uint256 allocatedCertifiedBootstrap) {
        _feesAndBootstrapState = feesAndBootstrapState;

        if (_settings.rewardAllocationActive) {
            uint256 generalFeesDelta = generalCommitteeSize == 0 ? 0 : collectedGeneralFees.div(generalCommitteeSize);
            uint256 certifiedFeesDelta = certifiedCommitteeSize == 0 ? 0 : generalFeesDelta.add(collectedCertifiedFees.div(certifiedCommitteeSize));

            _feesAndBootstrapState.generalFeesPerMember = _feesAndBootstrapState.generalFeesPerMember.add(generalFeesDelta);
            _feesAndBootstrapState.certifiedFeesPerMember = _feesAndBootstrapState.certifiedFeesPerMember.add(certifiedFeesDelta);

            uint duration = currentTime.sub(_feesAndBootstrapState.lastAssigned);
            uint256 generalBootstrapDelta = uint256(_settings.generalCommitteeAnnualBootstrap).mul(duration).div(365 days);
            uint256 certifiedBootstrapDelta = generalBootstrapDelta.add(uint256(_settings.certifiedCommitteeAnnualBootstrap).mul(duration).div(365 days));

            _feesAndBootstrapState.generalBootstrapPerMember = _feesAndBootstrapState.generalBootstrapPerMember.add(generalBootstrapDelta);
            _feesAndBootstrapState.certifiedBootstrapPerMember = _feesAndBootstrapState.certifiedBootstrapPerMember.add(certifiedBootstrapDelta);
            _feesAndBootstrapState.lastAssigned = uint32(currentTime);

            allocatedGeneralBootstrap = generalBootstrapDelta.mul(generalCommitteeSize);
            allocatedCertifiedBootstrap = certifiedBootstrapDelta.mul(certifiedCommitteeSize);
        }
    }
// Solidity 0.6.12
/// Updates the global Fees and Bootstrap rewards state
/// @dev utilizes _getFeesAndBootstrapState to calculate the global state 
/// @param generalCommitteeSize is the current number of members in the certified committee
/// @param certifiedCommitteeSize is the current number of members in the general committee
/// @return _feesAndBootstrapState is a FeesAndBootstrapState struct with the updated state
function _updateFeesAndBootstrapState(uint generalCommitteeSize, uint certifiedCommitteeSize) private returns (FeesAndBootstrapState memory _feesAndBootstrapState) {
        Settings memory _settings = settings;
        if (!_settings.rewardAllocationActive) {
            return feesAndBootstrapState;
        }

        uint256 collectedGeneralFees = generalFeesWallet.collectFees();
        uint256 collectedCertifiedFees = certifiedFeesWallet.collectFees();
        uint256 allocatedGeneralBootstrap;
        uint256 allocatedCertifiedBootstrap;

        (_feesAndBootstrapState, allocatedGeneralBootstrap, allocatedCertifiedBootstrap) = _getFeesAndBootstrapState(generalCommitteeSize, certifiedCommitteeSize, collectedGeneralFees, collectedCertifiedFees, block.timestamp, _settings);
        bootstrapRewardsWallet.withdraw(allocatedGeneralBootstrap.add(allocatedCertifiedBootstrap));

        feesAndBootstrapState = _feesAndBootstrapState;

        emit FeesAllocated(collectedGeneralFees, _feesAndBootstrapState.generalFeesPerMember, collectedCertifiedFees, _feesAndBootstrapState.certifiedFeesPerMember);
        emit BootstrapRewardsAllocated(allocatedGeneralBootstrap, _feesAndBootstrapState.generalBootstrapPerMember, allocatedCertifiedBootstrap, _feesAndBootstrapState.certifiedBootstrapPerMember);
    }
// Solidity 0.6.12
/// Returns the current guardian Fees and Bootstrap rewards state 
/// @dev receives the relevant guardian committee membership data and the global state
/// @param guardian is the guardian to query
/// @param inCommittee indicates whether the guardian is currently in the committee
/// @param isCertified indicates whether the guardian is currently certified
/// @param nextCertification indicates whether after the change, the guardian is certified
/// @param _feesAndBootstrapState is the current updated global fees and bootstrap state
/// @return guardianFeesAndBootstrap is a struct with the guardian updated fees and bootstrap state
/// @return addedBootstrapAmount is the amount added to the guardian bootstrap balance
/// @return addedFeesAmount is the amount added to the guardian fees balance
function _getGuardianFeesAndBootstrap(address guardian, bool inCommittee, bool isCertified, bool nextCertification, FeesAndBootstrapState memory _feesAndBootstrapState) private view returns (FeesAndBootstrap memory guardianFeesAndBootstrap, uint256 addedBootstrapAmount, uint256 addedFeesAmount) {
        guardianFeesAndBootstrap = feesAndBootstrap[guardian];

        if (inCommittee) {
            addedBootstrapAmount = (isCertified ? _feesAndBootstrapState.certifiedBootstrapPerMember : _feesAndBootstrapState.generalBootstrapPerMember).sub(guardianFeesAndBootstrap.lastBootstrapPerMember);
            guardianFeesAndBootstrap.bootstrapBalance = guardianFeesAndBootstrap.bootstrapBalance.add(addedBootstrapAmount);

            addedFeesAmount = (isCertified ? _feesAndBootstrapState.certifiedFeesPerMember : _feesAndBootstrapState.generalFeesPerMember).sub(guardianFeesAndBootstrap.lastFeesPerMember);
            guardianFeesAndBootstrap.feeBalance = guardianFeesAndBootstrap.feeBalance.add(addedFeesAmount);
        }

        guardianFeesAndBootstrap.lastBootstrapPerMember = nextCertification ?  _feesAndBootstrapState.certifiedBootstrapPerMember : _feesAndBootstrapState.generalBootstrapPerMember;
        guardianFeesAndBootstrap.lastFeesPerMember = nextCertification ?  _feesAndBootstrapState.certifiedFeesPerMember : _feesAndBootstrapState.generalFeesPerMember;
    }
// Solidity 0.6.12
/// Updates a guardian Fees and Bootstrap rewards state
/// @dev receives the relevant guardian committee membership data
/// @dev updates the global Fees and Bootstrap state prior to calculating the guardian's
/// @dev utilizes _getGuardianFeesAndBootstrap
/// @param guardian is the guardian to update
/// @param inCommittee indicates whether the guardian is currently in the committee
/// @param isCertified indicates whether the guardian is currently certified
/// @param nextCertification indicates whether after the change, the guardian is certified
/// @param generalCommitteeSize indicates the general committee size prior to the change
/// @param certifiedCommitteeSize indicates the certified committee size prior to the change
function _updateGuardianFeesAndBootstrap(address guardian, bool inCommittee, bool isCertified, bool nextCertification, uint generalCommitteeSize, uint certifiedCommitteeSize) private {
        uint256 addedBootstrapAmount;
        uint256 addedFeesAmount;

        FeesAndBootstrapState memory _feesAndBootstrapState = _updateFeesAndBootstrapState(generalCommitteeSize, certifiedCommitteeSize);
        FeesAndBootstrap memory guardianFeesAndBootstrap;
        (guardianFeesAndBootstrap, addedBootstrapAmount, addedFeesAmount) = _getGuardianFeesAndBootstrap(guardian, inCommittee, isCertified, nextCertification, _feesAndBootstrapState);
        feesAndBootstrap[guardian] = guardianFeesAndBootstrap;

        emit BootstrapRewardsAssigned(guardian, addedBootstrapAmount, guardianFeesAndBootstrap.withdrawnBootstrap.add(guardianFeesAndBootstrap.bootstrapBalance), isCertified, guardianFeesAndBootstrap.lastBootstrapPerMember);
        emit FeesAssigned(guardian, addedFeesAmount, guardianFeesAndBootstrap.withdrawnFees.add(guardianFeesAndBootstrap.feeBalance), isCertified, guardianFeesAndBootstrap.lastFeesPerMember);
    }
// Solidity 0.6.12
/// Returns the guardian Fees and Bootstrap rewards state for a given time
/// @dev if the time to estimate is in the future, estimates the fees and rewards for the given time
/// @dev for future time estimation assumes no change in the guardian committee membership and certification
/// @param guardian is the guardian to query
/// @param currentTime is the time to calculate the fees and bootstrap for
/// @return guardianFeesAndBootstrap is a struct with the guardian updated fees and bootstrap state
/// @return certified is the guardian certification status
function getGuardianFeesAndBootstrap(address guardian, uint256 currentTime) private view returns (FeesAndBootstrap memory guardianFeesAndBootstrap, bool certified) {
        ICommittee _committeeContract = committeeContract;
        (uint generalCommitteeSize, uint certifiedCommitteeSize, ) = _committeeContract.getCommitteeStats();
        (FeesAndBootstrapState memory _feesAndBootstrapState,,) = _getFeesAndBootstrapState(generalCommitteeSize, certifiedCommitteeSize, generalFeesWallet.getOutstandingFees(currentTime), certifiedFeesWallet.getOutstandingFees(currentTime), currentTime, settings);
        bool inCommittee;
        (inCommittee, , certified,) = _committeeContract.getMemberInfo(guardian);
        (guardianFeesAndBootstrap, ,) = _getGuardianFeesAndBootstrap(guardian, inCommittee, certified, certified, _feesAndBootstrapState);
    }