Skip to content

Commit

Permalink
fix: fee estimation eth bug (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-eren authored Jul 15, 2024
1 parent de5b85a commit 5970dff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
3 changes: 2 additions & 1 deletion website/.env.example
Original file line number Diff line number Diff line change
@@ -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"
PROVIDER_URL="https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/your_api_key"
NETWORK_NAME="SN_SEPOLIA" # SN_SEPOLIA, SN_MAIN
17 changes: 12 additions & 5 deletions website/src/app/api/deposit/calldata.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand All @@ -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],
});
Expand Down
15 changes: 6 additions & 9 deletions website/src/app/api/deposit/estimate-claim/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
Expand All @@ -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});
}
Expand All @@ -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,
},
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions website/src/constants/contracts.ts
Original file line number Diff line number Diff line change
@@ -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',
),
};
7 changes: 4 additions & 3 deletions website/src/constants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 5970dff

Please sign in to comment.