PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How many state bytes will contract address variable consume in Solidity?

 July 28, 2022     blockchain, ethereum, smartcontracts, solidity     No comments   

Issue

I am wondering how many bytes will be if you declare Contract type in the state variable of a contract like below.

contract ContractA {
  // some code...
}
contract ContractB {
  ContractA contractA; // how much bytes will this state take up?
  constructor (address addr) {
    contractA = ContractA(addr);
  }
}

I am curious about this because 32 bytes in a single storage slot in solidity fit multiple variables into a single slot besides contract type variable.

Is it the same as the address type, 20 bytes?


Solution

In Solidity, all storage slots are 32 bytes

this is correct but that does not mean that state variables will consume 32 bytes. For example

uint8 public a=7 // 1 byte
uint16 public b=12 // 2 bytes
address public c=0xasdkhfk...... // 20 bytes
bool d=true //1 byte
uint64 public e=12 //8 bytes
uint256 public f=213 // 32 bytes

Ethereum virtual machine will pack the state variables into the slots. In the above state variables, the first 5 variables a, b, c, d and e will be placed into the first slot. as you see the order of items matters in solidity.

Based on a great answer here: https://ethereum.stackexchange.com/questions/115413/what-does-an-explicit-conversion-from-an-address-to-a-contract-type-actually-do

In Solidity a contract variable is really just an address under the hood.

that means it takes up 20 bytes. The author of the answer explains the difference betweeen address and contract type. we can call the contract with the address itself, too:

You can actually interact with code deployed at a particular address without having a contract variable. address has member functions like .call() for example. These functions are a low-level EVM way to make an external call. At this level functions do not really exist. You specify some call data to be sent to that code and receive back a piece of return data.

Member of address type

Here why do we use contract type:

Solidity's contract types are an abstraction to make that external code look more like classes you may be familiar with from other languages. Classes can have methods, and in Solidity the compiler simulates this by having the bytecode expect a function ID (selector) in the first 4 bytes of call data and do different things based on which ID was sent. In particular the ID determines how to interpret the data that follows it (i.e. the parameters).

Instead of using the low-level .call() on an address and manually constructing the right call data, you can just call an external function on a contract, and the compiler will convert that for you to a low-level call in the bytecode it generates. To do this, however, it needs to know the address of that contract. You can give it this information by casting the address to the contract type. For convenience, you can store the result of such a cast in a contract variable instead of having to cast it every time.



Answered By - Yilmaz
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing