code-423n4 / 2022-04-jpegd-findings

1 stars 1 forks source link

Gas Optimizations #226

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS

1. repetition in the same boolean operation

summary

POC https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/NFTVault.sol#L699-L728

before '''

require( position.borrowType == BorrowType.NOT_CONFIRMED ||
    (position.borrowType == BorrowType.USE_INSURANCE && _useInsurance) ||
    (position.borrowType == BorrowType.NON_INSURANCE && !_useInsurance),
        "invalid_insurance_mode"
    );

if (position.borrowType == BorrowType.USE_INSURANCE || _useInsurance) {
    //some codes
}

if (position.borrowType == BorrowType.NOT_CONFIRMED) {
    //some codes
}

'''

after '''

bool isNotConfirmed =  position.borrowType == BorrowType.NOT_CONFIRMED;
bool isUseInsurance = position.borrowType == BorrowType.USE_INSURANCE;
bool isNotUseInsurance = position.borrowType == BorrowType.NON_INSURANCE;

require( isNotConfirmed  ||
    (isUseInsurance && _useInsurance) ||
    (isNotUseInsurance && !_useInsurance),
        "invalid_insurance_mode"
    );

if (isUseInsurance || _useInsurance) {
    //some code s
}

if (isNotConfirmed) {
    //some codes
}

'''

2. return the same address

summary

whether the nft is owned by this contract or not, it will return the same address. so that this conditional statement can be omitted to save gas

POC https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/helpers/CryptoPunksHelper.sol#L31

before '''

address account = ICryptoPunks(nftAddress).punkIndexToAddress(_idx);
return account == address(this) ? owner() : account;

'''

after '''

address account = ICryptoPunks(nftAddress).punkIndexToAddress(_idx);
return account;

'''