I thought using an interface would be lighter in terms of gas cost, but the experiment I did showed otherwise. See the following two snippets.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
interface IToken {
function totalSupply() external returns(uint256);
}
contract Test {
IToken token;
constructor(address addr) {
token = IToken(addr);
}
function getter() public returns(uint256) {
return token.totalSupply();
}
}
Versus this:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;
import "./Token.sol";
contract Test {
Token token;
constructor(address addr) {
token = Token(addr);
}
function getter() public returns(uint256) {
return token.totalSupply();
}
}
My thought was the one above, not importing a contract and only using an interface, should have a smaller gas cost, whereas the one below should cost more. The numbers are saying the opposite, although the difference is not a lot.
I am wondering why this is the case and which one is a better approach. Thanks.