Skip to content

Commit

Permalink
mints gas estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
benisgold committed Mar 5, 2024
1 parent 3f8e15c commit 83c23fa
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/screens/mints/MintSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ButtonPressAnimation } from '@/components/animations';
import { useFocusEffect, useRoute } from '@react-navigation/native';
import { ReservoirCollection } from '@/graphql/__generated__/arcDev';
import { format } from 'date-fns';
import { NewTransaction, RainbowTransaction } from '@/entities';
import { NewTransaction } from '@/entities';
import * as i18n from '@/languages';
import { analyticsV2 } from '@/analytics';
import { event } from '@/analytics/event';
Expand All @@ -48,15 +48,15 @@ import {
multiply,
} from '@/helpers/utilities';
import { RainbowError, logger } from '@/logger';
import { useDispatch } from 'react-redux';
import { QuantityButton } from './components/QuantityButton';
import { estimateGas, getProviderForNetwork } from '@/handlers/web3';
import { getRainbowFeeAddress } from '@/resources/reservoir/utils';
import { IS_ANDROID, IS_IOS } from '@/env';
import { EthCoinIcon } from '@/components/coin-icon/EthCoinIcon';
import { addNewTransaction } from '@/state/pendingTransactions';
import { getUniqueId } from '@/utils/ethereumUtils';
import { getNextNonce } from '@/state/nonces';
import { metadataPOSTClient } from '@/graphql';
import { Transaction } from '@/graphql/__generated__/metadataPOST';

const NFT_IMAGE_HEIGHT = 250;
// inset * 2 -> 28 *2
Expand Down Expand Up @@ -130,7 +130,6 @@ const MintSheet = () => {
const { nativeCurrency } = useAccountSettings();
const { height: deviceHeight, width: deviceWidth } = useDimensions();
const { navigate } = useNavigation();
const dispatch = useDispatch();
const { colors, isDarkMode } = useTheme();
const { isReadOnlyWallet, isHardwareWallet } = useWallets();
const [insufficientEth, setInsufficientEth] = useState(false);
Expand Down Expand Up @@ -247,7 +246,6 @@ const MintSheet = () => {
useEffect(() => {
const estimateMintGas = async () => {
const networkObj = getNetworkObj(currentNetwork);
const provider = await getProviderForNetwork(currentNetwork);
const signer = createWalletClient({
account: accountAddress,
chain: networkObj,
Expand All @@ -260,35 +258,36 @@ const MintSheet = () => {
chainId: networkObj.id,
precheck: true,
onProgress: async (steps: Execute['steps']) => {
const txs: Transaction[] = [];
steps.forEach(step => {
if (step.error) {
logger.error(new RainbowError(`NFT Mints: Gas Step Error: ${step.error}`));
return;
}
step.items?.forEach(async item => {
// could add safety here if unable to calc gas limit
const tx = {
to: item.data?.to,
from: item.data?.from,
data: item.data?.data,
value: item.data?.value,
};
const gas = await estimateGas(tx, provider);
let l1GasFeeOptimism = null;
// add l1Fee for OP Chains
if (getNetworkObj(currentNetwork).gas.OptimismTxFee) {
l1GasFeeOptimism = await ethereumUtils.calculateL1FeeOptimism(tx as RainbowTransaction, provider);
}
if (gas) {
setGasError(false);
if (l1GasFeeOptimism) {
updateTxFee(gas, null, l1GasFeeOptimism);
} else {
updateTxFee(gas, null);
}
step.items?.forEach(item => {
if (item?.data?.to && item?.data?.from && item?.data?.data) {
txs.push({
to: item.data?.to,
from: item.data?.from,
data: item.data?.data,
value: item.data?.value ?? '0x0',
});
}
});
});
const txSimEstimate = parseInt(
(
await metadataPOSTClient.simulateTransactions({
chainId: networkObj.id,
transactions: txs,
})
)?.simulateTransactions?.[0]?.gas?.estimate ?? '0x0',
16
);
if (txSimEstimate) {
setGasError(false);
updateTxFee(txSimEstimate, null);
}
},
});
} catch (e) {
Expand Down

0 comments on commit 83c23fa

Please sign in to comment.