You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It will save quite a bit of gas to cache tokenA and tokenB decimals value as uint128 so that both values can be stored in a single storage slot and retrieved, to avoid making the decimals() calls.
structTokenDecimals{uint128decimalsA;uint128decimalsB;}TokenDecimalstokenDecimals;functioninitialize(address_lpTokenMaster,address_controller,IERC20_tokenA,IERC20_tokenB)external{require(address(tokenA)==address(0),"LendingPair: already initialized");require(address(_tokenA)!=address(0)&&address(_tokenB)!=address(0),"LendingPair: cannot be ZERO address");controller=IController(_controller);tokenA=address(_tokenA);tokenB=address(_tokenB);uint256_decimalsA=_tokenA.decimals();uint256_decimalsB=_tokenB.decimals();require(_decimalsA>0&&_decimalsA<type(uint128).max,'invalid token decimals');require(_decimalsB>0&&_decimalsB<type(uint128).max,'invalid token decimals');tokenDecimals=TokenDecimals({decimalsA: uint128(_decimalsA),decimalsB: uint128(_decimalsB)});lastBlockAccrued=block.number;lpToken[tokenA]=_createLpToken(_lpTokenMaster);lpToken[tokenB]=_createLpToken(_lpTokenMaster);}// note that both _fromToken and _toToken have to be tokenA and tokenB// though each can be either token (eg. _fromToken = tokenA or tokenB)// currently, all functions of this contract that call this function// either perform the required validation checks// or explicitly use tokenA and tokenB as the input argumentsfunction_convertTokenValues(address_fromToken,address_toToken,uint_inputAmount)internalviewreturns(uint){// 1 sload for gas optimizationTokenDecimalsmemory_tokenDecimals=tokenDecimals;uintpriceFrom=controller.tokenPrice(_fromToken)*1e18;uintpriceTo=controller.tokenPrice(_toToken)*1e18;if(_fromToken==tokenA){priceFrom=priceFrom/10**_tokenDecimals.decimalsA;priceTo=priceTo/10**_tokenDecimals.decimalsB;}else{priceFrom=priceFrom/10**_tokenDecimals.decimalsB;priceTo=priceTo/10**_tokenDecimals.decimalsA;}return_inputAmount*priceFrom/priceTo;}
The text was updated successfully, but these errors were encountered:
Handle
greiart
Vulnerability details
Impact
It will save quite a bit of gas to cache
tokenA
andtokenB
decimals value asuint128
so that both values can be stored in a single storage slot and retrieved, to avoid making thedecimals()
calls.Referenced Codelines
https://github.com/code-423n4/2021-07-wildcredit/blob/main/contracts/LendingPair.sol
Proof Of Concept
The text was updated successfully, but these errors were encountered: