alchemy-sdk-js
alchemy-sdk-js copied to clipboard
Impossible to subscribe to `newHeads` event
Environment
- Browser version: Node v16.13
- Alchemy SDK version: 2.2.3
Describe the problem
It's impossible to subscribe to newHeads
websocket event via sdk methods. Subscription to block
event only returns block number.
How to reproduce:
Method 1:
alchemy.ws.on('newHeads', (data) => {
console.log(data);
});
// result:
unhandled: Event {
tag: 'newheads',
listener: [Function (anonymous)],
once: false,
_lastBlockNumber: -2,
_inflight: false
}
no further events
Method 2:
alchemy.ws.on({ method: 'newHeads' }, (data) => {
console.log(data);
});
// Unhandled rejection Error: Invalid method name newHeads. Accepted method names: alchemy_pendingTransactions,alchemy_minedTransactions
Relevant code or sample repro:
You must use 'block' as the event name when using the SDK, like this:
alchemy.ws.on('block', (blockNumber: number) => {
console.log(blockNumber)
})
@luisgerardodev yes, but i want receive full header object. Alchemy WS API supports it.
@andkom There hasn't been much demand for subscribing to the full block info, and the underlying ethers dependency does not support it either. May I ask what your use case is for the full block info?
In the meantime, you can subscribe directly by accessing the underlying ethers websocket provider to send your own custom subscription:
const provider = await alchemy.config.getWebsocketProvider();
await provider._subscribe("newHeads", ["newHeads"], (result) => {
// Parse and format the block data into the same format as `getBlock()` would provide
const block = this.formatter.block(result);
// your code here...
});
This code block was copied over from the ethers discussion.
@thebrianchen thanks, I need it to receive base fee updates.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs