Skip to content

Commit

Permalink
Check maxSpendable in xrpdex
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Nov 4, 2023
1 parent 0e124ea commit f0e1832
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# edge-exchange-plugins

## Unreleased

- fixed: Check max spendable amount of source wallet in xrpdex quote

## 1.0.1 (2023-10-30)

- fixed: Properly report Thorchain quotes as isEstimate
Expand Down
25 changes: 15 additions & 10 deletions src/swap/defi/xrpDex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { round, sub } from 'biggystring'
import { gt, round, sub } from 'biggystring'
import { asArray, asNumber, asObject, asOptional, asString } from 'cleaners'
import {
EdgeCorePluginOptions,
Expand All @@ -7,6 +7,7 @@ import {
EdgeSwapQuote,
EdgeSwapRequest,
EdgeTxSwap,
InsufficientFundsError,
JsonObject,
SwapCurrencyError
} from 'edge-core-js/types'
Expand Down Expand Up @@ -251,6 +252,16 @@ export function makeXrpDexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const request = convertRequest(req)
const { fromCurrencyCode, fromTokenId, fromWallet, quoteFor } = request

// Get the balance of the wallet minus reserve
const maxSpendable = await fromWallet.getMaxSpendable({
currencyCode: fromCurrencyCode,
spendTargets: [
{
publicAddress: DUMMY_XRP_ADDRESS
}
]
})

let swapOrder: SwapOrder
if (quoteFor === 'max') {
request.quoteFor = 'from'
Expand All @@ -261,20 +272,14 @@ export function makeXrpDexPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin {
const quote = await makeSwapPluginQuote(swapOrder)
const swapFee = quote.networkFee.nativeAmount

// Get the balance of the wallet minus reserve
const maxSpendable = await fromWallet.getMaxSpendable({
currencyCode: fromCurrencyCode,
spendTargets: [
{
publicAddress: DUMMY_XRP_ADDRESS
}
]
})
request.nativeAmount = sub(maxSpendable, swapFee)
return await this.fetchSwapQuote(request, userSettings, opts)
}
} else {
swapOrder = await fetchSwapQuoteInner(request)
if (gt(swapOrder.fromNativeAmount, maxSpendable)) {
throw new InsufficientFundsError()
}
}
return await makeSwapPluginQuote(swapOrder)
}
Expand Down

0 comments on commit f0e1832

Please sign in to comment.