SPDX license and pragma version help make your smart contract clear and safe. SPDX shows the license type, and pragma tells the compiler which Solidity version to use.
0
0
SPDX license and pragma version in Blockchain / Solidity
Introduction
When you create a new Solidity smart contract to share or publish.
When you want to make sure your contract works with a specific Solidity compiler version.
When you want to clearly state the license for your contract code to others.
When you want to avoid errors from using incompatible compiler versions.
When you prepare your contract for deployment on blockchain networks.
Syntax
Blockchain / Solidity
// SPDX-License-Identifier: <license> pragma solidity ^<version>;
The SPDX license line must be the very first line in your Solidity file.
The pragma line tells the compiler which Solidity version(s) your code supports.
Examples
This example uses the MIT license and supports Solidity version 0.8.0 and above, but less than 0.9.0.
Blockchain / Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;This example uses the GPL-3.0 license and requires exactly Solidity version 0.7.6.
Blockchain / Solidity
// SPDX-License-Identifier: GPL-3.0 pragma solidity 0.7.6;
This example has no license (unlicensed) and supports Solidity versions from 0.6.0 up to but not including 0.9.0.
Blockchain / Solidity
// SPDX-License-Identifier: UNLICENSED pragma solidity >=0.6.0 <0.9.0;
Sample Program
This simple contract uses the MIT license and Solidity version 0.8.0 or higher. It stores a greeting message.
Blockchain / Solidity
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract HelloWorld { string public greet = "Hello, blockchain!"; }
OutputSuccess
Important Notes
Always put the SPDX license identifier as the very first line to avoid compiler warnings.
Use the caret (^) in pragma to allow compatible newer versions but avoid breaking changes.
Choosing the right license helps others know how they can use your code.
Summary
SPDX license line states the code license clearly.
Pragma version controls which Solidity compiler versions can compile your code.
Both help keep your smart contract safe, clear, and compatible.