Skip to content

Commit

Permalink
Remove partial flag from thorchain and maya protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
peachbits committed Nov 7, 2024
1 parent 23d5753 commit 22112f4
Showing 1 changed file with 50 additions and 112 deletions.
162 changes: 50 additions & 112 deletions src/swap/defi/thorchain/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const asQuoteSwap = asObject({
router: asOptional(asString), // "0xD37BbE5744D730a1d98d8DC97c42F0Ca46aD7146",
// slippage_bps: asNumber, // 92,
// streaming_slippage_bps: asNumber, // 5,
streaming_swap_blocks: asNumber, // 170,
// streaming_swap_blocks: asNumber, // 170,
total_swap_seconds: asOptional(asNumber) // 1020,
})

Expand All @@ -249,21 +249,20 @@ interface CalcSwapParams {
toWallet: EdgeCurrencyWallet
toCurrencyCode: string
toAddress: string
isEstimate: boolean
// isEstimate: boolean
nativeAmount: string
quoteFor: EdgeSwapRequestPlugin['quoteFor']
sourcePool: Pool
destPool: Pool
thorname: string
volatilitySpreadFinal: string
volatilitySpreadStreamingFinal: string
// volatilitySpreadStreamingFinal: string
affiliateFeeBasis: string
streamingInterval: number
streamingQuantity: number
}

