UNPKG

@yoroi/swap

Version:
327 lines (326 loc) 10.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformersMaker = exports.toSwapProtocol = exports.fromSwapProtocol = exports.MuesliswapProtocols = void 0; var _types = require("@yoroi/types"); var _types2 = require("./types"); const transformersMaker = ({ primaryTokenInfo, address, partner }) => { return { tokens: { response: res => res.map(({ ticker, name, policyId, hexName, decimals, verified }) => { const id = `${policyId}.${hexName}`; const isPrimary = id === primaryTokenInfo.id; if (isPrimary) return primaryTokenInfo; if (decimals === null) return null; return { status: verified ? _types.Portfolio.Token.Status.Valid : _types.Portfolio.Token.Status.Invalid, id, ticker, name, type: _types.Portfolio.Token.Type.FT, nature: _types.Portfolio.Token.Nature.Secondary, application: _types.Portfolio.Token.Application.General, fingerprint: '', decimals, description: '', originalImage: '', symbol: '', reference: '', tag: '', website: '' }; }).filter(v => !!v) }, ordersHistory: { response: ({ orders }) => orders.map(({ dex, outputIdx, fromToken, toToken, placedAt, finalizedAt, receivedAmount, toAmount, fromAmount, txHash, finalizedTxHash, status }) => ({ status, txHash, aggregator: _types.Swap.Aggregator.Muesliswap, outputIndex: outputIdx, tokenIn: fromToken, tokenOut: toToken, updateTxHash: finalizedTxHash ?? txHash, placedAt: placedAt ? placedAt * 1000 : undefined, lastUpdate: finalizedAt ? finalizedAt * 1000 : undefined, amountIn: Number(fromAmount), actualAmountOut: Number(receivedAmount), expectedAmountOut: Number(toAmount), protocol: toSwapProtocol(dex) })) }, cancel: { request: ({ order }) => ({ order_ids: [`${order.txHash}#${order.outputIndex ?? 0}`] }), response: ({ tx_cbor }) => ({ cbor: tx_cbor }) }, limitQuote: { request: ({ amountIn = 0, wantedPrice = 0, protocol, tokenIn, tokenOut }) => ({ numbers_have_decimals: true, sell_token: tokenIn, buy_token: tokenOut, sell_amount: String(amountIn), buy_amount: String(amountIn * wantedPrice), order_contract: protocol ? fromSwapProtocol(protocol) : undefined }) }, quote: { request: ({ protocol, blockedProtocols, tokenIn, tokenOut, amountIn, amountOut, slippage }) => ({ numbers_have_decimals: true, sell_token: tokenIn, buy_token: tokenOut, ...(amountOut !== undefined && { buy_amount: String(amountOut) }), ...(amountIn !== undefined && { sell_amount: String(amountIn) }), ...(partner !== undefined && { partner }), // muesli expects slippage as a percentage slippage: slippage / 100, excluded_sources: protocol ? Object.values(_types2.Dex).filter(dex => dex !== _types2.Dex.Unsupported && dex !== fromSwapProtocol(protocol)) : blockedProtocols?.map(fromSwapProtocol) ?? [] }), response: ({ buy_token_decimals, sell_token_decimals, net_price, net_price_impact, splits, total_batcher_fee, total_deposit, total_input, service_fee, total_output, total_output_without_slippage }) => ({ aggregatorFee: 0, frontendFee: 0, netPrice: net_price * 10 ** (sell_token_decimals - buy_token_decimals), priceImpact: net_price_impact, batcherFee: Number(total_batcher_fee), deposits: Number(total_deposit), totalFee: Number((Number(total_batcher_fee) + Number(service_fee)).toFixed(primaryTokenInfo.decimals)), totalInput: Number(total_input), totalOutput: Number(total_output), totalOutputWithoutSlippage: total_output_without_slippage === undefined ? undefined : Number(total_output_without_slippage), splits: splits.map(toSwapSplit) }) }, create: { request: ({ slippage = 0, protocol, blockedProtocols, tokenIn, tokenOut, amountIn, inputs }) => ({ numbers_have_decimals: true, sell_token: tokenIn, buy_token: tokenOut, sell_amount: String(amountIn), user_address: address, ...(partner !== undefined && { partner }), slippage: slippage / 100, excluded_sources: protocol ? Object.values(_types2.Dex).filter(dex => dex !== _types2.Dex.Unsupported && dex !== fromSwapProtocol(protocol)) : blockedProtocols?.map(fromSwapProtocol) ?? [], utxos: inputs }), response: ({ quote: { buy_token_decimals, sell_token_decimals, net_price, net_price_impact, splits, service_fee, total_batcher_fee, total_deposit, total_input, total_output, total_output_without_slippage }, tx_cbor }) => ({ aggregator: _types.Swap.Aggregator.Muesliswap, aggregatorFee: 0, frontendFee: Number(service_fee), cbor: tx_cbor, netPrice: net_price * 10 ** (sell_token_decimals - buy_token_decimals), priceImpact: net_price_impact, batcherFee: Number(total_batcher_fee), deposits: Number(total_deposit), totalFee: Number((Number(total_batcher_fee) + Number(service_fee)).toFixed(primaryTokenInfo.decimals)), totalInput: Number(total_input), totalOutput: Number(total_output), totalOutputWithoutSlippage: Number(total_output_without_slippage), splits: splits.map(toSwapSplit) }) }, createLimit: { request: ({ protocol = _types.Swap.Protocol.Unsupported, wantedPrice = 0, tokenIn, tokenOut, amountIn, inputs }) => ({ order_contract: fromSwapProtocol(protocol), buy_amount: String(amountIn * wantedPrice), numbers_have_decimals: true, sell_token: tokenIn, buy_token: tokenOut, sell_amount: String(amountIn), user_address: address, utxos: inputs }), response: ({ quote: { buy_token_decimals, sell_token_decimals, net_price, net_price_impact, splits, service_fee, total_batcher_fee, total_deposit, total_input, total_output, total_output_without_slippage }, tx_cbor }) => ({ cbor: tx_cbor, aggregator: _types.Swap.Aggregator.Muesliswap, aggregatorFee: 0, frontendFee: Number(service_fee), netPrice: net_price * 10 ** (sell_token_decimals - buy_token_decimals), priceImpact: net_price_impact, batcherFee: Number(total_batcher_fee), deposits: Number(total_deposit), totalFee: Number((Number(total_batcher_fee) + Number(service_fee)).toFixed(primaryTokenInfo.decimals)), totalInput: Number(total_input), totalOutput: Number(total_output), totalOutputWithoutSlippage: Number(total_output_without_slippage), splits: splits.map(toSwapSplit) }) } }; }; exports.transformersMaker = transformersMaker; const toSwapSplit = ({ amount_in, batcher_fee, deposit, dex, expected_output, expected_output_without_slippage, final_price, initial_price, pool_fee, price_impact, price_distortion, source_id }) => ({ fee: pool_fee, finalPrice: final_price, initialPrice: initial_price, poolFee: pool_fee, poolId: source_id, priceDistortion: price_distortion, priceImpact: price_impact, amountIn: Number(amount_in), batcherFee: Number(batcher_fee), deposits: Number(deposit), expectedOutput: Number(expected_output), expectedOutputWithoutSlippage: Number(expected_output_without_slippage ?? expected_output), protocol: toSwapProtocol(dex) }); const toSwapProtocol = dex => ({ [_types2.Dex.Minswap_v1]: _types.Swap.Protocol.Minswap_v1, [_types2.Dex.Minswap_v2]: _types.Swap.Protocol.Minswap_v2, [_types2.Dex.Minswap_stable]: _types.Swap.Protocol.Minswap_stable, [_types2.Dex.Wingriders_v1]: _types.Swap.Protocol.Wingriders_v1, [_types2.Dex.Wingriders_v2]: _types.Swap.Protocol.Wingriders_v2, [_types2.Dex.Vyfi_v1]: _types.Swap.Protocol.Vyfi_v1, [_types2.Dex.Sundaeswap_v1]: _types.Swap.Protocol.Sundaeswap_v1, [_types2.Dex.Sundaeswap_v3]: _types.Swap.Protocol.Sundaeswap_v3, [_types2.Dex.Muesliswap_v2]: _types.Swap.Protocol.Muesliswap_v2, [_types2.Dex.Muesliswap_clp]: _types.Swap.Protocol.Muesliswap_clp, [_types2.Dex.Spectrum_v1]: _types.Swap.Protocol.Spectrum_v1, [_types2.Dex.Teddy_v1]: _types.Swap.Protocol.Teddy_v1, [_types2.Dex.Unsupported]: _types.Swap.Protocol.Unsupported })[dex] ?? _types.Swap.Protocol.Unsupported; exports.toSwapProtocol = toSwapProtocol; const fromSwapProtocol = dex => ({ [_types.Swap.Protocol.Cswap]: _types2.Dex.Unsupported, [_types.Swap.Protocol.Minswap_v1]: _types2.Dex.Minswap_v1, [_types.Swap.Protocol.Minswap_v2]: _types2.Dex.Minswap_v2, [_types.Swap.Protocol.Minswap_stable]: _types2.Dex.Minswap_stable, [_types.Swap.Protocol.Wingriders_v1]: _types2.Dex.Wingriders_v1, [_types.Swap.Protocol.Wingriders_v2]: _types2.Dex.Wingriders_v2, [_types.Swap.Protocol.Vyfi_v1]: _types2.Dex.Vyfi_v1, [_types.Swap.Protocol.Sundaeswap_v1]: _types2.Dex.Sundaeswap_v1, [_types.Swap.Protocol.Sundaeswap_v3]: _types2.Dex.Sundaeswap_v3, [_types.Swap.Protocol.Splash_v1]: _types2.Dex.Unsupported, [_types.Swap.Protocol.Teddy_v1]: _types2.Dex.Teddy_v1, [_types.Swap.Protocol.Muesliswap_v2]: _types2.Dex.Muesliswap_v2, [_types.Swap.Protocol.Muesliswap_clp]: _types2.Dex.Muesliswap_clp, [_types.Swap.Protocol.Spectrum_v1]: _types2.Dex.Spectrum_v1, [_types.Swap.Protocol.Unsupported]: _types2.Dex.Unsupported })[dex] ?? _types2.Dex.Unsupported; exports.fromSwapProtocol = fromSwapProtocol; const MuesliswapProtocols = exports.MuesliswapProtocols = Object.values(_types2.Dex).filter(p => p !== _types2.Dex.Unsupported).map(toSwapProtocol); //# sourceMappingURL=transformers.js.map