Skip to main content

Import Statement

Leo programs can import other programs to reuse functionality. Imports are declared before the program scope:

Import Syntax

The basic import syntax is:
Multiple programs can be imported:

Using Imported Programs

Once imported, you can reference types and call functions from imported programs:

Accessing Imported Types

Access types from imported programs using dot notation:

Calling Imported Functions

While Leo supports imports for type definitions, calling external program functions typically happens through the Aleo VM’s program composition at runtime.

Built-in Programs

Credits Program

The credits.aleo program is available by default:

Import Resolution

Leo resolves imports in the following order:
  1. Local programs: Programs in your project
  2. Dependencies: Programs specified in program.json
  3. Published programs: Programs deployed on the Aleo network

Project Structure

Dependencies Configuration

Specify external dependencies in program.json:

Import Patterns

Wrapper Pattern

Wrap imported functionality with your own interface:

Adapter Pattern

Adapt imported types to your program’s needs:

Composition Pattern

Combine multiple imported programs:

Import Limitations

No Wildcard Imports

Leo does not support wildcard imports:

No Selective Imports

You must import the entire program, not individual items:

No Aliasing

Leo does not support import aliases:

Best Practices

1. Explicit Imports

Import only what you need:

2. Document Dependencies

Comment on why imports are needed:

3. Version Dependencies

Specify exact versions in program.json:

4. Validate Imported Data

Always validate data from imported programs:

Circular Dependencies

Leo does not allow circular dependencies:
Circular imports will cause a compilation error. Refactor shared functionality into a separate program.

Resolving Circular Dependencies

Create a common base program:

Standard Library Patterns

Common patterns for importing standard functionality:

Token Standard

NFT Standard

Local Development

When developing locally, ensure imported programs are available:

Deployment Considerations

When deploying programs with imports:
  1. Deploy dependencies first: Imported programs must be deployed before programs that import them
  2. Verify program IDs: Ensure imported program IDs match deployed versions
  3. Network consistency: All programs must be on the same network (mainnet, testnet, etc.)

Example: Multi-Program Application

Complete example using multiple imports:

Next Steps

Programs

Learn about program structure

Records

Work with imported records

Data Types

Understand type compatibility

Functions

Use imported types in functions