Skip to content

Commit

Permalink
chore: token out with fot fix (Uniswap#540)
Browse files Browse the repository at this point in the history
* chore: token out with fot fix

* missing var

* fix tests, tests pending to add

* add test for fot flag

* reset to main

* tokenOutHasFot

* test FOT and Fee

* undefined aware

* fix

* review feedback
  • Loading branch information
mikeki authored Apr 22, 2024
1 parent f73799d commit 8f6963b
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 58 deletions.
8 changes: 7 additions & 1 deletion src/providers/portion-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface IPortionProvider {
getPortionAmount(
tokenOutAmount: CurrencyAmount,
tradeType: TradeType,
tokenOutHasFot?: boolean,
swapConfig?: SwapOptions
): CurrencyAmount | undefined;

Expand Down Expand Up @@ -116,9 +117,10 @@ export class PortionProvider implements IPortionProvider {
getPortionAmount(
tokenOutAmount: CurrencyAmount,
tradeType: TradeType,
tokenOutHasFot?: boolean,
swapConfig?: SwapOptions
): CurrencyAmount | undefined {
if (swapConfig?.type !== SwapType.UNIVERSAL_ROUTER) {
if (tokenOutHasFot || swapConfig?.type !== SwapType.UNIVERSAL_ROUTER) {
return undefined;
}

Expand Down Expand Up @@ -202,9 +204,13 @@ export class PortionProvider implements IPortionProvider {
}

return routeWithQuotes.map((routeWithQuote) => {
const tokenOut =
routeWithQuote.tokenPath[routeWithQuote.tokenPath.length - 1];
const tokenOutHasFot = tokenOut && tokenOut.buyFeeBps?.gt(0);
const portionAmount = this.getPortionAmount(
routeWithQuote.quote,
tradeType,
tokenOutHasFot,
swapConfig
);

Expand Down
31 changes: 21 additions & 10 deletions src/routers/alpha-router/alpha-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1039,10 +1039,30 @@ export class AlphaRouter
partialRoutingConfig: Partial<AlphaRouterConfig> = {}
): Promise<SwapRoute | null> {
const originalAmount = amount;

const { currencyIn, currencyOut } =
this.determineCurrencyInOutFromTradeType(
tradeType,
amount,
quoteCurrency
);

const tokenIn = currencyIn.wrapped;
const tokenOut = currencyOut.wrapped;

const tokenOutProperties =
await this.tokenPropertiesProvider.getTokensProperties(
[tokenOut],
partialRoutingConfig
);
const buyFeeBps = tokenOutProperties[tokenOut.address.toLowerCase()]?.tokenFeeResult?.buyFeeBps;
const tokenOutHasFot = buyFeeBps && buyFeeBps.gt(0);

if (tradeType === TradeType.EXACT_OUTPUT) {
const portionAmount = this.portionProvider.getPortionAmount(
amount,
tradeType,
tokenOutHasFot,
swapConfig
);
if (portionAmount && portionAmount.greaterThan(ZERO)) {
Expand All @@ -1056,16 +1076,6 @@ export class AlphaRouter
}
}

const { currencyIn, currencyOut } =
this.determineCurrencyInOutFromTradeType(
tradeType,
amount,
quoteCurrency
);

const tokenIn = currencyIn.wrapped;
const tokenOut = currencyOut.wrapped;

metric.setProperty('chainId', this.chainId);
metric.setProperty('pair', `${tokenIn.symbol}/${tokenOut.symbol}`);
metric.setProperty('tokenIn', tokenIn.address);
Expand Down Expand Up @@ -1455,6 +1465,7 @@ export class AlphaRouter
const portionAmount = this.portionProvider.getPortionAmount(
tokenOutAmount,
tradeType,
tokenOutHasFot,
swapConfig
);
const portionQuoteAmount = this.portionProvider.getPortionQuoteAmount(
Expand Down
10 changes: 10 additions & 0 deletions test/integ/routers/alpha-router/alpha-router.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,10 @@ describe('alpha router integration', () => {
slippageTolerance: LARGE_SLIPPAGE,
deadlineOrPreviousBlockhash: parseDeadline(360),
simulate: { fromAddress: WHALES(tokenIn!) },
fee: {
fee: new Percent(FLAT_PORTION.bips, 10_000),
recipient: FLAT_PORTION.recipient
}
},
{
...ROUTING_CONFIG,
Expand All @@ -2830,6 +2834,12 @@ describe('alpha router integration', () => {
expect(swap!.methodParameters).toBeDefined();
expect(swap!.methodParameters!.to).toBeDefined();

if (enableFeeOnTransferFeeFetching && tokenOut?.address === BULLET_WITHOUT_TAX.address) {
expect(swap?.portionAmount?.quotient).toBeUndefined();
} else {
expect(swap?.portionAmount?.quotient?.toString()).not.toEqual("0");
}

return { enableFeeOnTransferFeeFetching, ...swap! }
})
)
Expand Down
Loading

0 comments on commit 8f6963b

Please sign in to comment.