Hi,
I just want to know why do you import another contract (in this case SafeMath.sol) if you don’t use it ?
I looked around and it’s seems that importing another cotnract doesn’t cost gas (is that true) ?
Best,
Caladay
Hi,
I just want to know why do you import another contract (in this case SafeMath.sol) if you don’t use it ?
I looked around and it’s seems that importing another cotnract doesn’t cost gas (is that true) ?
Best,
Caladay
Hi @Caladay,
Welcome to the community
The following line in Fallout.sol actually uses the SafeMath add
function.
allocations[msg.sender] = allocations[msg.sender].add(msg.value);
Importing a contract costs additional gas on deployment (but only if you are using the contract)
You can test this out by deploying the following contracts in Remix
No import + no use
pragma solidity ^0.5.0;
contract Test1 {
}
Import + no use
pragma solidity ^0.5.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/ownership/Ownable.sol';
contract Test2 {
}
Import + used
pragma solidity ^0.5.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.4.0/contracts/ownership/Ownable.sol';
contract Test3 is Ownable {
}
Test1: Gas Used by Transaction: 66,850
https://rinkeby.etherscan.io/tx/0x71c254ef3a822ec6733a7265c09c7f4a112cd60f74b64cca0ae0f464576cbd46
Test2: Gas Used by Transaction: 66,850
https://rinkeby.etherscan.io/tx/0x3ffb8b7d2cfa5a0b92361b8b1cc76e8335d63a16a298592ca5c1c15325e6f9bd
Test3: Gas Used by Transaction: 353,460
https://rinkeby.etherscan.io/tx/0x426d008d71ab2204293e27ada924091dcf8bbe79b0cd03d5c66ae7e52d22ff03
Dear Abcoathup,
Thank you for your answer. Please forgive me as I made a mistake in the title ( I wanted to refer to the Fallback contract).
In the case of the Fallback contract, is it a good practice to include safemath even if you don’t use it ?
Thank you very much for you detailed tests.
Best,
Caladay
Hi @Caladay,
I would recommend against importing a contract and not using it.
In the case of Fallback SafeMath isn’t used, so could be removed.
Dear Abcoathup,
Thank you very much for your help.
Hi @Caladay,
You are very welcome. Feel free to ask all the questions that you need.
If you are working through Ethernaut, there are a number of community solutions: Ethernaut Community Solutions
You could also share your own solutions too.