-
Notifications
You must be signed in to change notification settings - Fork 2.6k
refactor: inconsistent BalanceConversion fn #13610
refactor: inconsistent BalanceConversion fn #13610
Conversation
pub trait BalanceConversion<InBalance, AssetId, OutBalance> { | ||
type Error; | ||
fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>; | ||
fn convert(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you are doing in #13608 PR, is different from what this meant for.
Here, the assets id, is id to which the in_balance should be converted, where in you example, its id from which it's being converted.
With updated function signature, the implementation can mean both, the id of in_balance, or the id of out_balance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With updated function signature, the implementation can mean both, the id of in_balance, or the id of out_balance.
IMO, that should be fine and expected of such a generic trait. Otherwise, its name should be more restricted (e.g. Option B) or it should provide a from_asset_balance
(Option A).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not do option A, to not break the api and to follow the segregation principle.
For option B, I would not change the name of the existing trait and name new trait as AssetConversion for example.
This way we don't break existing APIs, and it look clear enough to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonyalaribe what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have the Convert
trait. I would also stick to the old name to_asset_balance
. But yeah, maybe the trait should be renamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah Option B. Generally the name of the trait BalanceConversion
and then appears Asset
in the function names is weird, but naming is always hard and I don't have a better idea :P from_asset_balance
also sounds reasonable.
bot merge |
Error: "Check reviews" status is not passing for paritytech/cumulus#2408 |
bot merge |
* refactor: inconsistent BalanceConversion fn * Revert "refactor: inconsistent BalanceConversion fn" This reverts commit 1177877. * refactor: rename BalanceConversion trait * feat: add ConversionFromAssetBalance
This pull request has been mentioned on Polkadot Forum. There might be relevant details there: https://forum.polkadot.network/t/april-updates-for-substrate-and-polkadot-devs/2764/1 |
* refactor: inconsistent BalanceConversion fn * Revert "refactor: inconsistent BalanceConversion fn" This reverts commit 1177877. * refactor: rename BalanceConversion trait * feat: add ConversionFromAssetBalance
Related: #13608
Description
Fixes an inconsistent function naming for
BalanceConversion
trait: Thefn to_asset_balance
does not align with genericsInBalance
andOutBalance
asto_asset_balance
impliesOutBalance
to never be the native one.By fixing this, we enable to useBalanceConversion
truly flexibly as desired, see #13608UPDATE: As a result of @bkchr's remark, renamed
BalanceConversion
toConversionToAssetBalance
and added an opposite traitConversionFromAssetBalance
which will be used in #13608. Basically, this applies Option B, see below.Alternative solutions
In case
to_asset_balance
API should not be broken, I see two alternatives:Option A
Add
from_asset_balance
toBalanceConversion
:pub trait BalanceConversion<InBalance, AssetId, OutBalance> { type Error; fn to_asset_balance(balance: InBalance, asset_id: AssetId) -> Result<OutBalance, Self::Error>; + fn from_asset_balance(balance: OutBalance, asset_id: AssetId) -> Result<InBalance, Self::Error>; }
The downside would be that this forces the only current implementor of
BalanceConversion
, which isBalanceToAssetBalance
, to implement a function which it will never use.Option B
BalanceConversion
for trueBalanceToAssetBalance
conversion and renameAssetBalanceToBalance
conversionThe downside here is that we reduce the flexibility of this trait and create more noise.
cumulus companion: paritytech/cumulus#2408