Skip to main content

A simple python utility library for representing cryptocurrency amounts

Project description

cryptils

Tests Coverage License PyPI TestPyPI
A utility library for representing cryptocurrency and fiat amounts in Python.

Installation

pip install cryptils

Quick Start

from cryptils import BTCAmount, ETHAmount, USDCAmount, USDTAmount, USDAmount, JPYAmount

# Create amounts with exact decimal precision
btc = BTCAmount("1.5")
print(btc)  # 1.50000000 BTC

eth = ETHAmount("2.0")
print(eth)  # 2.000000000000000000 ETH

# Arithmetic with other amounts
result = BTCAmount("0.5") + BTCAmount("0.25")
print(result)  # 0.75000000 BTC

# Arithmetic with built-in types (int, float, Decimal)
from decimal import Decimal

btc = BTCAmount("1.0")
print(btc + 0.5)  # 1.50000000 BTC (added float)
print(btc + 2)  # 3.00000000 BTC (added int)
print(btc + Decimal("0.5"))  # 1.50000000 BTC (added Decimal)
print(2 * btc)  # 2.00000000 BTC (int * BTC)
print(10 - BTCAmount("2.5"))  # 7.50000000 BTC (int - BTC)

# Different currencies maintain their own precision
usdc = USDCAmount("100")
print(usdc)  # 100.000000 USDC

# Access the raw Decimal value
print(btc.to_decimal())  # Decimal('1.50000000')

# Get the formatted string explicitly
print(btc.to_string())  # BTC 1.50000000

# Fiat amounts with proper precision
usd = USDAmount("99.99")
print(usd)              # 99.99
print(usd.to_string())  # USD 99.99

jpy = JPYAmount("150")
print(jpy)              # 150
print(jpy.to_string())  # JPY 150

Features

  • Uses decimal.Decimal internally to avoid floating-point errors.
  • Consistent precision handling per currency (e.g., 8 decimals for BTC, 6 for USDC).
  • Full fiat currency support (USD, EUR, GBP, JPY, etc.) with correct decimal precision.
  • Simple, explicit API designed for financial precision.

Pydantic Support

CurrencyAmount subclasses (crypto and fiat) work as Pydantic v2 field types:

from pydantic import BaseModel
from cryptils import BTCAmount, ETHAmount, USDCAmount, USDAmount


class WalletBalance(BaseModel):
    btc: BTCAmount
    eth: ETHAmount
    usdc: USDCAmount
    usd: USDAmount


wallet = WalletBalance(btc="1.5", eth=2, usdc=100.0, usd="99.99")
print(wallet.model_dump_json())
# {"btc":"1.50000000","eth":"2.000000000000000000","usdc":"100.000000","usd":"99.99"}

Requires Pydantic v2 (pip install pydantic).

Development

This project uses uv for environment management, ruff for linting and formatting, and tox for testing across Python versions.

# Setup environment
uv sync

# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov

# Run tests across all supported Python versions
uv run tox

# Format and lint
uv run ruff check --fix .
uv run ruff format .

# Install pre-commit hooks
uv run pre-commit install

# Run pre-commit on all files
uv run pre-commit run --all-files

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cryptils-0.1.0b1.tar.gz (52.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cryptils-0.1.0b1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file cryptils-0.1.0b1.tar.gz.

File metadata

  • Download URL: cryptils-0.1.0b1.tar.gz
  • Upload date:
  • Size: 52.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptils-0.1.0b1.tar.gz
Algorithm Hash digest
SHA256 5ba302dd2f6aadb41dc96dc89f45877e5c12b24bc95270c1402c9874c6f6d398
MD5 35daa0bd3851c36d9b83d08c29d00666
BLAKE2b-256 d8852ef698909869337c4eff594551539be0173b0e6d3d9ff92bc83815a71b9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptils-0.1.0b1.tar.gz:

Publisher: publish-pypi.yml on seba3c/cryptils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cryptils-0.1.0b1-py3-none-any.whl.

File metadata

  • Download URL: cryptils-0.1.0b1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cryptils-0.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 09ca7d98c434b198ca761adc1169d525b85d2b7e929c22b8f77bb6d6d534cf48
MD5 de63fd9aacabc1b78630deece37458ef
BLAKE2b-256 13744d544c5fec1910ac4b3d85759de2b2bbcea4c42ca70d16d1d6a3130ab16c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cryptils-0.1.0b1-py3-none-any.whl:

Publisher: publish-pypi.yml on seba3c/cryptils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page