[swap] Change gasPrice check to a range check (#399)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { WETH9Contract } from '@0x/contract-wrappers';
|
||||
import { DummyERC20TokenContract } from '@0x/contracts-erc20';
|
||||
import { assertRoughlyEquals, expect, getRandomInteger, randomAddress } from '@0x/contracts-test-utils';
|
||||
import { expect, getRandomInteger, randomAddress } from '@0x/contracts-test-utils';
|
||||
import { BlockchainLifecycle } from 'dev-utils-deprecated';
|
||||
import { isNativeSymbolOrAddress } from '@0x/token-metadata';
|
||||
import { ObjectMap } from '@0x/types';
|
||||
@@ -19,7 +19,7 @@ import { BUY_SOURCE_FILTER_BY_CHAIN_ID, ERC20BridgeSource, LimitOrderFields } fr
|
||||
import * as config from '../src/config';
|
||||
import { AFFILIATE_FEE_TRANSFORMER_GAS, GAS_LIMIT_BUFFER_MULTIPLIER, SWAP_PATH } from '../src/constants';
|
||||
import { getDBConnectionOrThrow } from '../src/db_connection';
|
||||
import { ValidationErrorCodes, ValidationErrorItem, ValidationErrorReasons } from '../src/errors';
|
||||
import { ValidationErrorCodes, ValidationErrorReasons } from '../src/errors';
|
||||
import { GetSwapQuoteResponse, SignedLimitOrder } from '../src/types';
|
||||
|
||||
import {
|
||||
@@ -41,9 +41,9 @@ import { setupDependenciesAsync, teardownDependenciesAsync } from './utils/deplo
|
||||
import { constructRoute, httpGetAsync } from './utils/http_utils';
|
||||
import { MockOrderWatcher } from './utils/mock_order_watcher';
|
||||
import { getRandomSignedLimitOrderAsync } from './utils/orders';
|
||||
import { StatusCodes } from 'http-status-codes';
|
||||
import { ChainId } from '@0x/contract-addresses';
|
||||
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||
import { expectCorrectQuoteResponse, expectSwapError } from './test_utils';
|
||||
|
||||
// Force reload of the app avoid variables being polluted between test suites
|
||||
// Warning: You probably don't want to move this
|
||||
@@ -573,12 +573,6 @@ describe(SUITE_NAME, () => {
|
||||
});
|
||||
});
|
||||
|
||||
interface SwapErrors {
|
||||
validationErrors?: ValidationErrorItem[];
|
||||
revertErrorReason?: string;
|
||||
generalUserError?: boolean;
|
||||
}
|
||||
|
||||
async function requestSwap(
|
||||
app: Express.Application,
|
||||
endpoint: 'price' | 'quote',
|
||||
@@ -594,69 +588,3 @@ async function requestSwap(
|
||||
});
|
||||
return await httpGetAsync({ app, route });
|
||||
}
|
||||
|
||||
async function expectSwapError(swapResponse: supertest.Response, swapErrors: SwapErrors) {
|
||||
expect(swapResponse.type).to.be.eq('application/json');
|
||||
expect(swapResponse.statusCode).not.eq(StatusCodes.OK);
|
||||
|
||||
if (swapErrors.revertErrorReason) {
|
||||
expect(swapResponse.status).to.be.eq(StatusCodes.BAD_REQUEST);
|
||||
expect(swapResponse.body.code).to.eq(105);
|
||||
expect(swapResponse.body.reason).to.be.eql(swapErrors.revertErrorReason);
|
||||
return swapResponse;
|
||||
}
|
||||
if (swapErrors.validationErrors) {
|
||||
expect(swapResponse.status).to.be.eq(StatusCodes.BAD_REQUEST);
|
||||
expect(swapResponse.body.code).to.eq(100);
|
||||
expect(swapResponse.body.validationErrors).to.be.deep.eq(swapErrors.validationErrors);
|
||||
return swapResponse;
|
||||
}
|
||||
if (swapErrors.generalUserError) {
|
||||
expect(swapResponse.status).to.be.eq(StatusCodes.BAD_REQUEST);
|
||||
return swapResponse;
|
||||
}
|
||||
}
|
||||
|
||||
const PRECISION = 2;
|
||||
function expectCorrectQuoteResponse(
|
||||
response: supertest.Response,
|
||||
expectedResponse: Partial<GetSwapQuoteResponse>,
|
||||
): void {
|
||||
expect(response.type).to.be.eq('application/json');
|
||||
expect(response.statusCode).eq(StatusCodes.OK);
|
||||
const quoteResponse = response.body as GetSwapQuoteResponse;
|
||||
|
||||
for (const prop of Object.keys(expectedResponse)) {
|
||||
const property = prop as keyof GetSwapQuoteResponse;
|
||||
if (BigNumber.isBigNumber(expectedResponse[property])) {
|
||||
assertRoughlyEquals(quoteResponse[property], expectedResponse[property], PRECISION);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prop === 'sources' && expectedResponse.sources !== undefined) {
|
||||
const expectedSources = expectedResponse.sources.map((source) => ({
|
||||
...source,
|
||||
proportion: source.proportion.toString(),
|
||||
}));
|
||||
expect(quoteResponse.sources).to.deep.include.members(expectedSources);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prop === 'debugData') {
|
||||
const { samplerGasUsage, blockNumber, ...rest } = quoteResponse[property];
|
||||
const {
|
||||
samplerGasUsage: expectedSamplerGasUsage,
|
||||
blockNumber: expectedBlockNumber,
|
||||
...expectedRest
|
||||
} = expectedResponse[property];
|
||||
expect(samplerGasUsage).gt(expectedSamplerGasUsage * 0.5, 'samplerGasUsage is too low');
|
||||
expect(samplerGasUsage).lt(expectedSamplerGasUsage * 1.5, 'samplerGasUsage is too high');
|
||||
expect(blockNumber).gt(expectedBlockNumber * 0.5, 'blockNumber is too low');
|
||||
expect(blockNumber).lt(expectedBlockNumber * 1.5, 'blockNumber is too high');
|
||||
expect(rest).to.be.deep.eq(expectedRest);
|
||||
continue;
|
||||
}
|
||||
|
||||
expect(quoteResponse[property], property).to.deep.eq(expectedResponse[property]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user