Protect from out of gas exceptions

How can the following code be written with EnumerableSet instead of the for loop in order to protect the contract from possible “OUT_OF_GAS” errors?

function _getCurrentSupply() private view returns(uint256, uint256) {

        uint256 rSupply = _rTotal;
        uint256 tSupply = _tTotal;
        for (uint256 i = 0; i < _excluded.length; i++) {
            if (_rOwned[_excluded[i]] > rSupply || _tOwned[_excluded[i]] > tSupply) return (_rTotal, _tTotal);
            rSupply = rSupply.sub(_rOwned[_excluded[i]]);
            tSupply = tSupply.sub(_tOwned[_excluded[i]]);
        }

        if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal);
        return (rSupply, tSupply);
    }

Many thanks in advance.

EnumerableSet doesn’t solve this. You need to maintain the current supply in a storage variable, keep it always updated so that you don’t have to recalculate it. Each change to _rTotal, _rOwned, and so on, would need to update the current supply variable accordingly.

1 Like

Thank you very much for the reply. Can you point me to a contract that incorporates this kind of behavior so I can learn from it?

This post explains the general idea:

1 Like