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

Use max of 6 decimals for xrp dex #303

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- fixed: Use appropriate send amount in spend targets for Uniswap-based providers
- fixed: XRP DEX swap error due to excessive decimals

## 1.0.2 (2023-11-06)

Expand Down
11 changes: 11 additions & 0 deletions src/swap/defi/xrpDex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const EXCHANGE_INFO_UPDATE_FREQ_MS = 60000
const VOLATILITY_SPREAD_DEFAULT = 0.0075
const DUMMY_XRP_ADDRESS = 'rfuESo7eHUnvebxgaFjfYxfwXhM2uBPAj3'

// This is a multiplier to enforce the maximum decimals used by the DEX.
// This does not necessarily correspond to the XRP denomination as tokens can have an
// arbitrary number of decimals since they are floats. But since the DEX errors with
// too many decimals (not sure how much), we for now cap swap amounts to 6 decimals
const MAX_DECIMALS_MULTIPLIER = 1000000
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there some reason to separate this from ripple's currencyInfo.denominations[0].multiplier?

Copy link
Member Author

Choose a reason for hiding this comment

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

will add comment


export const asExchangeInfo = asObject({
swap: asObject({
plugins: asObject({
Expand Down Expand Up @@ -170,6 +176,8 @@ export function makeXrpDexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
{ client, showLogs: false }
)
quote = quote * (1 - volatilitySpread)
quote =
Math.round(quote * MAX_DECIMALS_MULTIPLIER) / MAX_DECIMALS_MULTIPLIER
toNativeAmount = await toWallet.denominationToNative(
String(quote),
toCurrencyCode
Expand Down Expand Up @@ -198,6 +206,9 @@ export function makeXrpDexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
{ client, showLogs: false }
)
quote = quote * (1 + volatilitySpread)
quote =
Math.round(quote * MAX_DECIMALS_MULTIPLIER) / MAX_DECIMALS_MULTIPLIER

fromNativeAmount = await fromWallet.denominationToNative(
String(quote),
fromCurrencyCode
Expand Down