Skip to content

Commit

Permalink
Construct swap-and-add parameters for SwapRouter.swapAndAddCallParame…
Browse files Browse the repository at this point in the history
…ters() (Uniswap#41)
  • Loading branch information
ewilz authored Dec 6, 2021
1 parent 7c744c8 commit 43833ea
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 43 deletions.
7 changes: 6 additions & 1 deletion cli/commands/quote-to-ratio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ export class QuoteToRatio extends BaseCommand {
tokenOutBalance,
position,
{
errorTolerance: new Fraction(1, 100),
ratioErrorTolerance: new Fraction(1, 100),
maxIterations: 6,
addLiquidityOptions: {
slippageTolerance: new Percent(5, 10_000),
deadline: 100,
recipient: '0x0000000000000000000000000000000000000001',
},
},
{
deadline: 100,
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"scripts": {
"compile-v3-types": "npx typechain --target ethers-v5 --out-dir src/types/v3 './node_modules/@uniswap/?(v3-core|v3-periphery)/artifacts/contracts/**/*.json'",
"compile-v2-types": "npx typechain --target ethers-v5 --out-dir src/types/v2 './node_modules/@uniswap/?(v2-core|v2-periphery)/build/*UniswapV2*.json'",
"compile-router": "npx typechain --target ethers-v5 --out-dir src/types/other './node_modules/@uniswap/swap-router-contracts/artifacts/contracts/SwapRouter02.sol/SwapRouter02.json'",
"compile-external-types": "npx typechain --target ethers-v5 --out-dir src/types/other 'src/abis/**/*.json'",
"build": "run-p compile-v3-types compile-v2-types compile-external-types && run-p build:*",
"build": "run-p compile-v3-types compile-v2-types compile-router compile-external-types && run-p build:*",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"fix": "run-s fix:*",
Expand All @@ -42,7 +43,8 @@
"@types/sinon": "^10.0.2",
"@types/stats-lite": "^2.2.0",
"@uniswap/default-token-list": "^2.0.0",
"@uniswap/router-sdk": "^1.0.1",
"@uniswap/router-sdk": "^1.0.2",
"@uniswap/swap-router-contracts": "1.0.0",
"@uniswap/token-lists": "^1.0.0-beta.25",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
Expand Down
1 change: 1 addition & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './eth-gas-station-info-gas-price-provider';
export * from './gas-price-provider';
export * from './multicall-provider';
export * from './multicall-uniswap-provider';
export * from './swap-router-provider';
export * from './token-provider';
export * from './uri-subgraph-provider';
export * from './v2/caching-subgraph-provider';
Expand Down
81 changes: 81 additions & 0 deletions src/providers/swap-router-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { ApprovalTypes } from '@uniswap/router-sdk';
import { Currency, CurrencyAmount } from '@uniswap/sdk-core';
import { SwapRouter02__factory } from '../types/other';
import { log } from '../util';
import { IMulticallProvider } from './multicall-provider';

type TokenApprovalTypes = {
approvalTokenIn: ApprovalTypes;
approvalTokenOut: ApprovalTypes;
};

const SWAP_ROUTER_ADDRESS = '0x075B36dE1Bd11cb361c5B3B1E80A9ab0e7aa8a60';

/**
* Provider for accessing the SwapRouter02 Contract .
*
* @export
* @interface IRouterProvider
*/
export interface ISwapRouterProvider {
/**
* Get the approval method needed for each token. Throws an error if either query fails.
*
* @param tokenInAmount The Currency Amount of tokenIn needed by the user
* @param tokenOutAmount The Currency Amount of tokenOut needed by the user
* @returns the Approval Types for each token.
*/
getApprovalType(
tokenInAmount: CurrencyAmount<Currency>,
tokenOutAmount: CurrencyAmount<Currency>
): Promise<TokenApprovalTypes>;
}

export class SwapRouterProvider implements ISwapRouterProvider {
constructor(protected multicall2Provider: IMulticallProvider) {}

public async getApprovalType(
tokenInAmount: CurrencyAmount<Currency>,
tokenOutAmount: CurrencyAmount<Currency>
): Promise<TokenApprovalTypes> {
const functionParams: [string, string][] = [
[
tokenInAmount.currency.wrapped.address,
tokenInAmount.quotient.toString(),
],
[
tokenOutAmount.currency.wrapped.address,
tokenOutAmount.quotient.toString(),
],
];

const tx =
await this.multicall2Provider.callSameFunctionOnContractWithMultipleParams<
[string, string],
[ApprovalTypes]
>({
address: SWAP_ROUTER_ADDRESS,
contractInterface: SwapRouter02__factory.createInterface(),
functionName: 'getApprovalType',
functionParams,
});

if (!tx.results[0]?.success || !tx.results[1]?.success) {
log.info(
{ results: tx.results },
'Failed to get approval type from swap router for token in or token out'
);
throw new Error(
'Failed to get approval type from swap router for token in or token out'
);
}

const { result: approvalTokenIn } = tx.results![0];
const { result: approvalTokenOut } = tx.results![1];

return {
approvalTokenIn: approvalTokenIn[0],
approvalTokenOut: approvalTokenOut[0],
};
}
}
Loading

0 comments on commit 43833ea

Please sign in to comment.