Bird
Raised Fist0
Blockchain / Solidityprogramming~30 mins

Variable packing in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Variable Packing in Solidity
📖 Scenario: You are building a smart contract to store user information efficiently on the Ethereum blockchain. Since storage costs gas, you want to pack multiple small variables into a single storage slot.
🎯 Goal: Create a Solidity contract that uses variable packing to store a user's age, a boolean flag for membership, and a small number representing user level in a single storage slot.
📋 What You'll Learn
Create a contract named UserInfo
Declare three state variables: uint8 age, bool isMember, and uint8 level
Use variable packing by declaring the variables in the correct order
Write a function setUserInfo to set all three variables
Write a function getUserInfo that returns all three variables
💡 Why This Matters
🌍 Real World
Variable packing helps reduce gas costs in Ethereum smart contracts by storing multiple small variables in a single 32-byte storage slot.
💼 Career
Understanding variable packing is important for blockchain developers to write efficient and cost-effective smart contracts.
Progress0 / 4 steps
1
Create the contract and declare packed variables
Create a Solidity contract named UserInfo. Inside it, declare three state variables in this order: uint8 age, bool isMember, and uint8 level. This order helps Solidity pack them into one storage slot.
Blockchain / Solidity
Hint

Declare the variables in the order: uint8 age, then bool isMember, then uint8 level to enable packing.

2
Add a function to set user info
Inside the UserInfo contract, write a public function named setUserInfo that takes three parameters: uint8 _age, bool _isMember, and uint8 _level. Assign these parameters to the state variables age, isMember, and level respectively.
Blockchain / Solidity
Hint

Define a public function setUserInfo with three parameters and assign them to the state variables.

3
Add a function to get user info
Inside the UserInfo contract, write a public view function named getUserInfo that returns three values: uint8 for age, bool for isMember, and uint8 for level. Return the state variables in the order age, isMember, level.
Blockchain / Solidity
Hint

Define a public view function getUserInfo that returns the three state variables in order.

4
Test the contract by setting and getting values
Write a simple test in Solidity by adding a public function named testUserInfo that calls setUserInfo with age = 30, isMember = true, and level = 5, then calls getUserInfo and returns the results.
Blockchain / Solidity
Hint

Create a function testUserInfo that sets values and returns them to verify packing works.

Practice

(1/5)
1.

What is the main benefit of variable packing in blockchain smart contracts?

easy
A. It reduces storage space and lowers gas fees.
B. It increases the number of variables allowed.
C. It makes the code run faster on all blockchains.
D. It automatically encrypts stored data.

Solution

  1. Step 1: Understand variable packing purpose

    Variable packing groups smaller variables to use less storage space.
  2. Step 2: Connect storage saving to gas fees

    Less storage means lower gas fees because blockchain charges for storage.
  3. Final Answer:

    It reduces storage space and lowers gas fees. -> Option A
  4. Quick Check:

    Variable packing = saves space and gas [OK]
Hint: Variable packing saves storage and gas fees [OK]
Common Mistakes:
  • Thinking it increases variable count
  • Assuming it speeds up all code execution
  • Believing it encrypts data automatically
2.

Which of the following Solidity variable declarations best uses variable packing?

uint8 a;
uint16 b;
uint32 c;
easy
A. Declare all as uint256 for best packing.
B. Declare as uint32 c; uint16 b; uint8 a; in this order.
C. Declare as uint8 a; uint16 b; uint32 c; in this order.
D. Declare as uint16 b; uint8 a; uint32 c; in this order.

Solution

  1. Step 1: Order variables from smallest to largest

    Variable packing works best when smaller variables come first to fill storage slots efficiently.
  2. Step 2: Check given orders

    Declare as uint8 a; uint16 b; uint32 c; in this order. orders variables from uint8 (smallest) to uint32 (largest), maximizing packing.
  3. Final Answer:

    Declare as uint8 a; uint16 b; uint32 c; in this order. -> Option C
  4. Quick Check:

    Smallest to largest order = best packing [OK]
Hint: Order variables smallest to largest for packing [OK]
Common Mistakes:
  • Placing largest variables first wastes space
  • Using all uint256 wastes storage
  • Mixing order without size consideration
3.

Consider this Solidity struct packed into one storage slot:

struct Data {
  uint8 x;
  uint16 y;
  uint8 z;
}
Data d = Data(1, 300, 2);

What is the total storage size used by d?

medium
A. 128 bits (16 bytes)
B. 256 bits (32 bytes)
C. 64 bits (8 bytes)
D. 32 bits (4 bytes)

Solution

  1. Step 1: Calculate bits for each variable

    uint8 = 8 bits, uint16 = 16 bits, uint8 = 8 bits; total = 8+16+8 = 32 bits.
  2. Step 2: Understand Solidity storage slot size

    Solidity packs variables into 256-bit slots, but here total variables use only 32 bits, so storage used is 32 bits.
  3. Final Answer:

    32 bits (4 bytes) -> Option D
  4. Quick Check:

    8+16+8 = 32 bits used [OK]
Hint: Sum bits of variables to find packed size [OK]
Common Mistakes:
  • Assuming full 256 bits always used
  • Adding bytes instead of bits incorrectly
  • Confusing variable sizes
4.

Identify the error in this Solidity contract snippet related to variable packing:

contract Example {
  uint256 a;
  uint8 b;
  uint16 c;
}

Why is this not optimized for variable packing?

medium
A. Variables are not ordered from smallest to largest size.
B. uint256 cannot be packed with smaller types.
C. uint8 and uint16 must be declared as uint256.
D. All variables must be declared as uint8 for packing.

Solution

  1. Step 1: Check variable order for packing

    Variable packing requires ordering from smallest to largest to fill storage slots efficiently.
  2. Step 2: Analyze given order

    Here, uint256 a is first (largest), then smaller uint8 and uint16, which wastes space.
  3. Final Answer:

    Variables are not ordered from smallest to largest size. -> Option A
  4. Quick Check:

    Order smallest to largest for packing [OK]
Hint: Order variables smallest to largest to fix packing [OK]
Common Mistakes:
  • Thinking uint256 can't be packed
  • Believing all must be uint8
  • Ignoring variable order importance
5.

You want to store these variables in a Solidity contract efficiently:

bool isActive;
uint8 count;
uint256 total;
uint16 code;

Which variable order best uses variable packing to minimize storage slots?

hard
A. uint256 total; uint16 code; uint8 count; bool isActive;
B. bool isActive; uint8 count; uint16 code; uint256 total;
C. uint16 code; uint256 total; bool isActive; uint8 count;
D. uint8 count; bool isActive; uint256 total; uint16 code;

Solution

  1. Step 1: Order variables from smallest to largest size

    bool (1 bit), uint8 (8 bits), uint16 (16 bits), uint256 (256 bits) is the correct size order.
  2. Step 2: Check options for this order

    bool isActive; uint8 count; uint16 code; uint256 total; lists variables in this order, maximizing packing into fewer storage slots.
  3. Final Answer:

    bool isActive; uint8 count; uint16 code; uint256 total; -> Option B
  4. Quick Check:

    Smallest to largest order = best packing [OK]
Hint: Place smallest variables first, largest last for packing [OK]
Common Mistakes:
  • Placing uint256 first wastes slots
  • Ignoring bool size as smallest
  • Mixing order without size logic