0
0
Blockchain / Solidityprogramming~10 mins

Library contracts in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Library contracts
Define Library Contract
Deploy Library
Link Library Address to Main Contract
Deploy Main Contract
Main Contract Calls Library Functions
Library Code Executes in Main Contract Context
End
Library contracts are deployed once and linked to main contracts, allowing code reuse without copying, executing in the caller's context.
Execution Sample
Blockchain / Solidity
library MathLib {
  function add(uint a, uint b) internal pure returns (uint) {
    return a + b;
  }
}

contract Calculator {
  function sum(uint x, uint y) public pure returns (uint) {
    return MathLib.add(x, y);
  }
}
This code defines a library with an add function and a contract that uses it to sum two numbers.
Execution Table
StepActionEvaluationResult
1Deploy MathLib libraryLibrary code stored on blockchainMathLib address assigned
2Deploy Calculator contract linked to MathLibCalculator knows MathLib addressCalculator deployed with link
3Call Calculator.sum(3, 4)Calls MathLib.add(3, 4)7 returned
4MathLib.add executes in Calculator contextAdds 3 + 47 computed
5Return result to caller7 returned from sum7 output
6EndNo more stepsExecution complete
💡 Execution stops after returning the sum result from the library function.
Variable Tracker
VariableStartAfter sum callAfter add callFinal
aundefined333
bundefined444
resultundefinedundefined77
Key Moments - 3 Insights
Why does the library code run in the main contract's context?
Because library functions are called using delegatecall, they execute with the main contract's storage and context, as shown in step 4 of the execution_table.
Does the library get deployed every time the main contract is deployed?
No, the library is deployed once separately (step 1), and the main contract links to its address (step 2), avoiding code duplication.
What happens if the library address is not linked correctly?
The main contract will fail to call the library functions, causing runtime errors or failed transactions, as linking is essential (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result returned by MathLib.add at step 4?
A7
B3
C4
D0
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does the Calculator contract get linked to the MathLib library?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step mentioning linking in the execution_table.
If the library was not deployed first, which step would fail?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Refer to the execution_table step where linking happens.
Concept Snapshot
Library contracts are reusable code blocks deployed once.
Main contracts link to libraries by address.
Library functions run in caller's context using delegatecall.
This saves gas and avoids code duplication.
Always deploy libraries before linking.
Full Transcript
Library contracts in blockchain are special contracts that hold reusable code. They are deployed once and then linked to other contracts that want to use their functions. When a main contract calls a library function, the library code runs in the main contract's context, meaning it uses the main contract's storage and environment. This is done using a special call called delegatecall. The process starts by deploying the library contract, then deploying the main contract linked to the library's address. When the main contract calls a library function, the library executes and returns the result. This approach saves space and gas because the library code is not copied into every contract. If the library is not deployed or linked correctly, calls to it will fail. The example code shows a MathLib library with an add function and a Calculator contract that uses it to sum two numbers. The execution table traces deployment, linking, calling, and returning results step by step.