Skip to content

Commit 4f098ba

Browse files
fix(roi): short-circuit offchain-deposit reader for on-chain-only agents; seed checkpoint on first run
Tightens the pre-deposit-as-loss helper so on-chain-only deployments do zero extra work and generate no log noise: - **Early return when ``mech_marketplace_config.use_offchain`` is False.** Production on-chain-only traders take this path and never touch the contract API. No RPC call, no persist, ROI number bit-identical to today. - **First-run checkpoint seeding.** When ``last_scanned_block == 0`` on an off-chain deployment, the helper now fetches the current head block via a new ``MechPrepaidReaderContract.get_current_block_number`` classmethod and stores it as the checkpoint instead of sweeping the full history. Off-chain has not shipped in production, so there are no pre-existing deposits to backfill; this avoids RPC ``eth_getLogs`` block-range caps (Alchemy 10k, Infura 100k, etc.) that would otherwise error on every cycle and leave the checkpoint pinned at zero. - **``balance_tracker_address`` added to deployment configs.** The Python default in ``mech_interact_abci/models.py`` seeded the address for both chains, but the deployment yamls did not, so an operator could not override it per-deployment. Added the real deployed addresses (Gnosis 0x7D686bD1fD3CFF6E45a40165154D61043af7D67c, Polygon 0xaaFBeef195BDAb1Bb6f3Dc9cEbA875Cd72499230) to ``trader_pearl`` and ``polymarket_trader`` service.yamls, plus zero-address placeholders in ``trader_abci/skill.yaml`` and ``trader`` aea-config.yaml. Existing behaviour under both modes stays correct: on-chain-only agents see zero on all paths, off-chain agents capture every ``Deposit`` from first run forward. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e7f540c commit 4f098ba

13 files changed

Lines changed: 122 additions & 53 deletions

File tree

