Skip to content

fix: remove double conversion in stake swap functionality [--swap_all]#328

Merged
thewhaleking merged 1 commit intoopentensor:stagingfrom
ashikshafi08:fix/stake-swap-conversion
Feb 19, 2025
Merged

fix: remove double conversion in stake swap functionality [--swap_all]#328
thewhaleking merged 1 commit intoopentensor:stagingfrom
ashikshafi08:fix/stake-swap-conversion

Conversation

@ashikshafi08
Copy link
Contributor

@ashikshafi08 ashikshafi08 commented Feb 19, 2025

Fix Double Conversion Bug in Stake Swap Functionality

Issue Description

A bug was discovered in the stake swap functionality when using the --swap-all flag. The bug caused massive stake amount inflation due to an unnecessary double conversion of the Balance object.

Problem Details

  • When using --swap-all, the code was incorrectly converting an already-converted Balance object
  • This resulted in massive stake amount inflation
  • Example:
    • Input stake amount: 21.5369 פ
    • Incorrectly inflated amount: 21,536,911,597.0000 פ

Root Cause

The bug occurred in move.py where:

# Buggy code
if swap_all:
    amount_to_swap = Balance.from_tao(current_stake).set_unit(origin_netuid)

The current_stake was already a Balance object, but Balance.from_tao() was being called on it again, causing double conversion.

before

Solution

Fixed by removing the unnecessary Balance.from_tao() conversion:

# Fixed code
if swap_all:
    amount_to_swap = current_stake.set_unit(origin_netuid)

This change ensures the Balance object is only converted once, maintaining the correct stake amount.

after

Testing Performed

  • Tested with real wallet and subnet data
  • Verified correct stake conversion amounts
  • Tested cross-subnet stake swap functionality
  • Confirmed no multiplication of amounts occurs
  • Example test case:
    • Wallet: coldkey_1
    • Hotkey: hot_25
    • Origin Subnet: 45 (21.5369 פ)
    • Destination Subnet: 0
    • Result: Correct conversion to 0.7464 τ

Impact

This fix prevents potential significant financial losses that could occur due to incorrect stake amount calculations when using the --swap-all flag.

@thewhaleking thewhaleking changed the base branch from main to staging February 19, 2025 19:16
@thewhaleking
Copy link
Contributor

Great catch!

@thewhaleking thewhaleking merged commit 1149ecd into opentensor:staging Feb 19, 2025
1 check passed
@ibraheem-abe ibraheem-abe mentioned this pull request Feb 20, 2025
@ashikshafi08 ashikshafi08 deleted the fix/stake-swap-conversion branch February 21, 2025 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants