From 5dc0bb3a1bc198ef7c5cb036c0a0c42562d21967 Mon Sep 17 00:00:00 2001 From: Uncle Fatso Date: Mon, 27 Apr 2026 21:54:48 +0300 Subject: [PATCH] unite onchain tx as one function with gas sanitization Signed-off-by: Uncle Fatso --- package.json | 2 +- src/hooks/bonds/index.js | 100 ++++++------- src/hooks/governance/index.js | 160 ++++++++------------ src/hooks/helpers.js | 57 +++++++ src/hooks/staking/index.js | 156 +++++++++----------- src/hooks/tokens/index.js | 196 ++++++++++--------------- src/hooks/uniswapv2/UniswapV2Router.js | 67 +++------ 7 files changed, 327 insertions(+), 411 deletions(-) diff --git a/package.json b/package.json index 5922724..26a0944 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ghost-dao-interface", "private": true, - "version": "0.7.20", + "version": "0.7.21", "type": "module", "scripts": { "dev": "vite", diff --git a/src/hooks/bonds/index.js b/src/hooks/bonds/index.js index 94aa089..af49386 100644 --- a/src/hooks/bonds/index.js +++ b/src/hooks/bonds/index.js @@ -1,10 +1,6 @@ import { useReadContract, useReadContracts } from "wagmi"; -import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; import toast from "react-hot-toast"; -import { isNetworkLegacyType } from "../../constants"; -import { config } from "../../config"; - import { BOND_DEPOSITORY_ADDRESSES, DAO_TREASURY_ADDRESSES, @@ -17,7 +13,14 @@ import { abi as BondingCalculatorAbi } from "../../abi/GhostBondingCalculator.js import { useReservePrice, useFtsoPrice } from "../prices"; import { useOrinalCoefficient } from "../treasury"; import { useTokenSymbol, useTokenSymbols } from "../tokens"; -import { getTokenAddress, getTokenIcons, getBondNameDisplayName, getTokenPurchaseLink } from "../helpers"; +import { + getTokenAddress, + getTokenIcons, + getBondNameDisplayName, + getTokenPurchaseLink, + executeOnChainTransaction +} from "../helpers"; + import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; import { shorten } from "../../helpers"; import { tokenNameConverter } from "../../helpers/tokenConverter"; @@ -267,78 +270,61 @@ export const useNotes = (chainId, address) => { } export const purchaseBond = async ({ chainId, bondId, amount, maxPrice, user, sender, referral, isNative }) => { - const args = [ - bondId, - amount, - maxPrice, - user, - referral - ]; + const args = [bondId, amount, maxPrice, user, referral]; const messages = { replacedMsg: "Bond transaction was replaced. Wait for inclusion please.", successMsg: `Bond successfully purchased for ${shorten(user)}! Wait until it'll mature before claim.`, errorMsg: "Bond transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + + await executeOnChainTransaction({ chainId, - "deposit", args, - sender, messages, - isNative ? amount : undefined - ); + abi: BondAbi, + address: BOND_DEPOSITORY_ADDRESSES[chainId], + functionName: "deposit", + account: user, + value: isNative ? amount : undefined + }); } export const redeem = async ({ chainId, user, isGhst, indexes }) => { - const args = [ - user, - isGhst, - indexes - ]; + const args = [user, isGhst, indexes]; const messages = { replacedMsg: "Redeem transaction was replaced. Wait for inclusion please.", successMsg: `Address ${shorten(user)} redeemed ${indexes.length} bonds in ${isGhst ? "GHST" : "STNK"}!`, errorMsg: `Redeem of ${indexes.length} bonds failed. Check logs for error detalization.`, }; - await executeOnChainTransaction( + + await executeOnChainTransaction({ chainId, - "redeem", args, - user, - messages - ); + messages, + abi: BondAbi, + address: BOND_DEPOSITORY_ADDRESSES[chainId], + functionName: "redeem", + account: user, + }); } -const executeOnChainTransaction = async ( - chainId, - functionName, - args, - account, - messages, - value -) => { - try { - const { request } = await simulateContract(config, { - abi: BondAbi, - address: BOND_DEPOSITORY_ADDRESSES[chainId], - functionName, - args, - account, - chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - value: value, - }); +export const forceRedeem = async ({ chainId, user, receiver, indexes }) => { + const args = [receiver, indexes]; + const messages = { + replacedMsg: "Bond breakout transaction was replaced. Wait for inclusion please.", + successMsg: `Address ${shorten(user)} succesfully breakout for ${indexes.length} bonds.`, + errorMsg: `Breakout of ${indexes.length} bonds failed. Check logs for error detalization.`, + }; - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast(messages.replacedMsg), - chainId - }); + const txHash = await executeOnChainTransaction({ + chainId, + args, + messages, + abi: BondAbi, + address: BOND_DEPOSITORY_ADDRESSES[chainId], + functionName: "forceRedeem", + account: user, + }); - toast.success(messages.successMsg); - } catch (err) { - console.error(err); - toast.error(messages.errorMsg) - } + return txHash; } diff --git a/src/hooks/governance/index.js b/src/hooks/governance/index.js index 210d200..dd6a20f 100644 --- a/src/hooks/governance/index.js +++ b/src/hooks/governance/index.js @@ -1,11 +1,7 @@ import { useMemo } from "react"; import { useReadContract, useReadContracts } from "wagmi"; -import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; -import toast from "react-hot-toast"; import { keccak256, stringToBytes } from 'viem' - -import { isNetworkLegacyType } from "../../constants"; -import { config } from "../../config"; +import toast from "react-hot-toast"; import { GHOST_GOVERNANCE_ADDRESSES, @@ -16,7 +12,7 @@ import { abi as GovernorStorageAbi } from "../../abi/GovernorStorage.json"; import { abi as GovernorVotesQuorumFractionAbi } from "../../abi/GovernorVotesQuorumFraction.json"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; -import { getTokenDecimals, getTokenAbi, getTokenAddress } from "../helpers"; +import { getTokenDecimals, getTokenAbi, getTokenAddress, executeOnChainTransaction } from "../helpers"; export const getVoteValue = (forVotes, totalVotes) => { if (totalVotes == 0n) return 0; @@ -538,87 +534,57 @@ export const useProposals = (chainId, depth, searchedIndexes) => { } export const releaseLocked = async (chainId, account, proposalId) => { - try { - const { request } = await simulateContract(config, { - abi: GovernorAbi, - address: GHOST_GOVERNANCE_ADDRESSES[chainId], - functionName: 'releaseLocked', - args: [proposalId], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); + const messages = { + replacedMsg: "Release locked transaction was replaced. Wait for inclusion please.", + successMsg: "Successfully release locked funds from the governor.", + errorMsg: "Release locked funds failed. Check logs for error detalization.", + }; - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Release locked transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("Successfully release locked funds from the governor."); - return true; - } catch (err) { - console.error(err); - toast.error("Release locked funds failed. Check logs for error detalization."); - return false; - } + await executeOnChainTransaction({ + chainId, + args: [proposalId], + abi: GovernorAbi, + address: GHOST_GOVERNANCE_ADDRESSES[chainId], + functionName: "releaseLocked", + account, + messages, + }); } export const executeProposal = async (chainId, account, proposalId) => { - try { - const { request } = await simulateContract(config, { - abi: GovernorAbi, - address: GHOST_GOVERNANCE_ADDRESSES[chainId], - functionName: 'execute', - args: [proposalId], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); + const messages = { + replacedMsg: "Proposal execution transaction was replaced. Wait for inclusion please.", + successMsg: "Proposal execution was successful, wait for updates.", + errorMsg: "Proposal execution failed. Check logs for error detalization.", + }; - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Proposal execution transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("Proposal execution was successful, wait for updates."); - return true; - } catch (err) { - console.error(err); - toast.error("Proposal execution failed. Check logs for error detalization."); - return false; - } + await executeOnChainTransaction({ + chainId, + args: [proposalId], + abi: GovernorAbi, + address: GHOST_GOVERNANCE_ADDRESSES[chainId], + functionName: "execute", + account, + messages, + }); } export const castVote = async (chainId, account, proposalId, support) => { - try { - const { request } = await simulateContract(config, { - abi: GovernorAbi, - address: GHOST_GOVERNANCE_ADDRESSES[chainId], - functionName: 'castVote', - args: [proposalId, support], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); + const messages = { + replacedMsg: "Cast vote transaction was replaced. Wait for inclusion please.", + successMsg: "Successfully casted a vote, should be applied the proposal.", + errorMsg: "Vote cast failed. Check logs for error detalization.", + }; - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Cast vote transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("Successfully casted a vote, should be applied the proposal."); - return true; - } catch (err) { - console.error(err); - toast.error("Vote cast failed. Check logs for error detalization."); - return false; - } + await executeOnChainTransaction({ + chainId, + args: [proposalId, support], + abi: GovernorAbi, + address: GHOST_GOVERNANCE_ADDRESSES[chainId], + functionName: "castVote", + account, + messages, + }); } export const propose = async (chainId, account, functions, description) => { @@ -626,29 +592,19 @@ export const propose = async (chainId, account, functions, description) => { const values = functions.map(f => f.value); const calldatas = functions.map(f => f.calldata); - try { - const { request } = await simulateContract(config, { - abi: GovernorAbi, - address: GHOST_GOVERNANCE_ADDRESSES[chainId], - functionName: 'propose', - args: [targets, values, calldatas, description], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); + const messages = { + replacedMsg: "Proposal transaction was replaced. Wait for inclusion please.", + successMsg: "Successfully proposed a set of functions to be executed.", + errorMsg: "Proposal creation failed. Check logs for error detalization.", + }; - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Proposal transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("Successfully proposed a set of functions to be executed."); - return true; - } catch (err) { - console.error(err); - toast.error("Proposal creation failed. Check logs for error detalization."); - return false; - } + await executeOnChainTransaction({ + chainId, + args: [targets, values, calldatas, description], + abi: GovernorAbi, + address: GHOST_GOVERNANCE_ADDRESSES[chainId], + functionName: "propose", + account, + messages, + }); } diff --git a/src/hooks/helpers.js b/src/hooks/helpers.js index 1eca503..9139bb1 100644 --- a/src/hooks/helpers.js +++ b/src/hooks/helpers.js @@ -1,3 +1,6 @@ +import toast from "react-hot-toast"; +import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; + import { RESERVE_ADDRESSES, FTSO_ADDRESSES, @@ -6,6 +9,8 @@ import { FTSO_DAI_LP_ADDRESSES, WETH_ADDRESSES, } from "../constants/addresses"; +import { isNetworkLegacyType } from "../constants"; +import { config } from "../config"; import { tokenNameConverter } from "../helpers/tokenConverter"; @@ -201,3 +206,55 @@ export const getTokenPurchaseLink = (chainId, tokenAddress, chainName) => { } return purchaseUrl; } + +const sanitizeTransactionRequest = (request, isLegacy) => { + const finalRequest = { ...request }; + if (isLegacy) { + delete finalRequest.maxFeePerGas; + delete finalRequest.maxPriorityFeePerGas; + } else { + delete finalRequest.gasPrice; + } + return finalRequest; +} + +export const executeOnChainTransaction = async ({ + chainId, + abi, + address, + functionName, + args, + account, + messages, + value +}) => { + try { + const isLegacy = isNetworkLegacyType(chainId); + const { request } = await simulateContract(config, { + abi, + address, + functionName, + args, + account, + chainId, + value, + type: isLegacy ? 'legacy' : 'eip1559', + }); + + const finalRequest = sanitizeTransactionRequest(request, isLegacy); + const txHash = await writeContract(config, finalRequest); + await waitForTransactionReceipt(config, { + hash: txHash, + onReplaced: () => toast(messages.replacedMsg), + chainId + }); + + toast.success(messages.successMsg); + + return txHash; + } catch (err) { + console.error(err); + toast.error(messages.errorMsg) + return undefined; + } +} diff --git a/src/hooks/staking/index.js b/src/hooks/staking/index.js index b667590..4fa1d6c 100644 --- a/src/hooks/staking/index.js +++ b/src/hooks/staking/index.js @@ -1,16 +1,13 @@ import { useReadContract } from "wagmi"; -import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; import toast from "react-hot-toast"; -import { config } from "../../config"; - -import { isNetworkLegacyType } from "../../constants"; import { STAKING_ADDRESSES } from "../../constants/addresses"; import { abi as StakingAbi } from "../../abi/GhostStaking.json"; import { abi as GatekeeperAbi } from "../../abi/GhostGatekeeper.json"; import { shorten } from "../../helpers"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; +import { executeOnChainTransaction } from "../helpers"; export const useGatekeeperApy = (chainId) => { const { data: gatekeeper, refetch } = useReadContract({ @@ -155,45 +152,39 @@ export const useGhostedSupply = (chainId) => { } export const stake = async (chainId, account, amount, isRebase, isClaim, ftsoSymbol, stnkSymbol, ghstSymbol) => { - const args = [ - amount, - account, - isRebase, - isClaim - ]; + const args = [amount, account, isRebase, isClaim]; const messages = { replacedMsg: "Staking transaction was replaced. Wait for inclusion please.", successMsg: `${ftsoSymbol} tokens staked successfully! Wait for the warmup period to claim your ${isRebase ? stnkSymbol : ghstSymbol}.`, errorMsg: "Staking transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + await executeOnChainTransaction({ chainId, - "stake", args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "stake", + }); } export const unstake = async (chainId, account, amount, isTrigger, isRebase, ftsoSymbol, stnkSymbol, ghstSymbol) => { - const args = [ - amount, - account, - isTrigger, - isRebase - ]; + const args = [amount, account, isTrigger, isRebase]; const messages = { replacedMsg: "Unstake transaction was replaced. Wait for inclusion please.", successMsg: `${isRebase ? stnkSymbol : ghstSymbol} tokens unstaked successfully! Check your ${ftsoSymbol} balance on ${shorten(account)}.`, errorMsg: "Unstake transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + await executeOnChainTransaction({ chainId, - "unstake", args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "unstake", + }); } export const forfeit = async (chainId, account, ftsoSymbol) => { @@ -202,13 +193,15 @@ export const forfeit = async (chainId, account, ftsoSymbol) => { successMsg: `Tokens forfeited successfully! Check your ${ftsoSymbol} balance on ${shorten(account)}.`, errorMsg: "Forfeit transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + await executeOnChainTransaction({ chainId, - "forfeit", - [], + messages, account, - messages - ); + args: [], + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "forfeit", + }); } export const claim = async (chainId, account, isStnk, stnkSymbol, ghstSymbol) => { @@ -218,94 +211,89 @@ export const claim = async (chainId, account, isStnk, stnkSymbol, ghstSymbol) => successMsg: `${isStnk ? stnkSymbol : ghstSymbol} tokens claimed successfully to ${shorten(account)}.`, errorMsg: "Claim transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + await executeOnChainTransaction({ chainId, - "claim", args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "claim", + }); +} + +export const breakout = async (chainId, account, receiver, amount) => { + const args = [receiver, amount]; + const messages = { + replacedMsg: "Breakout transaction was replaced. Wait for inclusion please.", + successMsg: `Staking breakout succesfully bridged to ${shorten(receiver)}.`, + errorMsg: "Breakout transaction failed. Check logs for error detalization.", + }; + const txHash = await executeOnChainTransaction({ + chainId, + args, + messages, + account, + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "breakout", + }); + + return txHash; } export const unwrap = async (chainId, account, amount, stnkSymbol) => { + const args = [account, amount]; const messages = { replacedMsg: "Unwrap transaction was replaced. Wait for inclusion please.", successMsg: `Tokens unwrapped successfully! Check your ${stnkSymbol} balance on ${shorten(account)}.`, errorMsg: "Unwrap transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + const txHash = await executeOnChainTransaction({ chainId, - "unwrap", - [account, amount], + args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "unwrap", + }); } export const wrap = async (chainId, account, amount, ghstSymbol) => { + const args = [account, amount]; const messages = { replacedMsg: "Wrap transaction was replaced. Wait for inclusion please.", successMsg: `Tokens wrapped successfully! Check your ${ghstSymbol} balance on ${shorten(account)}.`, errorMsg: "Wrap transaction failed. Check logs for error detalization.", }; - await executeOnChainTransaction( + const txHash = await executeOnChainTransaction({ chainId, - "wrap", - [account, amount], + args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "wrap", + }); } export const ghost = async (chainId, account, receiver, amount) => { + const ars = [receiver, amount]; const messages = { replacedMsg: "Bridge transaction was replaced. Wait for inclusion please.", successMsg: `Amount successfully bridged! Check tx hash status or wait for slow clap finalization.`, errorMsg: "Bridge transaction failed. Check logs for error detalization.", }; - - const txHash = await executeOnChainTransaction( + const txHash = await executeOnChainTransaction({ chainId, - "ghost", - [receiver, amount], + args, + messages, account, - messages - ); + abi: StakingAbi, + address: STAKING_ADDRESSES[chainId], + functionName: "ghost", + }); return txHash; } - -const executeOnChainTransaction = async ( - chainId, - functionName, - args, - account, - messages -) => { - try { - const { request } = await simulateContract(config, { - abi: StakingAbi, - address: STAKING_ADDRESSES[chainId], - functionName, - args, - account, - chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast(messages.replacedMsg), - chainId - }); - - toast.success(messages.successMsg); - - return txHash; - } catch (err) { - console.error(err); - toast.error(messages.errorMsg) - return undefined; - } -} diff --git a/src/hooks/tokens/index.js b/src/hooks/tokens/index.js index da97e1c..0cca106 100644 --- a/src/hooks/tokens/index.js +++ b/src/hooks/tokens/index.js @@ -1,14 +1,16 @@ import { useReadContract, useReadContracts, useToken, useBalance as useInnerBalance } from "wagmi"; -import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; import toast from "react-hot-toast"; -import { getTokenAbi, getTokenAddress, getTokenDecimals } from "../helpers"; +import { + getTokenAbi, + getTokenAddress, + getTokenDecimals, + executeOnChainTransaction +} from "../helpers"; -import { isNetworkLegacyType } from "../../constants"; import { DecimalBigNumber } from "../../helpers/DecimalBigNumber"; import { shorten } from "../../helpers"; import { tokenNameConverter } from "../../helpers/tokenConverter"; -import { config } from "../../config"; import { WETH_ADDRESSES } from "../../constants/addresses"; export const usePastVotes = (chainId, name, timepoint, address) => { @@ -204,132 +206,86 @@ export const useBalanceForShares = (chainId, name, amount) => { }; export const approveTokens = async (chainId, name, owner, spender, value) => { - try { - const { request } = await simulateContract(config, { - abi: getTokenAbi(name), - address: getTokenAddress(chainId, name), - functionName: 'approve', - args: [spender, value], - account: owner, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Approve transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success(name + " tokens successfully approved to " + shorten(spender)); - } catch (err) { - console.error(err); - toast.error(name + " tokens approval failed. Check logs for error detalization.") - } + const messages = { + replacedMsg: "Approve transaction was replaced. Wait for inclusion please.", + successMsg: `${name} tokens successfully approved to ${shorten(spender)}`, + errorMsg: `${name} tokens approval failed. Check logs for error detalization.` + }; + await executeOnChainTransaction({ + chainId, + args: [spender, value], + abi: getTokenAbi(name), + address: getTokenAddress(chainId, name), + functionName: "approve", + account: owner, + messages, + }); } export const mintDai = async (chainId, account, value) => { - try { - const { request } = await simulateContract(config, { - abi: getTokenAbi("GDAI"), - address: getTokenAddress(chainId, "GDAI"), - functionName: 'mint', - args: [account], - account: account, - value: value, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Mint transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("gDAI successfully minted to your wallet! Funds will be used on Ghost Faucet."); - } catch (err) { - console.error(err); - toast.error("Minting gDAI from the faucet failed. Check logs for error detalization.") - } + const messages = { + replacedMsg: "Mint transaction was replaced. Wait for inclusion please.", + successMsg: "gDAI successfully minted to your wallet! Funds will be used on Ghost Faucet.", + errorMsg: "Minting gDAI from the faucet failed. Check logs for error detalization." + }; + await executeOnChainTransaction({ + chainId, + args: [account], + abi: getTokenAbi("GDAI"), + address: getTokenAddress(chainId, "GDAI"), + functionName: "mint", + account, + messages, + }); } export const burnDai = async (chainId, account, value) => { - try { - const { request } = await simulateContract(config, { - abi: getTokenAbi("GDAI"), - address: getTokenAddress(chainId, "GDAI"), - functionName: 'burn', - args: [value], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("Burn transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("gDAI successfully burned for native coins! Check your wallet balance."); - } catch (err) { - console.error(err); - toast.error("Burning gDAI from the faucet failed. Check logs for error detalization.") - } + const messages = { + replacedMsg: "Burn transaction was replaced. Wait for inclusion please.", + successMsg: "gDAI successfully burned for native coins! Check your wallet balance.", + errorMsg: "Burning gDAI from the faucet failed. Check logs for error detalization." + }; + await executeOnChainTransaction({ + chainId, + abi: getTokenAbi("GDAI"), + address: getTokenAddress(chainId, "GDAI"), + functionName: 'burn', + args: [value], + account, + messages, + }); } export const depositNative = async (chainId, account, value) => { - try { - const { request } = await simulateContract(config, { - abi: getTokenAbi("WETH"), - address: getTokenAddress(chainId, "WETH"), - functionName: 'deposit', - account: account, - chainId: chainId, - value: value, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - const txHash = await writeContract(config, request); - - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("WETH9 deposit transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("WETH9 successfully minted to your wallet! Check your wallet balance."); - } catch (err) { - console.error(err); - toast.error("WETH9 wrapping failed. Check logs for error detalization.") - } + const messages = { + replacedMsg: "WETH9 deposit transaction was replaced. Wait for inclusion please.", + successMsg: "WETH9 successfully minted to your wallet! Check your wallet balance.", + errorMsg: "WETH9 wrapping failed. Check logs for error detalization." + }; + await executeOnChainTransaction({ + chainId, + abi: getTokenAbi("WETH"), + address: getTokenAddress(chainId, "WETH"), + functionName: 'deposit', + value, + account, + messages, + }); } export const withdrawWeth = async (chainId, account, value) => { - try { - const { request } = await simulateContract(config, { - abi: getTokenAbi("WETH"), - address: getTokenAddress(chainId, "WETH"), - functionName: 'withdraw', - args: [value], - account: account, - chainId: chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast("WETH9 withdraw transaction was replaced. Wait for inclusion please."), - chainId - }); - - toast.success("WETH9 successfully burned for native coins! Check your wallet balance."); - } catch (err) { - console.error(err); - toast.error("WETH9 unwrapping failed. Check logs for error detalization.") - } + const messages = { + replacedMsg: "WETH9 withdraw transaction was replaced. Wait for inclusion please.", + successMsg: "WETH9 successfully burned for native coins! Check your wallet balance.", + errorMsg: "WETH9 unwrapping failed. Check logs for error detalization." + }; + await executeOnChainTransaction({ + chainId, + abi: getTokenAbi("WETH"), + address: getTokenAddress(chainId, "WETH"), + functionName: 'withdraw', + args: [value], + account, + messages, + }); } diff --git a/src/hooks/uniswapv2/UniswapV2Router.js b/src/hooks/uniswapv2/UniswapV2Router.js index 0ff83da..488674b 100644 --- a/src/hooks/uniswapv2/UniswapV2Router.js +++ b/src/hooks/uniswapv2/UniswapV2Router.js @@ -1,12 +1,8 @@ -import { simulateContract, writeContract, waitForTransactionReceipt } from "@wagmi/core"; import toast from "react-hot-toast"; -import { isNetworkLegacyType } from "../../constants"; import { UNISWAP_V2_ROUTER, WETH_ADDRESSES } from "../../constants/addresses"; import { abi as RouterAbi } from "../../abi/UniswapV2Router.json"; -import { getTokenAddress } from "../helpers"; - -import { config } from "../../config"; +import { getTokenAddress, executeOnChainTransaction } from "../helpers"; const swapMessages = { replacedMsg: "Swap transaction was replaced. Wait for inclusion please.", @@ -27,6 +23,7 @@ export const swapExactETHForTokens = async ({ tokenNameTop, tokenNameBottom, destination, + address, deadline }) => { const args = [ @@ -38,9 +35,11 @@ export const swapExactETHForTokens = async ({ await executeOnChainTransaction({ chainId, - functionName: "swapExactETHForTokens", args, - account: destination, + abi: RouterAbi, + address: UNISWAP_V2_ROUTER[chainId], + functionName: "swapExactETHForTokens", + account: address, messages: swapMessages, value: amountADesired, }); @@ -66,8 +65,10 @@ export const swapExactTokensForETH = async ({ await executeOnChainTransaction({ chainId, - functionName: "swapExactTokensForETH", args, + abi: RouterAbi, + address: UNISWAP_V2_ROUTER[chainId], + functionName: "swapExactTokensForETH", account: address, messages: swapMessages, }); @@ -93,10 +94,12 @@ export const swapExactTokensForTokens = async ({ await executeOnChainTransaction({ chainId, - functionName: "swapExactTokensForTokens", args, + abi: RouterAbi, + address: UNISWAP_V2_ROUTER[chainId], + functionName: "swapExactTokensForTokens", account: address, - messages: swapMessages + messages: swapMessages, }); } @@ -125,10 +128,12 @@ export const addLiquidity = async ({ await executeOnChainTransaction({ chainId, - functionName: "addLiquidity", args, + abi: RouterAbi, + address: UNISWAP_V2_ROUTER[chainId], + functionName: "addLiquidity", account: address, - messages: addMessages + messages: addMessages, }); } @@ -169,44 +174,12 @@ export const addLiquidityETH = async ({ await executeOnChainTransaction({ chainId, - functionName: "addLiquidityETH", args, + abi: RouterAbi, + address: UNISWAP_V2_ROUTER[chainId], + functionName: "addLiquidityETH", account: address, messages: addMessages, value: amountETHDesired, }); } - -const executeOnChainTransaction = async ({ - chainId, - functionName, - args, - account, - messages, - value, -}) => { - try { - const { request } = await simulateContract(config, { - abi: RouterAbi, - address: UNISWAP_V2_ROUTER[chainId], - functionName, - args, - account, - chainId, - type: isNetworkLegacyType(chainId) ? 'legacy' : 'eip1559', - value: value ?? 0n, - }); - - const txHash = await writeContract(config, request); - await waitForTransactionReceipt(config, { - hash: txHash, - onReplaced: () => toast(messages.replacedMsg), - chainId - }); - - toast.success(messages.successMsg); - } catch (err) { - console.error(err); - toast.error(messages.errorMsg) - } -}