interface CalcSwapResponse {
canBePartial: boolean
fromNativeAmount: string
fromExchangeAmount: string
maxFulfillmentSeconds?: number
Expand Down Expand Up @@ -331,12 +330,12 @@ export function makeThorchainBasedPlugin(

let midgardServers: string[] = MIDGARD_SERVERS_DEFAULT
let thornodeServersWithPath: string[] = THORNODE_SERVERS_DEFAULT
let likeKindVolatilitySpread: number = LIKE_KIND_VOLATILITY_SPREAD_DEFAULT
let likeKindVolatilitySpreadStreaming: number = LIKE_KIND_VOLATILITY_SPREAD_STREAMING_DEFAULT
let volatilitySpread: number = VOLATILITY_SPREAD_DEFAULT
let volatilitySpreadStreaming: number = VOLATILITY_SPREAD_STREAMING_DEFAULT
let perAssetSpread: AssetSpread[] = PER_ASSET_SPREAD_DEFAULT
let perAssetSpreadStreaming: AssetSpread[] = PER_ASSET_SPREAD_DEFAULT
// const likeKindVolatilitySpread: number = LIKE_KIND_VOLATILITY_SPREAD_DEFAULT
// const likeKindVolatilitySpreadStreaming: number = LIKE_KIND_VOLATILITY_SPREAD_STREAMING_DEFAULT
// const volatilitySpread: number = VOLATILITY_SPREAD_DEFAULT
// const volatilitySpreadStreaming: number = VOLATILITY_SPREAD_STREAMING_DEFAULT
// const perAssetSpread: AssetSpread[] = PER_ASSET_SPREAD_DEFAULT
// const perAssetSpreadStreaming: AssetSpread[] = PER_ASSET_SPREAD_DEFAULT
let streamingInterval: number = STREAMING_INTERVAL_DEFAULT
let streamingQuantity: number = STREAMING_QUANTITY_DEFAULT

Expand Down Expand Up @@ -385,23 +384,23 @@ export function makeThorchainBasedPlugin(
}

if (infoServer.exchangeInfo != null) {
likeKindVolatilitySpread =
infoServer.exchangeInfo.likeKindVolatilitySpread
volatilitySpread = infoServer.exchangeInfo.volatilitySpread
likeKindVolatilitySpreadStreaming =
infoServer.exchangeInfo.likeKindVolatilitySpreadStreaming ??
likeKindVolatilitySpreadStreaming
volatilitySpreadStreaming =
infoServer.exchangeInfo.volatilitySpreadStreaming ??
volatilitySpreadStreaming
// likeKindVolatilitySpread =
// infoServer.exchangeInfo.likeKindVolatilitySpread
// volatilitySpread = infoServer.exchangeInfo.volatilitySpread
// likeKindVolatilitySpreadStreaming =
// infoServer.exchangeInfo.likeKindVolatilitySpreadStreaming ??
// likeKindVolatilitySpreadStreaming
// volatilitySpreadStreaming =
// infoServer.exchangeInfo.volatilitySpreadStreaming ??
// volatilitySpreadStreaming
midgardServers = infoServer.exchangeInfo.midgardServers
thornodeServersWithPath =
infoServer.exchangeInfo.thornodeServersWithPath ??
thornodeServersWithPath
perAssetSpread = infoServer.exchangeInfo.perAssetSpread
perAssetSpreadStreaming =
infoServer.exchangeInfo.perAssetSpreadStreaming ??
perAssetSpreadStreaming
// perAssetSpread = infoServer.exchangeInfo.perAssetSpread
// perAssetSpreadStreaming =
// infoServer.exchangeInfo.perAssetSpreadStreaming ??
// perAssetSpreadStreaming
affiliateFeeBasis =
infoServer.exchangeInfo.affiliateFeeBasis ?? affiliateFeeBasis
streamingInterval =
Expand All @@ -410,38 +409,14 @@ export function makeThorchainBasedPlugin(
infoServer.exchangeInfo.streamingQuantity ?? streamingQuantity
}

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

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

log.warn(`volatilitySpreadFinal: ${volatilitySpreadFinal.toString()}`)
log.warn(
`volatilitySpreadStreamingFinal: ${volatilitySpreadStreamingFinal.toString()}`
)
// log.warn(`volatilitySpreadFinal: ${volatilitySpreadFinal.toString()}`)
// log.warn(
// `volatilitySpreadStreamingFinal: ${volatilitySpreadStreamingFinal.toString()}`
// )

// Get current pool
const poolResponse = await fetchWaterfall(
Expand Down Expand Up @@ -497,14 +472,14 @@ export function makeThorchainBasedPlugin(
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
// isEstimate,
nativeAmount,
quoteFor,
sourcePool,
destPool,
thorname,
volatilitySpreadFinal,
volatilitySpreadStreamingFinal,
// volatilitySpreadStreamingFinal,
affiliateFeeBasis,
streamingInterval,
streamingQuantity
Expand All @@ -521,21 +496,20 @@ export function makeThorchainBasedPlugin(
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
// isEstimate,
nativeAmount,
quoteFor,
sourcePool,
destPool,
thorname,
volatilitySpreadFinal,
volatilitySpreadStreamingFinal,
// volatilitySpreadStreamingFinal,
affiliateFeeBasis,
streamingInterval,
streamingQuantity
})
}
const {
canBePartial,
fromNativeAmount,
maxFulfillmentSeconds,
toNativeAmount,
Expand Down Expand Up @@ -638,7 +612,7 @@ export function makeThorchainBasedPlugin(

return {
addTxidToOrderUri: true,
canBePartial,
canBePartial: false,
maxFulfillmentSeconds,
request,
makeTxParams,
Expand Down Expand Up @@ -733,7 +707,7 @@ export function makeThorchainBasedPlugin(

return {
addTxidToOrderUri: true,
canBePartial,
canBePartial: false,
maxFulfillmentSeconds,
request,
spendInfo,
Expand Down Expand Up @@ -818,14 +792,14 @@ const calcSwapFrom = async ({
toWallet,
toCurrencyCode,
toAddress,
isEstimate,
// isEstimate,
nativeAmount,
quoteFor,
sourcePool,
destPool,
thorname,
volatilitySpreadFinal,
volatilitySpreadStreamingFinal,
// volatilitySpreadStreamingFinal,
affiliateFeeBasis,
streamingInterval,
streamingQuantity
Expand Down Expand Up @@ -872,29 +846,17 @@ const calcSwapFrom = async ({

const {
inbound_address: thorAddress,
memo: preMemo,
memo,
router,
streaming_swap_blocks: streamingSwapBlocks,
total_swap_seconds: maxFulfillmentSeconds
} = bestQuote
const toThorAmount = getExpectedAmount(bestQuote)

const canBePartial = !isEstimate || streamingSwapBlocks > 1

let toThorAmountWithSpread: string
if (canBePartial) {
log(`volatilitySpreadStreamingFinal: ${volatilitySpreadStreamingFinal}`)
toThorAmountWithSpread = round(
mul(sub('1', volatilitySpreadStreamingFinal), toThorAmount),
0
)
} else {
log(`volatilitySpreadFinal: ${volatilitySpreadFinal}`)
toThorAmountWithSpread = round(
mul(sub('1', volatilitySpreadFinal), toThorAmount),
0
)
}
log(`volatilitySpreadFinal: ${volatilitySpreadFinal}`)
const toThorAmountWithSpread = round(
mul(sub('1', volatilitySpreadFinal), toThorAmount),
0
)

log(`toThorAmountWithSpread = limit: ${toThorAmountWithSpread}`)

Expand All @@ -908,12 +870,7 @@ const calcSwapFrom = async ({
const toNativeAmount = round(toNativeAmountFloat, 0)
log(`toNativeAmount: ${toNativeAmount}`)

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

return {
canBePartial,
fromNativeAmount,
fromExchangeAmount,
maxFulfillmentSeconds,
Expand All @@ -937,12 +894,12 @@ const calcSwapTo = async ({
toCurrencyCode,
toAddress,
nativeAmount,
isEstimate,
// isEstimate,
sourcePool,
destPool,
thorname,
volatilitySpreadFinal,
volatilitySpreadStreamingFinal,
// volatilitySpreadStreamingFinal,
affiliateFeeBasis,
streamingInterval,
streamingQuantity
Expand Down Expand Up @@ -1001,18 +958,13 @@ const calcSwapTo = async ({

const {
inbound_address: thorAddress,
memo: preMemo,
memo,
router,
streaming_swap_blocks: streamingSwapBlocks,
total_swap_seconds: maxFulfillmentSeconds
} = bestQuote

const toThorAmount = getExpectedAmount(bestQuote)

// If we get a streaming quote, this should be considered a fully executing
// transaction since we don't put a slippage limit
const canBePartial = !isEstimate || streamingSwapBlocks > 1

// Get the percent drop from the 'to' amount the user wanted compared to the
// 'to' amount returned by the API. Add that percent to the 'from' amount to
// estimate how much more the user has to send.
Expand All @@ -1021,20 +973,11 @@ const calcSwapTo = async ({

const fromThorAmount = mul(requestedFromThorAmount, feeRatio)

let fromThorAmountWithSpread: string
if (canBePartial) {
log(`volatilitySpreadStreamingFinal: ${volatilitySpreadStreamingFinal}`)
fromThorAmountWithSpread = round(
mul(add('1', volatilitySpreadStreamingFinal), fromThorAmount),
0
)
} else {
log(`volatilitySpreadFinal: ${volatilitySpreadFinal}`)
fromThorAmountWithSpread = round(
mul(add('1', volatilitySpreadFinal), fromThorAmount),
0
)
}
log(`volatilitySpreadFinal: ${volatilitySpreadFinal}`)
const fromThorAmountWithSpread = round(
mul(add('1', volatilitySpreadFinal), fromThorAmount),
0
)

log(`fromThorAmountWithSpread = limit: ${fromThorAmountWithSpread}`)

Expand All @@ -1048,12 +991,7 @@ const calcSwapTo = async ({
const fromNativeAmount = round(fromNativeAmountFloat, 0)
log(`fromNativeAmount: ${fromNativeAmount}`)

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

return {
canBePartial,
fromNativeAmount,
fromExchangeAmount,
toNativeAmount,
Expand Down

0 comments on commit 22112f4

Please sign in to comment.