Skip to content

Commit f298347

Browse files
[Fix] Use a single swap and claim in the Python example
1 parent f64612f commit f298347

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

‎shield-swap-sdk/examples/first-swap/README.md‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ ID. Job completion does not guarantee every token transfer succeeded.
4141
The faucet allows one request per address per 15 minutes. A rate-limit error
4242
stops the example; inspect the existing funding before requesting again.
4343
The example waits for the scanner to report at least 1.5 USDCx before trading.
44-
It finds
45-
a direct USDCx/ETH pool and calls `swap_many(count=1)`, which quotes the trade,
46-
selects a token record, and records the submitted handle in the SDK journal.
44+
It quotes a direct USDCx/ETH pool with `get_route()`, then calls
45+
`swap(...).delegate(wait=True)` with that quote and a 0.5% slippage limit.
46+
The SDK selects a token record and saves the swap handle in its journal.
4747
One unspent record must cover 1.5 USDCx.
4848

4949
## Completion and recovery
5050

51-
A successful run ends after `collect_all()` reports the swap's claim.
52-
`claim["transaction_id"]` identifies the claim and `claim["amount_out"]`
53-
contains the received ETH in base units. The example writes no console logs
51+
After the swap confirms, `claim_swap_output(handle).delegate(wait=True)`
52+
submits one claim and waits for confirmation. The example records the claim
53+
in the SDK journal. `claim.transaction_id` identifies the claim and
54+
`claim.amount_out` contains the received ETH in base units. The example writes no console logs
5455
or additional result files.
5556

5657
Each run submits a new trade. To recover an interrupted run, load the same

‎shield-swap-sdk/examples/first-swap/swap.py‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Create and fund a testnet account, swap 1.5 USDCx for ETH, and claim the output."""
22
import time
3+
from decimal import Decimal
34

45
from aleo import testnet
56

@@ -44,26 +45,27 @@
4445
else:
4546
raise RuntimeError("USDCx is not available; inspect funding.results and the account balance")
4647

47-
# Submit one swap with a 0.5% slippage limit. The SDK journals its handle.
48-
swaps = dex.swap_many(
48+
# Quote the selected pool and convert the expected ETH output to base units.
49+
quote = dex.api.get_route(
50+
token_in=source.id, token_out=target.id, amount_in="1.5", pool_key=pool.key,
51+
)
52+
if not quote.estimated_amount_out:
53+
raise RuntimeError("The selected pool returned no quote")
54+
expected_out = int(Decimal(quote.estimated_amount_out) * 10**target.decimals)
55+
if expected_out <= 0:
56+
raise RuntimeError("The quote returned no ETH")
57+
58+
# Submit one swap and wait for confirmation. The SDK journals its handle.
59+
handle = dex.swap(
4960
pool_key=pool.key,
5061
token_in_id=source.id,
5162
amount_in=amount_in,
52-
count=1,
63+
expected_out=expected_out,
5364
slippage_bps=50,
54-
)
55-
if swaps.failures or len(swaps.handles) != 1:
56-
raise RuntimeError("Swap submission needs inspection; use the SDK journal before retrying")
65+
).delegate(wait=True)
5766

58-
# Collect the output after confirmation, without submitting another swap.
59-
for attempt in range(40):
60-
claims = dex.collect_all()
61-
claim = next((item for item in claims.claimed
62-
if item["swap_id"] == swaps.handles[0].swap_id), None)
63-
if claim is not None:
64-
if claim["amount_out"] <= 0:
65-
raise RuntimeError("The claim returned no ETH")
66-
break
67-
time.sleep(15)
68-
else:
69-
raise RuntimeError("Claim is still pending; resume with dex.collect_all()")
67+
# Claim the confirmed swap's output once, then update the SDK journal.
68+
claim = dex.claim_swap_output(handle).delegate(wait=True)
69+
dex.journal.record_claim(handle.swap_id, claim.transaction_id, claim.amount_out)
70+
if claim.amount_out <= 0:
71+
raise RuntimeError("The claim returned no ETH")

0 commit comments

Comments
 (0)