Skip to content

Make Tao a type-safe#1917

Merged
ales-otf merged 5 commits intodevnet-readyfrom
sasha/chore/currency-types
Aug 8, 2025
Merged

Make Tao a type-safe#1917
ales-otf merged 5 commits intodevnet-readyfrom
sasha/chore/currency-types

Conversation

@ales-otf
Copy link
Contributor

@ales-otf ales-otf commented Aug 4, 2025

Description

This is the second part of this PR.

Related Issue(s)

  • Closes #[issue number]

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe): refactoring

Breaking Change

If this PR introduces a breaking change, please provide a detailed description of the impact and the migration path for existing applications.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run cargo fmt and cargo clippy to ensure my code is formatted and linted correctly
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Screenshots (if applicable)

Please include any relevant screenshots or GIFs that demonstrate the changes made.

Additional Notes

Please provide any additional information or context that may be helpful for reviewers.

@ales-otf ales-otf added skip-cargo-audit This PR fails cargo audit but needs to be merged anyway run-bittensor-e2e-tests labels Aug 4, 2025
@ales-otf ales-otf marked this pull request as ready for review August 4, 2025 15:34
Copy link
Collaborator

@shamil-gadelshin shamil-gadelshin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just one question about SwapInterface.

open-junius
open-junius previously approved these changes Aug 5, 2025
Copy link
Contributor

@open-junius open-junius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. It is very helpful to understand the code. and make code more like typed Rust. Thanks!

l0r1s
l0r1s previously approved these changes Aug 5, 2025
Copy link
Collaborator

@l0r1s l0r1s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gives a lot more type safety and document the code tremendously, well done!

gztensor
gztensor previously approved these changes Aug 5, 2025
@ales-otf ales-otf force-pushed the sasha/chore/currency-types branch from 282f481 to 608108d Compare August 5, 2025 18:25
.saturating_to_num::<u64>();
return Self::get_max_amount_add(destination_netuid, destination_subnet_price)
.map(Into::into);
// FIXME: mixed types alpha/tao
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixme?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in different places get_max_amount_add is used both: for calculating tao and alpha. it needs to be refactored, but it's outside of the scope of a wrapper for Tao/Alpha. i just highlighted this issue. the code is not changed.

Comment on lines +865 to +866
.saturating_to_num::<u64>()
.into();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be cool to impl From for these floating point types so we can skip the intermediary u64 step. not a blocker

// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
let pool_initial_tao = Self::get_network_min_lock();
let pool_initial_alpha = AlphaCurrency::from(Self::get_network_min_lock());
// FIXME: the result from function is used as a mixed type alpha/tao
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixme?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above

_,
Identity,
NetUid,
TaoCurrency,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need one, but should double check don't need a migration for these

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the code this storage holds Tao

/// --- MAP ( key ) --> last_tx_block_delegate_take
pub type LastTxBlockDelegateTake<T: Config> =
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
// FIXME: this storage is used interchangably for alpha/tao
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIXME?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is pretty clear again, as I said above

Comment on lines +1813 to +1814
// FIXME this function is used both to calculate for alpha stake amount as well as tao
// amount
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixme?

// We are using the sp_arithmetic sqrt here, which works for u128
let liquidity = helpers_128bit::sqrt(
(tao_reserve as u128).saturating_mul(u64::from(alpha_reserve) as u128),
(tao_reserve.to_u64() as u128).saturating_mul(alpha_reserve.to_u64() as u128),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably makes sense to impl From<TaoCurrency> for u128?

Comment on lines +238 to +240
fn to_u64(&self) -> u64 {
(*self).into()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious what the purpose of this wrapper is, instead of using into

Copy link
Contributor Author

@ales-otf ales-otf Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in some cases compiler can not know exact type in compilation time, so you can't use into, and then you should do something like Into::<u64>::into(value) or u64::from(value). here because the function returns u64, compiler knows into which type to convert.

position_id,
liquidity: liquidity_delta,
tao: (result.tao as i64).neg(),
tao: (result.tao.to_u64() as i64).neg(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think result.tao.to_u64() as i64 is safe, it will wrap for large numbers

Copy link
Contributor Author

@ales-otf ales-otf Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my change doesn't introduce any changes in this context. result.tao previously was u64 already. my change just converts TaoCurrency into u64 as it was before.

position_id,
liquidity: liquidity_delta,
tao: result.tao as i64,
tao: result.tao.to_u64() as i64,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think result.tao.to_u64() as i64 is safe, it will wrap for large numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same here as I said before

Copy link
Collaborator

@liamaharon liamaharon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! it would be great to open some issues for the final todos

@ales-otf ales-otf merged commit a578738 into devnet-ready Aug 8, 2025
56 checks passed
@ales-otf ales-otf deleted the sasha/chore/currency-types branch October 13, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants