Ethernaut Fallback: import '*/SafeMath.sol'; + import gas cost

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

1 Like

Hi @Caladay,

Welcome to the community :wave:

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

Test1

No import + no use

pragma solidity ^0.5.0;

contract Test1 {
}

Test2

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 {
}

Test3

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

1 Like

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.

1 Like

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.