Skip to content

refactoring: deduplicate wallet, rate source, and API handler logic - #237

Open
olegsta wants to merge 2 commits into
vsys-host:mainfrom
olegsta:features/refact_code
Open

refactoring: deduplicate wallet, rate source, and API handler logic#237
olegsta wants to merge 2 commits into
vsys-host:mainfrom
olegsta:features/refact_code

Conversation

@olegsta

@olegsta olegsta commented Jun 25, 2026

Copy link
Copy Markdown
Contributor
  • Extract shared UTXO HTTP wallet logic into UtxoHttpWallet for BTC, LTC, and DOGE
  • Consolidate EVM chain classes (ETH, Polygon, Arbitrum, Optimism, BNB, Avalanche, Solana, TON, XRP) into configurable Ethereum base with chain-specific attributes and hooks
  • Simplify Binance, Coinbase, Kraken, and KuCoin rate providers to use shared normalization
  • Add api/helpers.py with resolve_available_crypto(), api_error_response(), verify_backend_key(), and handle_request_error()
  • Refactor api_v1 endpoints to use shared helpers and remove duplicated error/backend-key handling

- Extract shared UTXO HTTP wallet logic into UtxoHttpWallet for BTC, LTC, and DOGE
- Consolidate EVM chain classes (ETH, Polygon, Arbitrum, Optimism, BNB, Avalanche, Solana, TON, XRP) into configurable Ethereum base with chain-specific attributes and hooks
- Move rate symbol normalization into RateSource.normalize_symbols(), including MATIC→POL and optional TON→GRAM mappings
- Simplify Binance, Coinbase, Kraken, and KuCoin rate providers to use shared normalization
- Add api/helpers.py with resolve_available_crypto(), api_error_response(), verify_backend_key(), and handle_request_error()
- Refactor api_v1 endpoints to use shared helpers and remove duplicated error/backend-key handling
- Register inject_theme once at app level in create_app() instead of duplicating it in auth and wallet blueprints
@olegsta olegsta changed the title refactor: deduplicate wallet, rate source, and API handler logic refactoring: deduplicate wallet, rate source, and API handler logic Jun 25, 2026
Comment thread shkeeper/api_v1.py
if error := verify_backend_key():
return error

Crypto.instances[crypto_name]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the variable because it was unused. Here we only need to validate that crypto_name exists in Crypto.instances; direct access Crypto.instances[crypto_name] is enough and keeps the intent explicit (raise Exception).

Comment thread shkeeper/api_v1.py
try:
crypto = Crypto.instances[crypto_name]
bkey = environ.get(f"SHKEEPER_BTC_BACKEND_KEY", "shkeeper")
Crypto.instances[crypto_name]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the variable because it was unused. Here we only need to validate that crypto_name exists in Crypto.instances; direct access Crypto.instances[crypto_name] is enough and keeps the intent explicit (raise KeyError if missing).

Comment thread shkeeper/api_v1.py Outdated
bkey = environ.get(f"SHKEEPER_BTC_BACKEND_KEY", "shkeeper")
Crypto.instances[crypto_name]
bkey = environ.get("SHKEEPER_BTC_BACKEND_KEY", "shkeeper")
if request.headers["X-Shkeeper-Backend-Key"] != bkey:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use verify_backend_key() ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to verify_backend_key

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the wallet, rate-source, and API layers to centralize repeated logic and make chain/provider-specific behavior configurable via shared base classes and helpers.

Changes:

  • Introduces UtxoHttpWallet for BTC/LTC/DOGE and refactors those coin classes to inherit from it.
  • Consolidates multiple chain classes into the Ethereum base by moving host/creds/status/payout logic into overridable hooks + chain-specific attributes.
  • Centralizes rate-symbol normalization in RateSource and API error/backend-key handling in shkeeper/api/helpers.py, then refactors api_v1 endpoints to use them.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
shkeeper/wallet.py Removes blueprint-level theme injection (now app-level).
shkeeper/auth.py Removes blueprint-level theme injection (now app-level).
shkeeper/init.py Adds app-wide inject_theme() context processor.
shkeeper/api/helpers.py Adds shared API helpers (crypto resolution, backend key, error wrapping).
shkeeper/api_v1.py Refactors endpoints to use shared API helpers.
shkeeper/modules/classes/utxo_http_wallet.py Adds shared HTTP wallet implementation for UTXO coins.
shkeeper/modules/classes/btc.py Switches BTC to UtxoHttpWallet and enables tx logging.
shkeeper/modules/classes/ltc.py Switches LTC to UtxoHttpWallet.
shkeeper/modules/classes/doge.py Switches DOGE to UtxoHttpWallet.
shkeeper/modules/classes/ethereum.py Adds configurable env prefixes/host + hook points for status/payout logic.
shkeeper/modules/classes/arbitrum.py Converts Arbitrum to Ethereum-configured subclass.
shkeeper/modules/classes/avalanche.py Converts Avalanche to Ethereum-configured subclass.
shkeeper/modules/classes/bnb.py Converts BNB to Ethereum-configured subclass.
shkeeper/modules/classes/optimism.py Converts Optimism to Ethereum-configured subclass with distinct creds prefix.
shkeeper/modules/classes/polygon.py Converts Polygon to Ethereum-configured subclass.
shkeeper/modules/classes/solana.py Converts Solana to Ethereum-configured subclass with status tuning.
shkeeper/modules/classes/ton.py Converts TON to Ethereum-configured subclass with float block interval + lag multiplier.
shkeeper/modules/classes/xrp.py Converts XRP to Ethereum-configured subclass with timestamp/fee hooks.
shkeeper/modules/classes/rate_source.py Adds shared symbol normalization + USDT/USD parity handling.
shkeeper/modules/rates/binance.py Uses shared normalization helper.
shkeeper/modules/rates/coinbase.py Uses shared normalization helper.
shkeeper/modules/rates/kraken.py Uses shared normalization helper (including parity shortcut).
shkeeper/modules/rates/kucoin.py Uses shared normalization helper with KuCoin-specific options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +14 to +16
def _subtract_payout_fee(self, amount, fee):
# 10 XRP need to keep the fee-deposit account active
return amount - fee - 10

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it, but that logic was already in the master branch

Comment thread shkeeper/modules/classes/utxo_http_wallet.py Outdated
Comment thread shkeeper/modules/classes/ethereum.py
Comment thread shkeeper/api/helpers.py
Comment on lines +88 to 92
block_interval = self.block_interval
if delta < block_interval * self.sync_lag_multiplier:
return "Synced"
else:
return "Sync In Progress (%d blocks behind)" % (delta // block_interval)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is the same as in master. This MR only refactors the code without changing its behavior

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Comment thread shkeeper/api_v1.py
Comment thread shkeeper/api_v1.py Outdated
- Change log text
- Change subtractpayout fee logic

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants