Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Merge branch 'fix/gas-est' into preprod
Browse files Browse the repository at this point in the history
  • Loading branch information
intergalacticspacehighway committed Nov 1, 2023
2 parents 9f57119 + 6876ebe commit 0aa138b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
16 changes: 5 additions & 11 deletions packages/app/hooks/creator-token/use-creator-token-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { publicClient } from "app/lib/wallet-public-client";
import { isDEV } from "app/utilities";

import { useWallet } from "../use-wallet";
import { useMaxGasPrices } from "./use-max-gas-prices";
import { usdcAddress } from "./utils";

export const useApproveToken = () => {
const wallet = useWallet();
const { getMaxFeePerGasAndPriorityPrice } = useMaxGasPrices();
const state = useSWRMutation<boolean | undefined>(
"approveToken",
async function approveToken(
Expand Down Expand Up @@ -44,18 +46,10 @@ export const useApproveToken = () => {
return true;
}

const { maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas } =
await publicClient.estimateFeesPerGas({
type: "eip1559",
});

const latestBlockBaseFeePerGas = (await publicClient.getBlock())
.baseFeePerGas;
const maxPrices = await getMaxFeePerGasAndPriorityPrice();

if (maxPriorityFeePerGas) {
const maxFeePerGas = latestBlockBaseFeePerGas
? latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas
: estimatedMaxFeePerGas;
if (maxPrices) {
const { maxFeePerGas, maxPriorityFeePerGas } = maxPrices;

console.log("gas price approve", {
maxFeePerGas,
Expand Down
16 changes: 5 additions & 11 deletions packages/app/hooks/creator-token/use-creator-token-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getPriceToBuyNextKey,
useCreatorTokenPriceToBuyNext,
} from "./use-creator-token-price-to-buy-next";
import { useMaxGasPrices } from "./use-max-gas-prices";
import { useSwitchChain } from "./use-switch-chain";
import { baseChain, creatorTokenSwapRouterAddress } from "./utils";

Expand All @@ -35,6 +36,7 @@ export const useCreatorTokenBuy = (params: {
const { data: profileData } = useUserProfile({ address: username });
const wallet = useWallet();
const approveToken = useApproveToken();
const { getMaxFeePerGasAndPriorityPrice } = useMaxGasPrices();
const priceToBuyNext = useCreatorTokenPriceToBuyNext({
address: profileData?.data?.profile.creator_token?.address,
tokenAmount,
Expand Down Expand Up @@ -65,18 +67,10 @@ export const useCreatorTokenBuy = (params: {
if (walletAddress && profileData?.data?.profile.creator_token) {
const switchChainSuccess = await switchChain.trigger();

const { maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas } =
await publicClient.estimateFeesPerGas({
type: "eip1559",
});

const latestBlockBaseFeePerGas = (await publicClient.getBlock())
.baseFeePerGas;
const maxPrices = await getMaxFeePerGasAndPriorityPrice();

if (maxPriorityFeePerGas) {
const maxFeePerGas = latestBlockBaseFeePerGas
? latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas
: estimatedMaxFeePerGas;
if (maxPrices) {
const { maxFeePerGas, maxPriorityFeePerGas } = maxPrices;

console.log("gas price buy", {
maxFeePerGas,
Expand Down
21 changes: 21 additions & 0 deletions packages/app/hooks/creator-token/use-max-gas-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { publicClient } from "app/lib/wallet-public-client";

export const useMaxGasPrices = () => {
const getMaxFeePerGasAndPriorityPrice = async () => {
// In Wei. Gwei = 0.001. Recommended by base team
const maxPriorityFeePerGas = 1000000n;
const latestBlockBaseFeePerGas = (await publicClient.getBlock())
.baseFeePerGas;
if (latestBlockBaseFeePerGas) {
const maxFeePerGas = latestBlockBaseFeePerGas * 2n + maxPriorityFeePerGas;
return {
maxFeePerGas,
maxPriorityFeePerGas,
};
}
};

return {
getMaxFeePerGasAndPriorityPrice,
};
};

0 comments on commit 0aa138b

Please sign in to comment.