@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,257 @@
|
|
|
1
|
+
"""On-demand EVM transfer activity; the provider gateway caches and budgets Alchemy reads."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import tempfile
|
|
10
|
+
import time
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any, Awaitable, Callable
|
|
13
|
+
|
|
14
|
+
from agent_wallet.config import settings, resolve_openclaw_home
|
|
15
|
+
from agent_wallet.exceptions import ProviderError
|
|
16
|
+
from agent_wallet.http_client import get_client
|
|
17
|
+
|
|
18
|
+
_DIRECTIONS = {"both": ("outgoing", "incoming"), "outgoing": ("outgoing",), "incoming": ("incoming",)}
|
|
19
|
+
# Already present on the grouped transaction; repeating them per transfer only adds tokens.
|
|
20
|
+
_TX_LEVEL_FIELDS = {"hash", "block_number", "timestamp"}
|
|
21
|
+
# A placeholder is resolved with one cheap receipt read and the verdict is stored,
|
|
22
|
+
# so each wallet transaction costs at most a few receipt reads over its lifetime.
|
|
23
|
+
_RECEIPT_MIN_AGE = 30.0
|
|
24
|
+
_RECEIPT_RECHECK = 30.0
|
|
25
|
+
_RECEIPT_MAX_PER_CALL = 5
|
|
26
|
+
_DROPPED_AFTER = 1800.0
|
|
27
|
+
_FINAL_STATUSES = {"confirmed", "failed", "dropped"}
|
|
28
|
+
|
|
29
|
+
ReceiptLookup = Callable[[str], Awaitable[dict[str, Any]]]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _pending_path(network: str, address: str) -> Path:
|
|
33
|
+
return resolve_openclaw_home() / "cache" / "evm-activity" / f"{network}-{address.lower()}.json"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _read_pending(network: str, address: str) -> list[dict[str, Any]]:
|
|
37
|
+
path = _pending_path(network, address)
|
|
38
|
+
try:
|
|
39
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
40
|
+
except (OSError, ValueError):
|
|
41
|
+
return []
|
|
42
|
+
if not isinstance(data, list):
|
|
43
|
+
return []
|
|
44
|
+
cutoff = time.time() - 86400
|
|
45
|
+
return [item for item in data if isinstance(item, dict) and item.get("recorded_at", 0) >= cutoff][:100]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _write_pending(network: str, address: str, entries: list[dict[str, Any]]) -> None:
|
|
49
|
+
path = _pending_path(network, address)
|
|
50
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
51
|
+
fd, temp_name = tempfile.mkstemp(prefix=".activity-", dir=path.parent)
|
|
52
|
+
temp_path = Path(temp_name)
|
|
53
|
+
try:
|
|
54
|
+
with os.fdopen(fd, "w", encoding="utf-8") as stream:
|
|
55
|
+
json.dump(entries[:100], stream, separators=(",", ":"))
|
|
56
|
+
os.replace(temp_path, path)
|
|
57
|
+
finally:
|
|
58
|
+
temp_path.unlink(missing_ok=True)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def record_submitted_transaction(network: str, address: str, tx_hash: str) -> None:
|
|
62
|
+
"""Keep a local placeholder until Alchemy indexes a wallet-submitted tx."""
|
|
63
|
+
if network not in {"ethereum", "base", "robinhood"}:
|
|
64
|
+
return
|
|
65
|
+
if not re.fullmatch(r"0x[a-fA-F0-9]{40}", address) or not re.fullmatch(r"0x[a-fA-F0-9]{64}", tx_hash):
|
|
66
|
+
return
|
|
67
|
+
entries = [item for item in _read_pending(network, address) if item.get("hash", "").lower() != tx_hash.lower()]
|
|
68
|
+
entries.insert(0, {"hash": tx_hash, "recorded_at": time.time()})
|
|
69
|
+
_write_pending(network, address, entries)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def _resolve_pending(
|
|
73
|
+
network: str, address: str, entries: list[dict[str, Any]], receipt_lookup: ReceiptLookup
|
|
74
|
+
) -> None:
|
|
75
|
+
"""Settle stale placeholders from their receipts and persist final verdicts.
|
|
76
|
+
|
|
77
|
+
Reverted and transfer-free transactions (e.g. ERC-20 approvals) never appear
|
|
78
|
+
in the transfer feed, so without this they would read as "submitted" for a day.
|
|
79
|
+
"""
|
|
80
|
+
now = time.time()
|
|
81
|
+
due = [
|
|
82
|
+
item for item in entries
|
|
83
|
+
if item.get("status") not in _FINAL_STATUSES
|
|
84
|
+
and now - item.get("recorded_at", 0) >= _RECEIPT_MIN_AGE
|
|
85
|
+
and now - item.get("checked_at", 0) >= _RECEIPT_RECHECK
|
|
86
|
+
][:_RECEIPT_MAX_PER_CALL]
|
|
87
|
+
if not due:
|
|
88
|
+
return
|
|
89
|
+
results = await asyncio.gather(*(receipt_lookup(item["hash"]) for item in due), return_exceptions=True)
|
|
90
|
+
for item, result in zip(due, results):
|
|
91
|
+
item["checked_at"] = now
|
|
92
|
+
if not isinstance(result, dict):
|
|
93
|
+
continue
|
|
94
|
+
status = result.get("status")
|
|
95
|
+
if status == "reverted":
|
|
96
|
+
item["status"] = "failed"
|
|
97
|
+
elif status == "confirmed":
|
|
98
|
+
item["status"] = "confirmed"
|
|
99
|
+
elif status == "not_found" and now - item.get("recorded_at", 0) >= _DROPPED_AFTER:
|
|
100
|
+
item["status"] = "dropped"
|
|
101
|
+
else:
|
|
102
|
+
continue
|
|
103
|
+
block = ((result.get("receipt") or {}).get("blockNumber"))
|
|
104
|
+
if isinstance(block, str) and block.startswith("0x"):
|
|
105
|
+
item["block_number"] = int(block, 16)
|
|
106
|
+
try:
|
|
107
|
+
_write_pending(network, address, entries)
|
|
108
|
+
except OSError:
|
|
109
|
+
pass # The verdict is still returned; it is just re-checked next time.
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
async def _fetch_page(
|
|
113
|
+
network: str, address: str, direction: str, page_key: str, page_size: int, include_nfts: bool
|
|
114
|
+
) -> dict[str, Any]:
|
|
115
|
+
if page_key == "__end__":
|
|
116
|
+
return {"transfers": [], "page_key": None}
|
|
117
|
+
gateway_url = str(settings.provider_gateway_url or "").strip()
|
|
118
|
+
if not gateway_url:
|
|
119
|
+
raise ProviderError("evm-activity", "Provider gateway URL is required for EVM activity.")
|
|
120
|
+
bearer = str(settings.provider_gateway_bearer_token or "").strip()
|
|
121
|
+
params: dict[str, Any] = {
|
|
122
|
+
"address": address,
|
|
123
|
+
"direction": direction,
|
|
124
|
+
"page_size": page_size,
|
|
125
|
+
}
|
|
126
|
+
if page_key:
|
|
127
|
+
params["page_key"] = page_key
|
|
128
|
+
if include_nfts:
|
|
129
|
+
params["include_nfts"] = "1"
|
|
130
|
+
try:
|
|
131
|
+
response = await get_client().get(
|
|
132
|
+
f"{gateway_url.rstrip('/')}/v1/evm/transfers/{network}",
|
|
133
|
+
params=params,
|
|
134
|
+
headers={"Authorization": f"Bearer {bearer}"} if bearer else None,
|
|
135
|
+
)
|
|
136
|
+
except Exception as exc:
|
|
137
|
+
raise ProviderError("evm-activity", f"Gateway request failed: {exc}") from exc
|
|
138
|
+
if response.status_code == 429:
|
|
139
|
+
try:
|
|
140
|
+
message = str(response.json().get("error") or "")
|
|
141
|
+
except ValueError:
|
|
142
|
+
message = ""
|
|
143
|
+
raise ProviderError(
|
|
144
|
+
"evm-activity",
|
|
145
|
+
message or "Transfer history is temporarily rate limited.",
|
|
146
|
+
details={"retry_after": response.headers.get("retry-after")},
|
|
147
|
+
)
|
|
148
|
+
if response.status_code != 200:
|
|
149
|
+
raise ProviderError("evm-activity", f"Gateway returned HTTP {response.status_code}.")
|
|
150
|
+
data = response.json()
|
|
151
|
+
if not isinstance(data, dict) or not isinstance(data.get("transfers"), list):
|
|
152
|
+
raise ProviderError("evm-activity", "Gateway returned invalid transfer data.")
|
|
153
|
+
return data
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
async def get_activity(
|
|
157
|
+
network: str, address: str, *, page_size: int = 20, direction: str = "both",
|
|
158
|
+
include_nfts: bool = False, outgoing_page_key: str = "", incoming_page_key: str = "",
|
|
159
|
+
receipt_lookup: ReceiptLookup | None = None,
|
|
160
|
+
) -> dict[str, Any]:
|
|
161
|
+
if network not in {"ethereum", "base", "robinhood"}:
|
|
162
|
+
raise ValueError("Activity is supported on ethereum, base, and robinhood.")
|
|
163
|
+
if not re.fullmatch(r"0x[a-fA-F0-9]{40}", address):
|
|
164
|
+
raise ValueError("Wallet address must be a 20-byte EVM address.")
|
|
165
|
+
if not isinstance(page_size, int) or isinstance(page_size, bool) or not 1 <= page_size <= 100:
|
|
166
|
+
raise ValueError("page_size must be between 1 and 100.")
|
|
167
|
+
if direction not in _DIRECTIONS:
|
|
168
|
+
raise ValueError("direction must be both, outgoing, or incoming.")
|
|
169
|
+
for key in (outgoing_page_key, incoming_page_key):
|
|
170
|
+
if not isinstance(key, str) or len(key) > 2048:
|
|
171
|
+
raise ValueError("Invalid activity page key.")
|
|
172
|
+
page_keys = {"outgoing": outgoing_page_key, "incoming": incoming_page_key}
|
|
173
|
+
directions = _DIRECTIONS[direction]
|
|
174
|
+
pages = dict(zip(directions, await asyncio.gather(*(
|
|
175
|
+
_fetch_page(network, address, name, page_keys[name], page_size, bool(include_nfts))
|
|
176
|
+
for name in directions
|
|
177
|
+
))))
|
|
178
|
+
by_hash: dict[str, dict[str, Any]] = {}
|
|
179
|
+
# A direction with more pages is only complete down to its oldest fetched
|
|
180
|
+
# transfer; anything older from the other direction may still have gaps.
|
|
181
|
+
complete_since: str | None = None
|
|
182
|
+
for name, page in pages.items():
|
|
183
|
+
timestamps = []
|
|
184
|
+
for transfer in page["transfers"]:
|
|
185
|
+
if not isinstance(transfer, dict):
|
|
186
|
+
continue
|
|
187
|
+
tx_hash = str(transfer.get("hash") or "")
|
|
188
|
+
if not re.fullmatch(r"0x[a-fA-F0-9]{64}", tx_hash):
|
|
189
|
+
continue
|
|
190
|
+
if transfer.get("timestamp"):
|
|
191
|
+
timestamps.append(str(transfer["timestamp"]))
|
|
192
|
+
entry = by_hash.setdefault(tx_hash.lower(), {
|
|
193
|
+
"hash": tx_hash,
|
|
194
|
+
"network": network,
|
|
195
|
+
"block_number": transfer.get("block_number"),
|
|
196
|
+
"timestamp": transfer.get("timestamp"),
|
|
197
|
+
"transfers": [],
|
|
198
|
+
})
|
|
199
|
+
transfer_id = transfer.get("id")
|
|
200
|
+
existing = next(
|
|
201
|
+
(item for item in entry["transfers"] if transfer_id is not None and item.get("id") == transfer_id),
|
|
202
|
+
None,
|
|
203
|
+
)
|
|
204
|
+
if existing is not None:
|
|
205
|
+
# The same transfer in both directions is a send to self.
|
|
206
|
+
if existing["direction"] != name:
|
|
207
|
+
existing["direction"] = "self"
|
|
208
|
+
else:
|
|
209
|
+
entry["transfers"].append({
|
|
210
|
+
**{key: value for key, value in transfer.items() if key not in _TX_LEVEL_FIELDS},
|
|
211
|
+
"direction": name,
|
|
212
|
+
})
|
|
213
|
+
if page.get("page_key") and timestamps:
|
|
214
|
+
oldest = min(timestamps)
|
|
215
|
+
complete_since = max(complete_since or oldest, oldest)
|
|
216
|
+
transactions = sorted(
|
|
217
|
+
by_hash.values(), key=lambda item: str(item.get("timestamp") or ""), reverse=True
|
|
218
|
+
)
|
|
219
|
+
# Placeholders belong to the newest activity only, never to older pages.
|
|
220
|
+
first_page = not outgoing_page_key and not incoming_page_key
|
|
221
|
+
# Wallet-sent transactions are outgoing, so an incoming-only view never shows them.
|
|
222
|
+
pending_entries = _read_pending(network, address) if first_page and direction != "incoming" else []
|
|
223
|
+
unindexed = [item for item in pending_entries if item.get("hash", "").lower() not in by_hash]
|
|
224
|
+
if len(unindexed) < len(pending_entries):
|
|
225
|
+
# Once indexed, a placeholder is obsolete; keeping it would resurface it as
|
|
226
|
+
# pending after the real entry scrolls past the first page.
|
|
227
|
+
try:
|
|
228
|
+
_write_pending(network, address, unindexed)
|
|
229
|
+
except OSError:
|
|
230
|
+
pass
|
|
231
|
+
pending_entries = unindexed
|
|
232
|
+
if unindexed and receipt_lookup is not None:
|
|
233
|
+
await _resolve_pending(network, address, pending_entries, receipt_lookup)
|
|
234
|
+
for pending in reversed(unindexed):
|
|
235
|
+
placeholder = {
|
|
236
|
+
"hash": pending.get("hash", ""), "network": network,
|
|
237
|
+
"status": pending.get("status", "submitted"), "timestamp": None, "transfers": [],
|
|
238
|
+
}
|
|
239
|
+
if pending.get("block_number") is not None:
|
|
240
|
+
placeholder["block_number"] = pending["block_number"]
|
|
241
|
+
transactions.insert(0, placeholder)
|
|
242
|
+
result: dict[str, Any] = {
|
|
243
|
+
"network": network,
|
|
244
|
+
"address": address,
|
|
245
|
+
"direction": direction,
|
|
246
|
+
"transactions": transactions,
|
|
247
|
+
"complete_since": complete_since,
|
|
248
|
+
"source": "alchemy-transfers",
|
|
249
|
+
"coverage": (
|
|
250
|
+
"asset movements; zero-value transfers, failed transactions and transfer-free "
|
|
251
|
+
"contract calls are not indexed"
|
|
252
|
+
+ ("" if include_nfts else "; NFTs excluded unless include_nfts is true")
|
|
253
|
+
),
|
|
254
|
+
}
|
|
255
|
+
for name, page in pages.items():
|
|
256
|
+
result[f"next_{name}_page_key"] = page.get("page_key") or "__end__"
|
|
257
|
+
return result
|