Skip to content

Commit

Permalink
Merge pull request #293 from EdgeApp/matthew/uniswap0Amount
Browse files Browse the repository at this point in the history
Throw an unsupported error if 0 amount is expected from trade
  • Loading branch information
peachbits committed Oct 2, 2023
2 parents 9b72c1a + 7e59943 commit 96f3502
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/swap/defi/uni-v2-based/plugins/spookySwap.ts
Expand Up @@ -52,7 +52,7 @@ export function makeSpookySwapPlugin(
request: EdgeSwapRequestPlugin,
uid: string
): Promise<SwapOrder> => {
const { fromWallet, toWallet, fromTokenId, toTokenId, quoteFor } = request
const { fromWallet, toWallet, fromTokenId, toTokenId } = request

// Sanity check: Both wallets should be of the same chain.
if (
Expand All @@ -75,8 +75,8 @@ export function makeSpookySwapPlugin(
const spookySwapRouter = makeSpookySwapRouterContract(provider)
const { amountToSwap, expectedAmountOut } = await getSwapAmounts(
spookySwapRouter,
quoteFor,
request.nativeAmount,
request,
swapInfo,
[fromTokenAddress, toTokenAddress],
isWrappingSwap
)
Expand Down
6 changes: 3 additions & 3 deletions src/swap/defi/uni-v2-based/plugins/tombSwap.ts
Expand Up @@ -52,7 +52,7 @@ export function makeTombSwapPlugin(
request: EdgeSwapRequestPlugin,
uid: string
): Promise<SwapOrder> => {
const { fromWallet, toWallet, fromTokenId, toTokenId, quoteFor } = request
const { fromWallet, toWallet, fromTokenId, toTokenId } = request

// Sanity check: Both wallets should be of the same chain.
if (
Expand All @@ -75,8 +75,8 @@ export function makeTombSwapPlugin(
const tombSwapRouter = makeTombSwapRouterContract(provider)
const { amountToSwap, expectedAmountOut } = await getSwapAmounts(
tombSwapRouter,
quoteFor,
request.nativeAmount,
request,
swapInfo,
[fromTokenAddress, toTokenAddress],
isWrappingSwap
)
Expand Down
6 changes: 3 additions & 3 deletions src/swap/defi/uni-v2-based/plugins/velodrome.ts
Expand Up @@ -43,7 +43,7 @@ export function makeVelodromePlugin(
request: EdgeSwapRequestPlugin,
uid: string
): Promise<SwapOrder> => {
const { fromWallet, toWallet, fromTokenId, toTokenId, quoteFor } = request
const { fromWallet, toWallet, fromTokenId, toTokenId } = request

if (
// Velodrome does not support reverse quotes
Expand Down Expand Up @@ -86,8 +86,8 @@ export function makeVelodromePlugin(

const { amountToSwap, expectedAmountOut } = await getSwapAmounts(
velodromeRouter,
quoteFor,
request.nativeAmount,
request,
swapInfo,
path,
isWrappingSwap
)
Expand Down
16 changes: 12 additions & 4 deletions src/swap/defi/uni-v2-based/uniV2Utils.ts
@@ -1,4 +1,5 @@
import { mul, round, sub } from 'biggystring'
import { EdgeSwapInfo, EdgeSwapRequest, SwapCurrencyError } from 'edge-core-js'
import { BigNumber, Contract, ethers, PopulatedTransaction } from 'ethers'

import { InOutTokenAddresses } from '../defiUtils'
Expand All @@ -10,11 +11,12 @@ import { makeErc20Contract } from './uniV2Contracts'
*/
export const getSwapAmounts = async (
router: Contract,
quoteFor: string,
nativeAmount: string,
request: EdgeSwapRequest,
swapInfo: EdgeSwapInfo,
path: unknown,
isWrappingSwap: boolean
): Promise<{ amountToSwap: string; expectedAmountOut: string }> => {
const { nativeAmount, quoteFor } = request
const [amountToSwap, expectedAmountOut] = (isWrappingSwap
? [nativeAmount, nativeAmount]
: quoteFor === 'to'
Expand All @@ -24,8 +26,14 @@ export const getSwapAmounts = async (
: []
).map(String)

if (amountToSwap == null || expectedAmountOut == null)
throw new Error(`Failed to calculate amounts`)
if (
amountToSwap == null ||
expectedAmountOut == null ||
amountToSwap === '0' ||
expectedAmountOut === '0'
) {
throw new SwapCurrencyError(swapInfo, request)
}

return { amountToSwap, expectedAmountOut }
}
Expand Down

0 comments on commit 96f3502

Please sign in to comment.