Skip to content

Commit

Permalink
fix route claim and estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Dec 12, 2024
1 parent eb6def0 commit 2e11d9e
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 181 deletions.
176 changes: 90 additions & 86 deletions apps/pwa/src/app/api/deposit/claim/route.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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},
);
}
}
194 changes: 99 additions & 95 deletions apps/pwa/src/app/api/deposit/estimate-claim/route.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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},
);
}
}

0 comments on commit 2e11d9e

Please sign in to comment.