Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change price conversion function in oracle pallet to work for assets with different decimals #490

Conversation

ebma
Copy link
Member

@ebma ebma commented Feb 13, 2024

This PR adds changes so that the prices of assets are converted taking their respective decimals into account.

This is achieved through an extra trait DecimalsLookup. Unfortunately, it was not possible to implement the decimals() directly on the CurrencyId struct since they vary per network, ie we need to have a different implementation for Pendulum compared to Amplitude and Foucoco. The name DecimalsLookup might not be ideal because it implements decimals() and one().
I decided to implement this trait for Pendulum and Amplitude in the Spacewalk repository already. This is debatable but the vault client needs to be able to have access to the decimals used on the chain it connects to which is easiest to do if we define them in this repository and re-use them in Pendulum. Otherwise, we have to duplicate the same code.

The price conversion is adopted from this function used by Nabla. I kept the functions to convert an asset to USD and vice versa around because they are nice to have in the portal since they are exposed by the RPC of our nodes.

Some tests had to be changed because they were taking simplified assumptions about the relation of price and decimals for the collateral and wrapped currency. They were mostly fixed by increasing the used numbers (because the numbers should have at least two trailing 0s in order not to face issues due to rounding) and using the price conversion in the Oracle pallet to convert between collateral and wrapped asset, taking the decimal difference into account.

Closes #489.

@ebma ebma force-pushed the 489-change-price-conversion-function-in-oracle-pallet-to-work-for-assets-with-different-decimals branch from d69ddf0 to 1e55298 Compare February 14, 2024 17:56
@ebma ebma force-pushed the 489-change-price-conversion-function-in-oracle-pallet-to-work-for-assets-with-different-decimals branch from 1e55298 to 4fbeed1 Compare February 15, 2024 17:44
@ebma ebma changed the title [WIP] change price conversion function in oracle pallet to work for assets with different decimals Change price conversion function in oracle pallet to work for assets with different decimals Feb 15, 2024
@ebma ebma marked this pull request as ready for review February 15, 2024 17:53
@ebma ebma requested a review from a team February 15, 2024 18:16
CurrencyId::Stellar(asset) => asset.decimals(),
CurrencyId::XCM(index) => match index {
Copy link
Contributor

Choose a reason for hiding this comment

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

I did not check whether this is correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

I adopted the decimals from the transactions we created for the asset metadata here and here.

@ebma ebma force-pushed the 489-change-price-conversion-function-in-oracle-pallet-to-work-for-assets-with-different-decimals branch from d4a2b3b to e427fbd Compare February 16, 2024 11:48
Copy link
Contributor

@gianfra-t gianfra-t left a comment

Choose a reason for hiding this comment

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

This all looks great @ebma. I like the use of the DecimalsLookup type, I hope we can keep up with updating it properly.
Just left some comments that are more like questions so I understand fully.

)),
);
assert!(max_wrapped_for_min_collateral.amount() + tolerance >= wrapped_amount);
assert!(max_wrapped_for_below_min_collateral.amount() < wrapped_amount);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we need the tolerance in the second assertion?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question. From empiric testing I can tell you that it is not problematic and the bound holds even without the tolerance. But it would make sense to add the tolerance both ways.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added that tolerance also to the other check now

Copy link
Contributor

@bogdanS98 bogdanS98 left a comment

Choose a reason for hiding this comment

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

Great changes 👍🏼 I already got the answers to my questions from the threads created by @gianfra-t

Copy link
Contributor

@TorstenStueber TorstenStueber left a comment

Choose a reason for hiding this comment

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

Looks all good to me now. Thanks!

@ebma ebma merged commit bda384b into main Feb 20, 2024
0 of 2 checks passed
@ebma ebma deleted the 489-change-price-conversion-function-in-oracle-pallet-to-work-for-assets-with-different-decimals branch February 20, 2024 16:42
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.

Change price conversion function in oracle pallet to work for assets with different decimals
4 participants