Skip to content

Commit

Permalink
feat: add ETH - wOETH support for balancer route
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Aug 2, 2024
1 parent fd438bf commit 7786f89
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
10 changes: 10 additions & 0 deletions libs/defi/oeth/src/swap/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type { OethSwapAction } from './types';

export const oethSwapRoutes: SwapRoute<OethSwapAction>[] = [
// Mint
{
tokenIn: tokens.arbitrum.ETH,
tokenOut: tokens.arbitrum.wOETH,
action: 'swap-balancer-oeth',
},
{
tokenIn: tokens.arbitrum.WETH,
tokenOut: tokens.arbitrum.wOETH,
Expand Down Expand Up @@ -34,6 +39,11 @@ export const oethSwapRoutes: SwapRoute<OethSwapAction>[] = [
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,
Expand Down
5 changes: 5 additions & 0 deletions libs/shared/contracts/src/whales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ export const whales = {
OUSD: '0x70fCE97d671E81080CA3ab4cc7A59aAc2E117137',
wOUSD: '0x3dD413Fd4D03b1d8fD2C9Ed34553F7DeC3B26F5C',
},
arbitrum: {
ETH: '0xF977814e90dA44bFA03b6295A0616a897441aceC',
wOETH: '0x9f4D6f98F29c1D482bCF0F85683155E0B3e015f5',
WETH: '0x70d95587d40A2caf56bd97485aB3Eec10Bee6336',
},
} as const;
54 changes: 22 additions & 32 deletions libs/shared/routes/src/oeth/swapBalancerOeth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { contracts } from '@origin/shared/contracts';
import { contracts, whales } from '@origin/shared/contracts';
import {
isNilOrEmpty,
subPercentage,
Expand All @@ -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';

Expand Down Expand Up @@ -50,7 +50,7 @@ const estimateAmount: EstimateAmount = async (
{ config },
{ tokenIn, tokenOut, amountIn },
) => {
if (amountIn === 0n || !tokenIn?.address || !tokenOut?.address) {
if (amountIn === 0n) {
return 0n;
}

Expand All @@ -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,
},
Expand All @@ -84,22 +84,19 @@ 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;
}

const minAmountOut = subPercentage(
[amountOut ?? 0n, tokenOut.decimals],
slippage,
);
const user = isNilOrEmpty(tokenIn?.address)
? whales.arbitrum.ETH
: whales.arbitrum.wOETH;

try {
gasEstimate = await publicClient.estimateContractGas({
Expand All @@ -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;
Expand All @@ -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, {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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,
},
Expand All @@ -289,6 +278,7 @@ const swap: Swap = async (
minAmountOut[0],
deadline,
],
...(isNilOrEmpty(tokenIn.address) && { value: amountIn }),
});
const hash = await writeContract(config, request);

Expand Down

0 comments on commit 7786f89

Please sign in to comment.