@hskswap/universal_router_sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # universal-router-sdk
2
+ This SDK facilitates interactions with the contracts in [Universal Router](https://github.com/Uniswap/universal-router)
3
+
4
+ ## Usage
5
+ Install latest version of universal-router-sdk. Then import the corresponding Trade class and Data object for each protocol you'd like to interact with.
6
+
7
+ ### Trading on Uniswap
8
+ warning: `swapERC20CallParameters()` to be deprecated in favor of `swapCallParameters()`
9
+ ```typescript
10
+ import { TradeType } from '@uniswap/sdk-core'
11
+ import { Trade as V2TradeSDK } from '@uniswap/v2-sdk'
12
+ import { Trade as V3TradeSDK } from '@uniswap/v3-sdk'
13
+ import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
14
+
15
+ const options = { slippageTolerance, recipient }
16
+ const routerTrade = new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT })
17
+ // Use the raw calldata and value returned to call into Universal Swap Router contracts
18
+ const { calldata, value } = SwapRouter.swapCallParameters(routerTrade, options)
19
+ ```
20
+
21
+ ## Running this package
22
+ Make sure you are running `node v18`
23
+ Install dependencies and run typescript unit tests
24
+ ```bash
25
+ yarn install
26
+ yarn test:hardhat
27
+ ```
28
+
29
+ Run forge integration tests
30
+ ```bash
31
+ forge install
32
+ yarn test:forge
33
+ ```
@@ -0,0 +1,12 @@
1
+ import { RoutePlanner } from '../utils/routerCommands';
2
+ export declare type TradeConfig = {
3
+ allowRevert: boolean;
4
+ };
5
+ export declare enum RouterActionType {
6
+ UniswapTrade = "UniswapTrade",
7
+ UnwrapWETH = "UnwrapWETH"
8
+ }
9
+ export interface Command {
10
+ tradeType: RouterActionType;
11
+ encode(planner: RoutePlanner, config: TradeConfig): void;
12
+ }
@@ -0,0 +1,2 @@
1
+ export * from './uniswap';
2
+ export * from './unwrapWETH';
@@ -0,0 +1,39 @@
1
+ import { RoutePlanner } from '../../utils/routerCommands';
2
+ import { Trade as RouterTrade, SwapOptions as RouterSwapOptions } from '@uniswap/router-sdk';
3
+ import { Permit2Permit } from '../../utils/inputTokens';
4
+ import { Currency, TradeType } from '@uniswap/sdk-core';
5
+ import { Command, RouterActionType, TradeConfig } from '../Command';
6
+ import { BigNumberish } from 'ethers';
7
+ export declare type FlatFeeOptions = {
8
+ amount: BigNumberish;
9
+ recipient: string;
10
+ };
11
+ export declare type SwapOptions = Omit<RouterSwapOptions, 'inputTokenPermit'> & {
12
+ useRouterBalance?: boolean;
13
+ inputTokenPermit?: Permit2Permit;
14
+ flatFee?: FlatFeeOptions;
15
+ safeMode?: boolean;
16
+ };
17
+ export declare class UniswapTrade implements Command {
18
+ trade: RouterTrade<Currency, Currency, TradeType>;
19
+ options: SwapOptions;
20
+ readonly tradeType: RouterActionType;
21
+ readonly payerIsUser: boolean;
22
+ constructor(trade: RouterTrade<Currency, Currency, TradeType>, options: SwapOptions);
23
+ get isAllV4(): boolean;
24
+ /**
25
+ * Checks if any route in a split trade ends with an ETH-WETH pool.
26
+ *
27
+ * When a split trade has routes ending in ETH-WETH pools alongside routes ending in WETH,
28
+ * V4 swaps that are last in their route must TAKE WETH (not ETH) at the endfor consistency.
29
+ * This ensures all routes accumulate WETH, which can then be unwrapped together to ETH
30
+ * in a single operation at the end.
31
+ */
32
+ get shouldForceV4UnwrapForSplitNativeOutput(): boolean;
33
+ get inputRequiresWrap(): boolean;
34
+ get inputRequiresUnwrap(): boolean;
35
+ get outputRequiresWrap(): boolean;
36
+ get outputRequiresUnwrap(): boolean;
37
+ get outputRequiresTransition(): boolean;
38
+ encode(planner: RoutePlanner, _config: TradeConfig): void;
39
+ }
@@ -0,0 +1,12 @@
1
+ import { BigNumberish } from 'ethers';
2
+ import { RoutePlanner } from '../../utils/routerCommands';
3
+ import { Permit2Permit } from '../../utils/inputTokens';
4
+ import { Command, RouterActionType, TradeConfig } from '../Command';
5
+ export declare class UnwrapWETH implements Command {
6
+ readonly tradeType: RouterActionType;
7
+ readonly permit2Data?: Permit2Permit;
8
+ readonly wethAddress: string;
9
+ readonly amount: BigNumberish;
10
+ constructor(amount: BigNumberish, chainId: number, permit2?: Permit2Permit);
11
+ encode(planner: RoutePlanner, _: TradeConfig): void;
12
+ }
@@ -0,0 +1,2 @@
1
+ export * from './actions';
2
+ export * from './Command';
@@ -0,0 +1,10 @@
1
+ export { SwapRouter } from './swapRouter';
2
+ export type { MigrateV3ToV4Options } from './swapRouter';
3
+ export * from './entities';
4
+ export * from './utils/routerTradeAdapter';
5
+ export { RoutePlanner, CommandType, COMMAND_DEFINITION, Parser, Subparser } from './utils/routerCommands';
6
+ export type { CommandDefinition, ParamType } from './utils/routerCommands';
7
+ export { UNIVERSAL_ROUTER_CREATION_BLOCK, UNIVERSAL_ROUTER_ADDRESS, ROUTER_AS_RECIPIENT, WETH_ADDRESS, UniversalRouterVersion, } from './utils/constants';
8
+ export { CommandParser, GenericCommandParser } from './utils/commandParser';
9
+ export type { UniversalRouterCommand, UniversalRouterCall, Param, CommandsDefinition } from './utils/commandParser';
10
+ export type { Permit2Permit } from './utils/inputTokens';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+
2
+ 'use strict'
3
+
4
+ if (process.env.NODE_ENV === 'production') {
5
+ module.exports = require('./universal_router_sdk.cjs.production.min.js')
6
+ } else {
7
+ module.exports = require('./universal_router_sdk.cjs.development.js')
8
+ }
@@ -0,0 +1,37 @@
1
+ import { Interface } from '@ethersproject/abi';
2
+ import { BigNumberish } from 'ethers';
3
+ import { MethodParameters, Position as V3Position, RemoveLiquidityOptions as V3RemoveLiquidityOptions } from '@uniswap/v3-sdk';
4
+ import { Position as V4Position, AddLiquidityOptions as V4AddLiquidityOptions } from '@uniswap/v4-sdk';
5
+ import { Trade as RouterTrade } from '@uniswap/router-sdk';
6
+ import { Currency, TradeType } from '@uniswap/sdk-core';
7
+ import { SwapOptions } from './entities/actions/uniswap';
8
+ export declare type SwapRouterConfig = {
9
+ sender?: string;
10
+ deadline?: BigNumberish;
11
+ };
12
+ export interface MigrateV3ToV4Options {
13
+ inputPosition: V3Position;
14
+ outputPosition: V4Position;
15
+ v3RemoveLiquidityOptions: V3RemoveLiquidityOptions;
16
+ v4AddLiquidityOptions: V4AddLiquidityOptions;
17
+ }
18
+ export declare abstract class SwapRouter {
19
+ static INTERFACE: Interface;
20
+ static swapCallParameters(trades: RouterTrade<Currency, Currency, TradeType>, options: SwapOptions): MethodParameters;
21
+ /**
22
+ * Builds the call parameters for a migration from a V3 position to a V4 position.
23
+ * Some requirements of the parameters:
24
+ * - v3RemoveLiquidityOptions.collectOptions.recipient must equal v4PositionManager
25
+ * - v3RemoveLiquidityOptions.liquidityPercentage must be 100%
26
+ * - input pool and output pool must have the same tokens
27
+ * - V3 NFT must be approved, or valid inputV3NFTPermit must be provided with UR as spender
28
+ */
29
+ static migrateV3ToV4CallParameters(options: MigrateV3ToV4Options, positionManagerOverride?: string): MethodParameters;
30
+ /**
31
+ * Encodes a planned route into a method name and parameters for the Router contract.
32
+ * @param planner the planned route
33
+ * @param nativeCurrencyValue the native currency value of the planned route
34
+ * @param config the router config
35
+ */
36
+ private static encodePlan;
37
+ }
@@ -0,0 +1,2 @@
1
+ import { MethodParameters } from '@uniswap/v3-sdk';
2
+ export declare function registerFixture(key: string, data: MethodParameters): void;
@@ -0,0 +1,5 @@
1
+ export declare const FORGE_V4_POSITION_MANAGER = "0x2e234DAe75C793f67A35089C9d99245E1C58470b";
2
+ export declare const FORGE_SENDER_ADDRESS = "0xcf03dd0a894ef79cb5b601a43c4b25e3ae4c67ed";
3
+ export declare const TEST_RECIPIENT_ADDRESS = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
4
+ export declare const TEST_FEE_RECIPIENT_ADDRESS = "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
5
+ export declare const PERMIT2_ADDRESS = "0x000000000022D473030F116dDEE9F6B43aC78BA3";
@@ -0,0 +1,2 @@
1
+ import { BigNumberish } from 'ethers';
2
+ export declare function hexToDecimalString(hex: BigNumberish): string;
@@ -0,0 +1,7 @@
1
+ import { Wallet } from 'ethers';
2
+ import { PermitSingle } from '@uniswap/permit2-sdk';
3
+ import { Permit2Permit } from '../../src/utils/inputTokens';
4
+ export declare function generatePermitSignature(permit: PermitSingle, signer: Wallet, chainId: number, permitAddress?: string): Promise<string>;
5
+ export declare function generateEip2098PermitSignature(permit: PermitSingle, signer: Wallet, chainId: number, permitAddress?: string): Promise<string>;
6
+ export declare function toInputPermit(signature: string, permit: PermitSingle): Permit2Permit;
7
+ export declare function makePermit(token: string, amount?: string, nonce?: string, routerAddress?: string): PermitSingle;
@@ -0,0 +1,24 @@
1
+ import { MixedRouteTrade, Trade as RouterTrade } from '@uniswap/router-sdk';
2
+ import { Trade as V2Trade, Pair } from '@uniswap/v2-sdk';
3
+ import { Trade as V3Trade, Pool, FeeAmount } from '@uniswap/v3-sdk';
4
+ import { Trade as V4Trade } from '@uniswap/v4-sdk';
5
+ import { SwapOptions } from '../../src';
6
+ import { TradeType, Ether, Token, Currency } from '@uniswap/sdk-core';
7
+ export declare const ETHER: Ether;
8
+ export declare const WETH: Token;
9
+ export declare const DAI: Token;
10
+ export declare const USDC: Token;
11
+ export declare const FEE_AMOUNT = FeeAmount.MEDIUM;
12
+ declare type UniswapPools = {
13
+ WETH_USDC_V2: Pair;
14
+ USDC_DAI_V2: Pair;
15
+ WETH_USDC_V3: Pool;
16
+ WETH_USDC_V3_LOW_FEE: Pool;
17
+ USDC_DAI_V3: Pool;
18
+ };
19
+ export declare function getUniswapPools(forkBlock?: number): Promise<UniswapPools>;
20
+ export declare function getPair(tokenA: Token, tokenB: Token, blockNumber: number): Promise<Pair>;
21
+ export declare function getPool(tokenA: Token, tokenB: Token, feeAmount: FeeAmount, blockNumber: number): Promise<Pool>;
22
+ export declare function swapOptions(options: Partial<SwapOptions>): SwapOptions;
23
+ export declare function buildTrade(trades: (V2Trade<Currency, Currency, TradeType> | V3Trade<Currency, Currency, TradeType> | V4Trade<Currency, Currency, TradeType> | MixedRouteTrade<Currency, Currency, TradeType>)[]): RouterTrade<Currency, Currency, TradeType>;
24
+ export {};