Skip to content

Commit

Permalink
Make Thorchain give estimate quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Oct 19, 2023
1 parent 15b54a1 commit 3a4d8d7
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions src/swap/defi/thorchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ interface CalcSwapParams {
toWallet: EdgeCurrencyWallet
toCurrencyCode: string
toAddress: string
isEstimate: boolean
nativeAmount: string
minAmount: MinAmount | undefined
sourcePool: Pool
Expand Down Expand Up @@ -300,7 +301,8 @@ export function makeThorchainPlugin(
}

const fetchSwapQuoteInner = async (
request: EdgeSwapRequestPlugin
request: EdgeSwapRequestPlugin,
isEstimate: boolean
): Promise<SwapOrder> => {
const {
fromCurrencyCode,
Expand Down Expand Up @@ -390,29 +392,33 @@ export function makeThorchainPlugin(
streamingQuantity = thorchain.streamingQuantity ?? streamingQuantity
}

const volatilitySpreadFinal = getVolatilitySpread({
fromPluginId: fromWallet.currencyInfo.pluginId,
fromTokenId,
fromCurrencyCode,
toPluginId: toWallet.currencyInfo.pluginId,
toTokenId,
toCurrencyCode,
likeKindVolatilitySpread,
volatilitySpread,
perAssetSpread
})
const volatilitySpreadFinal = isEstimate
? '0'
: getVolatilitySpread({
fromPluginId: fromWallet.currencyInfo.pluginId,
fromTokenId,
fromCurrencyCode,
toPluginId: toWallet.currencyInfo.pluginId,
toTokenId,
toCurrencyCode,
likeKindVolatilitySpread,
volatilitySpread,
perAssetSpread
})

const volatilitySpreadStreamingFinal = getVolatilitySpread({
fromPluginId: fromWallet.currencyInfo.pluginId,
fromTokenId,
fromCurrencyCode,
toPluginId: toWallet.currencyInfo.pluginId,
toTokenId,
toCurrencyCode,
likeKindVolatilitySpread: likeKindVolatilitySpreadStreaming,
volatilitySpread: volatilitySpreadStreaming,
perAssetSpread: perAssetSpreadStreaming
})
const volatilitySpreadStreamingFinal = isEstimate
? '0'
: getVolatilitySpread({
fromPluginId: fromWallet.currencyInfo.pluginId,
fromTokenId,
fromCurrencyCode,
toPluginId: toWallet.currencyInfo.pluginId,
toTokenId,
toCurrencyCode,
likeKindVolatilitySpread: likeKindVolatilitySpreadStreaming,
volatilitySpread: volatilitySpreadStreaming,
perAssetSpread: perAssetSpreadStreaming
})

log.warn(`volatilitySpreadFinal: ${volatilitySpreadFinal.toString()}`)
log.warn(
Expand Down Expand Up @@ -476,6 +482,7 @@ export function makeThorchainPlugin(
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
nativeAmount,
minAmount,
sourcePool,
Expand All @@ -498,6 +505,7 @@ export function makeThorchainPlugin(
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
nativeAmount,
minAmount,
sourcePool,
Expand Down Expand Up @@ -634,8 +642,12 @@ export function makeThorchainPlugin(
async fetchSwapQuote(req: EdgeSwapRequest): Promise<EdgeSwapQuote> {
const request = convertRequest(req)

const newRequest = await getMaxSwappable(fetchSwapQuoteInner, request)
const swapOrder = await fetchSwapQuoteInner(newRequest)
const newRequest = await getMaxSwappable(
fetchSwapQuoteInner,
request,
true
)
const swapOrder = await fetchSwapQuoteInner(newRequest, true)
return await makeSwapPluginQuote(swapOrder)
}
}
Expand All @@ -652,6 +664,7 @@ const calcSwapFrom = async ({
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
nativeAmount,
minAmount,
sourcePool,
Expand Down Expand Up @@ -770,7 +783,9 @@ const calcSwapFrom = async ({
const toNativeAmount = round(toNativeAmountFloat, 0)
log(`toNativeAmount: ${toNativeAmount}`)

const memo = preMemo.replace(':0/', `:${toThorAmountWithSpread}/`)
const memo = isEstimate
? preMemo
: preMemo.replace(':0/', `:${toThorAmountWithSpread}/`)

return {
canBePartial,
Expand All @@ -796,7 +811,7 @@ const calcSwapTo = async ({
toCurrencyCode,
toAddress,
nativeAmount,
minAmount,
isEstimate,
sourcePool,
destPool,
thorname,
Expand Down Expand Up @@ -906,7 +921,9 @@ const calcSwapTo = async ({
const fromNativeAmount = round(fromNativeAmountFloat, 0)
log(`fromNativeAmount: ${fromNativeAmount}`)

const memo = preMemo.replace(':0/', `:${requestedToThorAmount}/`)
const memo = isEstimate
? preMemo
: preMemo.replace(':0/', `:${requestedToThorAmount}/`)

return {
canBePartial,
Expand Down

0 comments on commit 3a4d8d7

Please sign in to comment.