diff --git a/JoyboyCommunity/src/screens/Tips/index.tsx b/JoyboyCommunity/src/screens/Tips/index.tsx index 065fa4c4..8ed183a7 100644 --- a/JoyboyCommunity/src/screens/Tips/index.tsx +++ b/JoyboyCommunity/src/screens/Tips/index.tsx @@ -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) { @@ -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 @@ -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 @@ -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: [ { @@ -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}`}); } } }; @@ -166,7 +162,7 @@ export const Tips: React.FC = () => { data={tips.data ?? []} ItemSeparatorComponent={() => } keyExtractor={(item) => item.event.transaction_hash} - renderItem={({ item }) => { + renderItem={({item}) => { const amount = new Fraction(item.amount, decimalsScale(item.token.decimals)).toFixed(6); return (