Utilities¶
- intents.utils.to_atomic(amount, decimals)[source]¶
Convert a human-readable token amount to its atomic (smallest unit) string.
Uses
Decimalinternally to avoid floating-point errors.- Parameters:
- Return type:
- 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:
- Return type:
- 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 readsdecimalsdirectly from the token object.- Parameters:
amount (
int|float|str|Decimal) – Human-readable amount, e.g.0.1.token (
TokenResponse) – Token metadata returned byget_tokens().
- Return type:
- Returns:
Atomic amount string ready to pass as
amountin aQuoteRequest.
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
Decimalusing aTokenResponse.- Parameters:
amount (
int|str) – Atomic amount string as returned by the API.token (
TokenResponse) – Token metadata returned byget_tokens().
- Return type:
- 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.1for 1% or0.5for 0.5%.- Return type:
- Returns:
Basis points as an integer, e.g.
100for 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.100for 1%.- Return type:
- 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 byget_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:
- 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'