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

Commit

Permalink
fix: pad gas price to fix underpriced transaction (#2472)
Browse files Browse the repository at this point in the history
* fix: pad gas price to fix underpriced transaction

* fix: comments

* fix: also pass a gasPrice to the simulation
  • Loading branch information
hirbod committed Oct 30, 2023
1 parent f484213 commit dd71286
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions packages/app/hooks/creator-token/use-creator-token-buy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { providers } from "ethers";
import { useSWRConfig } from "swr";
import useSWRMutation from "swr/mutation";

Expand Down Expand Up @@ -67,6 +68,29 @@ export const useCreatorTokenBuy = (params: {
if (result) {
let requestPayload: any;

console.log(
"Using following RPC for determing gas price",
baseChain?.rpcUrls.default.http[0]
);

// Fetch current gas price from the Ethereum network
const provider = new providers.JsonRpcProvider(
baseChain?.rpcUrls.default.http[0]
);
let currentGasPrice = await provider.getGasPrice();
console.log("current Gas price fetched from RPC", currentGasPrice);

// Adjust the gas price (increase it by 20% to be more competitive)
const paddedGasPrice = currentGasPrice.mul(120).div(100);
console.log("padded gas price after calculation:", paddedGasPrice);

const paddedGasPriceBigInt = BigInt(paddedGasPrice.toString());

console.log(
"padded gas price after conversion:",
paddedGasPriceBigInt
);

if (tokenAmount === 1) {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
Expand All @@ -75,8 +99,10 @@ export const useCreatorTokenBuy = (params: {
functionName: "buy",
args: [priceToBuyNext.data?.totalPrice],
chain: baseChain,
gasPrice: paddedGasPriceBigInt,
});
requestPayload = request;
console.log("token amount 1 simulation ", request);
} else {
const { request } = await publicClient.simulateContract({
address: profileData?.data?.profile.creator_token.address,
Expand All @@ -86,14 +112,16 @@ export const useCreatorTokenBuy = (params: {
args: [tokenAmount, priceToBuyNext.data?.totalPrice],
chain: baseChain,
});
console.log("bulk buy ", request);
console.log("bulk buy request", request);
requestPayload = request;
}

console.log("simulate ", requestPayload);
const transactionHash = await walletClient?.writeContract?.(
requestPayload
);

const transactionHash = await walletClient?.writeContract?.({
...requestPayload,
});

console.log("Buy transaction hash ", requestPayload);
if (transactionHash) {
const transaction = await publicClient.waitForTransactionReceipt({
Expand Down

0 comments on commit dd71286

Please sign in to comment.