From 2e11d9ec9e85a4f9aa3be2ead3e7191957880d87 Mon Sep 17 00:00:00 2001 From: MSGhais Date: Thu, 12 Dec 2024 15:08:21 +0100 Subject: [PATCH] fix route claim and estimate --- apps/pwa/src/app/api/deposit/claim/route.ts | 176 ++++++++-------- .../app/api/deposit/estimate-claim/route.ts | 194 +++++++++--------- 2 files changed, 189 insertions(+), 181 deletions(-) diff --git a/apps/pwa/src/app/api/deposit/claim/route.ts b/apps/pwa/src/app/api/deposit/claim/route.ts index 178fd3f77..623ecee4d 100644 --- a/apps/pwa/src/app/api/deposit/claim/route.ts +++ b/apps/pwa/src/app/api/deposit/claim/route.ts @@ -1,6 +1,10 @@ +import {fetchBuildExecuteTransaction, fetchQuotes} from '@avnu/avnu-sdk'; import {NextRequest, NextResponse} from 'next/server'; import {Calldata} from 'starknet'; +import {ESCROW_ADDRESSES, ETH_ADDRESSES, STRK_ADDRESSES} from '@/constants/contracts'; +import {AVNU_URL, CHAIN_ID, Entrypoint} from '@/constants/misc'; +import {account} from '@/services/account'; import {ErrorCode} from '@/utils/errors'; import {HTTPStatus} from '@/utils/http'; import {ClaimSchema} from '@/utils/validation'; @@ -34,90 +38,90 @@ export async function POST(request: NextRequest) { throw error; } - // try { - // if ( - // gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] || - // gasTokenAddress === STRK_ADDRESSES[CHAIN_ID] - // ) { - // // ETH | STRK transaction - - // const {transaction_hash} = await account.execute( - // [ - // { - // contractAddress: ESCROW_ADDRESSES[CHAIN_ID], - // entrypoint: Entrypoint.CLAIM, - // calldata: claimCallData, - // }, - // ], - // { - // version: gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] ? 1 : 3, - // maxFee: gasAmount, - // }, - // ); - - // return NextResponse.json({transaction_hash}, {status: HTTPStatus.OK}); - // } else { - // // ERC20 transaction - - // const result = await account.estimateInvokeFee([ - // { - // contractAddress: ESCROW_ADDRESSES[CHAIN_ID], - // entrypoint: Entrypoint.CLAIM, - // calldata: claimCallData, - // }, - // ]); - - // const gasFeeQuotes = await fetchQuotes( - // { - // buyTokenAddress: ETH_ADDRESSES[CHAIN_ID], - // sellTokenAddress: gasTokenAddress, - // sellAmount: gasAmount, - // }, - // {baseUrl: AVNU_URL}, - // ); - // const gasFeeQuote = gasFeeQuotes[0]; - - // if (!gasFeeQuote) { - // return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); - // } - - // if (result.overall_fee > gasFeeQuote.buyAmount) { - // return NextResponse.json( - // {code: ErrorCode.INVALID_GAS_AMOUNT}, - // {status: HTTPStatus.BadRequest}, - // ); - // } - - // const {calls: swapCalls} = await fetchBuildExecuteTransaction( - // gasFeeQuote.quoteId, - // account.address, - // undefined, - // undefined, - // {baseUrl: AVNU_URL}, - // ); - - // const {transaction_hash} = await account.execute( - // [ - // { - // contractAddress: ESCROW_ADDRESSES[CHAIN_ID], - // entrypoint: Entrypoint.CLAIM, - // calldata: claimCallData, - // }, - // ...swapCalls, - // ], - // { - // maxFee: gasFeeQuote.buyAmount, - // }, - // ); - - // return NextResponse.json({transaction_hash}, {status: HTTPStatus.OK}); - // } - // } catch (error) { - // console.error(error); - - // return NextResponse.json( - // {code: ErrorCode.TRANSACTION_ERROR, error}, - // {status: HTTPStatus.InternalServerError}, - // ); - // } + try { + if ( + gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] || + gasTokenAddress === STRK_ADDRESSES[CHAIN_ID] + ) { + // ETH | STRK transaction + + const {transaction_hash} = await account.execute( + [ + { + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], + entrypoint: Entrypoint.CLAIM, + calldata: claimCallData, + }, + ], + { + version: gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] ? 1 : 3, + maxFee: gasAmount, + }, + ); + + return NextResponse.json({transaction_hash}, {status: HTTPStatus.OK}); + } else { + // ERC20 transaction + + const result = await account.estimateInvokeFee([ + { + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], + entrypoint: Entrypoint.CLAIM, + calldata: claimCallData, + }, + ]); + + const gasFeeQuotes = await fetchQuotes( + { + buyTokenAddress: ETH_ADDRESSES[CHAIN_ID], + sellTokenAddress: gasTokenAddress, + sellAmount: gasAmount, + }, + {baseUrl: AVNU_URL}, + ); + const gasFeeQuote = gasFeeQuotes[0]; + + if (!gasFeeQuote) { + return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); + } + + if (result.overall_fee > gasFeeQuote.buyAmount) { + return NextResponse.json( + {code: ErrorCode.INVALID_GAS_AMOUNT}, + {status: HTTPStatus.BadRequest}, + ); + } + + const {calls: swapCalls} = await fetchBuildExecuteTransaction( + gasFeeQuote.quoteId, + account.address, + undefined, + undefined, + {baseUrl: AVNU_URL}, + ); + + const {transaction_hash} = await account.execute( + [ + { + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], + entrypoint: Entrypoint.CLAIM, + calldata: claimCallData, + }, + ...swapCalls, + ], + { + maxFee: gasFeeQuote.buyAmount, + }, + ); + + return NextResponse.json({transaction_hash}, {status: HTTPStatus.OK}); + } + } catch (error) { + console.error(error); + + return NextResponse.json( + {code: ErrorCode.TRANSACTION_ERROR, error}, + {status: HTTPStatus.InternalServerError}, + ); + } } diff --git a/apps/pwa/src/app/api/deposit/estimate-claim/route.ts b/apps/pwa/src/app/api/deposit/estimate-claim/route.ts index 74a882bce..19216f041 100644 --- a/apps/pwa/src/app/api/deposit/estimate-claim/route.ts +++ b/apps/pwa/src/app/api/deposit/estimate-claim/route.ts @@ -1,6 +1,10 @@ +import {fetchBuildExecuteTransaction, fetchQuotes} from '@avnu/avnu-sdk'; import {NextRequest, NextResponse} from 'next/server'; import {Calldata} from 'starknet'; +import {ESCROW_ADDRESSES, ETH_ADDRESSES, STRK_ADDRESSES} from '@/constants/contracts'; +import {AVNU_URL, CHAIN_ID, Entrypoint} from '@/constants/misc'; +import {account} from '@/services/account'; import {ErrorCode} from '@/utils/errors'; import {HTTPStatus} from '@/utils/http'; import {ClaimSchema} from '@/utils/validation'; @@ -32,99 +36,99 @@ export async function POST(request: NextRequest) { throw error; } - // try { - // if ( - // gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] || - // gasTokenAddress === STRK_ADDRESSES[CHAIN_ID] - // ) { - // // ETH | STRK fee estimation - - // const result = await account.estimateInvokeFee( - // [ - // { - // contractAddress: ESCROW_ADDRESSES[CHAIN_ID], - // entrypoint: Entrypoint.CLAIM, - // calldata: claimCallData, - // }, - // ], - // { - // version: gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] ? 1 : 3, - // }, - // ); - - // // Using 1.1 as a multiplier to ensure the fee is enough - // const fee = ((result.overall_fee * BigInt(11)) / BigInt(10)).toString(); - - // return NextResponse.json({gasFee: fee, tokenFee: fee}, {status: HTTPStatus.OK}); - // } else { - // // ERC20 fee estimation - - // const quotes = await fetchQuotes( - // { - // sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], - // buyTokenAddress: gasTokenAddress, - // sellAmount: BigInt(1), - // takerAddress: account.address, - // }, - // {baseUrl: AVNU_URL}, - // ); - // const quote = quotes[0]; - - // if (!quote) { - // return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); - // } - - // const {calls: swapCalls} = await fetchBuildExecuteTransaction( - // quote.quoteId, - // account.address, - // undefined, - // undefined, - // {baseUrl: AVNU_URL}, - // ); - - // const result = await account.estimateInvokeFee( - // [ - // { - // contractAddress: ESCROW_ADDRESSES[CHAIN_ID], - // entrypoint: Entrypoint.CLAIM, - // calldata: claimCallData, - // }, - // ...swapCalls, - // ], - // { - // version: 1, - // }, - // ); - - // // Using 1.1 as a multiplier to ensure the fee is enough - // const ethFee = (result.overall_fee * BigInt(11)) / BigInt(10); - - // const feeQuotes = await fetchQuotes( - // { - // sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], - // buyTokenAddress: gasTokenAddress, - // sellAmount: ethFee, - // takerAddress: account.address, - // }, - // {baseUrl: AVNU_URL}, - // ); - // const feeQuote = feeQuotes[0]; - - // if (!feeQuote) { - // return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); - // } - - // return NextResponse.json( - // {gasFee: ethFee, tokenFee: feeQuote.buyAmount}, - // {status: HTTPStatus.OK}, - // ); - // } - // } catch (error) { - // console.error(error); - - // return NextResponse.json( - // {code: ErrorCode.ESTIMATION_ERROR, error}, - // {status: HTTPStatus.InternalServerError}, - // ); - // } + try { + if ( + gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] || + gasTokenAddress === STRK_ADDRESSES[CHAIN_ID] + ) { + // ETH | STRK fee estimation + + const result = await account.estimateInvokeFee( + [ + { + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], + entrypoint: Entrypoint.CLAIM, + calldata: claimCallData, + }, + ], + { + version: gasTokenAddress === ETH_ADDRESSES[CHAIN_ID] ? 1 : 3, + }, + ); + + // Using 1.1 as a multiplier to ensure the fee is enough + const fee = ((result.overall_fee * BigInt(11)) / BigInt(10)).toString(); + + return NextResponse.json({gasFee: fee, tokenFee: fee}, {status: HTTPStatus.OK}); + } else { + // ERC20 fee estimation + + const quotes = await fetchQuotes( + { + sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], + buyTokenAddress: gasTokenAddress, + sellAmount: BigInt(1), + takerAddress: account.address, + }, + {baseUrl: AVNU_URL}, + ); + const quote = quotes[0]; + + if (!quote) { + return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); + } + + const {calls: swapCalls} = await fetchBuildExecuteTransaction( + quote.quoteId, + account.address, + undefined, + undefined, + {baseUrl: AVNU_URL}, + ); + + const result = await account.estimateInvokeFee( + [ + { + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], + entrypoint: Entrypoint.CLAIM, + calldata: claimCallData, + }, + ...swapCalls, + ], + { + version: 1, + }, + ); + + // Using 1.1 as a multiplier to ensure the fee is enough + const ethFee = (result.overall_fee * BigInt(11)) / BigInt(10); + + const feeQuotes = await fetchQuotes( + { + sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], + buyTokenAddress: gasTokenAddress, + sellAmount: ethFee, + takerAddress: account.address, + }, + {baseUrl: AVNU_URL}, + ); + const feeQuote = feeQuotes[0]; + + if (!feeQuote) { + return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); + } + + return NextResponse.json( + {gasFee: ethFee, tokenFee: feeQuote.buyAmount}, + {status: HTTPStatus.OK}, + ); + } + } catch (error) { + console.error(error); + + return NextResponse.json( + {code: ErrorCode.ESTIMATION_ERROR, error}, + {status: HTTPStatus.InternalServerError}, + ); + } }