Utilities

intents.utils.to_atomic(amount, decimals)[source]

Convert a human-readable token amount to its atomic (smallest unit) string.

Uses Decimal internally to avoid floating-point errors.

Parameters:
  • amount (int | float | str | Decimal) – Human-readable amount, e.g. 0.1 or "0.1".

  • decimals (int) – Number of decimals for the token (from TokenResponse.decimals).

Return type:

str

Returns:

Atomic amount as a string, e.g. "100000000000000000" for 0.1 ETH.

Raises:

ValueError – If the amount is negative or has more decimal places than the token supports.

Examples

>>> to_atomic("0.1", 18)   # 0.1 ETH → wei
'100000000000000000'
>>> to_atomic(1, 24)       # 1 NEAR → yoctoNEAR
'1000000000000000000000000'
intents.utils.from_atomic(amount, decimals)[source]

Convert an atomic (smallest unit) token amount to a human-readable Decimal.

Parameters:
  • amount (int | str) – Atomic amount as returned by the API, e.g. "100000000000000000".

  • decimals (int) – Number of decimals for the token.

Return type:

Decimal

Returns:

Human-readable amount, e.g. Decimal("0.1") for 0.1 ETH.

Examples

>>> from_atomic("100000000000000000", 18)
Decimal('0.1')
>>> from_atomic("1000000000000000000000000", 24)
Decimal('1')
intents.utils.token_to_atomic(amount, token)[source]

Convert a human-readable amount to atomic units using a TokenResponse.

Convenience wrapper around to_atomic() that reads decimals directly from the token object.

Parameters:
Return type:

str

Returns:

Atomic amount string ready to pass as amount in a QuoteRequest.

Examples

>>> tokens = await client.get_tokens()
>>> eth = find_token(tokens, "eth", "ETH")
>>> token_to_atomic(0.1, eth)
'100000000000000000'
intents.utils.token_from_atomic(amount, token)[source]

Convert an atomic amount to a human-readable Decimal using a TokenResponse.

Parameters:
Return type:

Decimal

Returns:

Human-readable Decimal.

intents.utils.pct_to_bps(percent)[source]

Convert a percentage to basis points.

Parameters:

percent (int | float | str | Decimal) – Percentage value, e.g. 1 for 1% or 0.5 for 0.5%.

Return type:

int

Returns:

Basis points as an integer, e.g. 100 for 1%.

Examples

>>> pct_to_bps(1)
100
>>> pct_to_bps(0.5)
50
intents.utils.bps_to_pct(bps)[source]

Convert basis points to a percentage.

Parameters:

bps (int) – Basis points, e.g. 100 for 1%.

Return type:

Decimal

Returns:

Percentage as a Decimal, e.g. Decimal("1") for 100 bps.

Examples

>>> bps_to_pct(100)
Decimal('1')
>>> bps_to_pct(50)
Decimal('0.50')
intents.utils.find_token(tokens, blockchain, symbol)[source]

Look up a token by blockchain and symbol from a token list.

Parameters:
  • tokens (list[TokenResponse]) – Token list returned by get_tokens().

  • blockchain (str) – The chain to search on, e.g. "eth", "arb", "sol".

  • symbol (str) – Token symbol, e.g. "ETH", "USDC". Case-insensitive.

Return type:

TokenResponse

Returns:

The matching TokenResponse.

Raises:

LookupError – If no token matches the given blockchain and symbol.

Examples

>>> tokens = await client.get_tokens()
>>> eth = find_token(tokens, "eth", "ETH")
>>> eth.asset_id
'nep141:eth.omft.near'