From 5970dffa9aadf0500067d04a0f48419bc0741512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=9Fur=20Eren?= Date: Mon, 15 Jul 2024 17:39:17 +0300 Subject: [PATCH] fix: fee estimation eth bug (#214) --- website/.env.example | 3 ++- website/src/app/api/deposit/calldata.ts | 17 ++++++++++++----- .../src/app/api/deposit/estimate-claim/route.ts | 15 ++++++--------- website/src/constants/contracts.ts | 11 +++++++---- website/src/constants/misc.ts | 7 ++++--- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/website/.env.example b/website/.env.example index 29354e87..314988a7 100644 --- a/website/.env.example +++ b/website/.env.example @@ -1,4 +1,5 @@ ACCOUNT_ADDRESS="0x" ACCOUNT_PRIVATE_KEY="0x" -PROVIDER_URL="https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/your_api_key" \ No newline at end of file +PROVIDER_URL="https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/your_api_key" +NETWORK_NAME="SN_SEPOLIA" # SN_SEPOLIA, SN_MAIN diff --git a/website/src/app/api/deposit/calldata.ts b/website/src/app/api/deposit/calldata.ts index d8a37682..6fe920de 100644 --- a/website/src/app/api/deposit/calldata.ts +++ b/website/src/app/api/deposit/calldata.ts @@ -1,8 +1,15 @@ import {verifyEvent} from 'nostr-tools'; -import {byteArray, cairo, CallData, uint256, validateAndParseAddress} from 'starknet'; +import { + byteArray, + cairo, + CallData, + getChecksumAddress, + uint256, + validateAndParseAddress, +} from 'starknet'; import {ESCROW_ADDRESSES} from '@/constants/contracts'; -import {Entrypoint} from '@/constants/misc'; +import {CHAIN_ID, Entrypoint} from '@/constants/misc'; import {provider} from '@/services/provider'; import {ErrorCode} from '@/utils/errors'; import {ClaimSchema} from '@/utils/validation'; @@ -22,8 +29,8 @@ export const getClaimCallData = async (data: (typeof ClaimSchema)['_output']) => } const depositId = cairo.felt(content[0]); - const recipientAddress = `0x${BigInt(content[1]).toString(16)}`; - const tokenAddress = `0x${BigInt(content[2]).toString(16)}`; + const recipientAddress = getChecksumAddress(`0x${BigInt(content[1]).toString(16)}`); + const tokenAddress = getChecksumAddress(`0x${BigInt(content[2]).toString(16)}`); const gasAmount = BigInt(content[3]); try { @@ -34,7 +41,7 @@ export const getClaimCallData = async (data: (typeof ClaimSchema)['_output']) => } const deposit = await provider.callContract({ - contractAddress: ESCROW_ADDRESSES[await provider.getChainId()], + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], entrypoint: Entrypoint.GET_DEPOSIT, calldata: [depositId], }); diff --git a/website/src/app/api/deposit/estimate-claim/route.ts b/website/src/app/api/deposit/estimate-claim/route.ts index e9093aa3..968525eb 100644 --- a/website/src/app/api/deposit/estimate-claim/route.ts +++ b/website/src/app/api/deposit/estimate-claim/route.ts @@ -3,9 +3,8 @@ import {NextRequest, NextResponse} from 'next/server'; import {Calldata} from 'starknet'; import {ESCROW_ADDRESSES, ETH_ADDRESSES} from '@/constants/contracts'; -import {AVNU_URL, Entrypoint} from '@/constants/misc'; +import {AVNU_URL, CHAIN_ID, Entrypoint} from '@/constants/misc'; import {account} from '@/services/account'; -import {provider} from '@/services/provider'; import {ErrorCode} from '@/utils/errors'; import {HTTPStatus} from '@/utils/http'; import {ClaimSchema} from '@/utils/validation'; @@ -38,12 +37,12 @@ export async function POST(request: NextRequest) { } try { - if (gasTokenAddress === ETH_ADDRESSES[await provider.getChainId()]) { + if (gasTokenAddress === ETH_ADDRESSES[CHAIN_ID]) { // ETH fee estimation const result = await account.estimateInvokeFee([ { - contractAddress: ESCROW_ADDRESSES[await provider.getChainId()], + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], entrypoint: Entrypoint.CLAIM, calldata: claimCallData, }, @@ -58,7 +57,7 @@ export async function POST(request: NextRequest) { const quotes = await fetchQuotes( { - sellTokenAddress: ETH_ADDRESSES[await provider.getChainId()], + sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], buyTokenAddress: gasTokenAddress, sellAmount: BigInt(1), takerAddress: account.address, @@ -67,8 +66,6 @@ export async function POST(request: NextRequest) { ); const quote = quotes[0]; - console.log(ETH_ADDRESSES[await provider.getChainId()], gasTokenAddress, quote); - if (!quote) { return NextResponse.json({code: ErrorCode.NO_ROUTE_FOUND}, {status: HTTPStatus.BadRequest}); } @@ -83,7 +80,7 @@ export async function POST(request: NextRequest) { const result = await account.estimateInvokeFee([ { - contractAddress: ESCROW_ADDRESSES[await provider.getChainId()], + contractAddress: ESCROW_ADDRESSES[CHAIN_ID], entrypoint: Entrypoint.CLAIM, calldata: claimCallData, }, @@ -95,7 +92,7 @@ export async function POST(request: NextRequest) { const feeQuotes = await fetchQuotes( { - sellTokenAddress: ETH_ADDRESSES[await provider.getChainId()], + sellTokenAddress: ETH_ADDRESSES[CHAIN_ID], buyTokenAddress: gasTokenAddress, sellAmount: ethFee, takerAddress: account.address, diff --git a/website/src/constants/contracts.ts b/website/src/constants/contracts.ts index 68565f6e..ea526f87 100644 --- a/website/src/constants/contracts.ts +++ b/website/src/constants/contracts.ts @@ -1,14 +1,17 @@ -import {constants} from 'starknet'; +import {constants, getChecksumAddress} from 'starknet'; export const ESCROW_ADDRESSES = { [constants.StarknetChainId.SN_MAIN]: '', // TODO: Add mainnet escrow address - [constants.StarknetChainId.SN_SEPOLIA]: + [constants.StarknetChainId.SN_SEPOLIA]: getChecksumAddress( '0x078a022e6906c83e049a30f7464b939b831ecbe47029480d7e89684f20c8d263', + ), }; export const ETH_ADDRESSES = { - [constants.StarknetChainId.SN_MAIN]: + [constants.StarknetChainId.SN_MAIN]: getChecksumAddress( '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7', - [constants.StarknetChainId.SN_SEPOLIA]: + ), + [constants.StarknetChainId.SN_SEPOLIA]: getChecksumAddress( '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7', + ), }; diff --git a/website/src/constants/misc.ts b/website/src/constants/misc.ts index 61ed60e9..7dd89ca0 100644 --- a/website/src/constants/misc.ts +++ b/website/src/constants/misc.ts @@ -15,7 +15,8 @@ export enum Entrypoint { } export const NETWORK_NAME = process.env.NETWORK_NAME as constants.NetworkName; -export const AVNU_URL = - process.env.NETWORK_NAME === 'SN_SEPOLIA' ? AVNU_SEPOLIA_BASE_URL : AVNU_BASE_URL; - if (!NETWORK_NAME) throw new Error('NETWORK_NAME is not set'); + +export const CHAIN_ID = constants.StarknetChainId[NETWORK_NAME]; + +export const AVNU_URL = NETWORK_NAME === 'SN_SEPOLIA' ? AVNU_SEPOLIA_BASE_URL : AVNU_BASE_URL;