Skip to content

Commit 5d3b734

Browse files
committed
Adds swap test
1 parent 2886372 commit 5d3b734

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

tests/e2e_tests/test_staking.py

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
354354
bob_wallet.hotkey.ss58_address,
355355
netuid=netuid,
356356
)
357-
assert full_stake >= stake_amount, "Full stake amount should be added"
358357

359358
# Test Unstaking Scenarios
360359
# 1. Strict params - should fail
@@ -385,7 +384,7 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
385384
alice_wallet,
386385
bob_wallet.hotkey.ss58_address,
387386
netuid=netuid,
388-
amount=stake_amount,
387+
amount=current_stake,
389388
wait_for_inclusion=True,
390389
wait_for_finalization=True,
391390
safe_staking=True,
@@ -414,3 +413,104 @@ def test_safe_staking_scenarios(subtensor, alice_wallet, bob_wallet):
414413
allow_partial_stake=False,
415414
)
416415
assert success is True
416+
417+
418+
def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
419+
"""
420+
Tests safe swap stake scenarios with different parameters.
421+
422+
Tests:
423+
1. Fails with strict threshold (0.5%)
424+
2. Succeeds with lenient threshold (10%)
425+
"""
426+
# Create new subnet (netuid 2) and register Alice
427+
origin_netuid = 2
428+
assert subtensor.register_subnet(bob_wallet)
429+
assert subtensor.subnet_exists(origin_netuid), "Subnet wasn't created successfully"
430+
dest_netuid = 3
431+
assert subtensor.register_subnet(bob_wallet)
432+
assert subtensor.subnet_exists(dest_netuid), "Subnet wasn't created successfully"
433+
434+
# Register Alice on both subnets
435+
subtensor.burned_register(
436+
alice_wallet,
437+
netuid=origin_netuid,
438+
wait_for_inclusion=True,
439+
wait_for_finalization=True,
440+
)
441+
subtensor.burned_register(
442+
alice_wallet,
443+
netuid=dest_netuid,
444+
wait_for_inclusion=True,
445+
wait_for_finalization=True,
446+
)
447+
448+
# Add initial stake to swap from
449+
initial_stake_amount = Balance.from_tao(10_000)
450+
success = subtensor.add_stake(
451+
alice_wallet,
452+
alice_wallet.hotkey.ss58_address,
453+
netuid=origin_netuid,
454+
amount=initial_stake_amount,
455+
wait_for_inclusion=True,
456+
wait_for_finalization=True,
457+
)
458+
assert success is True
459+
460+
origin_stake = subtensor.get_stake(
461+
alice_wallet.coldkey.ss58_address,
462+
alice_wallet.hotkey.ss58_address,
463+
netuid=origin_netuid,
464+
)
465+
assert origin_stake > Balance(0), "Origin stake should be non-zero"
466+
467+
# 1. Try swap with strict threshold - should fail
468+
success = subtensor.swap_stake(
469+
wallet=alice_wallet,
470+
hotkey_ss58=alice_wallet.hotkey.ss58_address,
471+
origin_netuid=origin_netuid,
472+
destination_netuid=dest_netuid,
473+
amount=origin_stake,
474+
wait_for_inclusion=True,
475+
wait_for_finalization=True,
476+
safe_staking=True,
477+
rate_threshold=0.005, # 0.5%
478+
allow_partial_stake=False,
479+
)
480+
assert success is False
481+
482+
# Verify no stake was moved
483+
dest_stake = subtensor.get_stake(
484+
alice_wallet.coldkey.ss58_address,
485+
alice_wallet.hotkey.ss58_address,
486+
netuid=dest_netuid,
487+
)
488+
assert dest_stake == Balance(0), "Destination stake should remain 0 after failed swap"
489+
490+
# 2. Try swap with higher threshold - should succeed
491+
success = subtensor.swap_stake(
492+
wallet=alice_wallet,
493+
hotkey_ss58=alice_wallet.hotkey.ss58_address,
494+
origin_netuid=origin_netuid,
495+
destination_netuid=dest_netuid,
496+
amount=origin_stake,
497+
wait_for_inclusion=True,
498+
wait_for_finalization=True,
499+
safe_staking=True,
500+
rate_threshold=0.3, # 10%
501+
allow_partial_stake=True,
502+
)
503+
assert success is True
504+
505+
# Verify stake was moved
506+
origin_stake = subtensor.get_stake(
507+
alice_wallet.coldkey.ss58_address,
508+
alice_wallet.hotkey.ss58_address,
509+
netuid=origin_netuid,
510+
)
511+
dest_stake = subtensor.get_stake(
512+
alice_wallet.coldkey.ss58_address,
513+
alice_wallet.hotkey.ss58_address,
514+
netuid=dest_netuid,
515+
)
516+
assert dest_stake > Balance(0), "Destination stake should be non-zero after successful swap"

0 commit comments

Comments
 (0)