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

Commit

Permalink
fix: also pass a gasPrice to the simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
hirbod committed Oct 30, 2023
1 parent d4b573c commit a9ad5be
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions packages/app/hooks/creator-token/use-creator-token-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,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 @@ -76,6 +99,7 @@ export const useCreatorTokenBuy = (params: {
functionName: "buy",
args: [priceToBuyNext.data?.totalPrice],
chain: baseChain,
gasPrice: paddedGasPriceBigInt,
});
requestPayload = request;
console.log("token amount 1 simulation ", request);
Expand All @@ -94,24 +118,8 @@ export const useCreatorTokenBuy = (params: {

console.log("simulate ", requestPayload);

console.log(
"Using following RPC for determing gas price",
requestPayload?.chain?.rpcUrls.default.http[0]
);
// Fetch current gas price from the Ethereum network
const provider = new providers.JsonRpcProvider(
requestPayload?.chain?.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 transactionHash = await walletClient?.writeContract?.({
...requestPayload,
gasPrice: paddedGasPrice,
});

console.log("Buy transaction hash ", requestPayload);
Expand Down

0 comments on commit a9ad5be

Please sign in to comment.