import { IClaimData } from '@energyweb/issuer' ; import { CertificateDTO , getIrecCertificateControllerGetAllQueryKey , } from '@energyweb/issuer-irec-api-react-query-client' ; import { BeneficiaryDTO } from '@energyweb/origin-organization-irec-api-react-query-client' ; import { NotificationTypeEnum , showNotification , } from '@energyweb/origin-ui-core' ; import { PowerFormatter } from '@energyweb/origin-ui-utils' ; import { Dayjs } from 'dayjs' ; import { BigNumber } from '@ethersproject/bignumber' ; import { Dispatch , SetStateAction } from 'react' ; import { useTranslation } from 'react-i18next' ; import { useQueryClient } from 'react-query' ; import { useGetBlockchainCertificateHandler } from '../fetching' ; export const useRetireCertificateHandler = ( selectedBeneficiary , resetList , startDate , endDate , purpose , setTxPending ) => { const { t } = useTranslation ( ) ; const queryClient = useQueryClient ( ) ; const blockchainCertificatesQueryKey = getIrecCertificateControllerGetAllQueryKey ( ) ; const { getBlockchainCertificate , isLoading : isGetBlockchainLoading } = useGetBlockchainCertificateHandler ( ) ; const retireHandler = async < Id > ( id , amount ) => { try { const onChainCertificate = await getBlockchainCertificate ( id as unknown as CertificateDTO [ 'id' ] ) ; const formattedAmount = BigNumber . from ( PowerFormatter . getBaseValueFromValueInDisplayUnit ( Number ( amount ) ) ) ; const claimData = { beneficiary : selectedBeneficiary . name , location : selectedBeneficiary . location , countryCode : selectedBeneficiary . countryCode , periodStartDate : startDate . toISOString ( ) , periodEndDate : endDate . toISOString ( ) , purpose , } ; const transaction = await onChainCertificate . claim ( claimData , formattedAmount ) ; setTxPending ( true ) ; const receipt = await transaction . wait ( ) ; if ( receipt . status === 0 ) { throw new Error ( 'Transaction failed' ) ; } else { setTxPending ( false ) ; showNotification ( t ( 'certificate.blockchainInbox.notifications.retireSuccess' ) , NotificationTypeEnum . Success ) ; queryClient . resetQueries ( blockchainCertificatesQueryKey ) ; resetList ( ) ; } } catch ( error ) { console . error ( error ) ; showNotification ( t ( 'certificate.blockchainInbox.notifications.retireError' ) , NotificationTypeEnum . Error ) ; } } ; const isLoading = isGetBlockchainLoading ; return { retireHandler , isLoading } ; } ;