Skip to content

Commit

Permalink
(registry, launch, govern) fix: sendTransaction error
Browse files Browse the repository at this point in the history
  • Loading branch information
Atatakai authored and Atatakai committed Aug 21, 2024
1 parent 3736094 commit e0a39a9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, isOwner,
const params = {
account: account as Address,
unitIds: [id],
unitTypes: [id],
unitTypes: [`${tokenomicsUnitType}`],
};

await claimOwnerIncentivesRequest(params);
Expand All @@ -171,7 +171,7 @@ export const RewardsSection: FC<RewardsSectionProps> = ({ ownerAddress, isOwner,
} finally {
setIsClaimLoading(false);
}
}, [account, id, canClaim, fetchPendingIncentives, refetch]);
}, [account, id, tokenomicsUnitType, canClaim, fetchPendingIncentives, refetch]);

return (
<Flex gap={16} vertical className="mt-12" data-testid={dataTestId}>
Expand Down
11 changes: 4 additions & 7 deletions apps/autonolas-registry/common-util/functions/requests.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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<string, string>,
});
const response = await sendTransaction(fn, account);
return response?.transactionHash;
} catch (error) {
window.console.log('Error occurred on claiming owner incentives');
Expand Down
14 changes: 6 additions & 8 deletions apps/govern/common-util/functions/requests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { readContract, readContracts } from '@wagmi/core';
import { ethers } from 'ethers';
import { AbiFunction, TransactionReceipt, parseUnits } from 'viem';
import { AbiFunction, parseUnits } from 'viem';
import { Address } from 'viem';
import { mainnet } from 'viem/chains';

import { sendTransaction } from '@autonolas/frontend-library';

import { 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';
Expand Down Expand Up @@ -198,7 +196,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;
Expand Down Expand Up @@ -227,7 +225,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;
Expand Down Expand Up @@ -259,7 +257,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;
Expand All @@ -282,7 +280,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;
Expand Down
11 changes: 6 additions & 5 deletions apps/launch/common-util/functions/web3.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 };
};
};
Expand Down
48 changes: 13 additions & 35 deletions libs/util-functions/src/lib/sendTransaction/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ethers } from 'ethers';

import { getUrl } from './index';
import { Chain, RpcUrl } from './types';

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

Expand All @@ -82,19 +78,15 @@ 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).
// If chainId is not supported, default to mainnet (ie. Use JSON-RPC provider)
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
Expand All @@ -105,18 +97,15 @@ 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
if (typeof provider === 'string') {
return new ethers.JsonRpcProvider(provider);
}

return new ethers.FallbackProvider([provider]);
return new ethers.BrowserProvider(provider);
};

/**
Expand All @@ -126,24 +115,22 @@ 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;
};

/**
* Same functionality as getChainIdOrDefaultToMainnet but with a different name for clarity
*/
export const getChainIdOrDefaultToFirstSupportedChain = (
SUPPORTED_CHAINS: Chain[],
chainIdPassed: string | number
chainIdPassed: string | number,
) => {
return getChainIdOrDefaultToMainnet(SUPPORTED_CHAINS, chainIdPassed);
};
Expand All @@ -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') {
Expand All @@ -172,21 +156,15 @@ 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).
// window?.ethereum?.chainId is chainId set by wallet
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)
Expand Down
10 changes: 5 additions & 5 deletions libs/util-functions/src/lib/sendTransaction/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 },
) => {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit e0a39a9

Please sign in to comment.