Skip to content

Commit

Permalink
fix: pwa build issues (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
addegbenga authored Nov 28, 2024
1 parent dc56e1e commit 6c4041e
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 278 deletions.
5 changes: 4 additions & 1 deletion apps/pwa/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
config.externals.push('encoding');
config.externals.push('pino-pretty', 'lokijs', 'encoding');
return config;
},
experimental: {
optimizePackageImports: ['@chakra-ui/react'],
},
async headers() {
return [
{
Expand Down
176 changes: 86 additions & 90 deletions apps/pwa/src/app/api/deposit/claim/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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 @@ -38,90 +34,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},
// );
// }
}
Loading

0 comments on commit 6c4041e

Please sign in to comment.