Created
March 13, 2025 19:32
-
-
Save pipoblak/8465040da53e9085034bfcbac65226d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JsonRpcProvider } from "@ethersproject/providers"; | |
import { CurrencyAmount, Percent, Token, TradeType } from "@uniswap/sdk-core"; | |
import { | |
AlphaRouter, | |
SwapOptionsUniversalRouter, | |
SwapType, | |
} from "@uniswap/smart-order-router"; | |
import { UniversalRouterVersion } from "@uniswap/universal-router-sdk"; | |
const provider = new JsonRpcProvider( | |
"https://polygon-mainnet.g.alchemy.com/v2/<KEY>" | |
); | |
const runRoute = async () => { | |
const tokenIn = new Token( | |
Number(137), | |
"0xE6A537a407488807F0bbeb0038B79004f19DDDFb", | |
18, | |
"BRLA", | |
"BRLA" | |
); | |
const tokenOut = new Token( | |
Number(137), | |
"0xc2132D05D31c914a87C6611C10748AEb04B58e8F", | |
6, | |
"USDT", | |
"USDT" | |
); | |
const router = new AlphaRouter({ | |
chainId: 137, | |
provider, | |
}); | |
console.time("Route time"); | |
const routeOptions: SwapOptionsUniversalRouter = { | |
recipient: "0x24c09364c62E695dbAbd7bBDB8fa65e485cF9E91", | |
slippageTolerance: new Percent(0.5 * 100, 10_000), // eg. 0.5 | |
type: SwapType.UNIVERSAL_ROUTER, | |
version: UniversalRouterVersion.V2_0, | |
}; | |
const amount = 1713.19; | |
const amountOutBigint = BigInt(amount * 10 ** 6).toString(); | |
const amountOut = CurrencyAmount.fromRawAmount( | |
tokenOut, | |
amountOutBigint.toString() | |
); | |
let route = await router.route( | |
amountOut, | |
tokenIn, | |
TradeType.EXACT_OUTPUT, | |
routeOptions | |
); | |
const quote = route?.trade | |
?.maximumAmountIn(routeOptions.slippageTolerance) | |
.quotient.toString(); | |
console.info("Swap Quote", { | |
amountOut: Number(amountOutBigint.toString()) / 10 ** 6, | |
quoteIn: Number(route.quote.quotient.toString()) / 10 ** 18, | |
}); | |
console.timeEnd("Route time"); | |
}; | |
runRoute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment