Skip to content

Commit

Permalink
if estimate fee UI failed => do check 0 balance and nonce 0
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed Jul 5, 2024
1 parent 42ba26a commit aa09e68
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions JoyboyCommunity/src/screens/Tips/index.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { NDKEvent, NDKKind } from '@nostr-dev-kit/ndk';
import { useAccount, useProvider } from '@starknet-react/core';
import { Fraction } from '@uniswap/sdk-core';
import { FlatList, RefreshControl, View } from 'react-native';
import { byteArray, cairo, CallData, uint256 } from 'starknet';

import { Button, Divider, Header, Text } from '../../components';
import { ESCROW_ADDRESSES } from '../../constants/contracts';
import { CHAIN_ID } from '../../constants/env';
import { Entrypoint } from '../../constants/misc';
import { ETH } from '../../constants/tokens';
import { useNostrContext } from '../../context/NostrContext';
import { useStyles, useTips, useWaitConnection } from '../../hooks';
import { useClaim, useEstimateClaim } from '../../hooks/api';
import { useToast, useTransaction, useTransactionModal, useWalletModal } from '../../hooks/modals';
import { decimalsScale } from '../../utils/helpers';
import {NDKEvent, NDKKind} from '@nostr-dev-kit/ndk';
import {useAccount, useProvider} from '@starknet-react/core';
import {Fraction} from '@uniswap/sdk-core';
import {FlatList, RefreshControl, View} from 'react-native';
import {byteArray, cairo, CallData, uint256} from 'starknet';

import {Button, Divider, Header, Text} from '../../components';
import {ESCROW_ADDRESSES} from '../../constants/contracts';
import {CHAIN_ID} from '../../constants/env';
import {Entrypoint} from '../../constants/misc';
import {ETH} from '../../constants/tokens';
import {useNostrContext} from '../../context/NostrContext';
import {useStyles, useTips, useWaitConnection} from '../../hooks';
import {useClaim, useEstimateClaim} from '../../hooks/api';
import {useToast, useTransaction, useTransactionModal, useWalletModal} from '../../hooks/modals';
import {decimalsScale} from '../../utils/helpers';
import stylesheet from './styles';

export const Tips: React.FC = () => {
const styles = useStyles(stylesheet);

const tips = useTips();
const { ndk } = useNostrContext();
const {ndk} = useNostrContext();

const { provider } = useProvider();
const {provider} = useProvider();
const account = useAccount();
const sendTransaction = useTransaction();
const claim = useClaim();
const estimateClaim = useEstimateClaim();
const walletModal = useWalletModal();
const waitConnection = useWaitConnection();
const { show: showTransactionModal } = useTransactionModal();
const { showToast } = useToast();
const {show: showTransactionModal} = useTransactionModal();
const {showToast} = useToast();

const onClaimPress = async (depositId: number) => {
if (!account.address) {
Expand All @@ -56,14 +56,14 @@ export const Tips: React.FC = () => {
// Default min to 0 => Change to a constant or UI estimate base claim
// Nonce first account tx => deduced it never do a tx before.
const minAmountBalanceToClaim = BigInt(0);
// const nonce= await connectedAccount?.account?.getNonce()
const nonce = await connectedAccount?.account?.getNonce();

const [balanceLow, balanceHigh] = await provider.callContract({
contractAddress: ETH[CHAIN_ID].address,
entrypoint: Entrypoint.BALANCE_OF,
calldata: [connectedAccount.address],
});
const balance = uint256.uint256ToBN({ low: balanceLow, high: balanceHigh });
const balance = uint256.uint256ToBN({low: balanceLow, high: balanceHigh});

// Send the claim through the wallet

Expand Down Expand Up @@ -93,23 +93,21 @@ export const Tips: React.FC = () => {

let estimateFee;
try {

estimateFee = await connectedAccount?.account?.estimateInvokeFee({
contractAddress: ESCROW_ADDRESSES[await provider.getChainId()],
entrypoint: Entrypoint.CLAIM,
calldata: claimCalldata,
},)
});
} catch (error) {
console.log("Can't estimate UI fees")
console.log("Can't estimate UI fees");
}


console.log("estimateFEe",estimateFee)
console.log('estimateFEe', estimateFee);
/** @TODO add estimate claim on the UI too? */
if ((estimateFee?.overall_fee && balance < estimateFee?.overall_fee) ||
balance <= minAmountBalanceToClaim
if (
(estimateFee?.overall_fee && balance < estimateFee?.overall_fee) ||
(balance <= minAmountBalanceToClaim && Number(nonce) == 0) // check 0 balance and 0 nonce on this account
) {

const feeResult = await estimateClaim.mutateAsync(await getNostrEvent(BigInt(1)));
const fee = BigInt(feeResult.data.fee);
// Send the claim through backend
Expand All @@ -120,19 +118,17 @@ export const Tips: React.FC = () => {
showTransactionModal(txHash, async (receipt) => {
if (receipt.isSuccess()) {
tips.refetch();
showToast({ type: 'success', title: 'Tip claimed successfully' });
showToast({type: 'success', title: 'Tip claimed successfully'});
} else {
let description = 'Please Try Again Later.';
if (receipt.isRejected()) {
description = receipt.transaction_failure_reason.error_message;
}

showToast({ type: 'error', title: `Failed to claim the tip. ${description}` });
showToast({type: 'error', title: `Failed to claim the tip. ${description}`});
}
});
} else {


const receipt = await sendTransaction({
calls: [
{
Expand All @@ -145,14 +141,14 @@ export const Tips: React.FC = () => {

if (receipt?.isSuccess()) {
tips.refetch();
showToast({ type: 'success', title: 'Tip claimed successfully' });
showToast({type: 'success', title: 'Tip claimed successfully'});
} else {
let description = 'Please Try Again Later.';
if (receipt?.isRejected()) {
description = receipt.transaction_failure_reason.error_message;
}

showToast({ type: 'error', title: `Failed to claim the tip. ${description}` });
showToast({type: 'error', title: `Failed to claim the tip. ${description}`});
}
}
};
Expand All @@ -166,7 +162,7 @@ export const Tips: React.FC = () => {
data={tips.data ?? []}
ItemSeparatorComponent={() => <View style={styles.separator} />}
keyExtractor={(item) => item.event.transaction_hash}
renderItem={({ item }) => {
renderItem={({item}) => {
const amount = new Fraction(item.amount, decimalsScale(item.token.decimals)).toFixed(6);

return (
Expand Down

0 comments on commit aa09e68

Please sign in to comment.