Trouble with gas fees when purchasing NFTs on Goerli using OpenSea SDK

I’m having trouble with gas fees while trying to buy NFTs using the opensea-js library. The listing part works fine, but when I try to make a purchase, I run into gas-related issues. This is happening on both the Goerli testnet and Ethereum mainnet.

Here’s a simplified version of my buy function:

async function purchaseNFT(privateKey, tokenId, tokenAddress, accountAddress) {
  const provider = setupProvider(privateKey);
  const openseaSDK = new OpenSeaSDK(provider, { networkName: Network.Goerli });
  
  const { orders } = await openseaSDK.api.getOrders({
    assetContractAddress: tokenAddress,
    tokenId,
    side: 'ask'
  });
  
  if (!orders) throw new Error('No orders found');
  
  const txHash = await openseaSDK.fulfillOrder({
    order: orders[0],
    accountAddress
  });
  
  return txHash;
}

I’m using Node.js with Express. Any ideas on what might be causing the gas issues or how to resolve them?

hey there elias87! i’ve been tinkering with nfts on goerli too and ran into similar headaches with gas fees. have you tried playing around with the gas settings in your transaction options? sometimes the default values don’t quite cut it, especially when the network’s busy.

also, curious - are you getting any specific error messages when the transaction fails? that could give us a better clue about what’s going on.

oh, and one thing that helped me was using a gas price estimator to get a better idea of current network conditions. have you tried that? it might help you set more accurate gas prices.

what kind of nfts are you trying to buy, btw? just wondering if there’s anything specific about the contracts that could be causing issues.

keep us posted on how it goes! nft stuff can be tricky but it’s so cool when you finally get it working, right?

yo elias87, i feel ya on those gas probs. have u checked ur wallet balance? sometimes i forget and run outta eth for gas lol. also, network congestion can mess things up. maybe try bumping ur gas limit a bit? like 350000 or sumthin. lemme know if that helps!

I’ve encountered similar issues when working with the OpenSea SDK. One potential solution is to explicitly set the gas price and limit in your transaction options. You can modify your fulfillOrder call like this:

const txHash = await openseaSDK.fulfillOrder({
  order: orders[0],
  accountAddress,
  transactionOptions: {
    gasLimit: 300000,
    gasPrice: ethers.utils.parseUnits('20', 'gwei')
  }
});

Adjust these values based on current network conditions. Also, ensure your account has sufficient ETH for gas fees. If the issue persists, consider experimenting with higher gas limits or prices, especially under different network conditions. This approach worked for me during my tests on Goerli.