# Import Libraries
You can create a library script containing definitions of functions and variables and import the library into other scripts. You can use functions and variables from the library as if they were defined in the script itself.
Requirements:
- The names of functions and variables must be unique within the main script and all libraries imported.
- An imported library can use built-in functions, structures, and variables from the Standard Library of version specified in the main script.
- Library names must not contain spaces.
Library import is supported in Waves IDE and Surfboard.
# Library Script
The library script should start with directives. For example:
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE LIBRARY #-}
{-# STDLIB_VERSION 8 #-}
For library scripts, only the CONTENT_TYPE
directive is required. The SCRIPT_TYPE
and STDLIB_VERSION
directives affect only error highlighting during code editing, while the compilator will use the values of these directives from the main script.
After the directives, you can define variables and functions. Functions must not have annotations.
Example:
{-# CONTENT_TYPE LIBRARY #-}
let someConstant = 42
func doSomething() = {
height + someConstant
}
# Import Library
To import a library, use the IMPORT directive.
In Waves IDE, you can specify names of the libraries from the local storage. Example:
{-# IMPORT lib1,my_lib2 #-}
The following script uses the above library saved as lib1
:
{-# STDLIB_VERSION 8 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# IMPORT lib1 #-}
@Callable(i)
func call() = {
[
IntegerEntry("key", doSomething())
]
}
In Surfboard, you can also specify path relative to the current folder:
{-# IMPORT lib3.ride,dir/lib4.ride #-}
⚠️ The IMPORT directive must be the last of directives, otherwise the script won't compile.
# Compilation
When compiling a script with imported libraries, the functions and variables from the libraries are added to the compiled script.
In Waves IDE, you can reduce the size of the compiled script. Before compilation, enable the checkboxes:
- Compaction: use shortened names of functions and variables with mapping to original names.
- Remove unused code: remove unused functions and variables defined globally.