Skip to content

Commit b150071

Browse files
committed
gas optimization
1 parent 6e35999 commit b150071

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

contracts/UFragments.sol

+24-17
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,28 @@ contract UFragments is ERC20Detailed, Ownable {
9494
onlyMonetaryPolicy
9595
returns (uint256)
9696
{
97+
uint256 oldTotalSupply = _totalSupply;
9798
if (supplyDelta == 0) {
98-
emit LogRebase(epoch, _totalSupply);
99-
return _totalSupply;
99+
emit LogRebase(epoch, oldTotalSupply);
100+
return oldTotalSupply;
100101
}
101102

103+
uint256 newTotalSupply;
102104
if (supplyDelta < 0) {
103-
_totalSupply = _totalSupply.sub(uint256(supplyDelta.abs()));
105+
newTotalSupply = oldTotalSupply.sub(uint256(supplyDelta.abs()));
104106
} else {
105-
_totalSupply = _totalSupply.add(uint256(supplyDelta));
107+
newTotalSupply = oldTotalSupply.add(uint256(supplyDelta));
106108
}
107109

108-
if (_totalSupply > MAX_SUPPLY) {
109-
_totalSupply = MAX_SUPPLY;
110+
if (newTotalSupply > MAX_SUPPLY) {
111+
newTotalSupply = MAX_SUPPLY;
110112
}
111113

112-
_gonsPerFragment = TOTAL_GONS.div(_totalSupply);
114+
_gonsPerFragment = TOTAL_GONS.div(newTotalSupply);
115+
_totalSupply = newTotalSupply;
113116

114117
// From this point forward, _gonsPerFragment is taken as the source of truth.
115-
// We recalculate a new _totalSupply to be in agreement with the _gonsPerFragment
118+
// We recalculate a newTotalSupply to be in agreement with the _gonsPerFragment
116119
// conversion rate.
117120
// This means our applied supplyDelta can deviate from the requested supplyDelta,
118121
// but this deviation is guaranteed to be < (_totalSupply^2)/(TOTAL_GONS - _totalSupply).
@@ -122,8 +125,8 @@ contract UFragments is ERC20Detailed, Ownable {
122125
// ever increased, it must be re-included.
123126
// _totalSupply = TOTAL_GONS.div(_gonsPerFragment)
124127

125-
emit LogRebase(epoch, _totalSupply);
126-
return _totalSupply;
128+
emit LogRebase(epoch, newTotalSupply);
129+
return newTotalSupply;
127130
}
128131

129132
function initialize(address owner_) public initializer {
@@ -232,10 +235,11 @@ contract UFragments is ERC20Detailed, Ownable {
232235
* @param addedValue The amount of tokens to increase the allowance by.
233236
*/
234237
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
235-
_allowedFragments[msg.sender][spender] = _allowedFragments[msg.sender][spender].add(
236-
addedValue
237-
);
238-
emit Approval(msg.sender, spender, _allowedFragments[msg.sender][spender]);
238+
uint256 oldValue = _allowedFragments[msg.sender][spender];
239+
uint256 newValue = oldValue.add(addedValue);
240+
241+
_allowedFragments[msg.sender][spender] = newValue;
242+
emit Approval(msg.sender, spender, newValue);
239243
return true;
240244
}
241245

@@ -247,12 +251,15 @@ contract UFragments is ERC20Detailed, Ownable {
247251
*/
248252
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
249253
uint256 oldValue = _allowedFragments[msg.sender][spender];
254+
uint256 newValue;
250255
if (subtractedValue >= oldValue) {
251-
_allowedFragments[msg.sender][spender] = 0;
256+
newValue = 0;
252257
} else {
253-
_allowedFragments[msg.sender][spender] = oldValue.sub(subtractedValue);
258+
newValue = oldValue.sub(subtractedValue);
254259
}
255-
emit Approval(msg.sender, spender, _allowedFragments[msg.sender][spender]);
260+
261+
_allowedFragments[msg.sender][spender] = newValue;
262+
emit Approval(msg.sender, spender, newValue);
256263
return true;
257264
}
258265
}

0 commit comments

Comments
 (0)