Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RPC-only transaction confirmation support #110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rroblak
Copy link

@rroblak rroblak commented Feb 11, 2025

Problem

Transaction confirmation currently requires WebSocket connections, which may not be available or desired in all environments. And there's no documented way to use the transaction confirmation functionality with just RPC (HTTP) polling.

As the great Rich Hickey said:

So let's say I had a function -- one of those A, B, C -- that already exists and you're already calling, and I wanted to enhance it. Well, if I require more from you than I used to, I've broken you. If I require less from you than I used to, I haven't.

The 2.0 API shouldn't break use cases by requiring a Websocket where only having an RPC connection will suffice.

N.b. I'm not suggesting that RPC-only is technically superior. Just that it should be possible.

Summary of Changes

  • Add support for RPC-only transaction confirmation through polling
  • Make rpcSubscriptions parameter optional in sendAndConfirmTransactionFactory
  • Add configurable polling interval option
  • Add new example demonstrating RPC-only transaction confirmation
  • Update documentation to reflect RPC-only support
  • Add tests for polling-based confirmation strategies

Copy link

changeset-bot bot commented Feb 11, 2025

🦋 Changeset detected

Latest commit: 197186d

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -0,0 +1,201 @@
/* eslint-disable sort-keys-fix/sort-keys-fix */
/**
* EXAMPLE
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied and minimally changed the existing example.ts to demonstrate the new RPC-only functionality.

packages/library/README.md Outdated Show resolved Hide resolved
@@ -143,7 +149,7 @@ try {
}
```

### `sendTransactionWithoutConfirmingFactory({rpc, rpcSubscriptions})`
Copy link
Author

@rroblak rroblak Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected this error (the method doesn't actually accept an rpcSubscriptions parameter).

@rroblak rroblak force-pushed the rroblak/rpc-only-transaction-confirmation branch 2 times, most recently from 1696ea8 to 21cbdf9 Compare February 11, 2025 20:00
Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr web3.js 2.0 doesn't do ‘modes.’

sendAndConfirmTransactionFactory() is already a super thin wrapper around createBlockHeightExceedencePromiseFactory(). I would totally consider including a new confirmer, that describes your new strategy, and implements your polling code.

const sendAndPollToConfirm = sendAndPollToConfirmTransactionFactory({
    interval: 1000,
    rpc,
});

await Promise.all([
let currentBlockHeight: bigint;

if (rpcSubscriptions) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this does is to create dead code, no matter which way you use this confirmation strategy. If you use subscriptions, all of the polling code is dead. If you use polling, all of the subscriptions code is dead. You get the dead code in your JS bundle unconditionally, which reduces package efficiency.

In short, ‘modes’ are bad, because they create dead code and reduce package efficiency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, that's fair. I'll extract these changes into a separate confirmer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveluscher Please see the updated changeset.

Add `sendAndPollToConfirmTransactionFactory` as a WebSocket-free
alternative for transaction confirmation. This implementation uses
polling instead of subscriptions, making it suitable for environments
where WebSocket connections are unavailable or undesired.

Key changes:
- Add new `sendAndPollToConfirmTransactionFactory` with configurable
  polling interval
- Implement block height and signature status polling strategies
- Add comprehensive tests for new polling mechanisms
- Update documentation with usage examples
- Add new example demonstrating RPC-only transaction confirmation
@rroblak rroblak force-pushed the rroblak/rpc-only-transaction-confirmation branch from 21cbdf9 to d246964 Compare February 13, 2025 00:04
rpc,
pollingInterval,
}: SendAndPollToConfirmTransactionWithBlockhashLifetimeFactoryConfig<'mainnet'>): SendAndPollToConfirmTransactionWithBlockhashLifetimeFunction;
export function sendAndPollToConfirmTransactionFactory<
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The gist of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants