diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fd6e05..81282317 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- added: Add Maya Protocol +- changed: Separate thorchain and thorchainda initOptions and exchangeInfo cleaner + ## 2.8.0 (2024-09-12) - added: `minReceiveAmount` passed in `EdgeSwapQuotes` for lifi and rango diff --git a/src/index.ts b/src/index.ts index e9f97930..25767289 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,8 +13,9 @@ import { make0xGaslessPlugin } from './swap/defi/0x/0xGasless' import { makeCosmosIbcPlugin } from './swap/defi/cosmosIbc' import { makeLifiPlugin } from './swap/defi/lifi' import { makeRangoPlugin } from './swap/defi/rango' -import { makeThorchainPlugin } from './swap/defi/thorchain' -import { makeThorchainDaPlugin } from './swap/defi/thorchainDa' +import { makeMayaProtocolPlugin } from './swap/defi/thorchain/mayaprotocol' +import { makeThorchainPlugin } from './swap/defi/thorchain/thorchain' +import { makeThorchainDaPlugin } from './swap/defi/thorchain/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' @@ -34,6 +35,7 @@ const plugins = { sideshift: makeSideshiftPlugin, spookySwap: makeSpookySwapPlugin, swapuz: makeSwapuzPlugin, + mayaprotocol: makeMayaProtocolPlugin, thorchain: makeThorchainPlugin, thorchainda: makeThorchainDaPlugin, tombSwap: makeTombSwapPlugin, diff --git a/src/swap/central/exolix.ts b/src/swap/central/exolix.ts index c40bd4f2..d16a9069 100644 --- a/src/swap/central/exolix.ts +++ b/src/swap/central/exolix.ts @@ -1,4 +1,4 @@ -import { div, gt, lt, mul } from 'biggystring' +import { gt, lt, mul } from 'biggystring' import { asEither, asMaybe, @@ -22,6 +22,7 @@ import { SwapCurrencyError } from 'edge-core-js/types' +import { div18 } from '../../util/biggystringplus' import { checkInvalidCodes, checkWhitelistedMainnetCodes, @@ -37,7 +38,6 @@ import { getAddress, memoType } from '../../util/utils' -import { DIVIDE_PRECISION } from '../defi/thorchain' import { asRatesResponse, EdgeSwapRequestPlugin, RatesRespose } from '../types' const pluginId = 'exolix' @@ -424,11 +424,7 @@ export function makeExolixPlugin(opts: EdgeCorePluginOptions): EdgeSwapPlugin { if (exchangeRate == null) throw new SwapCurrencyError(swapInfo, request) const usdValue = mul(exchangeAmount, exchangeRate) - const maxExchangeAmount = div( - MAX_USD_VALUE, - exchangeRate, - DIVIDE_PRECISION - ) + const maxExchangeAmount = div18(MAX_USD_VALUE, exchangeRate) const maxNativeAmount = mul(maxExchangeAmount, denomToNative) if (gt(usdValue, MAX_USD_VALUE)) { diff --git a/src/swap/defi/thorchain.ts b/src/swap/defi/thorchain/common.ts similarity index 88% rename from src/swap/defi/thorchain.ts rename to src/swap/defi/thorchain/common.ts index d6130cee..980c1f98 100644 --- a/src/swap/defi/thorchain.ts +++ b/src/swap/defi/thorchain/common.ts @@ -2,6 +2,7 @@ import { add, div, gt, mul, round, sub } from 'biggystring' import { asArray, asBoolean, + asMaybe, asNumber, asObject, asOptional, @@ -24,6 +25,7 @@ import { SwapCurrencyError } from 'edge-core-js/types' +import { div18 } from '../../../util/biggystringplus' import { checkInvalidCodes, getMaxSwappable, @@ -31,7 +33,7 @@ import { isLikeKind, makeSwapPluginQuote, SwapOrder -} from '../../util/swapHelpers' +} from '../../../util/swapHelpers' import { convertRequest, fetchInfo, @@ -40,27 +42,16 @@ import { makeQueryParams, promiseWithTimeout, QueryParams -} from '../../util/utils' -import { EdgeSwapRequestPlugin, MakeTxParams } from '../types' -import { getEvmApprovalData, getEvmTokenData } from './defiUtils' - -const pluginId = 'thorchain' -const swapInfo: EdgeSwapInfo = { - pluginId, - isDex: true, - displayName: 'Thorchain', - supportEmail: 'support@edge.app' -} +} from '../../../util/utils' +import { EdgeSwapRequestPlugin, MakeTxParams } from '../../types' +import { getEvmApprovalData, getEvmTokenData } from '../defiUtils' -export const MIDGARD_SERVERS_DEFAULT = ['https://midgard.thorchain.info'] -export const THORNODE_SERVERS_DEFAULT = ['https://thornode.ninerealms.com'] export const EXPIRATION_MS = 1000 * 60 -export const DIVIDE_PRECISION = 16 export const EXCHANGE_INFO_UPDATE_FREQ_MS = 60000 export const EVM_SEND_GAS = '80000' export const EVM_TOKEN_SEND_GAS = '80000' export const THOR_LIMIT_UNITS = '100000000' -const AFFILIATE_FEE_BASIS_DEFAULT = '50' +export const AFFILIATE_FEE_BASIS_DEFAULT = '50' const STREAMING_INTERVAL_DEFAULT = 10 const STREAMING_QUANTITY_DEFAULT = 10 const STREAMING_INTERVAL_NOSTREAM = 1 @@ -101,6 +92,15 @@ export const PER_ASSET_SPREAD_DEFAULT: AssetSpread[] = [ destTokenId: undefined, destCurrencyCode: undefined }, + { + sourcePluginId: 'dash', + volatilitySpread: 0.01, + sourceTokenId: undefined, + sourceCurrencyCode: undefined, + destPluginId: undefined, + destTokenId: undefined, + destCurrencyCode: undefined + }, { sourcePluginId: 'dogecoin', volatilitySpread: 0.01, @@ -131,11 +131,13 @@ export const INVALID_CURRENCY_CODES: InvalidCurrencyCodes = { } export const EVM_CURRENCY_CODES: { [cc: string]: boolean } = { + ARB: true, AVAX: true, BCH: false, BNB: false, BSC: true, BTC: false, + DASH: false, DOGE: false, ETC: true, ETH: true, @@ -144,26 +146,11 @@ export const EVM_CURRENCY_CODES: { [cc: string]: boolean } = { THOR: false } -// Network names that don't match parent network currency code -export const MAINNET_CODE_TRANSCRIPTION: { [cc: string]: ChainTypes } = { - avalanche: 'AVAX', - binancechain: 'BNB', - binancesmartchain: 'BSC', - bitcoin: 'BTC', - bitcoincash: 'BCH', - dogecoin: 'DOGE', - ethereum: 'ETH', - litecoin: 'LTC', - thorchainrune: 'THOR' -} - export const asInitOptions = asObject({ appId: asOptional(asString, 'edge'), affiliateFeeBasis: asOptional(asString, AFFILIATE_FEE_BASIS_DEFAULT), ninerealmsClientId: asOptional(asString, ''), - thorname: asOptional(asString, 'ej'), - thorswapApiKey: asOptional(asString), - thorswapXApiKey: asOptional(asString) + thorname: asOptional(asString, 'ej') }) export const asInboundAddresses = asArray( @@ -196,26 +183,23 @@ export const asAssetSpread = asObject({ volatilitySpread: asNumber }) -export const asExchangeInfo = asObject({ +const asExchangeInfo = asObject({ + perAssetSpread: asArray(asAssetSpread), + perAssetSpreadStreaming: asOptional(asArray(asAssetSpread)), + volatilitySpread: asNumber, + volatilitySpreadStreaming: asOptional(asNumber), + likeKindVolatilitySpread: asNumber, + likeKindVolatilitySpreadStreaming: asOptional(asNumber), + midgardServers: asArray(asString), + affiliateFeeBasis: asOptional(asString), + streamingInterval: asOptional(asNumber), + streamingQuantity: asOptional(asNumber), + thornodeServers: asOptional(asArray(asString)) +}) + +const asExchangeInfoMap = asObject({ swap: asObject({ - plugins: asObject({ - thorchain: asObject({ - perAssetSpread: asArray(asAssetSpread), - perAssetSpreadStreaming: asOptional(asArray(asAssetSpread)), - volatilitySpread: asNumber, - volatilitySpreadStreaming: asOptional(asNumber), - likeKindVolatilitySpread: asNumber, - likeKindVolatilitySpreadStreaming: asOptional(asNumber), - daVolatilitySpread: asNumber, - midgardServers: asArray(asString), - affiliateFeeBasis: asOptional(asString), - nineRealmsServers: asOptional(asArray(asString)), - streamingInterval: asOptional(asNumber), - streamingQuantity: asOptional(asNumber), - thornodeServers: asOptional(asArray(asString)), - thorSwapServers: asOptional(asArray(asString)) - }) - }) + plugins: asObject(asMaybe(asExchangeInfo)) }) }) @@ -255,10 +239,11 @@ type AssetSpread = ReturnType type Pool = ReturnType type ExchangeInfo = ReturnType interface CalcSwapParams { + swapInfo: EdgeSwapInfo log: Function fetch: EdgeFetchFunction thornodes: string[] - thornodesFetchOptions: any + thornodesFetchOptions: Record fromWallet: EdgeCurrencyWallet fromCurrencyCode: string toWallet: EdgeCurrencyWallet @@ -292,19 +277,33 @@ interface CalcSwapResponse { let exchangeInfo: ExchangeInfo | undefined let exchangeInfoLastUpdate: number = 0 -export function makeThorchainPlugin( - opts: EdgeCorePluginOptions +interface ThorchainOpts { + MAINNET_CODE_TRANSCRIPTION: { [cc: string]: string } + MIDGARD_SERVERS_DEFAULT: string[] + THORNODE_SERVERS_DEFAULT: string[] + orderUri: string + swapInfo: EdgeSwapInfo + thornodesFetchOptions?: Record +} + +export function makeThorchainBasedPlugin( + opts: EdgeCorePluginOptions, + thorchainOpts: ThorchainOpts ): EdgeSwapPlugin { const { io, log } = opts const { fetchCors = io.fetch } = io const initOptions = asInitOptions(opts.initOptions) - const { appId, thorname, ninerealmsClientId } = initOptions + const { appId, thorname } = initOptions let { affiliateFeeBasis = AFFILIATE_FEE_BASIS_DEFAULT } = initOptions - const headers = { - 'Content-Type': 'application/json', - 'x-client-id': ninerealmsClientId - } + const { + MAINNET_CODE_TRANSCRIPTION, + MIDGARD_SERVERS_DEFAULT, + THORNODE_SERVERS_DEFAULT, + orderUri, + swapInfo, + thornodesFetchOptions = {} + } = thorchainOpts const fetchSwapQuoteInner = async ( request: EdgeSwapRequestPlugin, @@ -364,7 +363,12 @@ export function makeThorchainPlugin( ) if (exchangeInfoResponse.ok === true) { - exchangeInfo = asExchangeInfo(await exchangeInfoResponse.json()) + const exchangeInfoMap = asExchangeInfoMap( + await exchangeInfoResponse.json() + ) + exchangeInfo = asExchangeInfo( + exchangeInfoMap.swap.plugins[swapInfo.pluginId] + ) exchangeInfoLastUpdate = now } else { // Error is ok. We just use defaults @@ -379,23 +383,21 @@ export function makeThorchainPlugin( } if (exchangeInfo != null) { - const { thorchain } = exchangeInfo.swap.plugins - likeKindVolatilitySpread = - exchangeInfo.swap.plugins.thorchain.likeKindVolatilitySpread - volatilitySpread = thorchain.volatilitySpread + likeKindVolatilitySpread = exchangeInfo.likeKindVolatilitySpread + volatilitySpread = exchangeInfo.volatilitySpread likeKindVolatilitySpreadStreaming = - exchangeInfo.swap.plugins.thorchain.likeKindVolatilitySpreadStreaming ?? + exchangeInfo.likeKindVolatilitySpreadStreaming ?? likeKindVolatilitySpreadStreaming volatilitySpreadStreaming = - thorchain.volatilitySpreadStreaming ?? volatilitySpreadStreaming - midgardServers = thorchain.midgardServers - thornodeServers = thorchain.thornodeServers ?? thornodeServers - perAssetSpread = thorchain.perAssetSpread + exchangeInfo.volatilitySpreadStreaming ?? volatilitySpreadStreaming + midgardServers = exchangeInfo.midgardServers + thornodeServers = exchangeInfo.thornodeServers ?? thornodeServers + perAssetSpread = exchangeInfo.perAssetSpread perAssetSpreadStreaming = - thorchain.perAssetSpreadStreaming ?? perAssetSpreadStreaming - affiliateFeeBasis = thorchain.affiliateFeeBasis ?? affiliateFeeBasis - streamingInterval = thorchain.streamingInterval ?? streamingInterval - streamingQuantity = thorchain.streamingQuantity ?? streamingQuantity + exchangeInfo.perAssetSpreadStreaming ?? perAssetSpreadStreaming + affiliateFeeBasis = exchangeInfo.affiliateFeeBasis ?? affiliateFeeBasis + streamingInterval = exchangeInfo.streamingInterval ?? streamingInterval + streamingQuantity = exchangeInfo.streamingQuantity ?? streamingQuantity } const volatilitySpreadFinal = isEstimate @@ -436,7 +438,7 @@ export function makeThorchainPlugin( fetchCors, midgardServers, 'v2/pools', - { headers } + thornodesFetchOptions ) if (!poolResponse.ok) { @@ -449,6 +451,7 @@ export function makeThorchainPlugin( const sourcePool = getPool( request, + swapInfo, fromMainnetCode, fromCurrencyCode, pools @@ -463,15 +466,22 @@ export function makeThorchainPlugin( : undefined log(`fromAsset: ${fromAsset}`) - const destPool = getPool(request, toMainnetCode, toCurrencyCode, pools) + const destPool = getPool( + request, + swapInfo, + toMainnetCode, + toCurrencyCode, + pools + ) let calcResponse: CalcSwapResponse if (quoteFor === 'from' || quoteFor === 'max') { calcResponse = await calcSwapFrom({ + swapInfo, log, fetch: fetchCors, thornodes: thornodeServers, - thornodesFetchOptions: { headers }, + thornodesFetchOptions, fromWallet, fromCurrencyCode, toWallet, @@ -491,10 +501,11 @@ export function makeThorchainPlugin( }) } else { calcResponse = await calcSwapTo({ + swapInfo, log, fetch: fetchCors, thornodes: thornodeServers, - thornodesFetchOptions: { headers }, + thornodesFetchOptions, fromWallet, fromCurrencyCode, toWallet, @@ -531,7 +542,7 @@ export function makeThorchainPlugin( const savedAction: EdgeTxActionSwap = { actionType: 'swap', swapInfo, - orderUri: 'https://track.ninerealms.com/', + orderUri, isEstimate, toAsset: { pluginId: toWallet.currencyInfo.pluginId, @@ -755,6 +766,7 @@ export function makeThorchainPlugin( const getPool = ( request: EdgeSwapRequestPlugin, + swapInfo: EdgeSwapInfo, mainnetCode: string, tokenCode: string, pools: Pool[] @@ -770,7 +782,7 @@ const getPool = ( const pool: Pool = { asset: 'THOR.RUNE', assetPrice: '1', - assetPriceUSD: div(assetPriceUSD, assetPrice, 16) + assetPriceUSD: div18(assetPriceUSD, assetPrice) } return pool } @@ -786,6 +798,7 @@ const getPool = ( } const calcSwapFrom = async ({ + swapInfo, log, fetch, thornodes, @@ -838,6 +851,7 @@ const calcSwapFrom = async ({ streaming_quantity: streamingQuantity } const bestQuote = await getBestQuote( + swapInfo, [noStreamParams, streamParams], fetch, thornodes, @@ -874,11 +888,7 @@ const calcSwapFrom = async ({ log(`toThorAmountWithSpread = limit: ${toThorAmountWithSpread}`) - const toExchangeAmount = div( - toThorAmountWithSpread, - THOR_LIMIT_UNITS, - DIVIDE_PRECISION - ) + const toExchangeAmount = div(toThorAmountWithSpread, THOR_LIMIT_UNITS) log(`toExchangeAmount: ${toExchangeAmount}`) const toNativeAmountFloat = await toWallet.denominationToNative( @@ -906,6 +916,7 @@ const calcSwapFrom = async ({ } const calcSwapTo = async ({ + swapInfo, log, fetch, thornodes, @@ -944,7 +955,7 @@ const calcSwapTo = async ({ const requestedFromExchangeAmount = mul( toExchangeAmount, - div(destPrice, sourcePrice, DIVIDE_PRECISION) + div18(destPrice, sourcePrice) ) const requestedFromThorAmount = round( @@ -969,6 +980,7 @@ const calcSwapTo = async ({ } const bestQuote = await getBestQuote( + swapInfo, [noStreamParams, streamParams], fetch, thornodes, @@ -994,7 +1006,7 @@ const calcSwapTo = async ({ // 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. - const feeRatio = div(requestedToThorAmount, toThorAmount, DIVIDE_PRECISION) + const feeRatio = div18(requestedToThorAmount, toThorAmount) log(`feeRatio: ${feeRatio}`) const fromThorAmount = mul(requestedFromThorAmount, feeRatio) @@ -1016,11 +1028,7 @@ const calcSwapTo = async ({ log(`fromThorAmountWithSpread = limit: ${fromThorAmountWithSpread}`) - const fromExchangeAmount = div( - fromThorAmountWithSpread, - THOR_LIMIT_UNITS, - DIVIDE_PRECISION - ) + const fromExchangeAmount = div18(fromThorAmountWithSpread, THOR_LIMIT_UNITS) log(`fromExchangeAmount: ${fromExchangeAmount}`) const fromNativeAmountFloat = await fromWallet.denominationToNative( @@ -1047,18 +1055,8 @@ const calcSwapTo = async ({ } } -type ChainTypes = - | 'BTC' - | 'ETH' - | 'BCH' - | 'BSC' - | 'DOGE' - | 'LTC' - | 'AVAX' - | 'BNB' - | 'THOR' - const getBestQuote = async ( + swapInfo: EdgeSwapInfo, params: QueryParams[], fetch: EdgeFetchFunction, thornodes: string[], @@ -1104,11 +1102,7 @@ const getBestQuote = async ( if (bestError == null) { throw new Error('Could not get quote') } else { - const minExchangeAmount = div( - bestError.minThorAmount, - THOR_LIMIT_UNITS, - DIVIDE_PRECISION - ) + const minExchangeAmount = div18(bestError.minThorAmount, THOR_LIMIT_UNITS) const minNativeAmount = await fromWallet.denominationToNative( minExchangeAmount, fromCurrencyCode @@ -1134,7 +1128,7 @@ const getQuote = async ( const response = await fetchWaterfall( fetch, thornodes, - `thorchain/quote/swap?${params}`, + `quote/swap?${params}`, thornodesFetchOptions ) let json @@ -1180,7 +1174,7 @@ const getQuote = async ( } export const getGasLimit = ( - chain: ChainTypes, + chain: string, tokenId: string | null ): string | undefined => { if (EVM_CURRENCY_CODES[chain]) { diff --git a/src/swap/defi/thorchain/mayaprotocol.ts b/src/swap/defi/thorchain/mayaprotocol.ts new file mode 100644 index 00000000..3653355b --- /dev/null +++ b/src/swap/defi/thorchain/mayaprotocol.ts @@ -0,0 +1,40 @@ +import { + EdgeCorePluginOptions, + EdgeSwapInfo, + EdgeSwapPlugin +} from 'edge-core-js/types' + +import { makeThorchainBasedPlugin } from './common' + +const swapInfo: EdgeSwapInfo = { + pluginId: 'mayaprotocol', + isDex: true, + displayName: 'Maya Protocol', + supportEmail: 'support@edge.app' +} +const orderUri = 'https://www.mayascan.org/tx/' + +const MIDGARD_SERVERS_DEFAULT = ['https://midgard.mayachain.info'] +const THORNODE_SERVERS_DEFAULT = ['https://mayanode.mayachain.info/mayachain'] + +// Network names that don't match parent network currency code +export const MAINNET_CODE_TRANSCRIPTION: { [cc: string]: string } = { + arbitrum: 'ARB', + bitcoin: 'BTC', + dash: 'DASH', + ethereum: 'ETH', + litecoin: 'LTC', + thorchainrune: 'THOR' +} + +export const makeMayaProtocolPlugin = ( + opts: EdgeCorePluginOptions +): EdgeSwapPlugin => { + return makeThorchainBasedPlugin(opts, { + MAINNET_CODE_TRANSCRIPTION, + MIDGARD_SERVERS_DEFAULT, + THORNODE_SERVERS_DEFAULT, + orderUri, + swapInfo + }) +} diff --git a/src/swap/defi/thorchain/thorchain.ts b/src/swap/defi/thorchain/thorchain.ts new file mode 100644 index 00000000..e8c67f32 --- /dev/null +++ b/src/swap/defi/thorchain/thorchain.ts @@ -0,0 +1,53 @@ +import { + EdgeCorePluginOptions, + EdgeSwapInfo, + EdgeSwapPlugin +} from 'edge-core-js/types' + +import { asInitOptions, makeThorchainBasedPlugin } from './common' + +const swapInfo: EdgeSwapInfo = { + pluginId: 'thorchain', + isDex: true, + displayName: 'Thorchain', + supportEmail: 'support@edge.app' +} +const orderUri = 'https://track.ninerealms.com/' + +const MIDGARD_SERVERS_DEFAULT = ['https://midgard.thorchain.info'] +export const THORNODE_SERVERS_DEFAULT = [ + 'https://thornode.ninerealms.com/thorchain' +] + +// Network names that don't match parent network currency code +export const MAINNET_CODE_TRANSCRIPTION: { [cc: string]: string } = { + avalanche: 'AVAX', + binancechain: 'BNB', + binancesmartchain: 'BSC', + bitcoin: 'BTC', + bitcoincash: 'BCH', + dogecoin: 'DOGE', + ethereum: 'ETH', + litecoin: 'LTC', + thorchainrune: 'THOR' +} + +export const makeThorchainPlugin = ( + opts: EdgeCorePluginOptions +): EdgeSwapPlugin => { + const initOptions = asInitOptions(opts.initOptions) + + const thornodesFetchOptions = { + 'Content-Type': 'application/json', + 'x-client-id': initOptions.ninerealmsClientId + } + + return makeThorchainBasedPlugin(opts, { + MAINNET_CODE_TRANSCRIPTION, + MIDGARD_SERVERS_DEFAULT, + THORNODE_SERVERS_DEFAULT, + orderUri, + swapInfo, + thornodesFetchOptions + }) +} diff --git a/src/swap/defi/thorchainDa.ts b/src/swap/defi/thorchain/thorchainDa.ts similarity index 91% rename from src/swap/defi/thorchainDa.ts rename to src/swap/defi/thorchain/thorchainDa.ts index b909456e..933eb364 100644 --- a/src/swap/defi/thorchainDa.ts +++ b/src/swap/defi/thorchain/thorchainDa.ts @@ -4,6 +4,7 @@ import { asBoolean, asEither, asNull, + asNumber, asObject, asOptional, asString, @@ -28,7 +29,7 @@ import { getMaxSwappable, makeSwapPluginQuote, SwapOrder -} from '../../util/swapHelpers' +} from '../../../util/swapHelpers' import { convertRequest, fetchInfo, @@ -36,19 +37,20 @@ import { getAddress, makeQueryParams, promiseWithTimeout -} from '../../util/utils' -import { EdgeSwapRequestPlugin } from '../types' -import { abiMap } from './abi/abiMap' -import { getEvmApprovalData, getEvmTokenData } from './defiUtils' +} from '../../../util/utils' +import { EdgeSwapRequestPlugin } from '../../types' +import { abiMap } from '../abi/abiMap' +import { getEvmApprovalData, getEvmTokenData } from '../defiUtils' import { - asExchangeInfo, + AFFILIATE_FEE_BASIS_DEFAULT, asInboundAddresses, - asInitOptions, EVM_CURRENCY_CODES, EXCHANGE_INFO_UPDATE_FREQ_MS, EXPIRATION_MS, getGasLimit, - INVALID_CURRENCY_CODES, + INVALID_CURRENCY_CODES +} from './common' +import { MAINNET_CODE_TRANSCRIPTION, THORNODE_SERVERS_DEFAULT } from './thorchain' @@ -97,6 +99,28 @@ const asThorSwapQuoteResponse = asObject({ routes: asArray(asThorSwapRoute) }) +const asExchangeInfo = asObject({ + swap: asObject({ + plugins: asObject({ + thorchainda: asObject({ + daVolatilitySpread: asOptional(asNumber), + affiliateFeeBasis: asOptional(asString), + thornodeServers: asOptional(asArray(asString)), + thorSwapServers: asOptional(asArray(asString)) + }) + }) + }) +}) + +const asInitOptions = asObject({ + appId: asOptional(asString, 'edge'), + affiliateFeeBasis: asOptional(asString, AFFILIATE_FEE_BASIS_DEFAULT), + ninerealmsClientId: asOptional(asString, ''), + thorname: asOptional(asString, 'ej'), + thorswapApiKey: asOptional(asString), + thorswapXApiKey: asOptional(asString) +}) + /** Max slippage for 5% for estimated quotes */ const DA_VOLATILITY_SPREAD_DEFAULT = 0.05 const THORSWAP_DEFAULT_SERVERS = ['https://api.thorswap.net/aggregator'] @@ -159,7 +183,7 @@ export function makeThorchainDaPlugin( const reverseQuote = quoteFor === 'to' const isEstimate = true - const daVolatilitySpread: number = DA_VOLATILITY_SPREAD_DEFAULT + let daVolatilitySpread: number = DA_VOLATILITY_SPREAD_DEFAULT let thornodeServers: string[] = THORNODE_SERVERS_DEFAULT let thorswapServers: string[] = THORSWAP_DEFAULT_SERVERS @@ -204,11 +228,11 @@ export function makeThorchainDaPlugin( } if (exchangeInfo != null) { - const { thorchain } = exchangeInfo.swap.plugins - // Uncomment line below to re-enable server override of volatility spread - // daVolatilitySpread = thorchain.daVolatilitySpread - thorswapServers = thorchain.thorSwapServers ?? THORSWAP_DEFAULT_SERVERS - thornodeServers = thorchain.thornodeServers ?? thornodeServers + const { thorchainda } = exchangeInfo.swap.plugins + daVolatilitySpread = + thorchainda.daVolatilitySpread ?? DA_VOLATILITY_SPREAD_DEFAULT + thorswapServers = thorchainda.thorSwapServers ?? THORSWAP_DEFAULT_SERVERS + thornodeServers = thorchainda.thornodeServers ?? thornodeServers } const volatilitySpreadFinal = daVolatilitySpread // Might add a likeKind spread later @@ -247,14 +271,9 @@ export function makeThorchainDaPlugin( log.warn(uri) const [iaResponse, thorSwapResponse] = await Promise.all([ - fetchWaterfall( - fetchCors, - thornodeServers, - 'thorchain/inbound_addresses', - { - headers - } - ), + fetchWaterfall(fetchCors, thornodeServers, 'inbound_addresses', { + headers + }), fetchWaterfall(fetchCors, thorswapServers, uri, { headers: thorswapHeaders }) diff --git a/test/thorchain.test.ts b/test/thorchain.test.ts index a72a571d..e59825ca 100644 --- a/test/thorchain.test.ts +++ b/test/thorchain.test.ts @@ -1,7 +1,7 @@ import { assert } from 'chai' import { describe, it } from 'mocha' -import { getVolatilitySpread } from '../src/swap/defi/thorchain' +import { getVolatilitySpread } from '../src/swap/defi/thorchain/common' describe(`getVolatilitySpread`, function () { it('bitcoin source', function () {