Skip to content

Commit ee98524

Browse files
authored
chore: FillAdjustor and clean up JS router and unused functions [TKR-403] (#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
1 parent 2aadbda commit ee98524

26 files changed

+709
-1171
lines changed

CODEOWNERS

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# See https://help.github.com/articles/about-codeowners/
2+
23
# for more info about CODEOWNERS file
34

45
# It uses the same pattern rule for gitignore file
6+
57
# https://git-scm.com/docs/gitignore#_pattern_format
68

7-
# Website
8-
packages/asset-swapper/ @BMillman19 @fragosti @dave4506
9-
packages/instant/ @BMillman19 @fragosti @dave4506
9+
packages/asset-swapper/ @dekz @mzhu25 @dextracker @kh-chang
1010

1111
# Dev tools & setup
12-
.circleci/ @dorothy-zbornak
13-
packages/contract-addresses/ @abandeali1
14-
packages/contract-artifacts/ @abandeali1
15-
packages/order-utils/ @dorothy-zbornak
12+
13+
.circleci/ @dekz @mzhu25
14+
packages/contract-addresses/ @dekz @mzhu25 @dextracker @kh-chang
15+
packages/contract-artifacts/ @dekz @mzhu25
16+
packages/protocol-utils/ @dekz @mzhu25
1617

1718
# Protocol/smart contracts
18-
contracts/ @abandeali1 @hysz @dorothy-zbornak @mzhu25
19+
20+
contracts/ @dekz @mzhu25 @dextracker

packages/asset-swapper/CHANGELOG.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
[
2+
{
3+
"version": "16.63.0",
4+
"changes": [
5+
{
6+
"note": "Remove JS router",
7+
"pr": 480
8+
},
9+
{
10+
"note": "Removed Median price in favour of best gas adjusted price",
11+
"pr": 480
12+
}
13+
]
14+
},
215
{
316
"version": "16.62.2",
417
"changes": [

packages/asset-swapper/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"config": {
4141
"publicInterfaceContracts": "ERC20BridgeSampler,BalanceChecker,FakeTaker",
4242
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
43-
"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",
43+
"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",
4444
"postpublish": {
4545
"assets": []
4646
}

packages/asset-swapper/src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export {
44
ContractTxFunctionObj,
55
SendTransactionOpts,
66
} from '@0x/base-contract';
7-
export { ContractAddresses } from '@0x/contract-addresses';
7+
export { ContractAddresses, ChainId, getContractAddressesForChainOrThrow } from '@0x/contract-addresses';
88
export {
99
V4RFQFirmQuote,
1010
V4RFQIndicativeQuote,
@@ -132,6 +132,7 @@ export {
132132
BUY_SOURCE_FILTER_BY_CHAIN_ID,
133133
SELL_SOURCE_FILTER_BY_CHAIN_ID,
134134
NATIVE_FEE_TOKEN_BY_CHAIN_ID,
135+
ZERO_AMOUNT,
135136
} from './utils/market_operation_utils/constants';
136137
export {
137138
Parameters,
@@ -141,7 +142,6 @@ export {
141142
export {
142143
BalancerFillData,
143144
BancorFillData,
144-
CollapsedFill,
145145
CurveFillData,
146146
CurveFunctionSelectors,
147147
CurveInfo,
@@ -150,7 +150,9 @@ export {
150150
ERC20BridgeSource,
151151
ExchangeProxyOverhead,
152152
FeeSchedule,
153+
GasSchedule,
153154
Fill,
155+
FillAdjustor,
154156
FillData,
155157
GetMarketOrdersRfqOpts,
156158
LiquidityProviderFillData,
@@ -159,7 +161,6 @@ export {
159161
MarketDepthSide,
160162
MooniswapFillData,
161163
MultiHopFillData,
162-
NativeCollapsedFill,
163164
NativeRfqOrderFillData,
164165
NativeLimitOrderFillData,
165166
NativeFillData,
@@ -168,6 +169,7 @@ export {
168169
TokenAdjacencyGraph,
169170
UniswapV2FillData,
170171
} from './utils/market_operation_utils/types';
172+
export { IdentityFillAdjustor } from './utils/market_operation_utils/identity_fill_adjustor';
171173
export { ProtocolFeeUtils } from './utils/protocol_fee_utils';
172174
export {
173175
BridgeQuoteReportEntry,
@@ -191,3 +193,5 @@ export type Native = ERC20BridgeSource.Native;
191193
export type MultiHop = ERC20BridgeSource.MultiHop;
192194

193195
export { rfqtMocker, RfqtQuoteEndpoint } from './utils/rfqt_mocker';
196+
197+
export { adjustOutput } from './utils/market_operation_utils/fills';

packages/asset-swapper/src/quote_consumers/exchange_proxy_swap_quote_consumer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
282282
// ETH buy/sell is supported
283283
![sellToken, buyToken].includes(NATIVE_FEE_TOKEN_BY_CHAIN_ID[ChainId.Mainnet])
284284
) {
285-
const fillData = slippedOrders[0].fills[0].fillData as CurveFillData;
285+
const fillData = slippedOrders[0].fillData as CurveFillData;
286286
return {
287287
calldataHexString: this._exchangeProxy
288288
.sellToLiquidityProvider(
@@ -311,7 +311,7 @@ export class ExchangeProxySwapQuoteConsumer implements SwapQuoteConsumerBase {
311311
this.chainId === ChainId.Mainnet &&
312312
isDirectSwapCompatible(quote, optsWithDefaults, [ERC20BridgeSource.Mooniswap])
313313
) {
314-
const fillData = slippedOrders[0].fills[0].fillData as MooniswapFillData;
314+
const fillData = slippedOrders[0].fillData as MooniswapFillData;
315315
return {
316316
calldataHexString: this._exchangeProxy
317317
.sellToLiquidityProvider(

packages/asset-swapper/src/swap_quoter.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import { DexOrderSampler } from './utils/market_operation_utils/sampler';
3333
import { SourceFilters } from './utils/market_operation_utils/source_filters';
3434
import {
3535
ERC20BridgeSource,
36-
FeeSchedule,
3736
FillData,
37+
GasSchedule,
3838
GetMarketOrdersOpts,
3939
MarketDepth,
4040
MarketDepthSide,
@@ -366,9 +366,11 @@ export class SwapQuoter {
366366
const calcOpts: GetMarketOrdersOpts = {
367367
...cloneOpts,
368368
gasPrice,
369-
feeSchedule: _.mapValues(opts.feeSchedule, gasCost => (fillData: FillData) =>
370-
gasCost === undefined ? 0 : gasPrice.times(gasCost(fillData)),
371-
),
369+
feeSchedule: _.mapValues(opts.gasSchedule, gasCost => (fillData: FillData) => {
370+
const gas = gasCost ? gasCost(fillData) : 0;
371+
const fee = gasPrice.times(gas);
372+
return { gas, fee };
373+
}),
372374
exchangeProxyOverhead: flags => gasPrice.times(opts.exchangeProxyOverhead(flags)),
373375
};
374376
// pass the QuoteRequestor on if rfqt enabled
@@ -502,7 +504,7 @@ function createSwapQuote(
502504
operation: MarketOperation,
503505
assetFillAmount: BigNumber,
504506
gasPrice: BigNumber,
505-
gasSchedule: FeeSchedule,
507+
gasSchedule: GasSchedule,
506508
slippage: number,
507509
): SwapQuote {
508510
const {
@@ -562,7 +564,7 @@ function calculateQuoteInfo(
562564
operation: MarketOperation,
563565
assetFillAmount: BigNumber,
564566
gasPrice: BigNumber,
565-
gasSchedule: FeeSchedule,
567+
gasSchedule: GasSchedule,
566568
slippage: number,
567569
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
568570
const bestCaseFillResult = simulateBestCaseFill({
@@ -591,25 +593,23 @@ function calculateQuoteInfo(
591593
function calculateTwoHopQuoteInfo(
592594
optimizedOrders: OptimizedMarketOrder[],
593595
operation: MarketOperation,
594-
gasSchedule: FeeSchedule,
596+
gasSchedule: GasSchedule,
595597
slippage: number,
596598
): { bestCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo; sourceBreakdown: SwapQuoteOrdersBreakdown } {
597599
const [firstHopOrder, secondHopOrder] = optimizedOrders;
598-
const [firstHopFill] = firstHopOrder.fills;
599-
const [secondHopFill] = secondHopOrder.fills;
600600
const gas = new BigNumber(
601601
gasSchedule[ERC20BridgeSource.MultiHop]!({
602-
firstHopSource: _.pick(firstHopFill, 'source', 'fillData'),
603-
secondHopSource: _.pick(secondHopFill, 'source', 'fillData'),
602+
firstHopSource: _.pick(firstHopOrder, 'source', 'fillData'),
603+
secondHopSource: _.pick(secondHopOrder, 'source', 'fillData'),
604604
}),
605605
).toNumber();
606606
const isSell = operation === MarketOperation.Sell;
607607

608608
return {
609609
bestCaseQuoteInfo: {
610-
makerAmount: isSell ? secondHopFill.output : secondHopFill.input,
611-
takerAmount: isSell ? firstHopFill.input : firstHopFill.output,
612-
totalTakerAmount: isSell ? firstHopFill.input : firstHopFill.output,
610+
makerAmount: isSell ? secondHopOrder.fill.output : secondHopOrder.fill.input,
611+
takerAmount: isSell ? firstHopOrder.fill.input : firstHopOrder.fill.output,
612+
totalTakerAmount: isSell ? firstHopOrder.fill.input : firstHopOrder.fill.output,
613613
feeTakerTokenAmount: constants.ZERO_AMOUNT,
614614
protocolFeeInWeiAmount: constants.ZERO_AMOUNT,
615615
gas,
@@ -635,7 +635,7 @@ function calculateTwoHopQuoteInfo(
635635
[ERC20BridgeSource.MultiHop]: {
636636
proportion: new BigNumber(1),
637637
intermediateToken: secondHopOrder.takerToken,
638-
hops: [firstHopFill.source, secondHopFill.source],
638+
hops: [firstHopOrder.source, secondHopOrder.source],
639639
},
640640
},
641641
};

packages/asset-swapper/src/utils/market_operation_utils/comparison_price.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function getComparisonPrices(
4848
} else {
4949
try {
5050
const fillFeeInEth = new BigNumber(
51-
(feeSchedule[ERC20BridgeSource.Native] as FeeEstimate)({ type: FillQuoteTransformerOrderType.Rfq }),
51+
(feeSchedule[ERC20BridgeSource.Native] as FeeEstimate)({ type: FillQuoteTransformerOrderType.Rfq }).fee,
5252
);
5353
const exchangeProxyOverheadInEth = new BigNumber(exchangeProxyOverhead(SOURCE_FLAGS.RfqOrder));
5454
feeInEth = fillFeeInEth.plus(exchangeProxyOverheadInEth);

packages/asset-swapper/src/utils/market_operation_utils/constants.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { formatBytes32String } from '@ethersproject/strings';
55

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

8+
import { IdentityFillAdjustor } from './identity_fill_adjustor';
89
import { SourceFilters } from './source_filters';
910
import {
1011
AaveV2FillData,
@@ -19,6 +20,7 @@ import {
1920
FeeSchedule,
2021
FillData,
2122
FinalUniswapV3FillData,
23+
GasSchedule,
2224
GeistFillData,
2325
GetMarketOrdersOpts,
2426
isFinalUniswapV3FillData,
@@ -2381,7 +2383,7 @@ const uniswapV2CloneGasSchedule = (fillData?: FillData) => {
23812383
* the ethereum transaction cost (21k)
23822384
*/
23832385
// tslint:disable:custom-no-magic-numbers
2384-
export const DEFAULT_GAS_SCHEDULE: Required<FeeSchedule> = {
2386+
export const DEFAULT_GAS_SCHEDULE: Required<GasSchedule> = {
23852387
[ERC20BridgeSource.Native]: fillData => {
23862388
// TODO jacob re-order imports so there is no circular rependency with SignedNativeOrder
23872389
const nativeFillData = fillData as { type: FillQuoteTransformerOrderType };
@@ -2569,10 +2571,21 @@ export const DEFAULT_GAS_SCHEDULE: Required<FeeSchedule> = {
25692571
[ERC20BridgeSource.Velodrome]: () => 160e3,
25702572
};
25712573

2572-
export const DEFAULT_FEE_SCHEDULE: Required<FeeSchedule> = { ...DEFAULT_GAS_SCHEDULE };
2574+
export const DEFAULT_FEE_SCHEDULE: Required<FeeSchedule> = Object.keys(DEFAULT_GAS_SCHEDULE).reduce((acc, key) => {
2575+
acc[key as ERC20BridgeSource] = (fillData: FillData) => {
2576+
return {
2577+
gas: DEFAULT_GAS_SCHEDULE[key as ERC20BridgeSource](fillData),
2578+
fee: ZERO_AMOUNT,
2579+
};
2580+
};
2581+
return acc;
2582+
// tslint:disable-next-line:no-object-literal-type-assertion
2583+
}, {} as Required<FeeSchedule>);
25732584

25742585
export const POSITIVE_SLIPPAGE_FEE_TRANSFORMER_GAS = new BigNumber(20000);
25752586

2587+
export const DEFAULT_FEE_ESTIMATE = { gas: 0, fee: ZERO_AMOUNT };
2588+
25762589
// tslint:enable:custom-no-magic-numbers
25772590

25782591
export const DEFAULT_GET_MARKET_ORDERS_OPTS: Omit<GetMarketOrdersOpts, 'gasPrice'> = {
@@ -2593,4 +2606,5 @@ export const DEFAULT_GET_MARKET_ORDERS_OPTS: Omit<GetMarketOrdersOpts, 'gasPrice
25932606
shouldIncludePriceComparisonsReport: false,
25942607
tokenAdjacencyGraph: { default: [] },
25952608
neonRouterNumSamples: 14,
2609+
fillAdjustor: new IdentityFillAdjustor(),
25962610
};

0 commit comments

Comments
 (0)