Skip to content

Commit

Permalink
feat: add allowance for curve
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Sep 13, 2023
1 parent e0a0a18 commit eed0b54
Showing 1 changed file with 35 additions and 1 deletion.
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

0 comments on commit eed0b54

Please sign in to comment.