* Init contracts and sdk * Add basic example of swapping with sushi and curve 2pool * Initial ts and graph setup Revert "Initial ts and graph setup" This reverts commit a07cd1280e985f2301de25ab9ffe5a5cccc106f7. Add sushi and uni request Add types for requests * Testing querying pools for 2 tokens * Add Trader Joe pools * Sequence updator of pools * fix get token pair for sushiswap * add uniswapv3 pool integration * query for uniswap and test time * Initial architecture for sending queries * Uniswap query send integration * Abstraction for dexes Irina helped a LOT * All files under 1 architecture * Add parseToPool for SushiSwapV2 DEX * trader joe integration with Pool class * Add algo for direct swap * Call init swap from UI to sdk * Moved sdk to UI/src folder * Dikstra routing architecture * Delete clashing dependencies * Add fallback RPC provider + USD amounts of tokens * Dijkstra UniswapV2 pool routing algorithm implementation * Organize npm and add quality of life scripts * Revert "Organize npm and add quality of life scripts" This reverts commit a5cf92153fa56b228ec061546ef6b2d6976d0095. * Update algorithm * Create quote for sushiswap and add basic test for swapping * Add RateX.js test and config files * Oracles for WBTC and WETH work * Update RPC Provider * Connect solidity to front * Organize npm and add quality of life scripts * Fix input delay caused by asynchronous React hooks * Fix bad price state updates on token select * Update prices on swap * M1_18: Send amount as bigNumber and fix missmatching addresses issue * M1_25: Automate deploy script * M1_26: Connect with the selected network * M1_22: Display and auto-refresh quoted prices * M1_23: Show price impact * M1_29: GraphQL - fetch pool IDs for 2 tokens * M1_30: Fetch Pool Data from graph for SushiV2 and UniV3 * M1_30: Fix token pairs pool info * M1_31: Improvement - format code for consistency * M1_34: Add and improve basic swap algorithm * M1_33: Routing - one hop swap * M1_38: Refactoring code before DP algo * M1_38: Implement DP algo * M1_32: Basic future-proof routing UI * M2_36: Implement quote call for uniswap v3 using default quoter * M2_42: Call quote on frontend and get the best pool * M2_39: Routing - one hop swap (with split) Proof of concept for later expansion to multi hop. Instead of checking every route for the best output amount in every iteration, there will be a DP algorithm to find the best route for that iteration. * M2_37: Implement swap function and connect it with frontend, also add some loading, error checking and styling on frontend * M2_45: Setup tenderly fork and fix env variables on frontend * M1_44: Reformat frontend (#1) Migrate to SASS, standardize variable names, reformat everything * Improvement/p23-40 reformat sdk code (#2) Co-authored-by: Daniil Grbić <daniilgrbic@gmail.com> * M3_27: Add debounce and loader on quote call (#4) * Synchronize async calls and add input constraints * Add debounce * Appease Dragan * M3_57: Retrieve Curve data on-chain * M3_56: Create view function for getting ticks data and test quoter with logic needed for SDK * M3_56: Create first version of off chain uniswap quoter * P23-53 Integrate multi-hop logic (#5) MultiHop algo integration * Revert "P23-53 Integrate multi-hop logic (#5)" (#6) This reverts commit f4928509f60cce21dc6c6902523dd260c811250a. * P23-53 Integrate multi-hop logic (#5) (#7) MultiHop algo integration * Feature/P23-64:Add_curve_logic_to_rateX_repo (#8) Add Curve queries and outputAmount calculation Co-authored-by: Brankonymous <brankogrbic01@gmail.com> * M3_56: Refactor offchain quoter and simplify data retrival from contract * P23-67: Update algo for pools with more than 2 tokens (#9) * Feature P23-63 get sushi swap pool info (#10) * Delete unnecessary files * Solidity SushiSwapHelper * Algo and types update, fetching pools data * Add proper quote typing (#11) * M3_56: Adapt uniswap quoter to dex interface and create simple storage for ticks * P23-73:Connect routing diagram to quote * Filter out Curve V2 pools when querying Graph API * Fix communication with multihop algorithm * Format, remove logs * M3_61: Add swap function for multihop and connect it with frontend * Improvement/minor UI improvements and bugfixes (#14) * Handle unknown tokens and better routing diagram * Visually improve loading animation * Add tooltips for tokens in route diagram * Use variables for ant css * M3_60: Paralleize calls to speed up time taken to find route * M3_75: Modify Curve Graph calls * M3: Remove unused code for previous milestones * M3_49: Compute all routes with backtrack * M3_49: Finish sdk part for uni like algo * Uncomment routing diagram and previous version * P23-71: Integrate curve into algo (#18) * Solve merge conflicts * Edit curve data retrieval * Remove old Curve contract * P23-89: Get direct pools * Fix deployment issues and remove duplicate script for tenderly deployment * Feature/Swap on curve (#19) * Add swap for curve * Fix imports * M3_86: Reformat and refactor addresses config * M3_86: Fix tests * M3_86: Refactor deployment scripts * Feature/p23-77: Update pools and split algo (#20) * Update function for curve and sushi, basic split algo test * Add split routing * Bugfix curve update pool * P23-90: Integrate Camelot Dex (#21) * P23-89: Get direct pools * Fix deployment issues and remove duplicate script for tenderly deployment * Add initial graph query Co-authored-by: irinatomic <irina.z.tomic@gmail.com> * Add Camelot off-chain calculations Co-Authored-By: Daniil Grbić <mi20042@alas.matf.bg.ac.rs> * Feature/Swap on curve (#19) * Add swap for curve * Fix imports * M3_86: Reformat and refactor addresses config * M3_86: Fix tests * M3_86: Refactor deployment scripts * Fetch pool info on-chain Co-authored-by: irinatomic <irina.z.tomic@gmail.com> * Retrieve stableSwap on-chain * CamelotHelper contract for fetching data on-chain Co-Authored-By: Daniil Grbić <mi20042@alas.matf.bg.ac.rs> * Add complete logic for on-chain data fetching Co-Authored-By: Daniil Grbić <mi20042@alas.matf.bg.ac.rs> * Redeployment * Camelot is a silly place Co-authored-by: irinatomic <irina.z.tomic@gmail.com> --------- Co-authored-by: irinatomic <irina.z.tomic@gmail.com> * Add update function for Camelot pool * Fix impersonated DAI address * Set initial tokenFromAmount display value to empty string Quality of life thing * M3_81: Adapt uniswap quoter to include state changes (#22) * Feature/p23-59 :retrieve balancer data for weighted pools (#23) * M3_66: Get Balancer pool info from Graph and fix refactoring mistakes * Initial Balancer Stable Math * Change placement of sdk utils folder * Fetch pool info on-chain * Initial Balancer Weighted Math * Trying to test Balancer Swap function * Add logic for testing Balancer Stable Math * M3_79: Retrieve Balancer data on-chain * Enable sending funds for testing Tenderly * Merge with M3 (fix additional bugs) * remove not used data * Resolve conflict with UniV3 * M3_79: Unify balancer data * M3_79: Fix bugs * M3_79: Fix bugs 2 * M3_79: UI improvement, test done on Sushi, Uni and Balancer * NOT using Balancer Stable * M3_88: Add custom address * Fix balancer stable constructor bug * M3_87: Base for executing swap. Error: BAL#401 while calling BalancerVault.swap() * Balancer solidty swap integration * Resolve conflicts with M1 Co-Authored-By: Daniil Grbić <mi20042@alas.matf.bg.ac.rs> * Resolve merge conflicts * Add Dai funding * Resolve mini M3 branch conflicts --------- Co-authored-by: Irina Tomic <irina.z.tomic@gmail.com> Co-authored-by: Daniil Grbić <daniilgrbic@gmail.com> Co-authored-by: Daniil Grbić <mi20042@alas.matf.bg.ac.rs> * Change hardhat to tenderly * Add comments * P23-41: Detailed routing diagram and various minor improvements * Hash intermediary values for Curve calculations * M3_98: Add Camelot swap * Add missing Radiant Oracle * Implement swap function including splits (#24) * M3_80: Implement first version of swap function with splitting * M3_80: Add missing amount in difference * M3_80: Refactor swap function for splitting and change format * M3_80: Refactor types for splitting and uni like algo and make it generic for split function * Add curve fix and include balancer poolId slice * M3_99: Refactor solidity and add some improvements (#25) * Introduction and app * Change * Add solidity and improvements docs * Add algo_unilike, enhanced introduction and app * Iterative splitting algo docs * Minor improvements added in introduction and solidity * Add result page * Add data retrieval page and change image sizes * Fix missing images * Add decenter logo * Change decenter logo * Change overall README * Delete .DS_Store * Center images * Add "Why Arbitrum" explanation * Mini improvement * Update 2_app.md * Choose pools * Update README.md --------- Co-authored-by: Rajko Zagorac <rajkozgrc4@gmail.com> Co-authored-by: Dragan Mitrašinović <dragan.mitrasinovic19@gmail.com> Co-authored-by: Daniil Grbić <daniilgrbic@gmail.com> Co-authored-by: Brankonymous <brankogrbic01@gmail.com> Co-authored-by: Rajko Zagorac <57317299+rajko-z@users.noreply.github.com> Co-authored-by: Brankonymous <45096660+Brankonymous@users.noreply.github.com> Co-authored-by: Daniil Grbić <mi20042@alas.matf.bg.ac.rs>
3.7 KiB
Solidity
RateX has one main contract, found in contracts/contracts/rateX/RateX.sol, that will group all DEXes implemented and unify them trough interface (each DEX would need to implement its swap function). When calling for quote from SDK, RateX will have same functionalities for every DEX, making the developer not relying on specific DEX functionalities.
Every DEX has it's own pool functionality needed for calculating swap (Dex.sol) and retrieving pool info (Helper.sol), so let's dive into each:
Uniswap V3
Pool info
For every pool we need :
struct PoolInfo {
address pool;
address token0;
address token1;
int24 tick;
int128 tickLiquidityNet;
int24 tickSpacing;
uint24 fee;
uint160 sqrtPriceX96;
uint128 liquidity;
}
and
struct TickData {
int24 tick;
bool initialized;
int128 liquidityNet;
}
ZeroForOneTicks and OneForZero ticks
We decided that 15 ticks is optimal way for balancing speed and prize. Check fetchPoolsData in UniswapHelper.sol for more info.
Swap
In swap function in UniswapV3Dex.sol we call exactInputSingle got from Uniswap router:
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
SushiSwap V2
Pool info
We need to get reserves for each token pair.
Check getPoolsData in SushiSwapHelper.sol for more info.
Swap
Call selected function in Sushi's router contract, found in SushiSwapDex.sol:
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
Curve
Pool info
We need to get balances, A and fee.
Check fetchPoolsData in CurveHelper.sol for more info.
Swap
Call exchange() function in Curve's pool address contract (example):
function exchange(int128 i, int128 j, uint256 dx, uint256 min_dy, address to) external returns(uint256);
Balancer
NOTE: We support only pools with weighted math
Pool info
We need to return
uint8 decimals, uint256 invariant, address[] memory tokens, uint256[] memory balances, uint256[] memory weights, uint256 feePercentage
Every part of the data needed can be extracted using different method, so check getWeightedPoolInfo for more info in file BalancerHelper.sol
Swap
We need to create structs SingleSwap (for pool and token info) FundManagement (for payment info).
Check BalancerDex.sol for more info.
Camelot
Pool info
We need to get only reserves for additional info. Check getPoolsData in CamelotHelper.sol
Swap
We call swapExactTokensForTokensSupportingFeeOnTransferTokens function in CamelotDex.sol:
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
address refferer,
uint256 deadline
) external;
Test
Test for every DEX (and also RateX) can be found under tests/ written in javascript. For testing our contracts, you should run:
npx hardhat test 'insert_file_name.js` --network hardhat
NOTE: Some test could not work currently during our latest phase of internship where we focused on presenting the work (changing the solidity side of the app), and did not update the tests. Sorry for inconvinience, we will fix this issue in the following period.