Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compiling Generated Solidity Code

After extracting your Ursus contract to Solidity, you need to compile the generated .sol file for deployment to an EVM-compatible blockchain.

Prerequisites

  • Solidity compiler (solc) version 0.8.0 or later
  • Generated .sol file from Ursus extraction
  • Node.js and npm (for Hardhat/Truffle)

Quick Compilation with solc

Install Solidity Compiler

Using npm:

npm install -g solc

Using binary:

# Download from Solidity releases page
# Or use version manager
brew install solidity  # macOS

Compile Contract

Basic compilation:

solc --bin --abi MyContract.sol -o build/

Options:

  • --bin - Generate bytecode
  • --abi - Generate ABI (Application Binary Interface)
  • -o build/ - Output directory

Example:

solc --bin --abi ERC20.sol -o build/

Output files:

  • build/ERC20.bin - Contract bytecode
  • build/ERC20.abi - Contract ABI (JSON)

Optimization

Enable optimizer for gas efficiency:

solc --optimize --optimize-runs 200 --bin --abi MyContract.sol -o build/

Optimization levels:

  • --optimize-runs 1 - Optimize for deployment cost
  • --optimize-runs 200 - Balanced (default)
  • --optimize-runs 1000000 - Optimize for execution cost

Using Hardhat

Hardhat is the recommended development environment for Solidity contracts.

Setup Hardhat Project

1. Initialize project:

mkdir my-ursus-project
cd my-ursus-project
npm init -y
npm install --save-dev hardhat

2. Create Hardhat config:

npx hardhat
# Select "Create an empty hardhat.config.js"

3. Install dependencies:

npm install --save-dev @nomiclabs/hardhat-ethers ethers

Configure Hardhat

Edit hardhat.config.js:

require("@nomiclabs/hardhat-ethers");

module.exports = {
  solidity: {
    version: "0.8.20",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: "./contracts",
    artifacts: "./artifacts"
  }
};

Compile with Hardhat

1. Place contract in contracts/ directory:

mkdir -p contracts
cp MyContract.sol contracts/

2. Compile:

npx hardhat compile

Output:

  • artifacts/contracts/MyContract.sol/MyContract.json - ABI and bytecode
  • Compilation artifacts in artifacts/ directory

Verify Compilation

npx hardhat compile --force

Check for warnings:

npx hardhat compile 2>&1 | grep -i warning

Using Truffle

Truffle is another popular development framework.

Setup Truffle Project

1. Install Truffle:

npm install -g truffle

2. Initialize project:

mkdir my-ursus-project
cd my-ursus-project
truffle init

Configure Truffle

Edit truffle-config.js:

module.exports = {
  compilers: {
    solc: {
      version: "0.8.20",
      settings: {
        optimizer: {
          enabled: true,
          runs: 200
        }
      }
    }
  }
};

Compile with Truffle

1. Place contract in contracts/ directory:

cp MyContract.sol contracts/

2. Compile:

truffle compile

Output:

  • build/contracts/MyContract.json - ABI and bytecode

Handling Dependencies

If your generated Solidity code imports other contracts:

OpenZeppelin Contracts

npm install @openzeppelin/contracts

In Solidity:

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Custom Imports

Ensure all imported files are in the correct directory structure:

contracts/
├── MyContract.sol
├── interfaces/
│   └── IERC20.sol
└── libraries/
    └── SafeMath.sol

Compilation Errors

Common Issues

Error: "Source file requires different compiler version"

Error: Source file requires different compiler version

Solution: Update solidity version in config to match contract pragma

Error: "Undeclared identifier"

Error: Undeclared identifier 'SafeMath'

Solution: Install missing dependencies or add import statements

Error: "Stack too deep"

Error: Stack too deep when compiling inline assembly

Solution: Refactor function to use fewer local variables or enable optimizer

Debugging

Verbose compilation:

solc --verbose MyContract.sol

Check AST:

solc --ast-compact-json MyContract.sol

Verification

After compilation, verify the contract:

Check Bytecode Size

# Bytecode should be < 24KB for deployment
wc -c build/MyContract.bin

Verify ABI

# Check ABI is valid JSON
cat build/MyContract.abi | jq .

Gas Estimation

Using Hardhat:

const Contract = await ethers.getContractFactory("MyContract");
const contract = await Contract.deploy();
console.log("Deployment gas:", contract.deployTransaction.gasLimit);

Next Steps

See Also