Skip to content

Commit

Permalink
Merge pull request #4 from violetprotocol/feat/remove-universal-router
Browse files Browse the repository at this point in the history
Feat/remove universal router
  • Loading branch information
ra-phael authored Feb 17, 2023
2 parents 4597967 + 11b2baf commit a1ac0fa
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 1,308 deletions.
4 changes: 2 additions & 2 deletions cli/commands/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export class Quote extends BaseCommand {
TradeType.EXACT_INPUT,
recipient
? {
type: SwapType.UNIVERSAL_ROUTER,
deadlineOrPreviousBlockhash: 10000000000000,
type: SwapType.SWAP_ROUTER_02,
deadline: 10000000000000,
recipient,
slippageTolerance: new Percent(5, 100),
simulate: simulate ? { fromAddress: recipient } : undefined,
Expand Down
104 changes: 4 additions & 100 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
"@uniswap/router-sdk": "^1.3.0",
"@uniswap/swap-router-contracts": "^1.3.0",
"@uniswap/token-lists": "^1.0.0-beta.25",
"@uniswap/universal-router": "^1.0.1",
"@uniswap/universal-router-sdk": "^1.3.0",
"@uniswap/v2-sdk": "^3.0.1",
"@uniswap/v3-sdk": "^3.7.0",
"async-retry": "^1.3.1",
Expand Down
69 changes: 24 additions & 45 deletions src/providers/eth-estimate-gas-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,31 @@ export class EthEstimateGasSimulator extends Simulator {
): Promise<SwapRoute> {
const currencyIn = route.trade.inputAmount.currency;
let estimatedGasUsed: BigNumber;
if (swapOptions.type == SwapType.UNIVERSAL_ROUTER) {
log.info(
{ methodParameters: route.methodParameters },
'Simulating using eth_estimateGas on Universal Router'
);
try {
estimatedGasUsed = await this.provider.estimateGas({
data: route.methodParameters!.calldata,
to: route.methodParameters!.to,
from: fromAddress,
value: BigNumber.from(
currencyIn.isNative ? route.methodParameters!.value : '0'
),
});
} catch (e) {
log.error({ e }, 'Error estimating gas');
return {
...route,
simulationStatus: SimulationStatus.Failed,
};
}
} else if (swapOptions.type == SwapType.SWAP_ROUTER_02) {
log.info(
{ methodParameters: route.methodParameters },
'Simulating using eth_estimateGas on SwapRouter02'
);
if (swapOptions.type == SwapType.SWAP_ROUTER_02) {
log.info(
{ methodParameters: route.methodParameters },
'Simulating using eth_estimateGas on SwapRouter02'
);

try {
estimatedGasUsed = await this.provider.estimateGas({
data: route.methodParameters!.calldata,
to: route.methodParameters!.to,
from: fromAddress,
value: BigNumber.from(
currencyIn.isNative ? route.methodParameters!.value : '0'
),
});
} catch (e) {
log.error({ e }, 'Error estimating gas');
return {
...route,
simulationStatus: SimulationStatus.Failed,
};
}
} else {
throw new Error(`Unsupported swap type ${swapOptions}`);
}
try {
estimatedGasUsed = await this.provider.estimateGas({
data: route.methodParameters!.calldata,
to: route.methodParameters!.to,
from: fromAddress,
value: BigNumber.from(
currencyIn.isNative ? route.methodParameters!.value : '0'
),
});
} catch (e) {
log.error({ e }, 'Error estimating gas');
return {
...route,
simulationStatus: SimulationStatus.Failed,
};
}
} else {
throw new Error(`Unsupported swap type ${swapOptions}`);
}

estimatedGasUsed = this.inflateGasLimit(estimatedGasUsed);

Expand Down
58 changes: 1 addition & 57 deletions src/providers/simulation-provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { JsonRpcProvider } from '@ethersproject/providers';
import { TradeType } from '@uniswap/sdk-core';
import { PERMIT2_ADDRESS } from '@uniswap/universal-router-sdk';
import { BigNumber } from 'ethers/lib/ethers';

import { SwapOptions, SwapRoute, SwapType } from '../routers';
import { Erc20__factory } from '../types/other/factories/Erc20__factory';
import { Permit2__factory } from '../types/other/factories/Permit2__factory';
import { ChainId, CurrencyAmount, log, SWAP_ROUTER_02_ADDRESS } from '../util';

import { ProviderConfig } from './provider';
Expand Down Expand Up @@ -144,61 +142,7 @@ export abstract class Simulator {
provider
);

if (swapOptions.type == SwapType.UNIVERSAL_ROUTER) {
const permit2Allowance = await tokenContract.allowance(
fromAddress,
PERMIT2_ADDRESS
);

// If a permit has been provided we don't need to check if UR has already been allowed.
if (swapOptions.inputTokenPermit) {
log.info(
{
permitAllowance: permit2Allowance.toString(),
inputAmount: inputAmount.quotient.toString(),
},
'Permit was provided for simulation on UR, checking that Permit2 has been approved.'
);
return permit2Allowance.gte(
BigNumber.from(inputAmount.quotient.toString())
);
}

// Check UR has been approved from Permit2.
const permit2Contract = Permit2__factory.connect(
PERMIT2_ADDRESS,
provider
);

const { amount: universalRouterAllowance, expiration: tokenExpiration } =
await permit2Contract.allowance(
fromAddress,
inputAmount.currency.wrapped.address,
SWAP_ROUTER_02_ADDRESS
);

const nowTimestampS = Math.round(Date.now() / 1000);
const inputAmountBN = BigNumber.from(inputAmount.quotient.toString());

const permit2Approved = permit2Allowance.gte(inputAmountBN);
const universalRouterApproved =
universalRouterAllowance.gte(inputAmountBN);
const expirationValid = tokenExpiration > nowTimestampS;
log.info(
{
permitAllowance: permit2Allowance.toString(),
tokenAllowance: universalRouterAllowance.toString(),
tokenExpirationS: tokenExpiration,
nowTimestampS,
inputAmount: inputAmount.quotient.toString(),
permit2Approved,
universalRouterApproved,
expirationValid,
},
`Simulating on UR, Permit2 approved: ${permit2Approved}, UR approved: ${universalRouterApproved}, Expiraton valid: ${expirationValid}.`
);
return permit2Approved && universalRouterApproved && expirationValid;
} else if (swapOptions.type == SwapType.SWAP_ROUTER_02) {
if (swapOptions.type == SwapType.SWAP_ROUTER_02) {
if (swapOptions.inputTokenPermit) {
log.info(
{
Expand Down
Loading

0 comments on commit a1ac0fa

Please sign in to comment.