Skip to content

Commit

Permalink
Merge pull request #280 from EdgeApp/paul/xrpDex
Browse files Browse the repository at this point in the history
Paul/xrp dex
  • Loading branch information
paullinator authored Aug 14, 2023
2 parents 2f4aa54 + fdf49b5 commit c2e79e8
Show file tree
Hide file tree
Showing 10 changed files with 920 additions and 221 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"ethers": "^5.7.0",
"hashjs": "^1.2.0",
"iso4217": "^0.2.0",
"utf8": "^3.0.0"
"utf8": "^3.0.0",
"xrpl": "^2.10.0"
},
"devDependencies": {
"@babel/core": "^7.0.0",
Expand All @@ -75,7 +76,7 @@
"buffer": "^6.0.3",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"edge-core-js": "^0.19.43",
"edge-core-js": "^1.4.0",
"eslint": "^7.14.0",
"eslint-config-standard-kit": "0.15.1",
"eslint-plugin-flowtype": "^5.2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { makeThorchainDaPlugin } from './swap/defi/thorchainDa'
import { makeSpookySwapPlugin } from './swap/defi/uni-v2-based/plugins/spookySwap'
import { makeTombSwapPlugin } from './swap/defi/uni-v2-based/plugins/tombSwap'
import { makeVelodromePlugin } from './swap/defi/uni-v2-based/plugins/velodrome'
import { makeXrpDexPlugin } from './swap/defi/xrpDex'
import { makeExolixPlugin } from './swap/exolix'
import { makeGodexPlugin } from './swap/godex'
import { makeLetsExchangePlugin } from './swap/letsexchange'
Expand Down Expand Up @@ -52,6 +53,7 @@ const edgeCorePlugins = {
thorchainda: makeThorchainDaPlugin,
transfer: makeTransferPlugin,
velodrome: makeVelodromePlugin,
xrpdex: makeXrpDexPlugin,
letsexchange: makeLetsExchangePlugin
}

Expand Down
47 changes: 38 additions & 9 deletions src/swap-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
EdgeSwapRequest,
EdgeSwapResult,
EdgeTransaction,
EdgeTxSwap,
JsonObject,
SwapCurrencyError
} from 'edge-core-js/types'

import { MakeTxParams } from './swap/defi/xrp/xrpDexTypes'
import { EdgeSwapRequestPlugin } from './swap/types'

const likeKindAssets = [
Expand All @@ -32,10 +34,21 @@ export function ensureInFuture(
return target < date.valueOf() ? date : new Date(target)
}

export interface SwapOrder {
interface SwapOrderSpendInfo {
spendInfo: EdgeSpendInfo
}

interface SwapOrderMakeTx {
makeTxParams: MakeTxParams
}

type SwapOrderInner = SwapOrderMakeTx | SwapOrderSpendInfo

export type SwapOrder = SwapOrderInner & {
canBePartial?: boolean
maxFulfillmentSeconds?: number
request: EdgeSwapRequest
swapInfo: EdgeSwapInfo
spendInfo: EdgeSpendInfo
fromNativeAmount: string
expirationDate?: Date
preTx?: EdgeTransaction
Expand All @@ -46,21 +59,32 @@ export async function makeSwapPluginQuote(
order: SwapOrder
): Promise<EdgeSwapQuote> {
const {
canBePartial,
maxFulfillmentSeconds,
fromNativeAmount,
request,
swapInfo,
spendInfo,
expirationDate,
preTx,
metadataNotes
} = order

const { fromWallet } = request
const tx = await fromWallet.makeSpend(spendInfo)
const toNativeAmount = spendInfo.swapData?.payoutNativeAmount
const destinationAddress = spendInfo.swapData?.payoutAddress
const isEstimate = spendInfo.swapData?.isEstimate ?? false
const quoteId = spendInfo.swapData?.orderId

let tx: EdgeTransaction
let swapData: EdgeTxSwap | undefined
if ('spendInfo' in order) {
const { spendInfo } = order
swapData = spendInfo.swapData
tx = await fromWallet.makeSpend(spendInfo)
} else {
const { makeTxParams } = order
swapData = makeTxParams.swapData
tx = await fromWallet.otherMethods.makeTx(makeTxParams)
}
const toNativeAmount = swapData?.payoutNativeAmount
const destinationAddress = swapData?.payoutAddress
const isEstimate = swapData?.isEstimate ?? false
const quoteId = swapData?.orderId
if (
fromNativeAmount == null ||
toNativeAmount == null ||
Expand All @@ -81,6 +105,8 @@ export async function makeSwapPluginQuote(
)

const out: EdgeSwapQuote = {
canBePartial,
maxFulfillmentSeconds,
request,
swapInfo,
fromNativeAmount,
Expand Down Expand Up @@ -138,6 +164,9 @@ export const getMaxSwappable = async <T extends any[]>(
requestCopy.nativeAmount = balance
requestCopy.quoteFor = 'from'
const swapOrder = await fetchSwap(requestCopy, ...args)
if (!('spendInfo' in swapOrder)) {
return request
}

// Then use getMaxSpendable with the partner's address
delete swapOrder.spendInfo.spendTargets[0].nativeAmount
Expand Down
Loading

0 comments on commit c2e79e8

Please sign in to comment.