Acton standard library
env
env.tolk standard library file
Module for accessing environment variables.
This module provides a type-safe way to access environment variables during test execution or script running.
It supports parsing environment variables into standard Tolk types like int, coins, bool,
string, slice, address, and cell.
env<T> supports only these types. For unsupported generic T, execution fails
via [Assert.fail].
Important null/present-value semantics:
- Missing variable: returns
nullfor every supported type. stringandslice: return the raw value unchanged, so empty strings and surrounding whitespace are preserved.??falls back only whenenv<T>returnednull. Present values like""orfalsedo not trigger the fallback branch.int,coins,address, andcell: invalid present values returnnull, so??can fall back.bool: when a variable is present, only"true"and"1"(case-insensitive) map totrue; any other present value maps tofalse, notnull.
Examples:
// Read an integer configuration
val iterations = env<int>("TEST_ITERATIONS") ?? 100;
// Read TON amount in nanotons
val minBalance = env<coins>("MIN_BALANCE") ?? ton("0.5");
// Read a target address
val target = env<address>("TARGET_ADDRESS");
if (target == null) {
throw 100; // error: target not found
}
// Read a feature flag
if (env<bool>("ENABLE_LOGGING") ?? false) {
println("Logging enabled");
}Definitions
env
fun env<T>(name: string): T?Returns value of the environment variable with the given name
or null if not found or parsing fails for a supported type.
Supported types:
int: parsed as decimal or hexadecimal (if prefixed with0x).coins: parsed the same way asintand returned as TON amount in nanotons.bool: for present variable, returns true if value is "true" or "1" (case-insensitive), otherwise false.string: returns the raw string value, including empty and whitespace-only values.slice: returns the raw string value as a slice, including empty and whitespace-only values.address: parsed as a TON address (raw or user-friendly); invalid present values returnnull.cell: parsed from base64 or hex encoded BoC; invalid present values returnnull.
Unsupported generic types do not return null and instead fail via Assert.fail.
Fallback behavior with ??:
env<string>("NAME") ?? "default"uses"default"only whenNAMEis missing. IfNAMEis present but empty, the result is still"".env<bool>("FLAG") ?? trueusestrueonly whenFLAGis missing. A present but malformed value resolves tofalse.
Example:
val foo = env<int>("FOO") ?? 42;
val amount = env<coins>("TRANSFER_VALUE") ?? ton("0.05");
val bar = env<string>("BAR") ?? "default";
val maybeDisabled = env<bool>("FEATURE_FLAG") ?? true;Last updated on