packages/packages.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
"contract/valory/relayer/0.1.0": "bafybeifrvzmwkumdachm7wuesfdqvz52xsybpnvdlelltm3qowosgd6tp4",
88
"contract/valory/market_maker/0.1.0": "bafybeiaogkujiilubosloromkikuhltgiiadjvtzw2b5p5hc3klzi4m4nq",
99
"contract/valory/deposit_wallet/0.1.0": "bafybeifmw77pznzla6zwq7mfncdzboxmlmm5ffmkvlxlqfg442oypfqjje",
10-
"contract/valory/mech_prepaid_reader/0.1.0": "bafybeihy66fulkrsc4fxucrpunnr5ulwzudvnzxw5xtwjc7zqarf6bj3mi",
10+
"contract/valory/mech_prepaid_reader/0.1.0": "bafybeieeh2xhktqn2xau527sgu6zvaqsf6nmd6um5zpgcds2bsg3l2onoa",
1111
"connection/valory/polymarket_client/0.1.0": "bafybeid4eo37pka4tfhsjs6rb3knrugmokcvihust2dpfrgob56y77xd44",
1212
"skill/valory/market_manager_abci/0.1.0": "bafybeig3ak2eoimxwuupybde4napyi2jlf7pxk35se2zclikete2ni5hti",
13-
"skill/valory/decision_maker_abci/0.1.0": "bafybeidel24uozve2ndxmwshhyrzsyv7itcodzqjcfzgqqikr4khpn4eiu",
14-
"skill/valory/trader_abci/0.1.0": "bafybeiarobiv2d5l6teaslt7s4o36gni3bkwr75nub253mypms56dqzn7y",
15-
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeibyonzs6wyvbcks5bztmc7rmdooynxmlfhiaub2cmuamqbtijv3ou",
13+
"skill/valory/decision_maker_abci/0.1.0": "bafybeiffplku3zbpzhck7pen7mgbn3pi3tdqlat4j4t5746qx5km5jidyq",
14+
"skill/valory/trader_abci/0.1.0": "bafybeiaoe2ldtkyygp3w3sjnuiwyhgz6ye5a7lnu4jhwjegmcgakq3ptei",
15+
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeicgh5cj75ojwhxttjz6bs7xtghkwekgfixnpw5fvbrpecumhrqhc4",
1616
"skill/valory/staking_abci/0.1.0": "bafybeia6csjgex5zxdt6vymiaengu75lyzjdgfrnp65ewlghthiniquhje",
17-
"skill/valory/agent_performance_summary_abci/0.1.0": "bafybeicejhpfa6cth6hmz5yxbpbp3awbaxk26sulw6xk3lkzp7ruxov3ni",
18-
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeidzz3x3m3cc2z76lqdhwacgrswjgbenc53ljx6ihmra2nnkg2khjm",
19-
"skill/valory/chatui_abci/0.1.0": "bafybeic6isfmqxmwswrgiuwzlyvmivnxkmwfhkgwzoc226753hcij6c3f4",
20-
"agent/valory/trader/0.1.0": "bafybeihmsgrivxffvdzr46dm4pftc4erf7v5j4vxdzl3q5blaysbeknree",
21-
"service/valory/trader_pearl/0.1.0": "bafybeic3peosuizvdyqeu6bycpy6le2ssl3vqlzopj6sl7ixs77uscr4ea",
22-
"service/valory/polymarket_trader/0.1.0": "bafybeifkpi37hfv5pvo4yncfk5ja6bhd4ee5j5iydnizmgvufyyvvgeu5q"
17+
"skill/valory/agent_performance_summary_abci/0.1.0": "bafybeibtuq7refjfnvzap4usjxnjwpgxpd5xbeeqwxut5nbnr7sjpo5nfi",
18+
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeid4dlfnxg7lo2wz4pilpavz2mfpwwjosmrueelrbtniuy22iw3xuq",
19+
"skill/valory/chatui_abci/0.1.0": "bafybeicdkch7riy2bui3j34rfjm57d4dbdqja6mkf5n5htpvets5gxa36e",
20+
"agent/valory/trader/0.1.0": "bafybeiglmixxve7juzdhkgtndzkrbwyzbxpe44z3dzbbrrrkjoq6zwsfhu",
21+
"service/valory/trader_pearl/0.1.0": "bafybeifjhqnzv6tbcytp44g4tpvaviaczptelo7hyqdh5drg6q6csniqr4",
22+
"service/valory/polymarket_trader/0.1.0": "bafybeigzkm662lypc2drcipklum5nlxafgjzocd6xi7ihycnmykx6r3jyy"
2323
},
2424
"third_party": {
2525
"protocol/valory/acn_data_share/0.1.0": "bafybeieqisfr6lhaaesq37lk7vyaxza2h6nucshksgyo2djqfyf2saqo4i",

packages/valory/agents/trader/aea-config.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ contracts:
4343
- valory/mech_marketplace:0.1.0:bafybeigrcnjaycggulxyx2tsjub4ssjij66d7nfzsavo6gd54hm7beliiu
4444
- valory/balance_tracker:0.1.0:bafybeiayyxl643qfpw57vopytxgyd6flxxmwassswuxen7myekqaxdxxvi
4545
- valory/balance_tracker_fixed_price_native:0.1.0:bafybeihyaxqkyw2ulroqlhpfcgkrwtrwkhshn35fdq2ystraezwyco3k4u
46-
- valory/mech_prepaid_reader:0.1.0:bafybeihy66fulkrsc4fxucrpunnr5ulwzudvnzxw5xtwjc7zqarf6bj3mi
46+
- valory/mech_prepaid_reader:0.1.0:bafybeieeh2xhktqn2xau527sgu6zvaqsf6nmd6um5zpgcds2bsg3l2onoa
4747
- valory/escrow_payment_condition:0.1.0:bafybeifghaewxrhssktvscpxpvydq3xvjd5rxx37oz54lhzsg2kelmwkai
4848
- valory/did_registry:0.1.0:bafybeianxf7gjparfype4xesfdep362odimhgriviweluinnexwuglm2ku
4949
- valory/nft_sales:0.1.0:bafybeidlzxmxpx25zcn5nhi3lb4kvfhfjsaw2dul2hogghshnxzmurfiom
@@ -70,15 +70,15 @@ skills:
7070
- valory/reset_pause_abci:0.1.0:bafybeifrnmlscfiki7wojs6zasppefupd2wkjyq6orhdllfppjud7isrxi
7171
- valory/termination_abci:0.1.0:bafybeih6sa2ttcvvjk7rcfn7ure2ws23kcfufssb4qmluzcfvednkfpvoi
7272
- valory/transaction_settlement_abci:0.1.0:bafybeihizaapype5sz7vlosms3nfo6jzfj4ujycuf5uthviq2otvmw6f6m
73-
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeibyonzs6wyvbcks5bztmc7rmdooynxmlfhiaub2cmuamqbtijv3ou
73+
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeicgh5cj75ojwhxttjz6bs7xtghkwekgfixnpw5fvbrpecumhrqhc4
7474
- valory/market_manager_abci:0.1.0:bafybeig3ak2eoimxwuupybde4napyi2jlf7pxk35se2zclikete2ni5hti
75-
- valory/decision_maker_abci:0.1.0:bafybeidel24uozve2ndxmwshhyrzsyv7itcodzqjcfzgqqikr4khpn4eiu
76-
- valory/trader_abci:0.1.0:bafybeiarobiv2d5l6teaslt7s4o36gni3bkwr75nub253mypms56dqzn7y
75+
- valory/decision_maker_abci:0.1.0:bafybeiffplku3zbpzhck7pen7mgbn3pi3tdqlat4j4t5746qx5km5jidyq
76+
- valory/trader_abci:0.1.0:bafybeiaoe2ldtkyygp3w3sjnuiwyhgz6ye5a7lnu4jhwjegmcgakq3ptei
7777
- valory/staking_abci:0.1.0:bafybeia6csjgex5zxdt6vymiaengu75lyzjdgfrnp65ewlghthiniquhje
78-
- valory/check_stop_trading_abci:0.1.0:bafybeidzz3x3m3cc2z76lqdhwacgrswjgbenc53ljx6ihmra2nnkg2khjm
78+
- valory/check_stop_trading_abci:0.1.0:bafybeid4dlfnxg7lo2wz4pilpavz2mfpwwjosmrueelrbtniuy22iw3xuq
7979
- valory/mech_interact_abci:0.1.0:bafybeici5tsgtlypnyrnp5colj7katnsyjfdzt6js4xdbnpdms4232muje
80-
- valory/chatui_abci:0.1.0:bafybeic6isfmqxmwswrgiuwzlyvmivnxkmwfhkgwzoc226753hcij6c3f4
81-
- valory/agent_performance_summary_abci:0.1.0:bafybeicejhpfa6cth6hmz5yxbpbp3awbaxk26sulw6xk3lkzp7ruxov3ni
80+
- valory/chatui_abci:0.1.0:bafybeicdkch7riy2bui3j34rfjm57d4dbdqja6mkf5n5htpvets5gxa36e
81+
- valory/agent_performance_summary_abci:0.1.0:bafybeibtuq7refjfnvzap4usjxnjwpgxpd5xbeeqwxut5nbnr7sjpo5nfi
8282
- valory/funds_manager:0.1.0:bafybeicfw7qjtnlcbw6awsvxdahqu42fxsfsd3wy7mbyhcbls4l5pee3by
8383
customs:
8484
- valory/kelly_criterion:0.1.0:bafybeidme2uybzyzagky3jtosheqmpgc6b6w4rum7cdxw2fc35nnpwblfe
@@ -258,7 +258,7 @@ models:
258258
mech_interaction_sleep_time: ${int:10}
259259
use_mech_marketplace: ${USE_MECH_MARKETPLACE:bool:false}
260260
policy_store_update_offset: ${int:259200}
261-
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x0000000000000000000000000000000000000000","priority_mech_address":"0x0000000000000000000000000000000000000000","priority_mech_staking_instance_address":"0x0000000000000000000000000000000000000000","priority_mech_service_id":0,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300}}
261+
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x0000000000000000000000000000000000000000","priority_mech_address":"0x0000000000000000000000000000000000000000","priority_mech_staking_instance_address":"0x0000000000000000000000000000000000000000","priority_mech_service_id":0,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300,"balance_tracker_address":"0x0000000000000000000000000000000000000000"}}
262262
expected_mech_response_time: ${int:300}
263263
mech_invalid_response: ${str:Invalid Response}
264264
mech_consecutive_failures_threshold: ${int:2}

packages/valory/contracts/mech_prepaid_reader/contract.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,27 @@ def get_deposit_events_for_requester(
113113
latest_block = block_number
114114

115115
return {"total_wei": total_wei, "latest_block": latest_block}
116+
117+
@classmethod
118+
def get_current_block_number(
119+
cls,
120+
ledger_api: LedgerApi,
121+
contract_address: str,
122+
) -> JSONLike:
123+
"""Return the current head block number of the ledger.
124+
125+
Used by the caller as a seed on first run so incremental log scans
126+
start from the current block rather than sweeping the full history —
127+
pre-migration on-chain-only Safes never emitted ``Deposit`` events for
128+
their account, so nothing is lost by skipping the past.
129+
130+
:param ledger_api: the ledger API object.
131+
:param contract_address: the BalanceTracker contract address, retained
132+
for framework symmetry with other contract-callable methods; the
133+
call itself does not touch the contract.
134+
:return: ``{"block_number": int}`` where the value is the latest block
135+
the RPC endpoint reports.
136+
"""
137+
del contract_address # unused — framework requires it in the signature
138+
ledger_api = cast(EthereumApi, ledger_api)
139+
return {"block_number": int(ledger_api.api.eth.block_number)}

packages/valory/contracts/mech_prepaid_reader/contract.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ aea_version: '>=2.0.0, <3.0.0'
88
fingerprint:
99
__init__.py: bafybeieearschnqbcpbecmkvxozipxh3bqktkutnb5tp2to3ko5phrcigm
1010
build/BalanceTracker.json: bafybeig2ywevvapgqoprxnnkid3atvq6qg7aqfhyd2uvqokdnydcfp2is4
11-
contract.py: bafybeibte6xnlv2e56n6jldfckf4j4j24e3efj6ntrq2qewh4fatcp3t34
11+
contract.py: bafybeie4okijr53pivy3aeon24bezqutprre5vfl2ejqeqwnccaoccorha
1212
tests/__init__.py: bafybeiadfytuqaujcf6s34242uiptiwqqp3dprxvcglxtdlnkchaepyvay
1313
fingerprint_ignore_patterns: []
1414
class_name: MechPrepaidReaderContract

packages/valory/services/polymarket_trader/service.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license: Apache-2.0
88
fingerprint:
99
README.md: bafybeichoh5tnk4qctaazcjcrxvpwc5yasodleqoeapnfapwrrdh3tm5gu
1010
fingerprint_ignore_patterns: []
11-
agent: valory/trader:0.1.0:bafybeihmsgrivxffvdzr46dm4pftc4erf7v5j4vxdzl3q5blaysbeknree
11+
agent: valory/trader:0.1.0:bafybeiglmixxve7juzdhkgtndzkrbwyzbxpe44z3dzbbrrrkjoq6zwsfhu
1212
number_of_agents: 1
1313
deployment:
1414
agent:
@@ -137,7 +137,7 @@ models:
137137
strategies_kwargs: ${STRATEGIES_KWARGS:dict:{"floor_balance":0,"default_max_bet_size":2500000,"absolute_min_bet_size":1000000,"absolute_max_bet_size":2500000,"n_bets":1,"min_edge":0.01,"max_edge":0.15,"min_oracle_prob":0.1,"fee_per_trade":10000,"grid_points":500}}
138138
use_subgraph_for_redeeming: ${USE_SUBGRAPH_FOR_REDEEMING:bool:true}
139139
use_mech_marketplace: ${USE_MECH_MARKETPLACE:bool:true}
140-
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x343F2B005cF6D70bA610CD9F1F1927049414B582","priority_mech_address":"0x1DCbC9368327776F8B20Dc041D40b8076C8AC173","priority_mech_staking_instance_address":"0x0000000000000000000000000000000000000000","priority_mech_service_id":25,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300,"use_offchain":false,"offchain_url":null,"offchain_deposit_target_calls":10,"auto_deposit_cap_per_cycle":5000000,"offchain_poll_interval_seconds":3.0,"offchain_poll_timeout_seconds":300.0,"offchain_failover_max_retries":2}}
140+
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x343F2B005cF6D70bA610CD9F1F1927049414B582","priority_mech_address":"0x1DCbC9368327776F8B20Dc041D40b8076C8AC173","priority_mech_staking_instance_address":"0x0000000000000000000000000000000000000000","priority_mech_service_id":25,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300,"balance_tracker_address":"0xaaFBeef195BDAb1Bb6f3Dc9cEbA875Cd72499230","use_offchain":false,"offchain_url":null,"offchain_deposit_target_calls":10,"auto_deposit_cap_per_cycle":5000000,"offchain_poll_interval_seconds":3.0,"offchain_poll_timeout_seconds":300.0,"offchain_failover_max_retries":2}}
141141
service_endpoint: ${SERVICE_ENDPOINT:str:https://trader.autonolas.tech/}
142142
rpc_sleep_time: ${RPC_SLEEP_TIME:int:10}
143143
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}

packages/valory/services/trader_pearl/service.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license: Apache-2.0
88
fingerprint:
99
README.md: bafybeibg7bdqpioh4lmvknw3ygnllfku32oca4eq5pqtvdrdsgw6buko7e
1010
fingerprint_ignore_patterns: []
11-
agent: valory/trader:0.1.0:bafybeihmsgrivxffvdzr46dm4pftc4erf7v5j4vxdzl3q5blaysbeknree
11+
agent: valory/trader:0.1.0:bafybeiglmixxve7juzdhkgtndzkrbwyzbxpe44z3dzbbrrrkjoq6zwsfhu
1212
number_of_agents: 1
1313
deployment:
1414
agent:
@@ -135,7 +135,7 @@ models:
135135
strategies_kwargs: ${STRATEGIES_KWARGS:dict:{"floor_balance":0,"default_max_bet_size":2000000000000000000,"absolute_min_bet_size":25000000000000000,"absolute_max_bet_size":2000000000000000000,"n_bets":1,"min_edge":0.03,"min_oracle_prob":0.5,"fee_per_trade":10000000000000000,"grid_points":500}}
136136
use_subgraph_for_redeeming: ${USE_SUBGRAPH_FOR_REDEEMING:bool:true}
137137
use_mech_marketplace: ${USE_MECH_MARKETPLACE:bool:false}
138-
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x4554fE75c1f5576c1d7F765B2A036c199Adae329","priority_mech_address":"0x0000000000000000000000000000000000000000","priority_mech_staking_instance_address":"0x998dEFafD094817EF329f6dc79c703f1CF18bC90","priority_mech_service_id":975,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300,"use_offchain":false,"offchain_url":null,"offchain_deposit_target_calls":10,"auto_deposit_cap_per_cycle":5000000000000000000,"offchain_poll_interval_seconds":3.0,"offchain_poll_timeout_seconds":300.0,"offchain_failover_max_retries":2}}
138+
mech_marketplace_config: ${MECH_MARKETPLACE_CONFIG:dict:{"mech_marketplace_address":"0x4554fE75c1f5576c1d7F765B2A036c199Adae329","priority_mech_address":"0x0000000000000000000000000000000000000000","priority_mech_staking_instance_address":"0x998dEFafD094817EF329f6dc79c703f1CF18bC90","priority_mech_service_id":975,"requester_staking_instance_address":"0x0000000000000000000000000000000000000000","response_timeout":300,"balance_tracker_address":"0x7D686bD1fD3CFF6E45a40165154D61043af7D67c","use_offchain":false,"offchain_url":null,"offchain_deposit_target_calls":10,"auto_deposit_cap_per_cycle":5000000000000000000,"offchain_poll_interval_seconds":3.0,"offchain_poll_timeout_seconds":300.0,"offchain_failover_max_retries":2}}
139139
service_endpoint: ${SERVICE_ENDPOINT:str:https://trader.autonolas.tech/}
140140
rpc_sleep_time: ${RPC_SLEEP_TIME:int:10}
141141
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}

packages/valory/skills/agent_performance_summary_abci/behaviours.py

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,39 +373,83 @@ def _fetch_offchain_prepaid_wei(
373373
requester-withdraw path — money committed is unrecoverable regardless
374374
of consumption.
375375
376-
This helper reads the ``Deposit(indexed account, indexed token, amount)``
377-
event stream from the last-scanned block cached in the performance
378-
summary JSON, sums the new entries into the running total, updates
379-
the checkpoint, and persists both fields under ``offchain_deposits``
380-
for the next cycle.
381-
382-
On-chain-only deployments (i.e. every production trader today) never
383-
emit these events for their Safe, so ``get_logs`` returns an empty
384-
list and the running total stays at 0. Existing ROI numbers do not
385-
change.
376+
The helper is a strict no-op for on-chain-only deployments — it
377+
short-circuits immediately when ``mech_marketplace_config.use_offchain``
378+
is ``False`` or when no ``balance_tracker_address`` is configured,
379+
so no contract call, no persist, no log noise.
380+
381+
On off-chain deployments the helper reads
382+
``Deposit(indexed account, indexed token, amount)`` events from the
383+
last-scanned block cached in the performance summary JSON, sums new
384+
entries into the running total, updates the checkpoint, and persists
385+
both fields under ``offchain_deposits`` for the next cycle. On the
386+
very first run (``last_scanned_block == 0``) it seeds the checkpoint
387+
to the current head block instead of sweeping history — off-chain has
388+
not shipped in production yet, so there are no pre-existing deposits
389+
to backfill, and this avoids RPC ``eth_getLogs`` block-range caps.
386390
387391
:param safe_address: the agent's own Safe address, used as the
388392
``account`` filter on the ``Deposit`` event.
389393
:return: cumulative sum (in wei-scaled units) of every
390394
``BalanceTracker.Deposit`` credit to this Safe up to and
391-
including the latest block scanned.
395+
including the latest block scanned. Zero for on-chain-only
396+
deployments, matching pre-migration ROI exactly.
392397
:yield: contract-API response(s) via the abstract-round-abci
393398
base behaviour helpers.
394399
"""
395-
# Read the checkpoint from the persisted summary.
400+
marketplace_config = self.params.mech_marketplace_config
401+
if not marketplace_config.use_offchain:
402+
# On-chain-only deployment (default for all production traders).
403+
# No contract call, no persist, ROI number is bit-identical to
404+
# pre-migration.
405+
return 0
406+
396407
summary = self.shared_state.read_existing_performance_summary()
397408
state = summary.offchain_deposits or OffchainDepositState()
398409
cached_total = int(state.total_deposited_wei)
399-
from_block = state.last_scanned_block
400410

401-
balance_tracker_address = (
402-
self.params.mech_marketplace_config.balance_tracker_address
403-
)
411+
balance_tracker_address = marketplace_config.balance_tracker_address
404412
if not balance_tracker_address:
405-
# No tracker configured (e.g. on-chain-only deployment where the
406-
# off-chain wiring was never populated). Nothing to sum.
413+
# Off-chain enabled but the tracker address is missing from the
414+
# config — misconfiguration, treat as zero and keep the cached
415+
# total (which will also be zero on first run).
407416
return cached_total
408417

418+
# First-run seed: don't sweep history from block 0. Off-chain hasn't
419+
# shipped in production, so there are no pre-existing deposits to
420+
# capture, and starting from the current head avoids RPC
421+
# ``eth_getLogs`` block-range caps.
422+
if state.last_scanned_block == 0:
423+
block_response = yield from self.get_contract_api_response(
424+
performative=ContractApiMessage.Performative.GET_STATE, # type: ignore
425+
contract_address=balance_tracker_address,
426+
contract_id=str(MechPrepaidReaderContract.contract_id),
427+
contract_callable="get_current_block_number",
428+
chain_id=self.params.mech_chain_id,
429+
)
430+
if block_response.performative != ContractApiMessage.Performative.STATE:
431+
self.context.logger.warning(
432+
"Failed to seed offchain-deposit checkpoint from current "
433+
f"block (response: {block_response}); leaving cached total."
434+
)
435+
return cached_total
436+
437+
current_block = int(block_response.state.body.get("block_number", 0))
438+
summary.offchain_deposits = OffchainDepositState(
439+
total_deposited_wei="0",
440+
last_scanned_block=current_block,
441+
)
442+
summary.timestamp = self.shared_state.synced_timestamp
443+
self.shared_state.overwrite_performance_summary(summary)
444+
self.context.logger.info(
445+
f"[Offchain Deposits] First run — seeded checkpoint to block "
446+
f"{current_block}, cumulative total starts at 0."
447+
)
448+
return 0
449+
450+
# Subsequent runs: scan strictly forward from the checkpoint.
451+
from_block = state.last_scanned_block + 1
452+
409453
response = yield from self.get_contract_api_response(
410454
performative=ContractApiMessage.Performative.GET_STATE, # type: ignore
411455
contract_address=balance_tracker_address,

0 commit comments

Comments
 (0)