Skip to content

Commit

Permalink
Use currency from token networkLocation as-is
Browse files Browse the repository at this point in the history
There are two correct 'currency' formats for XRP https://xrpl.org/docs/references/protocol/data-types/currency-formats#currency-codes

We should use the one found in the networkLocation instead of converting or looking for it in the tokenId.
  • Loading branch information
peachbits committed Feb 25, 2025
1 parent ab65cd5 commit 3efa324
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
23 changes: 0 additions & 23 deletions src/swap/defi/xrp/xrpDexHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,6 @@ export const hexToString = (hex: string): string => {
return string
}

/**
* Converts a human readable currency code to hexadecimal.
* If the currency has 3 characters (XRP, EUR, USD...) then return immediately this currency code.
* If the currency has more than 3 characters, then encode it to the XRP ledger format.
* Example: USDM will become 5553444D00000000000000000000000000000000
*
* @param currencyCode The currency code to potentially encode to the XRP ledger format.
* @returns A {@link string}
*/
const convertCurrencyCodeToHex = (currencyCode: string): string => {
if (currencyCode.length > 3) {
return Buffer.from(currencyCode, 'ascii')
.toString('hex')
.toUpperCase()
.padEnd(NON_STANDARD_CODE_LENGTH, '0')
}
return currencyCode
}

/**
* Helper to correctly display the currency code if its length is more than 3.
* Example: 5553444D00000000000000000000000000000000 will become USDM
Expand All @@ -104,10 +85,6 @@ export const getBookOffers = async (
{ taker_gets, taker_pays, ...rest }: BookOffersRequest,
{ client, showLogs }: MethodOptions
): Promise<BookOffersResponse> => {
// Convert currencies to hex if needed
taker_gets.currency = convertCurrencyCodeToHex(taker_gets.currency)
taker_pays.currency = convertCurrencyCodeToHex(taker_pays.currency)

// Send the request
const response = await client.request({
taker_gets,
Expand Down
6 changes: 6 additions & 0 deletions src/swap/defi/xrp/xrpDexTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { asObject, asString } from 'cleaners'
import { Client, Wallet } from 'xrpl'

/**
Expand Down Expand Up @@ -29,3 +30,8 @@ export interface BookOfferCurrency {
currency: string
issuer?: string
}

export const asXrpNetworkLocation = asObject({
currency: asString,
issuer: asString
})
26 changes: 20 additions & 6 deletions src/swap/defi/xrpDex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../../util/utils'
import { EdgeSwapRequestPlugin, MakeTxParams } from '../types'
import { getBuyQuote, getSellQuote } from './xrp/xrpDexHelpers'
import { asXrpNetworkLocation } from './xrp/xrpDexTypes'

const asInitOptions = asObject({
appId: asOptional(asString, 'edge')
Expand Down Expand Up @@ -95,12 +96,25 @@ const fetchSwapQuoteInner = async (
const rippleServers: string[] = RIPPLE_SERVERS_DEFAULT
const volatilitySpread: number = VOLATILITY_SPREAD_DEFAULT

const fromIssuer = fromTokenId != null ? fromTokenId.split('-')[1] : undefined
const toIssuer = toTokenId != null ? toTokenId.split('-')[1] : undefined
const fromCurrency =
fromTokenId != null ? fromTokenId.split('-')[0] : fromCurrencyCode
const toCurrency =
toTokenId != null ? toTokenId.split('-')[0] : toCurrencyCode
let fromIssuer: string | undefined
let fromCurrency: string = fromCurrencyCode
if (fromTokenId != null) {
const fromToken = fromWallet.currencyConfig.allTokens[fromTokenId]
const fromTokenNetworkLocation = asXrpNetworkLocation(
fromToken.networkLocation
)
fromIssuer = fromTokenNetworkLocation.issuer
fromCurrency = fromTokenNetworkLocation.currency
}

let toIssuer: string | undefined
let toCurrency: string = toCurrencyCode
if (toTokenId != null) {
const toToken = toWallet.currencyConfig.allTokens[toTokenId]
const toTokenNetworkLocation = asXrpNetworkLocation(toToken.networkLocation)
toIssuer = toTokenNetworkLocation.issuer
toCurrency = toTokenNetworkLocation.currency
}

// Grab addresses:
const toAddress = await getAddress(toWallet)
Expand Down

0 comments on commit 3efa324

Please sign in to comment.