Skip to content

Commit

Permalink
Fix quoteAdjustedForGas values for low liq pools (#195)
Browse files Browse the repository at this point in the history
* Fix build errors

* Refactor buildGasModel to calculate synthetic gas cost in quote token

* fix token0 issue w/ new inputNativePool calc

* take #190

* fixed swapOptions missing (#190)

Co-authored-by: jmmshn <[email protected]>

* Pass all integ tests

* Change calculations - use execution.quote(gasCostInTermsOfInput) and compare to original

* fix unit tests, unskip integ

* pass eslint checks

* Resolve comments

* prettier

* clean up comments

* Change amountToken from tokenIn to amount.currency.wrapped

* move executionPrice into if statement

* remove excess comment

Co-authored-by: Jimmy Shen <[email protected]>
Co-authored-by: jmmshn <[email protected]>
  • Loading branch information
3 people authored Jan 23, 2023
1 parent 3ebd768 commit 4db0b29
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 85 deletions.
2 changes: 1 addition & 1 deletion cli/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export abstract class BaseCommand extends Command {
blockNumber: BigNumber,
estimatedGasUsed: BigNumber,
gasPriceWei: BigNumber,
simulationStatus: SimulationStatus | undefined
simulationStatus?: SimulationStatus
) {
this.logger.info(`Best Route:`);
this.logger.info(`${routeAmountsToString(routeAmounts)}`);
Expand Down
2 changes: 2 additions & 0 deletions cli/commands/quote-to-ratio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
parseAmount,
SwapToRatioResponse,
SwapToRatioStatus,
SwapType,
} from '../../src';
import { BaseCommand } from '../base-command';

Expand Down Expand Up @@ -118,6 +119,7 @@ export class QuoteToRatio extends BaseCommand {
recipient: '0x0000000000000000000000000000000000000001',
},
swapOptions: {
type: SwapType.SWAP_ROUTER_02,
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
Expand Down
2 changes: 1 addition & 1 deletion src/providers/caching-token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
TokenAccessor,
UNI_ARBITRUM_RINKEBY,
USDC_ARBITRUM,
USDC_ARBITRUM_RINKEBY,
USDC_ARBITRUM_GOERLI,
USDC_ARBITRUM_RINKEBY,
USDC_ETHEREUM_GNOSIS,
USDC_MAINNET,
USDC_MOONBEAM,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/multicall-uniswap-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { BaseProvider } from '@ethersproject/providers';
import _ from 'lodash';
import stats from 'stats-lite';

import { UniswapInterfaceMulticall__factory } from '../types/v3/factories/UniswapInterfaceMulticall__factory';
import { UniswapInterfaceMulticall } from '../types/v3/UniswapInterfaceMulticall';
import { UniswapInterfaceMulticall__factory } from '../types/v3/factories/UniswapInterfaceMulticall__factory';
import { ChainId } from '../util';
import { UNISWAP_MULTICALL_ADDRESSES } from '../util/addresses';
import { log } from '../util/log';
Expand Down
1 change: 1 addition & 0 deletions src/providers/on-chain-quote-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { CurrencyAmount } from '../util/amounts';
import { log } from '../util/log';
import { routeToString } from '../util/routes';

import { Result } from './multicall-provider';
import { UniswapMulticallProvider } from './multicall-uniswap-provider';
import { ProviderConfig } from './provider';
Expand Down
12 changes: 11 additions & 1 deletion src/providers/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ export const DAI_ARBITRUM_RINKEBY = new Token(
'Dai Stablecoin'
);

export const DAI_ARBITRUM_GOERLI = new Token(
ChainId.ARBITRUM_GOERLI,
'0x0000000000000000000000000000000000000000', // TODO: add address
18,
'DAI',
'Dai Stablecoin'
);

export const USDT_ARBITRUM_RINKEBY = new Token(
ChainId.ARBITRUM_RINKEBY,
'0x920b9301c2de92186299cd2abc7199e25b9728b3',
Expand All @@ -321,7 +329,7 @@ export const UNI_ARBITRUM_RINKEBY = new Token(
'Uni token'
);

// Bridged version of official Goerli USDC
// Bridged version of official Goerli USDC
export const USDC_ARBITRUM_GOERLI = new Token(
ChainId.ARBITRUM_GOERLI,
'0x8FB1E3fC51F3b789dED7557E680551d93Ea9d892',
Expand Down Expand Up @@ -635,6 +643,8 @@ export const DAI_ON = (chainId: ChainId): Token => {
return DAI_ARBITRUM;
case ChainId.ARBITRUM_RINKEBY:
return DAI_ARBITRUM_RINKEBY;
case ChainId.ARBITRUM_GOERLI:
return DAI_ARBITRUM_GOERLI;
case ChainId.POLYGON:
return DAI_POLYGON;
case ChainId.POLYGON_MUMBAI:
Expand Down
6 changes: 4 additions & 2 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,17 @@ export class AlphaRouter
chainId: this.chainId,
gasPriceWei,
v3poolProvider: this.v3PoolProvider,
token: quoteToken,
amountToken: amount.currency.wrapped,
quoteToken,
v2poolProvider: this.v2PoolProvider,
l2GasDataProvider: this.l2GasDataProvider,
}),
this.mixedRouteGasModelFactory.buildGasModel({
chainId: this.chainId,
gasPriceWei,
v3poolProvider: this.v3PoolProvider,
token: quoteToken,
amountToken: amount.currency.wrapped,
quoteToken,
v2poolProvider: this.v2PoolProvider,
}),
]);
Expand Down
1 change: 1 addition & 0 deletions src/routers/alpha-router/functions/compute-all-routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Token } from '@uniswap/sdk-core';
import { Pair } from '@uniswap/v2-sdk';
import { Pool } from '@uniswap/v3-sdk';

import { log } from '../../../util/log';
import { poolToString, routeToString } from '../../../util/routes';
import { MixedRoute, V2Route, V3Route } from '../../router';
Expand Down
2 changes: 1 addition & 1 deletion src/routers/alpha-router/functions/get-candidate-pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ export async function getMixedRouteCandidatePools({
].map((poolId) => poolId.id)
);

let V2topByTVLSortedPools = _(V2subgraphPools)
const V2topByTVLSortedPools = _(V2subgraphPools)
.filter((pool) => V2topByTVLPoolIds.has(pool.id))
.sortBy((pool) => -pool.reserveUSD)
.value();
Expand Down
6 changes: 4 additions & 2 deletions src/routers/alpha-router/gas-models/gas-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export type BuildOnChainGasModelFactoryType = {
chainId: ChainId;
gasPriceWei: BigNumber;
v3poolProvider: IV3PoolProvider;
token: Token;
amountToken: Token;
quoteToken: Token;
v2poolProvider: IV2PoolProvider;
l2GasDataProvider?:
| IL2GasDataProvider<OptimismGasData>
Expand Down Expand Up @@ -160,7 +161,8 @@ export abstract class IOnChainGasModelFactory {
chainId,
gasPriceWei,
v3poolProvider: V3poolProvider,
token,
amountToken,
quoteToken,
v2poolProvider: V2poolProvider,
l2GasDataProvider,
}: BuildOnChainGasModelFactoryType): Promise<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class MixedRouteHeuristicGasModelFactory extends IOnChainGasModelFactory
chainId,
gasPriceWei,
v3poolProvider: V3poolProvider,
token,
quoteToken,
v2poolProvider: V2poolProvider,
}: BuildOnChainGasModelFactoryType): Promise<
IGasModel<MixedRouteWithValidQuote>
Expand All @@ -70,7 +70,7 @@ export class MixedRouteHeuristicGasModelFactory extends IOnChainGasModelFactory
// of the quote token in order to produce a gas adjusted amount.
// We do return a gas use in USD however, so we still convert to usd.
const nativeCurrency = WRAPPED_NATIVE_CURRENCY[chainId]!;
if (token.equals(nativeCurrency)) {
if (quoteToken.equals(nativeCurrency)) {
const estimateGasCost = (
routeWithValidQuote: MixedRouteWithValidQuote
): {
Expand Down Expand Up @@ -109,14 +109,14 @@ export class MixedRouteHeuristicGasModelFactory extends IOnChainGasModelFactory
// If the quote token is not in the native currency, we convert the gas cost to be in terms of the quote token.
// We do this by getting the highest liquidity <quoteToken>/<nativeCurrency> pool. eg. <quoteToken>/ETH pool.
const nativeV3Pool: Pool | null = await getHighestLiquidityV3NativePool(
token,
quoteToken,
V3poolProvider
);

let nativeV2Pool: Pair | null;
if (V2poolProvider) {
/// MixedRoutes
nativeV2Pool = await getV2NativePool(token, V2poolProvider);
nativeV2Pool = await getV2NativePool(quoteToken, V2poolProvider);
}

const usdToken =
Expand All @@ -139,11 +139,11 @@ export class MixedRouteHeuristicGasModelFactory extends IOnChainGasModelFactory

if (!nativeV3Pool && !nativeV2Pool) {
log.info(
`Unable to find ${nativeCurrency.symbol} pool with the quote token, ${token.symbol} to produce gas adjusted costs. Route will not account for gas.`
`Unable to find ${nativeCurrency.symbol} pool with the quote token, ${quoteToken.symbol} to produce gas adjusted costs. Route will not account for gas.`
);
return {
gasEstimate: baseGasUse,
gasCostInToken: CurrencyAmount.fromRawAmount(token, 0),
gasCostInToken: CurrencyAmount.fromRawAmount(quoteToken, 0),
gasCostInUSD: CurrencyAmount.fromRawAmount(usdToken, 0),
};
}
Expand Down
Loading

0 comments on commit 4db0b29

Please sign in to comment.