From 756b7d18eb3234100b9153a908586101b08941b9 Mon Sep 17 00:00:00 2001 From: Atatakai Date: Tue, 27 Aug 2024 17:53:24 +0400 Subject: [PATCH] (registry, launch, govern) fix: sendTransaction error --- .../Details/DetailsSubInfo/RewardsSection.tsx | 4 +- .../common-util/functions/requests.ts | 11 ++--- apps/govern/common-util/functions/requests.ts | 18 ++++--- apps/launch/common-util/functions/web3.ts | 11 +++-- .../src/lib/sendTransaction/helpers.ts | 48 +++++-------------- .../src/lib/sendTransaction/index.ts | 10 ++-- 6 files changed, 38 insertions(+), 64 deletions(-) diff --git a/apps/autonolas-registry/common-util/Details/DetailsSubInfo/RewardsSection.tsx b/apps/autonolas-registry/common-util/Details/DetailsSubInfo/RewardsSection.tsx index 3182492b..497f22ab 100644 --- a/apps/autonolas-registry/common-util/Details/DetailsSubInfo/RewardsSection.tsx +++ b/apps/autonolas-registry/common-util/Details/DetailsSubInfo/RewardsSection.tsx @@ -158,7 +158,7 @@ export const RewardsSection: FC = ({ ownerAddress, isOwner, const params = { account: account as Address, unitIds: [id], - unitTypes: [id], + unitTypes: [`${tokenomicsUnitType}`], }; await claimOwnerIncentivesRequest(params); @@ -171,7 +171,7 @@ export const RewardsSection: FC = ({ ownerAddress, isOwner, } finally { setIsClaimLoading(false); } - }, [account, id, canClaim, fetchPendingIncentives, refetch]); + }, [account, id, tokenomicsUnitType, canClaim, fetchPendingIncentives, refetch]); return ( diff --git a/apps/autonolas-registry/common-util/functions/requests.ts b/apps/autonolas-registry/common-util/functions/requests.ts index 2fc1a835..9e3c2a6a 100644 --- a/apps/autonolas-registry/common-util/functions/requests.ts +++ b/apps/autonolas-registry/common-util/functions/requests.ts @@ -1,11 +1,11 @@ import { Address } from 'viem'; -import { getEstimatedGasLimit, notifyError, sendTransaction } from 'libs/util-functions/src'; +import { getEstimatedGasLimit, notifyError } from 'libs/util-functions/src'; -import { SUPPORTED_CHAINS } from 'common-util/Login'; +import { sendTransaction } from 'common-util/functions'; import { DEFAULT_SERVICE_CREATION_ETH_TOKEN_ZEROS } from '../../util/constants'; -import { RPC_URLS, getDispenserContract, getServiceOwnerMultisigContract } from '../Contracts'; +import { getDispenserContract, getServiceOwnerMultisigContract } from '../Contracts'; import { checkIfGnosisSafe, getEthersProvider } from './index'; const FALLBACK_HANDLER_STORAGE_SLOT = @@ -66,10 +66,7 @@ export const claimOwnerIncentivesRequest = async ({ const estimatedGas = await getEstimatedGasLimit(claimFn, account); const fn = claimFn.send({ from: account, gasLimit: estimatedGas }); - const response = await sendTransaction(fn, account, { - supportedChains: SUPPORTED_CHAINS, - rpcUrls: RPC_URLS as Record, - }); + const response = await sendTransaction(fn, account); return response?.transactionHash; } catch (error) { window.console.log('Error occurred on claiming owner incentives'); diff --git a/apps/govern/common-util/functions/requests.ts b/apps/govern/common-util/functions/requests.ts index 28c506aa..b673fb4d 100644 --- a/apps/govern/common-util/functions/requests.ts +++ b/apps/govern/common-util/functions/requests.ts @@ -1,17 +1,15 @@ import { readContract, readContracts } from '@wagmi/core'; import { ethers } from 'ethers'; -import { Abi, AbiFunction, TransactionReceipt, parseUnits } from 'viem'; +import { Abi, AbiFunction, parseUnits } from 'viem'; import { Address } from 'viem'; import { mainnet } from 'viem/chains'; -import { sendTransaction } from '@autonolas/frontend-library'; - import { SERVICE_REGISTRY, STAKING_FACTORY, VE_OLAS, } from 'libs/util-contracts/src/lib/abiAndAddresses'; -import { getEstimatedGasLimit } from 'libs/util-functions/src'; +import { getEstimatedGasLimit, sendTransaction } from 'libs/util-functions/src'; import { SUPPORTED_CHAINS, wagmiConfig } from 'common-util/config/wagmi'; import { RPC_URLS } from 'common-util/constants/rpcs'; @@ -208,7 +206,7 @@ export const createLockRequest = async ({ rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (error) { window.console.log('Error occurred on creating lock for veOLAS'); throw error; @@ -237,7 +235,7 @@ export const updateIncreaseAmount = async ({ rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (e) { window.console.log('Error occurred on increasing amount with estimated gas'); throw e; @@ -269,7 +267,7 @@ export const updateIncreaseUnlockTime = async ({ rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (error) { window.console.log('Error occurred on increasing unlock time'); throw error; @@ -292,7 +290,7 @@ export const withdrawVeolasRequest = async ({ account }: { account: Address }) = rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (error) { window.console.log('Error occurred on withdrawing veOlas'); throw error; @@ -314,7 +312,7 @@ export const checkpointRequest = async ({ account }: { account: Address }) => { rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (error) { window.console.log('Error occurred on starting new epoch'); throw error; @@ -378,7 +376,7 @@ export const depositServiceDonationRequest = async ({ rpcUrls: RPC_URLS, }); - return (response as TransactionReceipt)?.transactionHash; + return response?.transactionHash; } catch (error) { window.console.log('Error occurred on depositing service donation'); throw error; diff --git a/apps/launch/common-util/functions/web3.ts b/apps/launch/common-util/functions/web3.ts index a8fc49c8..b44fee87 100644 --- a/apps/launch/common-util/functions/web3.ts +++ b/apps/launch/common-util/functions/web3.ts @@ -1,13 +1,14 @@ -import { ContractTransactionReceipt, EventLog } from 'ethers'; +import { BaseContract, EventLog } from 'ethers'; import { Address } from 'viem'; import Web3 from 'web3'; import { AbiItem } from 'web3-utils'; -import { sendTransaction as sendTransactionFn } from '@autonolas/frontend-library'; - import { RPC_URLS } from 'libs/util-constants/src'; import { STAKING_FACTORY, VOTE_WEIGHTING } from 'libs/util-contracts/src/lib/abiAndAddresses'; -import { getEstimatedGasLimit } from 'libs/util-functions/src'; +import { + getEstimatedGasLimit, + sendTransaction as sendTransactionFn, +} from 'libs/util-functions/src'; import { SUPPORTED_CHAINS } from 'common-util/config/wagmi'; import { getChainId, getProvider } from 'common-util/functions/frontend-library'; @@ -82,7 +83,7 @@ export const createStakingContract = async ({ const createFn = contract.methods.createStakingInstance(implementation, initPayload); const result = await sendTransaction(createFn, account); - return result as ContractTransactionReceipt & { + return result as BaseContract & { events?: { InstanceCreated: InstanceCreatedEvent }; }; }; diff --git a/libs/util-functions/src/lib/sendTransaction/helpers.ts b/libs/util-functions/src/lib/sendTransaction/helpers.ts index d11145c2..d4c60df6 100644 --- a/libs/util-functions/src/lib/sendTransaction/helpers.ts +++ b/libs/util-functions/src/lib/sendTransaction/helpers.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { ethers } from 'ethers'; + import { getUrl } from './index'; import { Chain, RpcUrl } from './types'; @@ -37,10 +38,7 @@ export const pollTransactionDetails = async (hash: string, chainId: number) => { * i/p: getIsValidChainId(1); * o/p: true */ -export const getIsValidChainId = ( - SUPPORTED_CHAINS: Chain[], - chainId: number | string -) => { +export const getIsValidChainId = (SUPPORTED_CHAINS: Chain[], chainId: number | string) => { if (!chainId) return false; return SUPPORTED_CHAINS.some((e) => e.id === Number(chainId)); @@ -69,9 +67,7 @@ export const getProvider = (supportedChains: Chain[], rpcUrls: RpcUrl) => { } if (typeof window === 'undefined') { - console.warn( - 'No provider found, fetching RPC URL from first supported chain' - ); + console.warn('No provider found, fetching RPC URL from first supported chain'); return rpcUrl; } @@ -82,9 +78,7 @@ export const getProvider = (supportedChains: Chain[], rpcUrls: RpcUrl) => { // if logged in via wallet-connect but chainId is not supported, // default to mainnet (ie. Use JSON-RPC provider) - return getIsValidChainId(supportedChains, walletConnectChainId) - ? walletProvider - : rpcUrl; + return getIsValidChainId(supportedChains, walletConnectChainId) ? walletProvider : rpcUrl; } // NOT logged in but has wallet installed (eg. Metamask). @@ -92,9 +86,7 @@ export const getProvider = (supportedChains: Chain[], rpcUrls: RpcUrl) => { const windowEthereum = getWindowEthereum(); if (windowEthereum?.chainId) { const walletChainId = Number(windowEthereum.chainId); - return getIsValidChainId(supportedChains, walletChainId) - ? windowEthereum - : rpcUrl; + return getIsValidChainId(supportedChains, walletChainId) ? windowEthereum : rpcUrl; } // fallback to mainnet JSON RPC provider @@ -105,10 +97,7 @@ export const getProvider = (supportedChains: Chain[], rpcUrls: RpcUrl) => { * gets ethers provider from the connected wallet or * installed wallet or fallback to mainnet */ -export const getEthersProvider = ( - supportedChains: Chain[], - rpcUrls: RpcUrl -) => { +export const getEthersProvider = (supportedChains: Chain[], rpcUrls: RpcUrl) => { const provider = getProvider(supportedChains, rpcUrls); // if provider is a string, it is a JSON-RPC provider @@ -116,7 +105,7 @@ export const getEthersProvider = ( return new ethers.JsonRpcProvider(provider); } - return new ethers.FallbackProvider([provider]); + return new ethers.BrowserProvider(provider); }; /** @@ -126,16 +115,14 @@ export const getEthersProvider = ( */ export const getChainIdOrDefaultToMainnet = ( SUPPORTED_CHAINS: Chain[], - chainIdPassed: string | number + chainIdPassed: string | number, ) => { if (!chainIdPassed) { throw new Error('chainId is not provided'); } const chain = Number(chainIdPassed); - return getIsValidChainId(SUPPORTED_CHAINS, chain) - ? chain - : SUPPORTED_CHAINS[0].id; + return getIsValidChainId(SUPPORTED_CHAINS, chain) ? chain : SUPPORTED_CHAINS[0].id; }; /** @@ -143,7 +130,7 @@ export const getChainIdOrDefaultToMainnet = ( */ export const getChainIdOrDefaultToFirstSupportedChain = ( SUPPORTED_CHAINS: Chain[], - chainIdPassed: string | number + chainIdPassed: string | number, ) => { return getChainIdOrDefaultToMainnet(SUPPORTED_CHAINS, chainIdPassed); }; @@ -152,10 +139,7 @@ export const getChainIdOrDefaultToFirstSupportedChain = ( * get chainId from the providers or fallback to default chainId (mainnet) * first element of supportedChains is the default chainId */ -export const getChainId = ( - supportedChains: Chain[], - chainId?: string | number | null -) => { +export const getChainId = (supportedChains: Chain[], chainId?: string | number | null) => { // if window is undefined, we are in server side // return undefined if (typeof window === 'undefined') { @@ -172,10 +156,7 @@ export const getChainId = ( const walletProvider = getModalProvider(); if (walletProvider?.chainId) { const walletConnectChainId = walletProvider.chainId; - return getChainIdOrDefaultToFirstSupportedChain( - supportedChains, - walletConnectChainId - ); + return getChainIdOrDefaultToFirstSupportedChain(supportedChains, walletConnectChainId); } // NOT logged in but has wallet installed (eg. metamask). @@ -183,10 +164,7 @@ export const getChainId = ( const windowEthereum = getWindowEthereum(); if (windowEthereum?.chainId) { const walletChainId = windowEthereum.chainId; - return getChainIdOrDefaultToFirstSupportedChain( - supportedChains, - walletChainId - ); + return getChainIdOrDefaultToFirstSupportedChain(supportedChains, walletChainId); } // has no wallet (eg. incognito mode or no wallet installed) diff --git a/libs/util-functions/src/lib/sendTransaction/index.ts b/libs/util-functions/src/lib/sendTransaction/index.ts index f297f09b..f087f72f 100644 --- a/libs/util-functions/src/lib/sendTransaction/index.ts +++ b/libs/util-functions/src/lib/sendTransaction/index.ts @@ -1,7 +1,8 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ +import { Contract } from 'ethers'; + import { notifyError, notifyWarning } from '../notifications'; import { getChainId, getEthersProvider, pollTransactionDetails } from './helpers'; -import { Chain, RpcUrl, Web3ReceiptType } from './types'; +import { Chain, RpcUrl } from './types'; export const SAFE_API_MAINNET = 'https://safe-transaction-mainnet.safe.global/api/v1/multisig-transactions'; @@ -36,7 +37,7 @@ export const getUrl = (hash: string, chainId: number) => { * poll until the hash has been approved */ export const sendTransaction = async ( - sendFn: any, + sendFn: Contract, account = (window as any)?.MODAL_PROVIDER?.accounts[0], { supportedChains, rpcUrls }: { supportedChains: Chain[]; rpcUrls: RpcUrl }, ) => { @@ -87,8 +88,7 @@ export const sendTransaction = async ( }); } else { // usual send function - const receipt: Web3ReceiptType = await sendFn(); - return receipt; + return sendFn; } } catch (e) { notifyError('Error occurred while sending transaction');