Skip to content

Commit

Permalink
feat: swap routes WIP 🚧
Browse files Browse the repository at this point in the history
- add common estimation methods
- wrap/unwrap estimate implementation
- find best route
  • Loading branch information
toniocodo committed Sep 5, 2023
1 parent e6d4ca4 commit 72592c5
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 149 deletions.
45 changes: 34 additions & 11 deletions libs/oeth/swap/src/actions/defaultApi.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
import { isNilOrEmpty } from '@origin/shared/utils';

import { getAvailableRoutes } from '../utils';
import type {
EstimateAmount,
EstimateGas,
EstimateRoute,
Swap,
} from '../types';

const estimateAmount: EstimateAmount = async (
_tokenIn,
_tokenOut,
amountIn,
) => {
if (amountIn === 0n) {
return 0n;
}

import type { SwapApi, SwapState } from '../types';
return amountIn;
};

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateGas: EstimateGas = async (_tokenIn, _tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn;
return 0n;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateRoute: EstimateRoute = async (
tokenIn,
tokenOut,
amountIn,
route,
) => {
if (amountIn === 0n) {
return [];
return { ...route, estimatedAmount: 0n, gas: 0n, rate: 0 };
}

return getAvailableRoutes(tokenIn, tokenOut);
const estimatedAmount = await estimateAmount(tokenIn, tokenOut, amountIn);

return { ...route, estimatedAmount, gas: 0n, rate: 0 };
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
const swap: Swap = async (_tokenIn, _tokenOut, amountIn, route) => {
if (amountIn === 0n || isNilOrEmpty(route)) {
return;
}
};

export default {
estimateAmount,
estimateRoutes,
estimateGas,
estimateRoute,
swap,
} as SwapApi;
};
24 changes: 3 additions & 21 deletions libs/oeth/swap/src/actions/mintVault.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import { isNilOrEmpty } from '@origin/shared/utils';
import type { EstimateAmount } from '../types';

import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (tokenIn, tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn * 2n;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return [];
}

return [];
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as Partial<SwapApi>;
};
6 changes: 3 additions & 3 deletions libs/oeth/swap/src/actions/redeemVault.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SwapApi, SwapState } from '../types';
import type { EstimateAmount } from '../types';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (tokenIn, tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}
Expand All @@ -10,4 +10,4 @@ const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {

export default {
estimateAmount,
} as Partial<SwapApi>;
};
29 changes: 13 additions & 16 deletions libs/oeth/swap/src/actions/swapCurve.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import curve from '@curvefi/api';
import { isNilOrEmpty } from '@origin/shared/utils';
import { formatUnits, parseUnits } from 'viem';

import { getAvailableRoutes } from '../utils';

import type { SwapApi, SwapState } from '../types';
import type { EstimateAmount, EstimateRoute } from '../types';

const ETH = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (tokenIn, tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}
Expand All @@ -22,22 +19,22 @@ const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
return parseUnits(routes.output, tokenOut.decimals);
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateRoute: EstimateRoute = async (
tokenIn,
tokenOut,
amountIn,
route,
) => {
if (amountIn === 0n) {
return [];
return { ...route, estimatedAmount: 0n, gas: 0n, rate: 0 };
}

return getAvailableRoutes(tokenIn, tokenOut);
};
const estimatedAmount = await estimateAmount(tokenIn, tokenOut, amountIn);

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
return { ...route, estimatedAmount, gas: 0n, rate: 0 };
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as Partial<SwapApi>;
estimateRoute,
};
26 changes: 3 additions & 23 deletions libs/oeth/swap/src/actions/swapZapperEth.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { isNilOrEmpty } from '@origin/shared/utils';
import type { EstimateAmount } from '../types';

import { getAvailableRoutes } from '../utils';

import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (tokenIn, tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return [];
}

return getAvailableRoutes(tokenIn, tokenOut);
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as SwapApi;
};
26 changes: 3 additions & 23 deletions libs/oeth/swap/src/actions/swapZapperSfrxeth.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { isNilOrEmpty } from '@origin/shared/utils';
import type { EstimateAmount } from '../types';

import { getAvailableRoutes } from '../utils';

import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (tokenIn, tokenOut, amountIn) => {
if (amountIn === 0n) {
return 0n;
}

return amountIn;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
if (amountIn === 0n) {
return [];
}

return getAvailableRoutes(tokenIn, tokenOut);
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
}
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as SwapApi;
};
77 changes: 62 additions & 15 deletions libs/oeth/swap/src/actions/unwrapWOETH.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { contracts } from '@origin/shared/contracts';
import { contracts, whales } from '@origin/shared/contracts';
import { isNilOrEmpty } from '@origin/shared/utils';
import { readContract } from '@wagmi/core';
import { getAccount, getPublicClient, readContract } from '@wagmi/core';

import { getAvailableRoutes } from '../utils';
import type { EstimateAmount, EstimateGas, EstimateRoute } from '../types';

import type { SwapApi, SwapState } from '../types';

const estimateAmount = async ({ amountIn }: SwapState) => {
const estimateAmount: EstimateAmount = async (
_tokenIn,
_tokenOut,
amountIn,
) => {
if (amountIn === 0n) {
return 0n;
}
Expand All @@ -21,22 +23,67 @@ const estimateAmount = async ({ amountIn }: SwapState) => {
return data;
};

const estimateRoutes = async ({ tokenIn, tokenOut, amountIn }: SwapState) => {
const estimateGas: EstimateGas = async (_tokenIn, _tokenOut, amountIn) => {
let gasEstimate = 0n;
let isError = false;

const publicClient = getPublicClient();

if (amountIn === 0n) {
return [];
return gasEstimate;
}

const { address } = getAccount();

if (!isNilOrEmpty(address)) {
try {
gasEstimate = await publicClient.estimateContractGas({
address: contracts.mainnet.WOETH.address,
abi: contracts.mainnet.WOETH.abi,
functionName: 'redeem',
args: [amountIn, address, address],
account: address,
});
} catch {
isError = true;
}
}

return getAvailableRoutes(tokenIn, tokenOut);
if (isError) {
try {
gasEstimate = await publicClient.estimateContractGas({
address: contracts.mainnet.WOETH.address,
abi: contracts.mainnet.WOETH.abi,
functionName: 'redeem',
args: [amountIn, whales.mainnet.WOETH, whales.mainnet.WOETH],
account: whales.mainnet.WOETH,
});
} catch {}
}

return gasEstimate;
};

const swap = async ({ tokenIn, tokenOut, amountIn, swapRoute }: SwapState) => {
if (amountIn === 0n || isNilOrEmpty(swapRoute)) {
return;
const estimateRoute: EstimateRoute = async (
tokenIn,
tokenOut,
amountIn,
route,
) => {
if (amountIn === 0n) {
return { ...route, estimatedAmount: 0n, gas: 0n, rate: 0 };
}

const [estimatedAmount, gas] = await Promise.all([
estimateAmount(tokenIn, tokenOut, amountIn),
estimateGas(tokenIn, tokenOut, amountIn),
]);

return { ...route, estimatedAmount, gas, rate: 0 };
};

export default {
estimateAmount,
estimateRoutes,
swap,
} as SwapApi;
estimateGas,
estimateRoute,
};
Loading

0 comments on commit 72592c5

Please sign in to comment.