20,000 USDC
View results
Submission Details
Severity: gas
Valid

Use assembly to perform hashing instead of Solidity to reduce gas costs.

Summary

Since the function getPoolId is used in multiple locations within the code, it is better to optimize the function in terms of gas cost. For performing the keccak256 hash operation, it is more efficient to use inline assembly. This approach saves nearly ~150 gas.

// Before Gas - 782
function getPoolId(
address lender,
address loanToken,
address collateralToken
) public pure returns (bytes32 poolId) {
poolId = keccak256(abi.encode(lender, loanToken, collateralToken));
}
// After Gas - 626
function getPoolIdAss(
address lender,
address loanToken,
address collateralToken
) public pure returns (bytes32 poolId) {
assembly {
let data := mload(0x40)
mstore(data, lender)
mstore(add(data, 0x20), loanToken)
mstore(add(data, 0x40), collateralToken)
poolId := keccak256(data, 0x60)
}
}

Tools Used

Remix

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.