@uniswap/v4-sdk
Version:
⚒️ An SDK for building applications on top of Uniswap V4
89 lines • 3.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
const tslib_1 = require("tslib");
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
const sdk_core_1 = require("@uniswap/sdk-core");
const pathCurrency_1 = require("../utils/pathCurrency");
/**
* Represents a list of pools through which a swap can occur
* @template TInput The input currency
* @template TOutput The output currency
*/
class Route {
/**
* Creates an instance of route.
* @param pools An array of `Pool` objects, ordered by the route the swap will take
* @param input The input currency
* @param output The output currency
*/
constructor(pools, input, output) {
var _a;
this._midPrice = null;
(0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
const chainId = pools[0].chainId;
const allOnSameChain = pools.every((pool) => pool.chainId === chainId);
(0, tiny_invariant_1.default)(allOnSameChain, 'CHAIN_IDS');
/**
* function throws if pools do not involve the input and output currency or the native/wrapped equivalent
**/
this.pathInput = (0, pathCurrency_1.getPathCurrency)(input, pools[0]);
this.pathOutput = (0, pathCurrency_1.getPathCurrency)(output, pools[pools.length - 1]);
// If the input is native and the first pool is an eth-weth pool, that means we already wrapped the input to weth
// so we need to set the path input to be the wrapped input
if (pools[0].currency0.wrapped.equals(pools[0].currency1)) {
if (this.pathInput.isNative && ((_a = pools[1]) === null || _a === void 0 ? void 0 : _a.currency0.isNative)) {
this.pathInput = pools[0].currency1;
}
else if (this.pathInput.equals(pools[0].currency1) && pools[1] && !pools[1].currency0.isNative) {
this.pathInput = pools[0].currency0;
}
}
/**
* Normalizes currency0-currency1 order and selects the next currency/fee step to add to the path
* */
const currencyPath = [this.pathInput];
for (const [i, pool] of pools.entries()) {
const currentInputCurrency = currencyPath[i];
(0, tiny_invariant_1.default)(currentInputCurrency.equals(pool.currency0) || currentInputCurrency.equals(pool.currency1), 'PATH');
const nextCurrency = currentInputCurrency.equals(pool.currency0) ? pool.currency1 : pool.currency0;
currencyPath.push(nextCurrency);
}
this.pools = pools;
this.currencyPath = currencyPath;
this.input = input;
this.output = output !== null && output !== void 0 ? output : currencyPath[currencyPath.length - 1];
}
get chainId() {
return this.pools[0].chainId;
}
/**
* Returns the mid price of the route
*/
get midPrice() {
if (this._midPrice !== null)
return this._midPrice;
const price = this.pools.slice(1).reduce(({ nextInput, price }, pool) => {
return nextInput.equals(pool.currency0)
? {
nextInput: pool.currency1,
price: price.multiply(pool.currency0Price),
}
: {
nextInput: pool.currency0,
price: price.multiply(pool.currency1Price),
};
}, this.pools[0].currency0.equals(this.pathInput)
? {
nextInput: this.pools[0].currency1,
price: this.pools[0].currency0Price,
}
: {
nextInput: this.pools[0].currency0,
price: this.pools[0].currency1Price,
}).price;
return (this._midPrice = new sdk_core_1.Price(this.input, this.output, price.denominator, price.numerator));
}
}
exports.Route = Route;
//# sourceMappingURL=route.js.map