-
Notifications
You must be signed in to change notification settings - Fork 111
chore(public): Improve Universal Router README with clearer instructions and example #119
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
6223c46
4720173
ffe7219
f645e89
8b49372
1052c8b
0fbefc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,73 @@ | |
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 V2TradeSDK } from '@uniswap/v2-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) | ||
|
||
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) | ||
// 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fine to uncomment this in my opinion! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uncommented |
||
|
||
console.log('Transaction parameters:', tx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed the console.log |
||
``` | ||
|
||
This example demonstrates how to: | ||
1. Import necessary classes and functions from Uniswap SDKs | ||
2. Define tokens for the trade | ||
3. Create a trade object (simplified in this example) | ||
4. Set up trade options including slippage tolerance and recipient | ||
5. Generate the calldata and value for the swap | ||
6. Prepare a transaction object for sending to the Universal Router | ||
|
||
Remember to handle errors, validate inputs, and implement proper transaction sending and confirmation in your actual implementation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can simplify this to: The code above is provided strictly as an example implementation of using the SDK to build a UniversalRouter swap. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. thx! |
||
|
||
## Running this package | ||
Make sure you are running `node v18` | ||
|
||
Install dependencies and run typescript unit tests | ||
```bash | ||
yarn install | ||
|
@@ -31,3 +80,6 @@ Run forge integration tests | |
forge install | ||
yarn test:forge | ||
``` | ||
|
||
## Contributing | ||
Contributions to improve the SDK or documentation are welcome. Please ensure you follow the existing code style and add appropriate tests for any new functionality. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove, we can create a separate CONTRIBUTING.md in the future There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicated import
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooops. thanks!