Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Make integration tests more extensible (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric authored Feb 1, 2024
1 parent 3860278 commit 8f894ca
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 506 deletions.
7 changes: 5 additions & 2 deletions lib/entities/context/DutchQuoteContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ export class DutchQuoteContext implements QuoteContext {
}
return true;
}

// large as defined by >= $10k USD
isLargeOrder(log: Logger, classicQuote: Quote): boolean {
// gasUseEstimateUSD on other chains seem to be unreliable
if (classicQuote.request.info.tokenInChainId !== ChainId.MAINNET || classicQuote.request.info.tokenOutChainId !== ChainId.MAINNET) {
if (
classicQuote.request.info.tokenInChainId !== ChainId.MAINNET ||
classicQuote.request.info.tokenOutChainId !== ChainId.MAINNET
) {
return false;
}
const quoteSizeEstimateUSD = getQuoteSizeEstimateUSD(classicQuote);
Expand Down
16 changes: 8 additions & 8 deletions lib/entities/quote/ClassicQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export class ClassicQuote implements IQuote {

public get amountOutGasAndPortionAdjusted(): BigNumber {
return this.request.info.type === TradeType.EXACT_INPUT
// there's a possibility that quoteGasAndPortionAdjusted doesn't get populated if the flag is off
// in that case fallback to existing quoteGasAdjusted.
// undefined will cause the request to fail due to BigNumber.from(undefined)
? BigNumber.from(this.quoteData.quoteGasAndPortionAdjusted ?? this.quoteData.quoteGasAdjusted)
? // there's a possibility that quoteGasAndPortionAdjusted doesn't get populated if the flag is off
// in that case fallback to existing quoteGasAdjusted.
// undefined will cause the request to fail due to BigNumber.from(undefined)
BigNumber.from(this.quoteData.quoteGasAndPortionAdjusted ?? this.quoteData.quoteGasAdjusted)
: BigNumber.from(this.quoteData.amount);
}

Expand All @@ -186,10 +186,10 @@ export class ClassicQuote implements IQuote {

public get amountInGasAndPortionAdjusted(): BigNumber {
return this.request.info.type === TradeType.EXACT_OUTPUT
// there's a possibility that quoteGasAndPortionAdjusted doesn't get populated if the flag is off
// in that case fallback to existing quoteGasAdjusted.
// undefined will cause the request to fail due to BigNumber.from(undefined)
? BigNumber.from(this.quoteData.quoteGasAndPortionAdjusted ?? this.quoteData.quoteGasAdjusted)
? // there's a possibility that quoteGasAndPortionAdjusted doesn't get populated if the flag is off
// in that case fallback to existing quoteGasAdjusted.
// undefined will cause the request to fail due to BigNumber.from(undefined)
BigNumber.from(this.quoteData.quoteGasAndPortionAdjusted ?? this.quoteData.quoteGasAdjusted)
: BigNumber.from(this.quoteData.amount);
}

Expand Down
24 changes: 17 additions & 7 deletions lib/entities/quote/DutchQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
UNISWAPX_BASE_GAS,
WETH_UNWRAP_GAS,
WETH_WRAP_GAS,
WETH_WRAP_GAS_ALREADY_APPROVED
WETH_WRAP_GAS_ALREADY_APPROVED,
} from '../../constants';
import { Portion } from '../../fetchers/PortionFetcher';
import { log } from '../../util/log';
Expand All @@ -27,7 +27,7 @@ import { LogJSON } from './index';

export type DutchQuoteDerived = {
largeTrade: boolean;
}
};

export type DutchQuoteDataJSON = {
orderInfo: DutchOrderInfoJSON;
Expand Down Expand Up @@ -250,8 +250,12 @@ export class DutchQuote implements IQuote {
permitData: this.getPermitData(),
// NOTE: important for URA to return 0 bps and amount, in case of no portion.
// this is FE requirement
portionBips: frontendAndUraEnablePortion(this.request.info.sendPortionEnabled) ? this.portionBips ?? 0 : undefined,
portionAmount: frontendAndUraEnablePortion(this.request.info.sendPortionEnabled) ? this.portionAmountOutStart.toString() ?? '0' : undefined,
portionBips: frontendAndUraEnablePortion(this.request.info.sendPortionEnabled)
? this.portionBips ?? 0
: undefined,
portionAmount: frontendAndUraEnablePortion(this.request.info.sendPortionEnabled)
? this.portionAmountOutStart.toString() ?? '0'
: undefined,
portionRecipient: this.portionRecipient,
};
}
Expand All @@ -273,7 +277,11 @@ export class DutchQuote implements IQuote {
endAmount: this.amountInEnd,
});

if (this.portionRecipient && this.portionBips && frontendAndUraEnablePortion(this.request.info.sendPortionEnabled)) {
if (
this.portionRecipient &&
this.portionBips &&
frontendAndUraEnablePortion(this.request.info.sendPortionEnabled)
) {
if (this.request.info.type === TradeType.EXACT_INPUT) {
// Amount to swapper
builder.output({
Expand Down Expand Up @@ -329,9 +337,11 @@ export class DutchQuote implements IQuote {
endAmountIn: this.amountInEnd.toString(),
endAmountOut: this.amountOutEnd.toString(),
amountInGasAdjusted: this.amountInStart.toString(),
amountInGasAndPortionAdjusted: this.request.info.type === TradeType.EXACT_OUTPUT ? this.amountInGasAndPortionAdjusted.toString() : undefined,
amountInGasAndPortionAdjusted:
this.request.info.type === TradeType.EXACT_OUTPUT ? this.amountInGasAndPortionAdjusted.toString() : undefined,
amountOutGasAdjusted: this.amountOutStart.toString(),
amountOutGasAndPortionAdjusted: this.request.info.type === TradeType.EXACT_INPUT ? this.amountOutGasAndPortionAdjusted.toString() : undefined,
amountOutGasAndPortionAdjusted:
this.request.info.type === TradeType.EXACT_INPUT ? this.amountOutGasAndPortionAdjusted.toString() : undefined,
swapper: this.swapper,
filler: this.filler,
routing: RoutingType[this.routingType],
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { BigNumber } from 'ethers';

import { SUPPORTED_CHAINS } from '../../config/chains';
import { DEFAULT_SLIPPAGE_TOLERANCE, RoutingType } from '../../constants';
import { Portion } from '../../fetchers/PortionFetcher';
import { ValidationError } from '../../util/errors';
import { ClassicConfig, ClassicConfigJSON, ClassicRequest } from './ClassicRequest';
import { DutchConfig, DutchConfigJSON, DutchRequest } from './DutchRequest';
import { Portion } from '../../fetchers/PortionFetcher';

export * from './ClassicRequest';
export * from './DutchRequest';
Expand Down
8 changes: 5 additions & 3 deletions lib/fetchers/PortionFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ export class PortionFetcher {

// We bypass the cache if `forcePortion` is true.
// We do it to avoid cache conflicts since `forcePortion` is only for testing purposes.
const portionFromCache = !forcePortion && this.portionCache.get<GetPortionResponse>(
this.PORTION_CACHE_KEY(tokenInChainId, tokenInAddress, tokenOutChainId, tokenOutAddress)
);
const portionFromCache =
!forcePortion &&
this.portionCache.get<GetPortionResponse>(
this.PORTION_CACHE_KEY(tokenInChainId, tokenInAddress, tokenOutChainId, tokenOutAddress)
);

if (portionFromCache) {
metrics.putMetric(`PortionFetcherCacheHit`, 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/util/portion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export let forcePortion = false;

export const setGlobalForcePortion = (_forcePortion: boolean) => forcePortion = _forcePortion;
export const setGlobalForcePortion = (_forcePortion: boolean) => (forcePortion = _forcePortion);
13 changes: 7 additions & 6 deletions lib/util/quoteMath.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BigNumber } from "ethers";
import { BigNumber as BN } from "bignumber.js";
import { ClassicQuoteDataJSON, Quote } from "../entities";
import { BigNumber as BN } from 'bignumber.js';
import { BigNumber } from 'ethers';
import { ClassicQuoteDataJSON, Quote } from '../entities';

// quoteSizeUSD = (multiple of gas-cost-equivalent quote token) * (gas cost in USD)
export function getQuoteSizeEstimateUSD(classicQuote: Quote) {
const classicQuoteData = classicQuote.toJSON() as ClassicQuoteDataJSON;
const classicQuoteData = classicQuote.toJSON() as ClassicQuoteDataJSON;
return new BN(
BigNumber.from(classicQuoteData.quoteGasAdjusted)
.div(BigNumber.from(classicQuoteData.gasUseEstimateQuote)).toString())
.times(new BN(classicQuoteData.gasUseEstimateUSD));
.div(BigNumber.from(classicQuoteData.gasUseEstimateQuote))
.toString()
).times(new BN(classicQuoteData.gasUseEstimateUSD));
}
Loading

0 comments on commit 8f894ca

Please sign in to comment.