Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/swap-curve #36

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions libs/oeth/swap/src/actions/defaultApi.ts

This file was deleted.

22 changes: 21 additions & 1 deletion libs/oeth/swap/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import defaultApi from './defaultApi';
import mintVault from './mintVault';
import redeemVault from './redeemVault';
import swapCurve from './swapCurve';
Expand All @@ -10,6 +9,27 @@ import wrapOETH from './wrapOETH';

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

const defaultApi: SwapApi = {
estimateAmount: async ({ amountIn }) => {
console.log('Amount estimation not implemented');

return amountIn;
},
estimateGas: async () => {
console.log('Gas estimation not implemented');

return 0n;
},
estimateRoute: async ({ amountIn, route }) => {
console.log('Route estimation not implemented');

return { ...route, estimatedAmount: amountIn, gas: 0n, rate: 0 };
},
swap: async () => {
console.log('Route swap operation not implemented');
},
};

export const swapActions: Record<SwapAction, SwapApi> = {
'swap-curve': { ...defaultApi, ...swapCurve },
'swap-curve-eth': { ...defaultApi, ...swapCurveEth },
Expand Down
36 changes: 35 additions & 1 deletion libs/oeth/swap/src/actions/swapCurve/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ETH_ADDRESS_CURVE, isNilOrEmpty } from '@origin/shared/utils';
import {
erc20ABI,
getAccount,
getPublicClient,
prepareWriteContract,
Expand Down Expand Up @@ -149,10 +150,43 @@ const swap: Swap = async ({
slippage,
curve,
}) => {
if (amountIn === 0n) {
const { address } = getAccount();

if (amountIn === 0n || isNilOrEmpty(address)) {
return;
}

if (!isNilOrEmpty(tokenIn.address) && !isNilOrEmpty(tokenOut.address)) {
const allowance = await readContract({
address: tokenIn.address,
abi: erc20ABI,
functionName: 'allowance',
args: [address, curve.CurveRegistryExchange.address],
});

if (allowance < amountIn) {
try {
const { request } = await prepareWriteContract({
address: tokenIn.address,
abi: erc20ABI,
functionName: 'approve',
args: [curve.CurveRegistryExchange.address, amountIn],
});
const { hash } = await writeContract(request);
await waitForTransaction({ hash });

// TODO trigger notification
console.log(`swap curve exchange multiple approval done!`);
} catch (e) {
// TODO trigger notification
console.error(
`swap curve exchange multiple approval error!\n${e.message}`,
);
return;
}
}
}

const minAmountOut = parseUnits(
(
+formatUnits(amountOut, tokenOut.decimals) -
Expand Down