Skip to content

Commit

Permalink
Additional logging for simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
willpote committed Nov 22, 2022
1 parent 8b668a9 commit 08914e1
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 2,958 deletions.
36 changes: 33 additions & 3 deletions cli/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import bunyan, { default as Logger } from 'bunyan';
import bunyanDebugStream from 'bunyan-debug-stream';
import _ from 'lodash';
import NodeCache from 'node-cache';

import {
AlphaRouter,
CachingGasStationProvider,
CachingTokenListProvider,
CachingTokenProviderWithFallback,
ChainId,
CachingV3PoolProvider,
CHAIN_IDS_LIST,
ChainId,
EIP1559GasPriceProvider,
FallbackTenderlySimulator,
GasPrice,
ID_TO_CHAIN_ID,
ID_TO_PROVIDER,
ID_TO_NETWORK_NAME,
ID_TO_PROVIDER,
IRouter,
ISwapToRatio,
ITokenProvider,
Expand All @@ -34,8 +37,10 @@ import {
RouteWithValidQuote,
setGlobalLogger,
setGlobalMetric,
SimulationStatus,
TokenProvider,
UniswapMulticallProvider,
V2PoolProvider,
V3PoolProvider,
V3RouteWithValidQuote,
} from '../src';
Expand Down Expand Up @@ -272,6 +277,27 @@ export abstract class BaseCommand extends Command {
// const useDefaultQuoteProvider =
// chainId != ChainId.ARBITRUM_ONE && chainId != ChainId.ARBITRUM_RINKEBY;

const v3PoolProvider = new CachingV3PoolProvider(
ChainId.MAINNET,
new V3PoolProvider(ChainId.MAINNET, multicall2Provider),
new NodeJSCache(new NodeCache({ stdTTL: 360, useClones: false }))
);
const v2PoolProvider = new V2PoolProvider(
ChainId.MAINNET,
multicall2Provider
);

const simulator = new FallbackTenderlySimulator(
ChainId.MAINNET,
process.env.TENDERLY_BASE_URL!,
process.env.TENDERLY_USER!,
process.env.TENDERLY_PROJECT!,
process.env.TENDERLY_ACCESS_KEY!,
provider,
v2PoolProvider,
v3PoolProvider
);

const router = new AlphaRouter({
provider,
chainId,
Expand All @@ -285,6 +311,7 @@ export abstract class BaseCommand extends Command {
),
gasPriceCache
),
simulator
});

this._swapToRatioRouter = router;
Expand All @@ -301,7 +328,9 @@ export abstract class BaseCommand extends Command {
methodParameters: MethodParameters | undefined,
blockNumber: BigNumber,
estimatedGasUsed: BigNumber,
gasPriceWei: BigNumber
gasPriceWei: BigNumber,
simulationStatus: SimulationStatus

) {
this.logger.info(`Best Route:`);
this.logger.info(`${routeAmountsToString(routeAmounts)}`);
Expand Down Expand Up @@ -333,6 +362,7 @@ export abstract class BaseCommand extends Command {
blockNumber: blockNumber.toString(),
estimatedGasUsed: estimatedGasUsed.toString(),
gasPriceWei: gasPriceWei.toString(),
simulationStatus: simulationStatus
});

const v3Routes: V3RouteWithValidQuote[] =
Expand Down
15 changes: 12 additions & 3 deletions cli/commands/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { Protocol } from '@uniswap/router-sdk';
import { Currency, Percent, TradeType } from '@uniswap/sdk-core';
import dotenv from 'dotenv';
import _ from 'lodash';

import {
ID_TO_CHAIN_ID,
nativeOnChain,
parseAmount,
SwapRoute,
SwapType,
} from '../../src';
import { NATIVE_NAMES_BY_ID, TO_PROTOCOL } from '../../src/util';
import { BaseCommand } from '../base-command';
Expand Down Expand Up @@ -37,6 +39,7 @@ export class Quote extends BaseCommand {
required: false,
default: false,
}),
simulate: flags.boolean({ required: false, default: false }),
};

async run() {
Expand Down Expand Up @@ -64,6 +67,7 @@ export class Quote extends BaseCommand {
protocols: protocolsStr,
forceCrossProtocol,
forceMixedRoutes,
simulate,
} = flags;

if ((exactIn && exactOut) || (!exactIn && !exactOut)) {
Expand Down Expand Up @@ -113,9 +117,11 @@ export class Quote extends BaseCommand {
TradeType.EXACT_INPUT,
recipient
? {
deadline: 100,
type: SwapType.UNIVERSAL_ROUTER,
deadlineOrPreviousBlockhash: 10000000000000,
recipient,
slippageTolerance: new Percent(5, 10_000),
slippageTolerance: new Percent(5, 100),
simulate: simulate ? { fromAddress: recipient } : undefined,
}
: undefined,
{
Expand Down Expand Up @@ -146,6 +152,7 @@ export class Quote extends BaseCommand {
TradeType.EXACT_OUTPUT,
recipient
? {
type: SwapType.SWAP_ROUTER_02,
deadline: 100,
recipient,
slippageTolerance: new Percent(5, 10_000),
Expand Down Expand Up @@ -192,6 +199,7 @@ export class Quote extends BaseCommand {
quote,
quoteGasAdjusted,
route: routeAmounts,
simulationStatus,
} = swapRoutes;

this.logSwapResults(
Expand All @@ -203,7 +211,8 @@ export class Quote extends BaseCommand {
methodParameters,
blockNumber,
estimatedGasUsed,
gasPriceWei
gasPriceWei,
simulationStatus
);
}
}
Loading

0 comments on commit 08914e1

Please sign in to comment.