-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from rainlanguage/2025-01-20-cleanup
cleanup
- Loading branch information
Showing
31 changed files
with
872 additions
and
1,596 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { PublicClient } from "viem"; | ||
import { Token } from "sushi/currency"; | ||
import { ChainId, Router } from "sushi"; | ||
import { BigNumber, ethers } from "ethers"; | ||
import { getDataFetcher } from "../src/config"; | ||
|
||
/** | ||
* Gets the route for tokens | ||
* @param chainId - The network chain id | ||
* @param rpcs - The rpcs | ||
* @param sellAmount - The sell amount, should be in onchain token value | ||
* @param fromTokenAddress - The from token address | ||
* @param fromTokenDecimals - The from token decimals | ||
* @param toTokenAddress - The to token address | ||
* @param toTokenDecimals - The to token decimals | ||
* @param receiverAddress - The address of the receiver | ||
* @param routeProcessorAddress - The address of the RouteProcessor contract | ||
* @param abiencoded - If the result should be abi encoded or not | ||
*/ | ||
export const getRouteForTokens = async ( | ||
chainId: number, | ||
rpcs: string[], | ||
sellAmount: BigNumber, | ||
fromTokenAddress: string, | ||
fromTokenDecimals: number, | ||
toTokenAddress: string, | ||
toTokenDecimals: number, | ||
receiverAddress: string, | ||
routeProcessorAddress: string, | ||
abiEncoded = false, | ||
) => { | ||
const amountIn = sellAmount.toBigInt(); | ||
const fromToken = new Token({ | ||
chainId: chainId, | ||
decimals: fromTokenDecimals, | ||
address: fromTokenAddress, | ||
}); | ||
const toToken = new Token({ | ||
chainId: chainId, | ||
decimals: toTokenDecimals, | ||
address: toTokenAddress, | ||
}); | ||
const dataFetcher = await getDataFetcher({ | ||
chain: { id: chainId }, | ||
rpc: rpcs, | ||
} as any as PublicClient); | ||
await dataFetcher.fetchPoolsForToken(fromToken, toToken); | ||
const pcMap = dataFetcher.getCurrentPoolCodeMap(fromToken, toToken); | ||
const route = Router.findBestRoute( | ||
pcMap, | ||
chainId as ChainId, | ||
fromToken, | ||
amountIn, | ||
toToken, | ||
Number(await dataFetcher.web3Client.getGasPrice()), | ||
// providers, | ||
// poolFilter | ||
); | ||
if (route.status == "NoWay") throw "NoWay"; | ||
else { | ||
let routeText = ""; | ||
route.legs.forEach((v, i) => { | ||
if (i === 0) | ||
routeText = | ||
routeText + | ||
v.tokenTo.symbol + | ||
"/" + | ||
v.tokenFrom.symbol + | ||
"(" + | ||
(v as any).poolName + | ||
")"; | ||
else | ||
routeText = | ||
routeText + | ||
" + " + | ||
v.tokenTo.symbol + | ||
"/" + | ||
v.tokenFrom.symbol + | ||
"(" + | ||
(v as any).poolName + | ||
")"; | ||
}); | ||
// eslint-disable-next-line no-console | ||
console.log("Route portions: ", routeText, "\n"); | ||
const rpParams = Router.routeProcessor4Params( | ||
pcMap, | ||
route, | ||
fromToken, | ||
toToken, | ||
receiverAddress as `0x${string}`, | ||
routeProcessorAddress as `0x${string}`, | ||
// permits | ||
// "0.005" | ||
); | ||
if (abiEncoded) return ethers.utils.defaultAbiCoder.encode(["bytes"], [rpParams.routeCode]); | ||
else return rpParams.routeCode; | ||
} | ||
}; |
This file contains 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
This file contains 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
Oops, something went wrong.