From 7786f897b62242e18a259d52168af0a4a8a490ae Mon Sep 17 00:00:00 2001 From: toniocodo Date: Fri, 2 Aug 2024 20:16:10 +0200 Subject: [PATCH] feat: add ETH - wOETH support for balancer route --- libs/defi/oeth/src/swap/constants.ts | 10 ++++ libs/shared/contracts/src/whales.ts | 5 ++ .../routes/src/oeth/swapBalancerOeth.ts | 54 ++++++++----------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/libs/defi/oeth/src/swap/constants.ts b/libs/defi/oeth/src/swap/constants.ts index 6410c3615..407dd38b1 100644 --- a/libs/defi/oeth/src/swap/constants.ts +++ b/libs/defi/oeth/src/swap/constants.ts @@ -6,6 +6,11 @@ import type { OethSwapAction } from './types'; export const oethSwapRoutes: SwapRoute[] = [ // Mint + { + tokenIn: tokens.arbitrum.ETH, + tokenOut: tokens.arbitrum.wOETH, + action: 'swap-balancer-oeth', + }, { tokenIn: tokens.arbitrum.WETH, tokenOut: tokens.arbitrum.wOETH, @@ -34,6 +39,11 @@ export const oethSwapRoutes: SwapRoute[] = [ action: 'swap-curve-oeth', }, // Redeem + { + tokenIn: tokens.arbitrum.wOETH, + tokenOut: tokens.arbitrum.ETH, + action: 'swap-balancer-oeth', + }, { tokenIn: tokens.arbitrum.wOETH, tokenOut: tokens.arbitrum.WETH, diff --git a/libs/shared/contracts/src/whales.ts b/libs/shared/contracts/src/whales.ts index e39df71e2..f823de850 100644 --- a/libs/shared/contracts/src/whales.ts +++ b/libs/shared/contracts/src/whales.ts @@ -8,4 +8,9 @@ export const whales = { OUSD: '0x70fCE97d671E81080CA3ab4cc7A59aAc2E117137', wOUSD: '0x3dD413Fd4D03b1d8fD2C9Ed34553F7DeC3B26F5C', }, + arbitrum: { + ETH: '0xF977814e90dA44bFA03b6295A0616a897441aceC', + wOETH: '0x9f4D6f98F29c1D482bCF0F85683155E0B3e015f5', + WETH: '0x70d95587d40A2caf56bd97485aB3Eec10Bee6336', + }, } as const; diff --git a/libs/shared/routes/src/oeth/swapBalancerOeth.ts b/libs/shared/routes/src/oeth/swapBalancerOeth.ts index 08a01be38..bda1e4f21 100644 --- a/libs/shared/routes/src/oeth/swapBalancerOeth.ts +++ b/libs/shared/routes/src/oeth/swapBalancerOeth.ts @@ -1,4 +1,4 @@ -import { contracts } from '@origin/shared/contracts'; +import { contracts, whales } from '@origin/shared/contracts'; import { isNilOrEmpty, subPercentage, @@ -11,7 +11,7 @@ import { simulateContract, writeContract, } from '@wagmi/core'; -import { erc20Abi, formatUnits } from 'viem'; +import { erc20Abi, formatUnits, maxUint256 } from 'viem'; import { defaultRoute } from '../defaultRoute'; @@ -50,7 +50,7 @@ const estimateAmount: EstimateAmount = async ( { config }, { tokenIn, tokenOut, amountIn }, ) => { - if (amountIn === 0n || !tokenIn?.address || !tokenOut?.address) { + if (amountIn === 0n) { return 0n; } @@ -62,8 +62,8 @@ const estimateAmount: EstimateAmount = async ( { poolId: WethWoethPoolId, kind: 0, - assetIn: tokenIn.address, - assetOut: tokenOut.address, + assetIn: tokenIn.address ?? ZERO_ADDRESS, + assetOut: tokenOut.address ?? ZERO_ADDRESS, amount: amountIn, userData: defaultUserData, }, @@ -84,15 +84,9 @@ const estimateGas: EstimateGas = async ( { tokenIn, tokenOut, amountIn, slippage, amountOut }, ) => { let gasEstimate = 0n; - const { address } = getAccount(config); const publicClient = getPublicClient(config, { chainId: tokenIn.chainId }); - if ( - amountIn === 0n || - !publicClient || - !tokenIn?.address || - !tokenOut.address - ) { + if (amountIn === 0n || !publicClient) { return gasEstimate; } @@ -100,6 +94,9 @@ const estimateGas: EstimateGas = async ( [amountOut ?? 0n, tokenOut.decimals], slippage, ); + const user = isNilOrEmpty(tokenIn?.address) + ? whales.arbitrum.ETH + : whales.arbitrum.wOETH; try { gasEstimate = await publicClient.estimateContractGas({ @@ -110,20 +107,21 @@ const estimateGas: EstimateGas = async ( { poolId: WethWoethPoolId, kind: 0, - assetIn: tokenIn.address, - assetOut: tokenOut.address, + assetIn: tokenIn.address ?? ZERO_ADDRESS, + assetOut: tokenOut.address ?? ZERO_ADDRESS, amount: amountIn, userData: defaultUserData, }, { - recipient: address ?? ZERO_ADDRESS, - sender: address ?? ZERO_ADDRESS, + recipient: user, + sender: user, fromInternalBalance: false, toInternalBalance: false, }, minAmountOut[0], deadline, ], + ...(isNilOrEmpty(tokenIn.address) && { value: amountIn }), }); } catch { gasEstimate = 220_000n; @@ -132,11 +130,11 @@ const estimateGas: EstimateGas = async ( return gasEstimate; }; -const allowance: Allowance = async ({ config }, { tokenIn }) => { +const allowance: Allowance = async ({ config }, { tokenIn, tokenOut }) => { const { address } = getAccount(config); if (!address || !tokenIn?.address) { - return 0n; + return maxUint256; } const allowance = await readContract(config, { @@ -152,7 +150,7 @@ const allowance: Allowance = async ({ config }, { tokenIn }) => { const estimateApprovalGas: EstimateApprovalGas = async ( { config }, - { tokenIn, amountIn }, + { tokenIn, amountIn, tokenOut }, ) => { let approvalEstimate = 0n; const { address } = getAccount(config); @@ -217,10 +215,7 @@ const estimateRoute: EstimateRoute = async ( }; }; -const approve: Approve = async ( - { config }, - { tokenIn, tokenOut, amountIn }, -) => { +const approve: Approve = async ({ config }, { tokenIn, amountIn }) => { if (!tokenIn?.address) { return null; } @@ -243,13 +238,7 @@ const swap: Swap = async ( ) => { const { address } = getAccount(config); - if ( - amountIn === 0n || - isNilOrEmpty(address) || - !tokenIn.address || - !tokenOut.address || - !address - ) { + if (amountIn === 0n || !address) { return null; } @@ -275,8 +264,8 @@ const swap: Swap = async ( { poolId: WethWoethPoolId, kind: 0, - assetIn: tokenIn.address, - assetOut: tokenOut.address, + assetIn: tokenIn.address ?? ZERO_ADDRESS, + assetOut: tokenOut.address ?? ZERO_ADDRESS, amount: amountIn, userData: defaultUserData, }, @@ -289,6 +278,7 @@ const swap: Swap = async ( minAmountOut[0], deadline, ], + ...(isNilOrEmpty(tokenIn.address) && { value: amountIn }), }); const hash = await writeContract(config, request);