forked from NSPG13/agent-bounties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaderboard_reward_pipeline.py
More file actions
718 lines (639 loc) · 27.9 KB
/
Copy pathleaderboard_reward_pipeline.py
File metadata and controls
718 lines (639 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#!/usr/bin/env python3
"""Build, independently sign, and relay closed solver-leaderboard awards."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import subprocess
import urllib.parse
import urllib.request
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any
CANDIDATE_SCHEMA = "agent-bounties/leaderboard-reward-candidate-v1"
ATTESTATION_SCHEMA = "agent-bounties/leaderboard-reward-attestation-v1"
MANIFEST_SCHEMA = "agent-bounties/leaderboard-reward-manifest-v1"
EVIDENCE_SCHEMA = "agent-bounties/leaderboard-reward-evidence-v1"
ADDRESS = re.compile(r"^0x[0-9a-f]{40}$")
HASH = re.compile(r"^0x[0-9a-f]{64}$")
SIGNATURE = re.compile(r"^0x[0-9a-f]{130}$")
ZERO_ADDRESS = "0x" + "0" * 40
BASE_MAINNET_USDC = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
FINALIZATION_DELAY = timedelta(hours=1)
FINALIZATION_DELAY_SECONDS = 3_600
MONDAY_EPOCH_OFFSET_SECONDS = 4 * 86_400
PERIODS = {
"daily": {"kind": 0, "reward": 3_000_000},
"weekly": {"kind": 1, "reward": 26_000_000},
}
class PipelineError(RuntimeError):
pass
class NoCandidate(RuntimeError):
pass
def canonical_json(value: object) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
def read_json(path: Path) -> Any:
return json.loads(path.read_text(encoding="utf-8"))
def write_json(path: Path, value: object) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, indent=2, sort_keys=True) + "\n", encoding="utf-8")
def normalize_address(value: object, field: str) -> str:
normalized = str(value or "").strip().lower()
if not ADDRESS.fullmatch(normalized):
raise PipelineError(f"{field} must be an EVM address")
return normalized
def normalize_hash(value: object, field: str) -> str:
normalized = str(value or "").strip().lower()
if not HASH.fullmatch(normalized):
raise PipelineError(f"{field} must be a bytes32 value")
return normalized
def parse_utc(value: object, field: str) -> datetime:
text = str(value or "")
try:
parsed = datetime.fromisoformat(text.replace("Z", "+00:00"))
except ValueError as error:
raise PipelineError(f"{field} must be RFC3339") from error
if parsed.tzinfo is None:
raise PipelineError(f"{field} must include a UTC offset")
return parsed.astimezone(timezone.utc)
def run(command: list[str], *, env: dict[str, str] | None = None) -> str:
completed = subprocess.run(
command,
check=False,
capture_output=True,
text=True,
env=env,
timeout=300,
)
if completed.returncode != 0:
detail = (completed.stderr or completed.stdout).strip()[:800]
raise PipelineError(f"command failed closed: {detail}")
return completed.stdout.strip()
def fetch_json(url: str, timeout: float = 30) -> Any:
request = urllib.request.Request(
url,
headers={
"Accept": "application/json",
"User-Agent": "agent-bounties-leaderboard-finalizer/1",
},
)
with urllib.request.urlopen(request, timeout=timeout) as response:
if response.status != 200:
raise PipelineError(f"leaderboard returned HTTP {response.status}")
return json.loads(response.read().decode("utf-8"))
def utc_text(value: datetime) -> str:
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
def period_references(now: datetime) -> dict[str, datetime]:
now = now.astimezone(timezone.utc)
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
this_week = today - timedelta(days=today.weekday())
return {
"daily": today - timedelta(seconds=1),
"weekly": this_week - timedelta(seconds=1),
}
def fetch_leaderboard(
api_base: str, network: str, reference: datetime
) -> dict[str, Any]:
query = urllib.parse.urlencode({"network": network, "at": utc_text(reference)})
value = fetch_json(
f"{api_base.rstrip('/')}/v1/base/autonomous-bounties/leaderboard?{query}"
)
if not isinstance(value, dict):
raise PipelineError("leaderboard response must be an object")
return value
def ranking_evidence(
response: dict[str, Any], period_name: str, contract: str
) -> dict[str, Any]:
if response.get("schema_version") != "agent-bounties/solver-leaderboard-v1":
raise PipelineError("leaderboard schema is unsupported")
network = str(response.get("network", ""))
period_output = response.get(period_name)
if not isinstance(period_output, dict):
raise PipelineError(f"{period_name} leaderboard is missing")
ranking = period_output.get("ranking")
if not isinstance(ranking, dict):
raise PipelineError(f"{period_name} ranking is missing")
period = ranking.get("period")
entries = ranking.get("entries")
rules = ranking.get("rules")
if not isinstance(period, dict) or not isinstance(entries, list) or not isinstance(rules, list):
raise PipelineError(f"{period_name} ranking shape is invalid")
if period.get("kind") != period_name:
raise PipelineError(f"{period_name} period kind drifted")
normalized_entries = []
for entry in entries:
if not isinstance(entry, dict):
raise PipelineError("leaderboard entry must be an object")
normalized_entries.append(
{
"rank": int(entry.get("rank", 0)),
"solver_wallet": normalize_address(
entry.get("solver_wallet"), "solver wallet"
),
"completed_bounties": int(entry.get("completed_bounties", 0)),
"prize_eligible_bounties": int(
entry.get("prize_eligible_bounties", 0)
),
"excluded_bounties": int(entry.get("excluded_bounties", 0)),
"eligible_solver_rewards_usdc_base_units": str(
entry.get("eligible_solver_rewards_usdc_base_units", "")
),
"last_eligible_settlement_at": entry.get(
"last_eligible_settlement_at"
),
"exclusion_counts": entry.get("exclusion_counts", {}),
}
)
leader = normalize_address(ranking.get("leader_wallet"), "leader wallet")
return {
"schema": EVIDENCE_SCHEMA,
"network": network,
"reward_contract": contract,
"period": {
"kind": period_name,
"starts_at": utc_text(parse_utc(period.get("starts_at"), "period start")),
"ends_at": utc_text(parse_utc(period.get("ends_at"), "period end")),
},
"minimum_solver_reward_usdc_base_units": str(
ranking.get("minimum_solver_reward_usdc_base_units", "")
),
"reward_usdc_base_units": str(ranking.get("reward_usdc_base_units", "")),
"leader_wallet": leader,
"entries": normalized_entries,
"rules": [str(rule) for rule in rules],
}
def build_candidate(
response: dict[str, Any],
period_name: str,
contract: str,
reference: datetime,
now: datetime,
fallback_pool_balance: int | None = None,
expected_token: str = BASE_MAINNET_USDC,
) -> dict[str, Any]:
settings = PERIODS[period_name]
contract = normalize_address(contract, "configured reward contract")
pool = response.get("reward_pool")
period_output = response.get(period_name)
if not isinstance(pool, dict) or not isinstance(period_output, dict):
raise PipelineError("reward pool or period output is missing")
balance = resolved_pool_balance(
pool,
period_output,
contract,
expected_token,
fallback_pool_balance,
)
ranking = period_output.get("ranking")
if not isinstance(ranking, dict):
raise PipelineError("period ranking is missing")
if ranking.get("leader_wallet") is None:
raise NoCandidate("period has no eligible winner")
evidence = ranking_evidence(response, period_name, contract)
starts_at = parse_utc(evidence["period"]["starts_at"], "period start")
ends_at = parse_utc(evidence["period"]["ends_at"], "period end")
if now.astimezone(timezone.utc) < ends_at + FINALIZATION_DELAY:
raise NoCandidate("period is not final")
if int(evidence["reward_usdc_base_units"]) != settings["reward"]:
raise PipelineError("reward amount drifted")
if period_output.get("period_status") != "closed":
raise PipelineError("finalized period is not closed")
payout_status = str(period_output.get("reward_payout_status", ""))
paid_wallet = period_output.get("reward_paid_wallet")
if payout_status == "paid":
raise NoCandidate("award is already paid")
if payout_status == "paid_to_different_wallet" or paid_wallet is not None:
raise PipelineError("award paid-winner evidence conflicts with the ranking")
if payout_status == "payout_unverified":
raise NoCandidate("paid-winner state is not verified")
if balance < settings["reward"]:
raise NoCandidate("reward pool balance is insufficient")
leader = evidence["leader_wallet"]
leader_entries = [
entry
for entry in evidence["entries"]
if entry["solver_wallet"] == leader and entry["prize_eligible_bounties"] > 0
]
if len(leader_entries) != 1 or leader_entries[0]["rank"] != 1:
raise PipelineError("leader is not one exact eligible rank-one entry")
completions = leader_entries[0]["prize_eligible_bounties"]
evidence_hash = "0x" + hashlib.sha256(canonical_json(evidence).encode()).hexdigest()
starts_at_unix = int(starts_at.timestamp())
return {
"schema": CANDIDATE_SCHEMA,
"candidate_id": hashlib.sha256(
f"{contract}:{settings['kind']}:{starts_at_unix}".encode()
).hexdigest(),
"network": str(response.get("network", "")),
"reference_at": utc_text(reference),
"reward_contract": contract,
"period_name": period_name,
"period_kind": settings["kind"],
"starts_at": starts_at_unix,
"ends_at": int(ends_at.timestamp()),
"winner": leader,
"eligible_completions": completions,
"reward_usdc_base_units": settings["reward"],
"evidence_hash": evidence_hash,
"evidence": evidence,
}
def current_candidate(
args: argparse.Namespace, candidate: dict[str, Any]
) -> dict[str, Any]:
reference = parse_utc(candidate.get("reference_at"), "candidate reference")
response = fetch_leaderboard(args.api_base, args.network, reference)
fallback_balance = None
pool = response.get("reward_pool")
if isinstance(pool, dict) and pool.get("contract") is None:
fallback_balance = reward_pool_balance(args)
return build_candidate(
response,
str(candidate.get("period_name", "")),
args.contract,
reference,
datetime.now(timezone.utc),
fallback_balance,
args.expected_token,
)
def assert_candidate_unchanged(
expected: dict[str, Any], current: dict[str, Any]
) -> None:
if canonical_json(expected) != canonical_json(current):
raise PipelineError("closed-period ranking changed; regenerate the candidate")
def chain_call(
args: argparse.Namespace, target: str, signature: str, *values: str
) -> str:
output = run(
[
str(args.cast),
"call",
"--json",
"--rpc-url",
args.rpc_url,
target,
signature,
*values,
]
)
try:
decoded = json.loads(output)
except json.JSONDecodeError as error:
raise PipelineError("cast call did not return JSON") from error
if not isinstance(decoded, list) or len(decoded) != 1:
raise PipelineError("cast call must return one value")
value = decoded[0]
if isinstance(value, bool):
return str(value).lower()
if not isinstance(value, (int, str)):
raise PipelineError("cast call returned an unsupported value")
return str(value).strip().lower()
def contract_call(args: argparse.Namespace, signature: str, *values: str) -> str:
return chain_call(args, args.contract, signature, *values)
def chain_int(value: str, field: str) -> int:
try:
parsed = int(value.strip(), 0)
except ValueError as error:
raise PipelineError(f"{field} is not an integer") from error
if parsed < 0:
raise PipelineError(f"{field} cannot be negative")
return parsed
def resolved_pool_balance(
pool: dict[str, Any],
period_output: dict[str, Any],
contract: str,
expected_token: str,
fallback_pool_balance: int | None,
) -> int:
pool_contract = pool.get("contract")
period_contract = period_output.get("reward_contract")
if pool_contract is None and period_contract is None:
if (
pool.get("funding_status") != "not_configured"
or period_output.get("reward_funding_status") != "not_configured"
or pool.get("balance_usdc_base_units") is not None
or period_output.get("reward_paid_wallet") is not None
or period_output.get("reward_payout_status")
not in {"no_winner", "not_due", "reward_not_configured"}
):
raise PipelineError("unconfigured API reward state is inconsistent")
if (
normalize_address(pool.get("settlement_token"), "API settlement token")
!= normalize_address(expected_token, "expected settlement token")
):
raise PipelineError("API settlement token does not match Base mainnet USDC")
if fallback_pool_balance is None:
raise PipelineError("API reward contract is unconfigured and no chain balance was supplied")
return chain_int(str(fallback_pool_balance), "on-chain reward pool balance")
if pool_contract is None or period_contract is None:
raise PipelineError("API reward contract configuration is partial")
if normalize_address(pool_contract, "API reward contract") != contract:
raise PipelineError("API reward contract does not match the configured contract")
if normalize_address(period_contract, "period reward contract") != contract:
raise PipelineError("period reward contract does not match the configured contract")
return chain_int(str(pool.get("balance_usdc_base_units") or ""), "API reward pool balance")
def reward_pool_balance(args: argparse.Namespace) -> int:
token = normalize_address(args.expected_token, "expected settlement token")
return chain_int(
chain_call(args, token, "balanceOf(address)(uint256)", args.contract),
"on-chain reward pool balance",
)
def validate_contract(args: argparse.Namespace) -> set[str]:
code = run(
[
str(args.cast),
"code",
"--rpc-url",
args.rpc_url,
args.contract,
]
).strip().lower()
if code == "0x" or not re.fullmatch(r"0x[0-9a-f]+", code):
raise PipelineError("leaderboard reward contract has no valid bytecode")
token = normalize_address(
contract_call(args, "settlementToken()(address)"), "settlement token"
)
expected_token = normalize_address(args.expected_token, "expected settlement token")
if args.network != "base-mainnet" or token != expected_token:
raise PipelineError("leaderboard contract is not pinned to Base mainnet USDC")
if chain_int(contract_call(args, "DAILY_REWARD()(uint256)"), "daily reward") != PERIODS["daily"]["reward"]:
raise PipelineError("leaderboard daily reward drifted")
if chain_int(contract_call(args, "WEEKLY_REWARD()(uint256)"), "weekly reward") != PERIODS["weekly"]["reward"]:
raise PipelineError("leaderboard weekly reward drifted")
if chain_int(contract_call(args, "FINALIZATION_DELAY()(uint64)"), "finalization delay") != FINALIZATION_DELAY_SECONDS:
raise PipelineError("leaderboard finalization delay drifted")
starts = contract_period_starts(args)
if starts["daily"] % 86_400 != 0:
raise PipelineError("leaderboard daily calendar is invalid")
if (
starts["weekly"] < MONDAY_EPOCH_OFFSET_SECONDS
or (starts["weekly"] - MONDAY_EPOCH_OFFSET_SECONDS) % (7 * 86_400) != 0
):
raise PipelineError("leaderboard weekly calendar is invalid")
signers = {
normalize_address(contract_call(args, "signerA()(address)"), "signer A"),
normalize_address(contract_call(args, "signerB()(address)"), "signer B"),
}
if ZERO_ADDRESS in signers or len(signers) != 2:
raise PipelineError("leaderboard signers must be two distinct nonzero addresses")
return signers
def contract_period_starts(args: argparse.Namespace) -> dict[str, int]:
return {
"daily": chain_int(
contract_call(args, "firstDailyStart()(uint64)"), "first daily start"
),
"weekly": chain_int(
contract_call(args, "firstWeeklyStart()(uint64)"), "first weekly start"
),
}
def award_digest(args: argparse.Namespace, candidate: dict[str, Any]) -> str:
return normalize_hash(
contract_call(
args,
"awardDigest(uint8,uint64,address,uint32,bytes32)(bytes32)",
str(candidate["period_kind"]),
str(candidate["starts_at"]),
candidate["winner"],
str(candidate["eligible_completions"]),
candidate["evidence_hash"],
),
"award digest",
)
def command_run(args: argparse.Namespace) -> None:
now = datetime.now(timezone.utc)
references = period_references(now)
validate_contract(args)
first_starts = contract_period_starts(args)
fallback_balance = reward_pool_balance(args)
cache: dict[str, dict[str, Any]] = {}
candidates = []
skipped = []
args.output.mkdir(parents=True, exist_ok=True)
for period_name, reference in references.items():
key = utc_text(reference)
if key not in cache:
cache[key] = fetch_leaderboard(args.api_base, args.network, reference)
response = cache[key]
try:
candidate = build_candidate(
response,
period_name,
args.contract,
reference,
now,
fallback_balance,
args.expected_token,
)
except NoCandidate as reason:
skipped.append({"period": period_name, "reason": str(reason)})
continue
if candidate["starts_at"] < first_starts[period_name]:
skipped.append({"period": period_name, "reason": "period predates program"})
continue
name = f"candidate-{candidate['candidate_id']}.json"
write_json(args.output / name, candidate)
candidates.append({"candidate_id": candidate["candidate_id"], "file": name})
write_json(
args.output / "manifest.json",
{
"schema": MANIFEST_SCHEMA,
"network": args.network,
"reward_contract": normalize_address(args.contract, "reward contract"),
"candidates": candidates,
"skipped": skipped,
},
)
def command_sign(args: argparse.Namespace) -> None:
key = os.environ.get(args.private_key_env, "").strip()
if not key:
raise PipelineError(f"{args.private_key_env} is required")
signer = normalize_address(
run([str(args.cast), "wallet", "address", "--private-key", key]), "signer"
)
if signer != normalize_address(args.expected_signer, "expected signer"):
raise PipelineError("private key does not match the expected signer")
if signer not in validate_contract(args):
raise PipelineError("signer is not committed by the reward contract")
manifest = read_json(args.candidates / "manifest.json")
if manifest.get("schema") != MANIFEST_SCHEMA:
raise PipelineError("candidate manifest schema is invalid")
args.output.mkdir(parents=True, exist_ok=True)
attestations = []
for entry in manifest.get("candidates", []):
candidate = read_json(args.candidates / entry["file"])
current = current_candidate(args, candidate)
assert_candidate_unchanged(candidate, current)
digest = award_digest(args, candidate)
signature = run(
[
str(args.cast),
"wallet",
"sign",
"--no-hash",
"--private-key",
key,
digest,
]
).lower()
if not SIGNATURE.fullmatch(signature):
raise PipelineError("signer returned an invalid signature")
name = f"attestation-{candidate['candidate_id']}.json"
write_json(
args.output / name,
{
"schema": ATTESTATION_SCHEMA,
"candidate_id": candidate["candidate_id"],
"signer": signer,
"digest": digest,
"signature": signature,
},
)
attestations.append({"candidate_id": candidate["candidate_id"], "file": name})
write_json(
args.output / "manifest.json",
{
"schema": ATTESTATION_SCHEMA,
"signer": signer,
"attestations": attestations,
},
)
def command_relay(args: argparse.Namespace) -> None:
keeper = os.environ.get(args.keeper_key_env, "").strip()
if not keeper:
raise PipelineError(f"{args.keeper_key_env} is required")
expected_signers = {
normalize_address(value, "expected signer") for value in args.expected_signer
}
if len(expected_signers) != 2 or validate_contract(args) != expected_signers:
raise PipelineError("workflow signers do not match the reward contract")
candidate_manifest = read_json(args.candidates / "manifest.json")
by_signer: dict[str, dict[str, Path]] = {}
for directory in args.attestations:
manifest = read_json(directory / "manifest.json")
signer = normalize_address(manifest.get("signer"), "attestation signer")
if signer in by_signer:
raise PipelineError("duplicate attestation signer")
by_signer[signer] = {
item["candidate_id"]: directory / item["file"]
for item in manifest.get("attestations", [])
}
if set(by_signer) != expected_signers:
raise PipelineError("attestation artifacts do not contain both committed signers")
for entry in candidate_manifest.get("candidates", []):
candidate = read_json(args.candidates / entry["file"])
current = current_candidate(args, candidate)
assert_candidate_unchanged(candidate, current)
digest = award_digest(args, candidate)
signed = []
for signer in sorted(expected_signers):
attestation = read_json(by_signer[signer][candidate["candidate_id"]])
if (
attestation.get("schema") != ATTESTATION_SCHEMA
or attestation.get("candidate_id") != candidate["candidate_id"]
or normalize_address(attestation.get("signer"), "attestation signer")
!= signer
or normalize_hash(attestation.get("digest"), "attestation digest")
!= digest
or not SIGNATURE.fullmatch(str(attestation.get("signature", "")))
):
raise PipelineError("attestation does not match the exact candidate")
signed.append(attestation["signature"])
award_id = normalize_hash(
contract_call(
args,
"awardId(uint8,uint64)(bytes32)",
str(candidate["period_kind"]),
str(candidate["starts_at"]),
),
"award id",
)
paid = normalize_address(
contract_call(args, "paidAwardWinner(bytes32)(address)", award_id),
"paid award winner",
)
if paid == candidate["winner"]:
print(f"already paid {candidate['candidate_id']} to {paid}")
continue
if paid != ZERO_ADDRESS:
raise PipelineError("award is already paid to a different wallet")
receipt = json.loads(
run(
[
str(args.cast),
"send",
"--json",
"--rpc-url",
args.rpc_url,
"--private-key",
keeper,
args.contract,
"pay(uint8,uint64,address,uint32,bytes32,bytes,bytes)",
str(candidate["period_kind"]),
str(candidate["starts_at"]),
candidate["winner"],
str(candidate["eligible_completions"]),
candidate["evidence_hash"],
signed[0],
signed[1],
]
)
)
tx_hash = normalize_hash(receipt.get("transactionHash"), "transaction hash")
if str(receipt.get("status", "")) not in {"0x1", "1"}:
raise PipelineError("reward relay did not return a successful receipt")
confirmed = normalize_address(
contract_call(args, "paidAwardWinner(bytes32)(address)", award_id),
"confirmed paid winner",
)
if confirmed != candidate["winner"]:
raise PipelineError("successful receipt lacks the expected paid-winner state")
print(f"paid {candidate['candidate_id']}: {tx_hash}")
def parser() -> argparse.ArgumentParser:
root = argparse.ArgumentParser(description=__doc__)
commands = root.add_subparsers(dest="command", required=True)
run_parser = commands.add_parser("run")
run_parser.add_argument("--api-base", default="https://api.agentbounties.app")
run_parser.add_argument("--network", default="base-mainnet")
run_parser.add_argument("--rpc-url", required=True)
run_parser.add_argument("--contract", required=True)
run_parser.add_argument("--expected-token", default=BASE_MAINNET_USDC)
run_parser.add_argument("--output", type=Path, required=True)
run_parser.add_argument("--cast", type=Path, default=Path("cast"))
run_parser.set_defaults(handler=command_run)
sign_parser = commands.add_parser("sign")
sign_parser.add_argument("--api-base", default="https://api.agentbounties.app")
sign_parser.add_argument("--network", default="base-mainnet")
sign_parser.add_argument("--rpc-url", required=True)
sign_parser.add_argument("--contract", required=True)
sign_parser.add_argument("--expected-token", default=BASE_MAINNET_USDC)
sign_parser.add_argument("--candidates", type=Path, required=True)
sign_parser.add_argument("--output", type=Path, required=True)
sign_parser.add_argument("--cast", type=Path, default=Path("cast"))
sign_parser.add_argument("--private-key-env", required=True)
sign_parser.add_argument("--expected-signer", required=True)
sign_parser.set_defaults(handler=command_sign)
relay_parser = commands.add_parser("relay")
relay_parser.add_argument("--api-base", default="https://api.agentbounties.app")
relay_parser.add_argument("--network", default="base-mainnet")
relay_parser.add_argument("--rpc-url", required=True)
relay_parser.add_argument("--contract", required=True)
relay_parser.add_argument("--expected-token", default=BASE_MAINNET_USDC)
relay_parser.add_argument("--candidates", type=Path, required=True)
relay_parser.add_argument("--attestations", type=Path, action="append", required=True)
relay_parser.add_argument("--expected-signer", action="append", required=True)
relay_parser.add_argument("--cast", type=Path, default=Path("cast"))
relay_parser.add_argument("--keeper-key-env", default="BASE_KEEPER_PRIVATE_KEY")
relay_parser.set_defaults(handler=command_relay)
return root
def main() -> int:
try:
args = parser().parse_args()
args.contract = normalize_address(args.contract, "reward contract")
args.handler(args)
except (PipelineError, NoCandidate, OSError, ValueError, KeyError, json.JSONDecodeError) as error:
print(f"leaderboard reward pipeline failed: {error}", file=os.sys.stderr)
return 1
return 0
if __name__ == "__main__":
raise SystemExit(main())