Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(public): Improve Universal Router README with clearer instructions and example #119

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions sdks/universal-router-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,63 @@
This SDK facilitates interactions with the contracts in [Universal Router](https://github.com/Uniswap/universal-router)

## Usage
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.
Install the latest version of universal-router-sdk.
```bash
npm install @uniswap/universal-router-sdk
```
Then import the corresponding Trade class and Data object for each protocol you'd like to interact with.

### Trading on Uniswap
warning: `swapERC20CallParameters()` to be deprecated in favor of `swapCallParameters()`
**Note**: `swapERC20CallParameters()` has been deprecated in favor of `swapCallParameters()`

```typescript
import { TradeType } from '@uniswap/sdk-core'
import { TradeType, CurrencyAmount, Token, Percent } from '@uniswap/sdk-core'
import { Trade as V2TradeSDK } from '@uniswap/v2-sdk'
import { Trade as V3TradeSDK } from '@uniswap/v3-sdk'
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@uniswap/router-sdk'
import { Trade as RouterTrade } from '@uniswap/router-sdk'
import { SwapRouter, UNIVERSAL_ROUTER_ADDRESS } from '@uniswap/universal-router-sdk'
import { Percent, Token, TradeType } from '@uniswap/sdk-core'

// Define your tokens
const WETH = new Token(1, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 18, 'WETH')
const USDC = new Token(1, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 'USDC')

// Create a trade object (this is a simplified example, you'd normally get this from a routing API or SDK)
const trade = new RouterTrade({
v2Routes: [/* ... */],
v3Routes: [/* ... */],
mixedRoutes: [/* ... */],
tradeType: TradeType.EXACT_INPUT
})

// Define trade options
const options = {
slippageTolerance: new Percent(50, 10_000), // 0.5% slippage tolerance
recipient: '0x...', // The address that will receive the output tokens
deadlineOrPreviousBlockhash: Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from now
}

// Get the parameters for the swap
const { calldata, value } = SwapRouter.swapCallParameters(trade, options)

// Use the calldata and value to send a transaction to the Universal Router
const tx = {
to: UNIVERSAL_ROUTER_ADDRESS,
data: calldata,
value: value
}

// Send the transaction using your preferred method (e.g., ethers.js, web3.js)
const txResponse = await wallet.sendTransaction(tx)
await txResponse.wait()

const options = { slippageTolerance, recipient }
const routerTrade = new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT })
// Use the raw calldata and value returned to call into Universal Swap Router contracts
const { calldata, value } = SwapRouter.swapCallParameters(routerTrade, options)
```

The code above is provided strictly as an example implementation of using the SDK to build a UniversalRouter swap.

## Running this package
Make sure you are running `node v18`

Install dependencies and run typescript unit tests
```bash
yarn install
Expand Down
Loading