@onecraft/wallet 0.0.0-stage → 0.1.145
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +20 -0
- package/.openclaw/AGENTS.md +100 -0
- package/.openclaw/extensions/agent-wallet/README.md +195 -0
- package/.openclaw/extensions/agent-wallet/dist/index.js +2198 -0
- package/.openclaw/extensions/agent-wallet/index.ts +2198 -0
- package/.openclaw/extensions/agent-wallet/openclaw.plugin.json +251 -0
- package/.openclaw/extensions/agent-wallet/package.json +49 -0
- package/.openclaw/extensions/agent-wallet/skills/cards/SKILL.md +211 -0
- package/.openclaw/extensions/agent-wallet/skills/wallet-operator/SKILL.md +30 -0
- package/CHANGELOG.md +1232 -0
- package/LICENSE +104 -0
- package/README.md +193 -2
- package/VERSION +1 -0
- package/agent-wallet/.env.example +63 -0
- package/agent-wallet/AGENTS.md +130 -0
- package/agent-wallet/CARD_VAULT.md +640 -0
- package/agent-wallet/README.md +615 -0
- package/agent-wallet/UPGRADE_COMPATIBILITY.md +66 -0
- package/agent-wallet/agent_wallet/__init__.py +16 -0
- package/agent-wallet/agent_wallet/approval.py +165 -0
- package/agent-wallet/agent_wallet/autonomous_permissions.py +281 -0
- package/agent-wallet/agent_wallet/autonomous_policy.py +305 -0
- package/agent-wallet/agent_wallet/autonomous_session.py +236 -0
- package/agent-wallet/agent_wallet/boot_key_migration.py +155 -0
- package/agent-wallet/agent_wallet/boot_key_recovery.py +57 -0
- package/agent-wallet/agent_wallet/bootstrap.py +194 -0
- package/agent-wallet/agent_wallet/card_catalog.py +243 -0
- package/agent-wallet/agent_wallet/card_runtime_secrets.py +108 -0
- package/agent-wallet/agent_wallet/card_vault.py +331 -0
- package/agent-wallet/agent_wallet/card_vault_cli.py +174 -0
- package/agent-wallet/agent_wallet/card_vault_import.py +65 -0
- package/agent-wallet/agent_wallet/card_vault_keychain.py +239 -0
- package/agent-wallet/agent_wallet/config.py +719 -0
- package/agent-wallet/agent_wallet/connector_cli.py +198 -0
- package/agent-wallet/agent_wallet/connectors/__init__.py +28 -0
- package/agent-wallet/agent_wallet/connectors/catalog.py +68 -0
- package/agent-wallet/agent_wallet/connectors/client.py +308 -0
- package/agent-wallet/agent_wallet/connectors/intent_policy.py +243 -0
- package/agent-wallet/agent_wallet/connectors/manifest.py +192 -0
- package/agent-wallet/agent_wallet/connectors/registry.py +341 -0
- package/agent-wallet/agent_wallet/encrypted_storage.py +314 -0
- package/agent-wallet/agent_wallet/evm_user_wallets.py +910 -0
- package/agent-wallet/agent_wallet/exceptions.py +10 -0
- package/agent-wallet/agent_wallet/file_ops.py +34 -0
- package/agent-wallet/agent_wallet/http_client.py +25 -0
- package/agent-wallet/agent_wallet/kernel_browser.py +479 -0
- package/agent-wallet/agent_wallet/kernel_cli.py +57 -0
- package/agent-wallet/agent_wallet/kernel_gateway.py +95 -0
- package/agent-wallet/agent_wallet/keystore.py +503 -0
- package/agent-wallet/agent_wallet/laso_card_vault.py +246 -0
- package/agent-wallet/agent_wallet/models.py +66 -0
- package/agent-wallet/agent_wallet/morpho_monitor.py +298 -0
- package/agent-wallet/agent_wallet/nonce_registry.py +59 -0
- package/agent-wallet/agent_wallet/openclaw_adapter.py +9129 -0
- package/agent-wallet/agent_wallet/openclaw_cli.py +658 -0
- package/agent-wallet/agent_wallet/openclaw_runtime.py +194 -0
- package/agent-wallet/agent_wallet/plugin_bundle.py +44 -0
- package/agent-wallet/agent_wallet/providers/__init__.py +1 -0
- package/agent-wallet/agent_wallet/providers/bags.py +202 -0
- package/agent-wallet/agent_wallet/providers/dexscreener.py +147 -0
- package/agent-wallet/agent_wallet/providers/evm_activity.py +257 -0
- package/agent-wallet/agent_wallet/providers/evm_portfolio.py +459 -0
- package/agent-wallet/agent_wallet/providers/imd.py +720 -0
- package/agent-wallet/agent_wallet/providers/jupiter.py +519 -0
- package/agent-wallet/agent_wallet/providers/kamino.py +404 -0
- package/agent-wallet/agent_wallet/providers/lifi.py +302 -0
- package/agent-wallet/agent_wallet/providers/solana_rpc.py +447 -0
- package/agent-wallet/agent_wallet/providers/tokenized_stocks.py +189 -0
- package/agent-wallet/agent_wallet/providers/wdk_evm_local.py +346 -0
- package/agent-wallet/agent_wallet/providers/x402.py +1972 -0
- package/agent-wallet/agent_wallet/sealed_keys.py +118 -0
- package/agent-wallet/agent_wallet/solana_stake.py +103 -0
- package/agent-wallet/agent_wallet/solana_tx.py +93 -0
- package/agent-wallet/agent_wallet/spending_limits.py +119 -0
- package/agent-wallet/agent_wallet/telemetry.py +481 -0
- package/agent-wallet/agent_wallet/transaction_policy.py +573 -0
- package/agent-wallet/agent_wallet/update_check.py +190 -0
- package/agent-wallet/agent_wallet/user_wallets.py +573 -0
- package/agent-wallet/agent_wallet/validation.py +31 -0
- package/agent-wallet/agent_wallet/vault_executor.py +456 -0
- package/agent-wallet/agent_wallet/wallet_layer/__init__.py +1 -0
- package/agent-wallet/agent_wallet/wallet_layer/base.py +1204 -0
- package/agent-wallet/agent_wallet/wallet_layer/base58.py +44 -0
- package/agent-wallet/agent_wallet/wallet_layer/factory.py +117 -0
- package/agent-wallet/agent_wallet/wallet_layer/solana.py +5246 -0
- package/agent-wallet/agent_wallet/wallet_layer/wdk_evm.py +2705 -0
- package/agent-wallet/examples/bootstrap_wallet.py +21 -0
- package/agent-wallet/examples/openclaw_runtime_onboarding.py +28 -0
- package/agent-wallet/examples/openclaw_user_wallet_example.py +31 -0
- package/agent-wallet/examples/openclaw_wallet_adapter_example.py +33 -0
- package/agent-wallet/openclaw.plugin.json +118 -0
- package/agent-wallet/pyproject.toml +36 -0
- package/agent-wallet/scripts/bootstrap_openclaw_evm.py +312 -0
- package/agent-wallet/scripts/build_release_bundle.py +210 -0
- package/agent-wallet/scripts/finalize_openclaw_local_wallet_config.py +121 -0
- package/agent-wallet/scripts/install_agent_wallet.py +1357 -0
- package/agent-wallet/scripts/install_openclaw_local_config.py +421 -0
- package/agent-wallet/scripts/install_openclaw_sealed_keys.py +117 -0
- package/agent-wallet/scripts/manage_openclaw_evm_wallet.py +333 -0
- package/agent-wallet/scripts/security_utils.py +37 -0
- package/agent-wallet/scripts/setup_evm_wallet.sh +140 -0
- package/agent-wallet/scripts/switch_openclaw_wallet_network.py +109 -0
- package/agent-wallet/skills/wallet-operator/SKILL.md +157 -0
- package/bin/lib/boot-key.mjs +184 -0
- package/bin/lib/evm-daemon.mjs +389 -0
- package/bin/lib/host-detection.mjs +259 -0
- package/bin/lib/integrations.mjs +578 -0
- package/bin/lib/release-python.mjs +9 -0
- package/bin/lib/update-transaction.mjs +235 -0
- package/bin/openclaw-agent-wallet.mjs +3354 -0
- package/claude-code/plugins/agent-wallet/.claude-plugin/plugin.json +19 -0
- package/claude-code/plugins/agent-wallet/.mcp.json +14 -0
- package/claude-code/plugins/agent-wallet/AGENTLAYER_AGENT_GUIDE.md +282 -0
- package/claude-code/plugins/agent-wallet/README.md +134 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-approve.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/agentlayer-autonomous-revoke.md +17 -0
- package/claude-code/plugins/agent-wallet/commands/cards.md +129 -0
- package/claude-code/plugins/agent-wallet/commands/guide.md +40 -0
- package/claude-code/plugins/agent-wallet/commands/imd.md +125 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-base.md +60 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-ethereum.md +37 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-evm.md +39 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-setup.md +25 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-sol.md +51 -0
- package/claude-code/plugins/agent-wallet/commands/wallet-statusline.md +34 -0
- package/claude-code/plugins/agent-wallet/commands/x402.md +121 -0
- package/claude-code/plugins/agent-wallet/commands/yield.md +62 -0
- package/claude-code/plugins/agent-wallet/hooks/hooks.json +16 -0
- package/claude-code/plugins/agent-wallet/scripts/bootstrap_backend.sh +136 -0
- package/claude-code/plugins/agent-wallet/scripts/enable_statusline.py +52 -0
- package/claude-code/plugins/agent-wallet/scripts/resolve_wallet_addresses.py +69 -0
- package/claude-code/plugins/agent-wallet/scripts/run_mcp.sh +66 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline.py +130 -0
- package/claude-code/plugins/agent-wallet/scripts/wallet_statusline_setup.sh +40 -0
- package/claude-code/plugins/agent-wallet/skills/wallet-operator/SKILL.md +21 -0
- package/codex/plugins/agent-wallet/.codex-plugin/plugin.json +37 -0
- package/codex/plugins/agent-wallet/.mcp.json +15 -0
- package/codex/plugins/agent-wallet/README.md +158 -0
- package/codex/plugins/agent-wallet/scripts/run_mcp.sh +47 -0
- package/codex/plugins/agent-wallet/server.py +1797 -0
- package/codex/plugins/agent-wallet/skills/cards/SKILL.md +225 -0
- package/codex/plugins/agent-wallet/skills/imd/SKILL.md +145 -0
- package/codex/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/codex/plugins/agent-wallet/skills/wallet-operator/SKILL.md +85 -0
- package/codex/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/codex/plugins/agent-wallet/skills/x402/SKILL.md +159 -0
- package/codex/plugins/agent-wallet/skills/yield/SKILL.md +65 -0
- package/connectors/AGENTS.md +37 -0
- package/connectors/DEVELOPER_GUIDE.md +131 -0
- package/connectors/README.md +139 -0
- package/connectors/READ_ONLY_BETA.md +54 -0
- package/connectors/RELEASING.md +48 -0
- package/connectors/conformance/README.md +31 -0
- package/connectors/conformance/package.json +54 -0
- package/connectors/conformance/scripts/sync-license.mjs +3 -0
- package/connectors/conformance/scripts/sync-spec.mjs +16 -0
- package/connectors/conformance/src/cli.ts +43 -0
- package/connectors/conformance/src/index.ts +7 -0
- package/connectors/conformance/src/runner.ts +314 -0
- package/connectors/conformance/src/types.ts +27 -0
- package/connectors/conformance/test/fixtures/invalid-response-identity.json +14 -0
- package/connectors/conformance/test/fixtures/missing-tool-fixture.json +4 -0
- package/connectors/conformance/test/runner.test.mjs +151 -0
- package/connectors/conformance/tsconfig.json +17 -0
- package/connectors/examples/reference-crypto-data/Dockerfile +21 -0
- package/connectors/examples/reference-crypto-data/README.md +51 -0
- package/connectors/examples/reference-crypto-data/conformance.json +11 -0
- package/connectors/examples/reference-crypto-data/connector.json +95 -0
- package/connectors/examples/reference-crypto-data/package.json +21 -0
- package/connectors/examples/reference-crypto-data/railway.toml +9 -0
- package/connectors/examples/reference-crypto-data/src/connector.ts +137 -0
- package/connectors/examples/reference-crypto-data/src/server.ts +13 -0
- package/connectors/examples/reference-crypto-data/test/connector.test.mjs +97 -0
- package/connectors/examples/reference-crypto-data/tsconfig.json +17 -0
- package/connectors/package-lock.json +561 -0
- package/connectors/package.json +19 -0
- package/connectors/scripts/smoke-packed-packages.mjs +121 -0
- package/connectors/sdk-typescript/README.md +47 -0
- package/connectors/sdk-typescript/package.json +49 -0
- package/connectors/sdk-typescript/scripts/sync-license.mjs +3 -0
- package/connectors/sdk-typescript/src/connector.ts +204 -0
- package/connectors/sdk-typescript/src/errors.ts +11 -0
- package/connectors/sdk-typescript/src/http.ts +116 -0
- package/connectors/sdk-typescript/src/index.ts +26 -0
- package/connectors/sdk-typescript/src/types.ts +101 -0
- package/connectors/sdk-typescript/test/sdk.test.mjs +181 -0
- package/connectors/sdk-typescript/tsconfig.json +21 -0
- package/connectors/spec/connector-manifest.schema.json +340 -0
- package/connectors/spec/connector-protocol.md +159 -0
- package/connectors/spec/transaction-intent.schema.json +281 -0
- package/connectors/templates/read-only/.env.example +1 -0
- package/connectors/templates/read-only/Dockerfile +11 -0
- package/connectors/templates/read-only/README.md +33 -0
- package/connectors/templates/read-only/conformance.json +11 -0
- package/connectors/templates/read-only/connector.json +64 -0
- package/connectors/templates/read-only/package.json +21 -0
- package/connectors/templates/read-only/railway.toml +9 -0
- package/connectors/templates/read-only/src/connector.ts +61 -0
- package/connectors/templates/read-only/src/server.ts +14 -0
- package/connectors/templates/read-only/test/connector.test.mjs +62 -0
- package/connectors/templates/read-only/tsconfig.json +17 -0
- package/cursor/plugins/agent-wallet/.cursor-plugin/plugin.json +14 -0
- package/cursor/plugins/agent-wallet/README.md +62 -0
- package/cursor/plugins/agent-wallet/mcp.json +16 -0
- package/cursor/plugins/agent-wallet/scripts/run_mcp.sh +33 -0
- package/cursor/plugins/agent-wallet/server.py +33 -0
- package/cursor/plugins/agent-wallet/skills/cards/SKILL.md +210 -0
- package/cursor/plugins/agent-wallet/skills/wallet-base/SKILL.md +56 -0
- package/cursor/plugins/agent-wallet/skills/wallet-operator/SKILL.md +23 -0
- package/cursor/plugins/agent-wallet/skills/wallet-sol/SKILL.md +29 -0
- package/cursor/plugins/agent-wallet/skills/x402/SKILL.md +130 -0
- package/hermes/plugins/agent_wallet/README.md +54 -0
- package/hermes/plugins/agent_wallet/__init__.py +55 -0
- package/hermes/plugins/agent_wallet/plugin.yaml +9 -0
- package/hermes/plugins/agent_wallet/schemas.py +211 -0
- package/hermes/plugins/agent_wallet/tools.py +622 -0
- package/install-from-github.sh +139 -0
- package/integrations/hedera/treasury-guard/README.md +92 -0
- package/integrations/hedera/treasury-guard/examples/treasury-policy.example.json +9 -0
- package/integrations/hedera/treasury-guard/package-lock.json +880 -0
- package/integrations/hedera/treasury-guard/package.json +8 -0
- package/integrations/hedera/treasury-guard/skills/onecraft-hedera-treasury/SKILL.md +22 -0
- package/integrations/hedera/treasury-guard/src/hedera-mirror-client.mjs +106 -0
- package/integrations/hedera/treasury-guard/src/hedera-signer.mjs +120 -0
- package/integrations/hedera/treasury-guard/src/treasury-cli.mjs +177 -0
- package/integrations/hedera/treasury-guard/src/treasury-policy.mjs +155 -0
- package/mcp/README.md +39 -0
- package/mcp/scripts/run_mcp.sh +37 -0
- package/mcp/server.py +33 -0
- package/package.json +106 -4
- package/scripts/check_release_version.mjs +72 -0
- package/scripts/version_targets.mjs +60 -0
- package/setup.sh +87 -0
- package/wdk-evm-wallet/.env.example +38 -0
- package/wdk-evm-wallet/README.md +254 -0
- package/wdk-evm-wallet/bootstrap.sh +8 -0
- package/wdk-evm-wallet/package-lock.json +2359 -0
- package/wdk-evm-wallet/package.json +43 -0
- package/wdk-evm-wallet/run-local.sh +12 -0
- package/wdk-evm-wallet/src/config.js +461 -0
- package/wdk-evm-wallet/src/imd.js +231 -0
- package/wdk-evm-wallet/src/json.js +35 -0
- package/wdk-evm-wallet/src/local_vault.js +407 -0
- package/wdk-evm-wallet/src/network_state.js +96 -0
- package/wdk-evm-wallet/src/pending_transactions.js +148 -0
- package/wdk-evm-wallet/src/server.js +849 -0
- package/wdk-evm-wallet/src/shutdown.js +63 -0
- package/wdk-evm-wallet/src/wdk_evm_wallet.js +10036 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
"""EVM portfolio helpers for token discovery and USD enrichment."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import time
|
|
6
|
+
from decimal import Decimal, InvalidOperation
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from agent_wallet.config import settings
|
|
10
|
+
from agent_wallet.exceptions import ProviderError
|
|
11
|
+
from agent_wallet.http_client import get_client
|
|
12
|
+
|
|
13
|
+
COINGECKO_API_URL = "https://api.coingecko.com/api/v3"
|
|
14
|
+
PORTFOLIO_TOKEN_CACHE_TTL_SECONDS = 30.0
|
|
15
|
+
PORTFOLIO_PRICE_CACHE_TTL_SECONDS = 60.0
|
|
16
|
+
ARC_USDC_ERC20_ADDRESS = "0x3600000000000000000000000000000000000000"
|
|
17
|
+
|
|
18
|
+
TOKEN_METADATA: dict[str, dict[str, dict[str, Any]]] = {
|
|
19
|
+
"ethereum": {
|
|
20
|
+
"0xdac17f958d2ee523a2206206994597c13d831ec7": {
|
|
21
|
+
"symbol": "USDT",
|
|
22
|
+
"name": "Tether USD",
|
|
23
|
+
"decimals": 6,
|
|
24
|
+
},
|
|
25
|
+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": {
|
|
26
|
+
"symbol": "USDC",
|
|
27
|
+
"name": "USD Coin",
|
|
28
|
+
"decimals": 6,
|
|
29
|
+
},
|
|
30
|
+
"0x6b175474e89094c44da98b954eedeac495271d0f": {
|
|
31
|
+
"symbol": "DAI",
|
|
32
|
+
"name": "Dai",
|
|
33
|
+
"decimals": 18,
|
|
34
|
+
},
|
|
35
|
+
"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599": {
|
|
36
|
+
"symbol": "WBTC",
|
|
37
|
+
"name": "Wrapped BTC",
|
|
38
|
+
"decimals": 8,
|
|
39
|
+
},
|
|
40
|
+
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": {
|
|
41
|
+
"symbol": "WETH",
|
|
42
|
+
"name": "Wrapped Ether",
|
|
43
|
+
"decimals": 18,
|
|
44
|
+
},
|
|
45
|
+
"0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0": {
|
|
46
|
+
"symbol": "wstETH",
|
|
47
|
+
"name": "Wrapped stETH",
|
|
48
|
+
"decimals": 18,
|
|
49
|
+
},
|
|
50
|
+
"0xae78736cd615f374d3085123a210448e74fc6393": {
|
|
51
|
+
"symbol": "rETH",
|
|
52
|
+
"name": "Rocket Pool ETH",
|
|
53
|
+
"decimals": 18,
|
|
54
|
+
},
|
|
55
|
+
"0x514910771af9ca656af840dff83e8264ecf986ca": {
|
|
56
|
+
"symbol": "LINK",
|
|
57
|
+
"name": "Chainlink",
|
|
58
|
+
"decimals": 18,
|
|
59
|
+
},
|
|
60
|
+
"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984": {
|
|
61
|
+
"symbol": "UNI",
|
|
62
|
+
"name": "Uniswap",
|
|
63
|
+
"decimals": 18,
|
|
64
|
+
},
|
|
65
|
+
"0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9": {
|
|
66
|
+
"symbol": "AAVE",
|
|
67
|
+
"name": "Aave",
|
|
68
|
+
"decimals": 18,
|
|
69
|
+
},
|
|
70
|
+
"0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2": {
|
|
71
|
+
"symbol": "MKR",
|
|
72
|
+
"name": "Maker",
|
|
73
|
+
"decimals": 18,
|
|
74
|
+
},
|
|
75
|
+
"0x5a98fcbea516cf06857215779fd812ca3bef1b32": {
|
|
76
|
+
"symbol": "LDO",
|
|
77
|
+
"name": "Lido DAO",
|
|
78
|
+
"decimals": 18,
|
|
79
|
+
},
|
|
80
|
+
"0xd533a949740bb3306d119cc777fa900ba034cd52": {
|
|
81
|
+
"symbol": "CRV",
|
|
82
|
+
"name": "Curve DAO Token",
|
|
83
|
+
"decimals": 18,
|
|
84
|
+
},
|
|
85
|
+
"0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce": {
|
|
86
|
+
"symbol": "SHIB",
|
|
87
|
+
"name": "Shiba Inu",
|
|
88
|
+
"decimals": 18,
|
|
89
|
+
},
|
|
90
|
+
"0x6982508145454ce325ddbe47a25d4ec3d2311933": {
|
|
91
|
+
"symbol": "PEPE",
|
|
92
|
+
"name": "Pepe",
|
|
93
|
+
"decimals": 18,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
"base": {
|
|
97
|
+
"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913": {
|
|
98
|
+
"symbol": "USDC",
|
|
99
|
+
"name": "USD Coin",
|
|
100
|
+
"decimals": 6,
|
|
101
|
+
},
|
|
102
|
+
"0x50c5725949a6f0c72e6c4a641f24049a917db0cb": {
|
|
103
|
+
"symbol": "DAI",
|
|
104
|
+
"name": "Dai",
|
|
105
|
+
"decimals": 18,
|
|
106
|
+
},
|
|
107
|
+
"0x4200000000000000000000000000000000000006": {
|
|
108
|
+
"symbol": "WETH",
|
|
109
|
+
"name": "Wrapped Ether",
|
|
110
|
+
"decimals": 18,
|
|
111
|
+
},
|
|
112
|
+
"0x2ae3f1ec7f1f5012cfeab0185bfc7aa3cf0dec22": {
|
|
113
|
+
"symbol": "cbETH",
|
|
114
|
+
"name": "Coinbase Wrapped Staked ETH",
|
|
115
|
+
"decimals": 18,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
"arc": {
|
|
119
|
+
ARC_USDC_ERC20_ADDRESS: {
|
|
120
|
+
"symbol": "USDC",
|
|
121
|
+
"name": "USD Coin",
|
|
122
|
+
"decimals": 6,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
COINGECKO_IDS = {
|
|
128
|
+
"ETH": "ethereum",
|
|
129
|
+
"WETH": "ethereum",
|
|
130
|
+
"USDC": "usd-coin",
|
|
131
|
+
"USDT": "tether",
|
|
132
|
+
"DAI": "dai",
|
|
133
|
+
"WBTC": "wrapped-bitcoin",
|
|
134
|
+
"LINK": "chainlink",
|
|
135
|
+
"UNI": "uniswap",
|
|
136
|
+
"AAVE": "aave",
|
|
137
|
+
"MKR": "maker",
|
|
138
|
+
"LDO": "lido-dao",
|
|
139
|
+
"CRV": "curve-dao-token",
|
|
140
|
+
"SHIB": "shiba-inu",
|
|
141
|
+
"PEPE": "pepe",
|
|
142
|
+
"RETH": "rocket-pool-eth",
|
|
143
|
+
"CBETH": "coinbase-wrapped-staked-eth",
|
|
144
|
+
"WSTETH": "wrapped-steth",
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_TOKEN_CACHE: dict[str, tuple[float, list[dict[str, Any]]]] = {}
|
|
148
|
+
_PRICE_CACHE: dict[str, tuple[float, float]] = {}
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _normalize_network(network: str) -> str:
|
|
152
|
+
normalized = str(network or "").strip().lower()
|
|
153
|
+
if normalized not in {"ethereum", "base", "robinhood", "arc"}:
|
|
154
|
+
raise ProviderError("evm-portfolio", f"Unsupported EVM portfolio network: {network}")
|
|
155
|
+
return normalized
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _cache_get_token_balances(cache_key: str) -> list[dict[str, Any]] | None:
|
|
159
|
+
cached = _TOKEN_CACHE.get(cache_key)
|
|
160
|
+
if not cached:
|
|
161
|
+
return None
|
|
162
|
+
expires_at, payload = cached
|
|
163
|
+
if expires_at <= time.time():
|
|
164
|
+
_TOKEN_CACHE.pop(cache_key, None)
|
|
165
|
+
return None
|
|
166
|
+
return payload
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _cache_set_token_balances(cache_key: str, payload: list[dict[str, Any]]) -> None:
|
|
170
|
+
_TOKEN_CACHE[cache_key] = (time.time() + PORTFOLIO_TOKEN_CACHE_TTL_SECONDS, payload)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _cache_get_price(symbol: str) -> float | None:
|
|
174
|
+
cached = _PRICE_CACHE.get(symbol.upper())
|
|
175
|
+
if not cached:
|
|
176
|
+
return None
|
|
177
|
+
expires_at, price = cached
|
|
178
|
+
if expires_at <= time.time():
|
|
179
|
+
_PRICE_CACHE.pop(symbol.upper(), None)
|
|
180
|
+
return None
|
|
181
|
+
return price
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _cache_set_price(symbol: str, price: float) -> None:
|
|
185
|
+
_PRICE_CACHE[symbol.upper()] = (time.time() + PORTFOLIO_PRICE_CACHE_TTL_SECONDS, price)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _format_decimal(value: Decimal | None, *, places: int | None = None) -> str | None:
|
|
189
|
+
if value is None:
|
|
190
|
+
return None
|
|
191
|
+
normalized = value
|
|
192
|
+
if places is not None:
|
|
193
|
+
quant = Decimal("1").scaleb(-places)
|
|
194
|
+
normalized = value.quantize(quant)
|
|
195
|
+
text = format(normalized.normalize(), "f")
|
|
196
|
+
if "." in text:
|
|
197
|
+
text = text.rstrip("0").rstrip(".")
|
|
198
|
+
return text or "0"
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _to_decimal(value: Any) -> Decimal | None:
|
|
202
|
+
if value is None:
|
|
203
|
+
return None
|
|
204
|
+
try:
|
|
205
|
+
return Decimal(str(value))
|
|
206
|
+
except (InvalidOperation, ValueError):
|
|
207
|
+
return None
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
async def _gateway_rpc_call(network: str, method: str, params: list[Any]) -> dict[str, Any]:
|
|
211
|
+
client = get_client()
|
|
212
|
+
payload = {"jsonrpc": "2.0", "id": 1, "method": method, "params": params}
|
|
213
|
+
gateway_url = str(settings.provider_gateway_url or "").strip()
|
|
214
|
+
bearer = str(settings.provider_gateway_bearer_token or "").strip()
|
|
215
|
+
if not gateway_url:
|
|
216
|
+
raise ProviderError(
|
|
217
|
+
"evm-portfolio",
|
|
218
|
+
"Provider gateway URL is required for EVM portfolio lookup on ethereum/base/robinhood/arc.",
|
|
219
|
+
)
|
|
220
|
+
try:
|
|
221
|
+
response = await client.post(
|
|
222
|
+
f"{gateway_url.rstrip('/')}/v1/evm/rpc/{network}?provider=alchemy",
|
|
223
|
+
json=payload,
|
|
224
|
+
headers={"Authorization": f"Bearer {bearer}"} if bearer else None,
|
|
225
|
+
)
|
|
226
|
+
except Exception as exc: # pragma: no cover - network path
|
|
227
|
+
raise ProviderError("evm-portfolio", f"Provider gateway request failed: {exc}") from exc
|
|
228
|
+
if response.status_code != 200:
|
|
229
|
+
raise ProviderError(
|
|
230
|
+
"evm-portfolio",
|
|
231
|
+
f"Provider gateway returned HTTP {response.status_code} for {method}.",
|
|
232
|
+
)
|
|
233
|
+
data = response.json()
|
|
234
|
+
if "error" in data:
|
|
235
|
+
raise ProviderError("evm-portfolio", f"Provider gateway RPC error: {data['error']}")
|
|
236
|
+
return data
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
async def fetch_token_balances(address: str, network: str) -> list[dict[str, Any]]:
|
|
240
|
+
normalized_network = _normalize_network(network)
|
|
241
|
+
cache_key = f"{normalized_network}:{address.lower()}"
|
|
242
|
+
cached = _cache_get_token_balances(cache_key)
|
|
243
|
+
if cached is not None:
|
|
244
|
+
return cached
|
|
245
|
+
|
|
246
|
+
data = await _gateway_rpc_call(
|
|
247
|
+
normalized_network,
|
|
248
|
+
"alchemy_getTokenBalances",
|
|
249
|
+
[address, "erc20"],
|
|
250
|
+
)
|
|
251
|
+
balances = []
|
|
252
|
+
metadata_by_address = TOKEN_METADATA.get(normalized_network, {})
|
|
253
|
+
token_balances = list((data.get("result") or {}).get("tokenBalances") or [])
|
|
254
|
+
for item in token_balances:
|
|
255
|
+
contract = str(item.get("contractAddress") or "").strip()
|
|
256
|
+
raw_hex = str(item.get("tokenBalance") or "").strip().lower()
|
|
257
|
+
if not contract or raw_hex in {"", "0x", "0x0"}:
|
|
258
|
+
continue
|
|
259
|
+
# Arc exposes the native USDC balance through an ERC-20 compatibility
|
|
260
|
+
# interface as well. The two representations are the same funds, so the
|
|
261
|
+
# ERC-20 mirror must not be counted as a second portfolio asset.
|
|
262
|
+
if normalized_network == "arc" and contract.lower() == ARC_USDC_ERC20_ADDRESS:
|
|
263
|
+
continue
|
|
264
|
+
try:
|
|
265
|
+
balance_raw = int(raw_hex, 16)
|
|
266
|
+
except ValueError:
|
|
267
|
+
continue
|
|
268
|
+
if balance_raw <= 0:
|
|
269
|
+
continue
|
|
270
|
+
known = metadata_by_address.get(contract.lower()) or {}
|
|
271
|
+
decimals = known.get("decimals")
|
|
272
|
+
balance_ui = None
|
|
273
|
+
if isinstance(decimals, int) and decimals >= 0:
|
|
274
|
+
balance_ui = Decimal(balance_raw) / (Decimal(10) ** decimals)
|
|
275
|
+
balances.append(
|
|
276
|
+
{
|
|
277
|
+
"contract_address": contract,
|
|
278
|
+
"symbol": known.get("symbol"),
|
|
279
|
+
"name": known.get("name"),
|
|
280
|
+
"decimals": decimals,
|
|
281
|
+
"balance_raw": str(balance_raw),
|
|
282
|
+
"balance_ui": _format_decimal(balance_ui) if balance_ui is not None else None,
|
|
283
|
+
"verified": bool(known),
|
|
284
|
+
"source": "alchemy_getTokenBalances",
|
|
285
|
+
}
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
balances.sort(
|
|
289
|
+
key=lambda item: (
|
|
290
|
+
item.get("balance_ui") is None,
|
|
291
|
+
-(Decimal(item["balance_ui"]) if item.get("balance_ui") is not None else Decimal(0)),
|
|
292
|
+
)
|
|
293
|
+
)
|
|
294
|
+
_cache_set_token_balances(cache_key, balances)
|
|
295
|
+
return balances
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
async def fetch_usd_prices(symbols: list[str]) -> dict[str, float]:
|
|
299
|
+
normalized_symbols = []
|
|
300
|
+
seen: set[str] = set()
|
|
301
|
+
prices: dict[str, float] = {}
|
|
302
|
+
for symbol in symbols:
|
|
303
|
+
normalized = str(symbol or "").strip().upper()
|
|
304
|
+
if not normalized or normalized in seen:
|
|
305
|
+
continue
|
|
306
|
+
seen.add(normalized)
|
|
307
|
+
cached = _cache_get_price(normalized)
|
|
308
|
+
if cached is not None:
|
|
309
|
+
prices[normalized] = cached
|
|
310
|
+
continue
|
|
311
|
+
if normalized in COINGECKO_IDS:
|
|
312
|
+
normalized_symbols.append(normalized)
|
|
313
|
+
|
|
314
|
+
if not normalized_symbols:
|
|
315
|
+
return prices
|
|
316
|
+
|
|
317
|
+
ids = [COINGECKO_IDS[symbol] for symbol in normalized_symbols]
|
|
318
|
+
client = get_client()
|
|
319
|
+
try:
|
|
320
|
+
response = await client.get(
|
|
321
|
+
f"{COINGECKO_API_URL}/simple/price",
|
|
322
|
+
params={
|
|
323
|
+
"ids": ",".join(ids),
|
|
324
|
+
"vs_currencies": "usd",
|
|
325
|
+
},
|
|
326
|
+
)
|
|
327
|
+
except Exception as exc: # pragma: no cover - network path
|
|
328
|
+
raise ProviderError("evm-portfolio", f"CoinGecko request failed: {exc}") from exc
|
|
329
|
+
|
|
330
|
+
if response.status_code != 200:
|
|
331
|
+
raise ProviderError("evm-portfolio", f"CoinGecko returned HTTP {response.status_code}.")
|
|
332
|
+
|
|
333
|
+
payload = response.json()
|
|
334
|
+
for symbol in normalized_symbols:
|
|
335
|
+
cg_id = COINGECKO_IDS[symbol]
|
|
336
|
+
usd_value = payload.get(cg_id, {}).get("usd")
|
|
337
|
+
if usd_value is None:
|
|
338
|
+
continue
|
|
339
|
+
price = float(usd_value)
|
|
340
|
+
prices[symbol] = price
|
|
341
|
+
_cache_set_price(symbol, price)
|
|
342
|
+
return prices
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
async def build_portfolio_snapshot(
|
|
346
|
+
*,
|
|
347
|
+
address: str,
|
|
348
|
+
network: str,
|
|
349
|
+
native_symbol: str,
|
|
350
|
+
native_balance_wei: str,
|
|
351
|
+
native_balance: str,
|
|
352
|
+
) -> dict[str, Any]:
|
|
353
|
+
normalized_network = _normalize_network(network)
|
|
354
|
+
tokens = await fetch_token_balances(address, normalized_network)
|
|
355
|
+
symbols = [native_symbol] + [
|
|
356
|
+
str(token.get("symbol") or "").strip()
|
|
357
|
+
for token in tokens
|
|
358
|
+
if str(token.get("symbol") or "").strip()
|
|
359
|
+
]
|
|
360
|
+
prices = await fetch_usd_prices(symbols)
|
|
361
|
+
|
|
362
|
+
native_balance_decimal = _to_decimal(native_balance) or Decimal(0)
|
|
363
|
+
native_price_usd = prices.get(str(native_symbol or "").upper())
|
|
364
|
+
native_value_usd = (
|
|
365
|
+
native_balance_decimal * Decimal(str(native_price_usd))
|
|
366
|
+
if native_price_usd is not None and native_balance_decimal > 0
|
|
367
|
+
else None
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
portfolio_tokens: list[dict[str, Any]] = []
|
|
371
|
+
total_value = native_value_usd or Decimal(0)
|
|
372
|
+
|
|
373
|
+
for token in tokens:
|
|
374
|
+
symbol = str(token.get("symbol") or "").strip().upper()
|
|
375
|
+
balance_ui_decimal = _to_decimal(token.get("balance_ui"))
|
|
376
|
+
price_usd = prices.get(symbol) if symbol else None
|
|
377
|
+
value_usd = (
|
|
378
|
+
balance_ui_decimal * Decimal(str(price_usd))
|
|
379
|
+
if price_usd is not None and balance_ui_decimal is not None and balance_ui_decimal > 0
|
|
380
|
+
else None
|
|
381
|
+
)
|
|
382
|
+
if value_usd is not None:
|
|
383
|
+
total_value += value_usd
|
|
384
|
+
portfolio_tokens.append(
|
|
385
|
+
{
|
|
386
|
+
"token_address": token["contract_address"],
|
|
387
|
+
"balance_raw": token["balance_raw"],
|
|
388
|
+
"balance_ui": token.get("balance_ui"),
|
|
389
|
+
"token_metadata": {
|
|
390
|
+
"address": token["contract_address"],
|
|
391
|
+
"name": token.get("name"),
|
|
392
|
+
"symbol": token.get("symbol"),
|
|
393
|
+
"decimals": token.get("decimals"),
|
|
394
|
+
"verified": bool(token.get("verified")),
|
|
395
|
+
"source": token.get("source") or "alchemy_getTokenBalances",
|
|
396
|
+
},
|
|
397
|
+
"price_usd": _format_decimal(Decimal(str(price_usd)), places=6) if price_usd is not None else None,
|
|
398
|
+
"value_usd": _format_decimal(value_usd, places=2),
|
|
399
|
+
}
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
portfolio_tokens.sort(
|
|
403
|
+
key=lambda item: _to_decimal(item.get("value_usd")) or Decimal("-1"),
|
|
404
|
+
reverse=True,
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
native_asset = {
|
|
408
|
+
"asset_type": "native",
|
|
409
|
+
"symbol": native_symbol,
|
|
410
|
+
"amount_raw": str(native_balance_wei),
|
|
411
|
+
"amount_ui": str(native_balance),
|
|
412
|
+
"price_usd": _format_decimal(Decimal(str(native_price_usd)), places=6)
|
|
413
|
+
if native_price_usd is not None
|
|
414
|
+
else None,
|
|
415
|
+
"value_usd": _format_decimal(native_value_usd, places=2),
|
|
416
|
+
"pricing_source": "coingecko" if native_price_usd is not None else None,
|
|
417
|
+
}
|
|
418
|
+
assets = [native_asset]
|
|
419
|
+
assets.extend(
|
|
420
|
+
{
|
|
421
|
+
"asset_type": "erc20",
|
|
422
|
+
"token_address": token.get("token_address"),
|
|
423
|
+
"symbol": (token.get("token_metadata") or {}).get("symbol"),
|
|
424
|
+
"amount_raw": token.get("balance_raw"),
|
|
425
|
+
"amount_ui": token.get("balance_ui"),
|
|
426
|
+
"decimals": (token.get("token_metadata") or {}).get("decimals"),
|
|
427
|
+
"price_usd": token.get("price_usd"),
|
|
428
|
+
"value_usd": token.get("value_usd"),
|
|
429
|
+
"pricing_source": "coingecko" if token.get("price_usd") is not None else None,
|
|
430
|
+
}
|
|
431
|
+
for token in portfolio_tokens
|
|
432
|
+
)
|
|
433
|
+
assets.sort(
|
|
434
|
+
key=lambda item: _to_decimal(item.get("value_usd")) or Decimal("-1"),
|
|
435
|
+
reverse=True,
|
|
436
|
+
)
|
|
437
|
+
priced_asset_count = sum(1 for asset in assets if asset.get("value_usd") is not None)
|
|
438
|
+
formatted_total_value = _format_decimal(total_value, places=2) if total_value > 0 else None
|
|
439
|
+
|
|
440
|
+
return {
|
|
441
|
+
"address": address,
|
|
442
|
+
"network": normalized_network,
|
|
443
|
+
"asset": native_symbol,
|
|
444
|
+
"balance_wei": str(native_balance_wei),
|
|
445
|
+
"balance_native": str(native_balance),
|
|
446
|
+
"native_price_usd": _format_decimal(Decimal(str(native_price_usd)), places=6)
|
|
447
|
+
if native_price_usd is not None
|
|
448
|
+
else None,
|
|
449
|
+
"native_value_usd": _format_decimal(native_value_usd, places=2),
|
|
450
|
+
"tokens": portfolio_tokens,
|
|
451
|
+
"token_count": len(portfolio_tokens),
|
|
452
|
+
"assets": assets,
|
|
453
|
+
"asset_count": len(assets),
|
|
454
|
+
"priced_asset_count": priced_asset_count,
|
|
455
|
+
"balance_usd": formatted_total_value,
|
|
456
|
+
"total_value_usd": formatted_total_value,
|
|
457
|
+
"pricing_source": "coingecko",
|
|
458
|
+
"token_discovery_source": "alchemy_getTokenBalances",
|
|
459
|
+
}
|