Skip to content

Commit

Permalink
Merge pull request #457 from yieldprotocol/feat/uniswapSlippage
Browse files Browse the repository at this point in the history
Add in slippage parameter Uniswap
  • Loading branch information
brucedonovan authored Sep 26, 2023
2 parents f220e44 + c1b3b16 commit 72e9700
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/current/MessageTranslator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export const Widget = (props: WidgetProps) => {
tokenInSymbol={parsedArgs[0]}
tokenOutSymbol={parsedArgs[1]}
inputAmount={parsedArgs[3]}
transactionKeyword={parsedArgs[2]} // BUYAMOUNT or SELLAMOUNT
slippage={parsedArgs[4]}
/>
);

Expand Down
13 changes: 9 additions & 4 deletions src/components/current/widgets/uniswap/Uniswap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ interface UniswapProps {
tokenInSymbol: string;
tokenOutSymbol: string;
inputAmount: string;
slippage: string;
transactionKeyword: 'BUYAMOUNT' | 'SELLAMOUNT';
}

const Uniswap = ({ tokenInSymbol, tokenOutSymbol, inputAmount }: UniswapProps) => {
const Uniswap = ({ tokenInSymbol, tokenOutSymbol, inputAmount, slippage, transactionKeyword }: UniswapProps) => {
const chainId = useChainId();
const isBuying = transactionKeyword === 'BUYAMOUNT';

const { address: recipient } = useAccount();
const { data: tokenIn, isETH: tokenInIsETH } = useToken(tokenInSymbol);
const { isETH: tokenOutIsETH } = useToken(tokenOutSymbol);
const { data: tokenInChecked } = useToken(tokenInIsETH ? 'WETH' : tokenInSymbol);
const { data: tokenOutChecked } = useToken(tokenOutIsETH ? 'WETH' : tokenOutSymbol);
const input = useInput(inputAmount, tokenInChecked?.symbol!);
const slippage = 2.0; // in percentage terms

const slippage_ = +slippage || 0.5; // in percentage terms

const getSlippageAdjustedAmount = (amount: BigNumber) =>
BigNumber.from(amount)
.mul(10000 - slippage * 100)
.mul(10000 - slippage_ * 100)
.div(10000);

// token out quote for amount in
Expand Down Expand Up @@ -250,7 +255,7 @@ const Uniswap = ({ tokenInSymbol, tokenOutSymbol, inputAmount }: UniswapProps) =
<ListResponse
title="Breakdown"
data={[
['Slippage', `${slippage}%`],
['Slippage', `${slippage_}%`],
['Minimum swap', cleanValue(amountOutMinimum_, 2)],
['Route', `${tokenInSymbol}-${tokenOutSymbol}`],
]}
Expand Down

0 comments on commit 72e9700

Please sign in to comment.