From e0a0a185270017b5976cdf2ca2b97edabb852e64 Mon Sep 17 00:00:00 2001 From: toniocodo Date: Tue, 12 Sep 2023 22:14:32 +0200 Subject: [PATCH 1/2] refactor: remove default api file --- libs/oeth/swap/src/actions/defaultApi.ts | 48 ------------------------ libs/oeth/swap/src/actions/index.ts | 22 ++++++++++- 2 files changed, 21 insertions(+), 49 deletions(-) delete mode 100644 libs/oeth/swap/src/actions/defaultApi.ts diff --git a/libs/oeth/swap/src/actions/defaultApi.ts b/libs/oeth/swap/src/actions/defaultApi.ts deleted file mode 100644 index e502e6582..000000000 --- a/libs/oeth/swap/src/actions/defaultApi.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { - EstimateAmount, - EstimateGas, - EstimateRoute, - Swap, -} from '../types'; - -const estimateAmount: EstimateAmount = async ({ amountIn }) => { - console.log('Amount estimation not implemented'); - - return amountIn; -}; - -const estimateGas: EstimateGas = async () => { - console.log('Gas estimation not implemented'); - - return 0n; -}; - -const estimateRoute: EstimateRoute = async ({ - amountIn, - route, - tokenIn, - tokenOut, - slippage, -}) => { - 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, slippage }), - ]); - - return { ...route, estimatedAmount, gas, rate: 0 }; -}; - -const swap: Swap = async () => { - console.log('Route swap operation not implemented'); -}; - -export default { - estimateAmount, - estimateGas, - estimateRoute, - swap, -}; diff --git a/libs/oeth/swap/src/actions/index.ts b/libs/oeth/swap/src/actions/index.ts index bc0e6f939..83351b477 100644 --- a/libs/oeth/swap/src/actions/index.ts +++ b/libs/oeth/swap/src/actions/index.ts @@ -1,4 +1,3 @@ -import defaultApi from './defaultApi'; import mintVault from './mintVault'; import redeemVault from './redeemVault'; import swapCurve from './swapCurve'; @@ -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 = { 'swap-curve': { ...defaultApi, ...swapCurve }, 'swap-curve-eth': { ...defaultApi, ...swapCurveEth }, From eed0b546dc1760e0fa3bccd68deb1978f5801d66 Mon Sep 17 00:00:00 2001 From: toniocodo Date: Wed, 13 Sep 2023 11:06:22 +0200 Subject: [PATCH 2/2] feat: add allowance for curve --- libs/oeth/swap/src/actions/swapCurve/index.ts | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libs/oeth/swap/src/actions/swapCurve/index.ts b/libs/oeth/swap/src/actions/swapCurve/index.ts index bb155c68e..6cac40f8f 100644 --- a/libs/oeth/swap/src/actions/swapCurve/index.ts +++ b/libs/oeth/swap/src/actions/swapCurve/index.ts @@ -1,5 +1,6 @@ import { ETH_ADDRESS_CURVE, isNilOrEmpty } from '@origin/shared/utils'; import { + erc20ABI, getAccount, getPublicClient, prepareWriteContract, @@ -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) -