alchemyplatform / alchemy-sdk-js

The easiest way to connect your dApp to the blockchain.
https://www.alchemy.com/sdk
MIT License
386 stars 167 forks source link

Missing response error If invoking SDK rpc functions within an AWS lambda #400

Closed nsatharasinghe closed 2 days ago

nsatharasinghe commented 7 months ago

Environment

AWS Lambda

Describe the problem

Missing response error If invoking SDK rpc functions within an AWS Lambda

missing response (requestBody="{\"method\":\"eth_gasPrice\",\"params\":[],\"id\":42,\"jsonrpc\":\"2.0\"}", requestMethod="POST", serverError={}, url="https://eth-sepolia.g.alchemy.com/v2/{API-KEY}, code=SERVER_ERROR, version=web/5.7.1) at Logger.makeError (/var/task/src/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:224:1) at Logger.throwError (/var/task/src/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:233:1) at /var/task/src/webpack:/node_modules/@ethersproject/web/lib.esm/index.js:217:1 at Generator.throw () at rejected (/var/task/src/webpack:/node_modules/@ethersproject/web/lib.esm/index.js:6:42) at processTicksAndRejections

How to reproduce:

Use the below code within an AWS lambda:

import { Alchemy, Network } from 'alchemy-sdk';

 const settings = {
            apiKey: 'api-key',
            network: Network.ETH_SEPOLIA,
        };

        const alchemy = new Alchemy(settings);
        try {
            const gp = await alchemy.core.getGasPrice();
            Logger.info('AlchemyTestHandler getGasPrice >>>', gp);
        } catch (error) {
            Logger.info('AlchemyTestHandler error', error);
        }

Relevant code or sample repro:

ChiuMungZitAlexander commented 6 months ago

This will also happen when using nextjs v14 ssr as below.

import { Alchemy, Network } from "alchemy-sdk";

export default async function Home() {
  const alchemy = new Alchemy({
    apiKey: "",
    network: Network.ETH_MAINNET,
  });

  const blockNumber = await alchemy.core.getBlockNumber();

  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      <div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
        <p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto  lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
          {blockNumber}
        </p>
      </div>
    </main>
  );
}
Error: missing response (requestBody="{\"method\":\"eth_blockNumber\" ...
leonacostaok commented 6 months ago

Anyone found an easy fix?

smblee commented 5 months ago

Can confirm this happens on nextjs 14

github-actions[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs

alexkubica commented 3 months ago

Can confirm this happens on nextjs 14

+1

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs

thebrianchen commented 2 months ago

I've added support for passing in a ConnectionInfo override via the AlchemySettings.connectionInfoOverrides property in v3.3.1 of the SDK. You should be able to follow the comments in this ethers thread and pass in skipFetchSetup or your own fetchOptions object to work around the issue.

rsproule commented 1 month ago

here is an example using @thebrianchen override workaround, this works in nextjs 14

const config = {
  apiKey: process.env.ALCHEMY_API, // Replace with your API key
  network: Network.BASE_MAINNET, // Replace with your network
  connectionInfoOverrides: {
    skipFetchSetup: true,
  },
} as AlchemySettings;

const alchemy = new Alchemy(config);
github-actions[bot] commented 1 week ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs