Solidity Contract Development: Relevance of Contract Class Name

contract-developmentsolidity

Let us consider a following contract generated by the wizard

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SomeClassNameHere is ERC20 {
    constructor() ERC20("MyToken", "MTK") {}
}

the class name is SomeClassNameHere. I wonder if that is relevant. Could I re-use same class name for many projects? I suppose after compiled to EVM there will be no trace of it anyway. Could someone confirm?

Best Answer

The name does not affect functionality of the contract in any way. Compiling the same source with just the name changed will give you the same exact bytecode (except for the metadata hash embedded at the end).

Any change in the source will affect the metadata though. The consequence is that the contract will not pass verification on services that compare both bytecode and metadata (like Sourcify). Most other verification services (e.g. Etherscan) ignore metadata though and will accept source even with much bigger modifications. Anything that does not affect the bytecode itself (changed contract names, variable names, file names and paths, comments, unused code that gets optimized out) will pass.

Could I re-use same class name for many projects?

You can actually reuse it even in the same project. You can give multiple contracts the same name as long as you don't import them into the same file. And even then you can work around it by using the import ... as ... syntax and giving them different names locally.

Related Topic