Skip to content

Commit

Permalink
chore: FillAdjustor and clean up JS router and unused functions [TKR-403
Browse files Browse the repository at this point in the history
] (#480)

* Remove old JS router and add a FillAdjustor

Clean up JS router and unused functions

Remove more unused functions, add adjustment of fills

Comment on why we use fill over sample

update CODEOWNERS

lint

Clean up Fill removing unused properties

Remove CollapsedFills, omit flags bigint

Create GasSchedule vs FeeSchedule, return Fill and gas on OptimizedOrder

Use Fill Adjustment in Phase2 of routing

Fix Limit orders being treated as VIP

* Fix case where dex liquidity is empty

* Use best gas adjusted pricing for fee sources

* CHANGELOG
  • Loading branch information
dekz authored Jun 29, 2022
1 parent 2aadbda commit ee98524
Show file tree
Hide file tree
Showing 26 changed files with 709 additions and 1,171 deletions.
18 changes: 10 additions & 8 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# See https://help.github.com/articles/about-codeowners/

# for more info about CODEOWNERS file

# It uses the same pattern rule for gitignore file

# https://git-scm.com/docs/gitignore#_pattern_format

# Website
packages/asset-swapper/ @BMillman19 @fragosti @dave4506
packages/instant/ @BMillman19 @fragosti @dave4506
packages/asset-swapper/ @dekz @mzhu25 @dextracker @kh-chang

# Dev tools & setup
.circleci/ @dorothy-zbornak
packages/contract-addresses/ @abandeali1
packages/contract-artifacts/ @abandeali1
packages/order-utils/ @dorothy-zbornak

.circleci/ @dekz @mzhu25
packages/contract-addresses/ @dekz @mzhu25 @dextracker @kh-chang
packages/contract-artifacts/ @dekz @mzhu25
packages/protocol-utils/ @dekz @mzhu25

# Protocol/smart contracts
contracts/ @abandeali1 @hysz @dorothy-zbornak @mzhu25

contracts/ @dekz @mzhu25 @dextracker
13 changes: 13 additions & 0 deletions packages/asset-swapper/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
[
{
"version": "16.63.0",
"changes": [
{
"note": "Remove JS router",
"pr": 480
},
{
"note": "Removed Median price in favour of best gas adjusted price",
"pr": 480
}
]
},
{
"version": "16.62.2",
"changes": [
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-swapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"config": {
"publicInterfaceContracts": "ERC20BridgeSampler,BalanceChecker,FakeTaker",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(ApproximateBuys|BalanceChecker|BalancerSampler|BalancerV2BatchSampler|BalancerV2Common|BalancerV2Sampler|BancorSampler|BancorV3Sampler|CompoundSampler|CurveSampler|DODOSampler|DODOV2Sampler|ERC20BridgeSampler|FakeTaker|GMXSampler|IBalancer|IBalancerV2Vault|IBancor|IBancorV3|ICurve|IGMX|IMStable|IMooniswap|IMultiBridge|IPlatypus|IShell|IUniswapExchangeQuotes|IUniswapV2Router01|KyberDmmSampler|LidoSampler|LiquidityProviderSampler|MStableSampler|MakerPSMSampler|MooniswapSampler|NativeOrderSampler|PlatypusSampler|SamplerUtils|ShellSampler|SmoothySampler|TestNativeOrderSampler|TwoHopSampler|UniswapSampler|UniswapV2Sampler|UniswapV3Sampler|UtilitySampler|VelodromeSampler).json",
"abis": "./test/generated-artifacts/@(ApproximateBuys|BalanceChecker|BalancerSampler|BalancerV2BatchSampler|BalancerV2Common|BalancerV2Sampler|BancorSampler|BancorV3Sampler|CompoundSampler|CurveSampler|DODOSampler|DODOV2Sampler|ERC20BridgeSampler|FakeTaker|GMXSampler|IBalancer|IBalancerV2Vault|IBancor|IBancorV3|ICurve|IGMX|IMStable|IMooniswap|IMultiBridge|IPlatypus|IShell|IUniswapExchangeQuotes|IUniswapV2Router01|KyberDmmSampler|LidoSampler|LiquidityProviderSampler|MStableSampler|MakerPSMSampler|MooniswapSampler|NativeOrderSampler|PlatypusSampler|SamplerUtils|ShellSampler|TestNativeOrderSampler|TwoHopSampler|UniswapSampler|UniswapV2Sampler|UniswapV3Sampler|UtilitySampler|VelodromeSampler).json",
"postpublish": {
"assets": []
}
Expand Down
10 changes: 7 additions & 3 deletions packages/asset-swapper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {
ContractTxFunctionObj,
SendTransactionOpts,
} from '@0x/base-contract';
export { ContractAddresses } from '@0x/contract-addresses';
export { ContractAddresses, ChainId, getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
export {
V4RFQFirmQuote,
V4RFQIndicativeQuote,
Expand Down Expand Up @@ -132,6 +132,7 @@ export {
BUY_SOURCE_FILTER_BY_CHAIN_ID,
SELL_SOURCE_FILTER_BY_CHAIN_ID,
NATIVE_FEE_TOKEN_BY_CHAIN_ID,
ZERO_AMOUNT,
} from './utils/market_operation_utils/constants';
export {
Parameters,
Expand All @@ -141,7 +142,6 @@ export {
export {
BalancerFillData,
BancorFillData,
CollapsedFill,
CurveFillData,
CurveFunctionSelectors,
CurveInfo,
Expand All @@ -150,7 +150,9 @@ export {
ERC20BridgeSource,
ExchangeProxyOverhead,
FeeSchedule,
GasSchedule,
Fill,
FillAdjustor,
FillData,
GetMarketOrdersRfqOpts,
LiquidityProviderFillData,
Expand All @@ -159,7 +161,6 @@ export {
MarketDepthSide,
MooniswapFillData,
MultiHopFillData,
NativeCollapsedFill,
NativeRfqOrderFillData,
NativeLimitOrderFillData,
NativeFillData,
Expand All @@ -168,6 +169,7 @@ export {
TokenAdjacencyGraph,
UniswapV2FillData,
} from './utils/market_operation_utils/types';
export { IdentityFillAdjustor } from './utils/market_operation_utils/identity_fill_adjustor';
export { ProtocolFeeUtils } from './utils/protocol_fee_utils';
export {
BridgeQuoteReportEntry,
Expand All @@ -191,3 +193,5 @@ export type Native = ERC20BridgeSource.Native;
export type MultiHop = ERC20BridgeSource.MultiHop;

export { rfqtMocker, RfqtQuoteEndpoint } from './utils/rfqt_mocker';

export { adjustOutput } from './utils/market_operation_utils/fills';
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
// ETH buy/sell is supported
![sellToken, buyToken].includes(NATIVE_FEE_TOKEN_BY_CHAIN_ID[ChainId.Mainnet])
) {
const fillData = slippedOrders[0].fills[0].fillData as CurveFillData;
const fillData = slippedOrders[0].fillData as CurveFillData;
return {
calldataHexString: this._exchangeProxy
.sellToLiquidityProvider(
Expand Down Expand Up @@ -311,7 +311,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
this.chainId === ChainId.Mainnet &&
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.Mooniswap])
) {
const fillData = slippedOrders[0].fills[0].fillData as MooniswapFillData;
const fillData = slippedOrders[0].fillData as MooniswapFillData;
return {
calldataHexString: this._exchangeProxy
.sellToLiquidityProvider(
Expand Down
30 changes: 15 additions & 15 deletions packages/asset-swapper/src/swap_quoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { DexOrderSampler } from './utils/market_operation_utils/sampler';
import { SourceFilters } from './utils/market_operation_utils/source_filters';
import {
ERC20BridgeSource,
FeeSchedule,
FillData,
GasSchedule,
GetMarketOrdersOpts,
MarketDepth,
MarketDepthSide,
Expand Down Expand Up @@ -366,9 +366,11 @@ export class SwapQuoter {
const calcOpts: GetMarketOrdersOpts = {
...cloneOpts,
gasPrice,
feeSchedule: _.mapValues(opts.feeSchedule, gasCost => (fillData: FillData) =>
gasCost === undefined ? 0 : gasPrice.times(gasCost(fillData)),
),
feeSchedule: _.mapValues(opts.gasSchedule, gasCost => (fillData: FillData) => {
const gas = gasCost ? gasCost(fillData) : 0;
const fee = gasPrice.times(gas);
return { gas, fee };
}),
exchangeProxyOverhead: flags => gasPrice.times(opts.exchangeProxyOverhead(flags)),
};
// pass the QuoteRequestor on if rfqt enabled
Expand Down Expand Up @@ -502,7 +504,7 @@ function createSwapQuote(
operation: MarketOperation,
assetFillAmount: BigNumber,
gasPrice: BigNumber,
gasSchedule: FeeSchedule,
gasSchedule: GasSchedule,
slippage: number,
): SwapQuote {
const {
Expand Down Expand Up @@ -562,7 +564,7 @@ function calculateQuoteInfo(
operation: MarketOperation,
assetFillAmount: BigNumber,
gasPrice: BigNumber,
gasSchedule: FeeSchedule,
gasSchedule: GasSchedule,
slippage: number,
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
const bestCaseFillResult = simulateBestCaseFill({
Expand Down Expand Up @@ -591,25 +593,23 @@ function calculateQuoteInfo(
function calculateTwoHopQuoteInfo(
optimizedOrders: OptimizedMarketOrder[],
operation: MarketOperation,
gasSchedule: FeeSchedule,
gasSchedule: GasSchedule,
slippage: number,
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
const [firstHopOrder, secondHopOrder] = optimizedOrders;
const [firstHopFill] = firstHopOrder.fills;
const [secondHopFill] = secondHopOrder.fills;
const gas = new BigNumber(
gasSchedule[ERC20BridgeSource.MultiHop]!({
firstHopSource: _.pick(firstHopFill, 'source', 'fillData'),
secondHopSource: _.pick(secondHopFill, 'source', 'fillData'),
firstHopSource: _.pick(firstHopOrder, 'source', 'fillData'),
secondHopSource: _.pick(secondHopOrder, 'source', 'fillData'),
}),
).toNumber();
const isSell = operation === MarketOperation.Sell;

return {
bestCaseQuoteInfo: {
makerAmount: isSell ? secondHopFill.output : secondHopFill.input,
takerAmount: isSell ? firstHopFill.input : firstHopFill.output,
totalTakerAmount: isSell ? firstHopFill.input : firstHopFill.output,
makerAmount: isSell ? secondHopOrder.fill.output : secondHopOrder.fill.input,
takerAmount: isSell ? firstHopOrder.fill.input : firstHopOrder.fill.output,
totalTakerAmount: isSell ? firstHopOrder.fill.input : firstHopOrder.fill.output,
feeTakerTokenAmount: constants.ZERO_AMOUNT,
protocolFeeInWeiAmount: constants.ZERO_AMOUNT,
gas,
Expand All @@ -635,7 +635,7 @@ function calculateTwoHopQuoteInfo(
[ERC20BridgeSource.MultiHop]: {
proportion: new BigNumber(1),
intermediateToken: secondHopOrder.takerToken,
hops: [firstHopFill.source, secondHopFill.source],
hops: [firstHopOrder.source, secondHopOrder.source],
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getComparisonPrices(
} else {
try {
const fillFeeInEth = new BigNumber(
(feeSchedule[ERC20BridgeSource.Native] as FeeEstimate)({ type: FillQuoteTransformerOrderType.Rfq }),
(feeSchedule[ERC20BridgeSource.Native] as FeeEstimate)({ type: FillQuoteTransformerOrderType.Rfq }).fee,
);
const exchangeProxyOverheadInEth = new BigNumber(exchangeProxyOverhead(SOURCE_FLAGS.RfqOrder));
feeInEth = fillFeeInEth.plus(exchangeProxyOverheadInEth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { formatBytes32String } from '@ethersproject/strings';

import { TokenAdjacencyGraphBuilder } from '../token_adjacency_graph_builder';

import { IdentityFillAdjustor } from './identity_fill_adjustor';
import { SourceFilters } from './source_filters';
import {
AaveV2FillData,
Expand All @@ -19,6 +20,7 @@ import {
FeeSchedule,
FillData,
FinalUniswapV3FillData,
GasSchedule,
GeistFillData,
GetMarketOrdersOpts,
isFinalUniswapV3FillData,
Expand Down Expand Up @@ -2381,7 +2383,7 @@ const uniswapV2CloneGasSchedule = (fillData?: FillData) => {
* the ethereum transaction cost (21k)
*/
// tslint:disable:custom-no-magic-numbers
export const DEFAULT_GAS_SCHEDULE: Required<FeeSchedule> = {
export const DEFAULT_GAS_SCHEDULE: Required<GasSchedule> = {
[ERC20BridgeSource.Native]: fillData => {
// TODO jacob re-order imports so there is no circular rependency with SignedNativeOrder
const nativeFillData = fillData as { type: FillQuoteTransformerOrderType };
Expand Down Expand Up @@ -2569,10 +2571,21 @@ export const DEFAULT_GAS_SCHEDULE: Required<FeeSchedule> = {
[ERC20BridgeSource.Velodrome]: () => 160e3,
};

export const DEFAULT_FEE_SCHEDULE: Required<FeeSchedule> = { ...DEFAULT_GAS_SCHEDULE };
export const DEFAULT_FEE_SCHEDULE: Required<FeeSchedule> = Object.keys(DEFAULT_GAS_SCHEDULE).reduce((acc, key) => {
acc[key as ERC20BridgeSource] = (fillData: FillData) => {
return {
gas: DEFAULT_GAS_SCHEDULE[key as ERC20BridgeSource](fillData),
fee: ZERO_AMOUNT,
};
};
return acc;
// tslint:disable-next-line:no-object-literal-type-assertion
}, {} as Required<FeeSchedule>);

export const POSITIVE_SLIPPAGE_FEE_TRANSFORMER_GAS = new BigNumber(20000);

export const DEFAULT_FEE_ESTIMATE = { gas: 0, fee: ZERO_AMOUNT };

// tslint:enable:custom-no-magic-numbers

export const DEFAULT_GET_MARKET_ORDERS_OPTS: Omit<GetMarketOrdersOpts, 'gasPrice'> = {
Expand All @@ -2593,4 +2606,5 @@ export const DEFAULT_GET_MARKET_ORDERS_OPTS: Omit<GetMarketOrdersOpts, 'gasPrice
shouldIncludePriceComparisonsReport: false,
tokenAdjacencyGraph: { default: [] },
neonRouterNumSamples: 14,
fillAdjustor: new IdentityFillAdjustor(),
};
Loading

0 comments on commit ee98524

Please sign in to comment.