0
0
Blockchain / Solidityprogramming~10 mins

SPDX license and pragma version in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SPDX license and pragma version
Start of Solidity file
Add SPDX License Identifier
Add pragma solidity version
Write contract code
Compile and deploy
This flow shows adding SPDX license and pragma version at the top of a Solidity file before writing contract code.
Execution Sample
Blockchain / Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Simple {
    uint x;
}
This code sets the license to MIT and specifies Solidity compiler version 0.8.0 or higher.
Execution Table
StepCode LineActionEffectNotes
1// SPDX-License-Identifier: MITAdd license identifierSpecifies MIT license for the codeHelps tools know license type
2pragma solidity ^0.8.0;Set compiler versionCompiler must be 0.8.0 or higher, less than 0.9.0Ensures compatibility
3contract Simple {Start contractDefines contract named SimpleStart of contract code
4 uint x;Declare variableCreates unsigned integer xInside contract
5}End contractCloses contract definitionEnd of contract
6CompileCheck license and versionCompiler checks pragma and SPDXIf mismatch, error occurs
💡 Compilation succeeds if SPDX and pragma are valid and code is correct
Variable Tracker
VariableStartAfter Step 4Final
xundefined0 (default uint value)0
Key Moments - 3 Insights
Why do we add the SPDX license line at the top?
The SPDX line tells tools and users what license the code uses. It is a comment but important for legal clarity, as shown in execution_table step 1.
What does the pragma solidity ^0.8.0 mean?
It means the code works with compiler version 0.8.0 or higher but less than 0.9.0. This prevents using incompatible compiler versions, as seen in execution_table step 2.
What happens if the pragma version does not match the compiler?
The compiler will give an error and stop compiling, shown in execution_table step 6 where compilation checks pragma version.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the purpose of the line '// SPDX-License-Identifier: MIT' at step 1?
AIt sets the compiler version
BIt specifies the license type for the code
CIt declares a contract
DIt initializes a variable
💡 Hint
See execution_table row 1 under 'Effect' and 'Notes'
At which step does the compiler check the pragma version?
AStep 6
BStep 1
CStep 4
DStep 2
💡 Hint
Check execution_table step 6 where compilation happens
If we change pragma to 'pragma solidity ^0.9.0;', what will happen during compilation?
ACompilation will succeed with any compiler version
BLicense will change automatically
CCompilation will fail if compiler is below 0.9.0
DVariable x will be initialized differently
💡 Hint
Refer to key_moments about pragma version compatibility and execution_table step 6
Concept Snapshot
// SPDX-License-Identifier: <license>
pragma solidity ^<version>;

- SPDX line declares license for legal clarity
- Pragma sets compiler version range
- Both go at top of Solidity files
- Compiler enforces pragma version
- Helps tools and users understand code licensing
Full Transcript
In Solidity files, the SPDX license identifier is a comment at the top that tells what license the code uses, like MIT. This helps tools and users know the legal terms. Next, the pragma solidity line sets the compiler version range, for example ^0.8.0 means version 0.8.0 or higher but less than 0.9.0. This ensures the code compiles only with compatible compiler versions. The contract code follows these lines. During compilation, the compiler checks the pragma version and license comment. If the pragma version does not match the compiler, compilation fails. The variable inside the contract starts with a default value. This flow helps keep Solidity code clear, legal, and compatible.