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

Change IInstruction to use ReadonlyUint8Array for data #3632

Merged
merged 2 commits into from
Nov 28, 2024

Conversation

mcintyre94
Copy link
Collaborator

This PR modifies the data field of IInstruction to use ReadonlyUint8Array. This means that you can create an IInstruction using bytes output from an encoder as input.

Copy link

changeset-bot bot commented Nov 26, 2024

🦋 Changeset detected

Latest commit: 584c637

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

Copy link

bundlemon bot commented Nov 26, 2024

BundleMon

Unchanged files (125)
Status Path Size Limits
@solana/web3.js production bundle
library/dist/index.production.min.js
33.76KB -
rpc-graphql/dist/index.browser.mjs
18.79KB -
rpc-graphql/dist/index.native.mjs
18.79KB -
rpc-graphql/dist/index.node.mjs
18.79KB -
errors/dist/index.node.mjs
14KB -
errors/dist/index.browser.mjs
13.99KB -
errors/dist/index.native.mjs
13.98KB -
transaction-messages/dist/index.browser.mjs
7.05KB -
transaction-messages/dist/index.native.mjs
7.05KB -
transaction-messages/dist/index.node.mjs
7.05KB -
codecs-data-structures/dist/index.native.mjs
4.77KB -
codecs-data-structures/dist/index.browser.mjs
4.77KB -
codecs-data-structures/dist/index.node.mjs
4.77KB -
webcrypto-ed25519-polyfill/dist/index.node.mj
s
3.49KB -
webcrypto-ed25519-polyfill/dist/index.browser
.mjs
3.47KB -
webcrypto-ed25519-polyfill/dist/index.native.
mjs
3.45KB -
rpc-subscriptions/dist/index.browser.mjs
3.33KB -
codecs-core/dist/index.browser.mjs
3.3KB -
codecs-core/dist/index.native.mjs
3.3KB -
codecs-core/dist/index.node.mjs
3.3KB -
rpc-subscriptions/dist/index.native.mjs
3.26KB -
rpc-subscriptions/dist/index.node.mjs
3.25KB -
rpc-transformers/dist/index.browser.mjs
2.92KB -
rpc-transformers/dist/index.native.mjs
2.92KB -
rpc-transformers/dist/index.node.mjs
2.92KB -
addresses/dist/index.browser.mjs
2.8KB -
addresses/dist/index.native.mjs
2.8KB -
addresses/dist/index.node.mjs
2.8KB -
library/dist/index.browser.mjs
2.67KB -
library/dist/index.native.mjs
2.67KB -
library/dist/index.node.mjs
2.67KB -
codecs-strings/dist/index.browser.mjs
2.53KB -
signers/dist/index.browser.mjs
2.53KB -
signers/dist/index.native.mjs
2.53KB -
signers/dist/index.node.mjs
2.53KB -
codecs-strings/dist/index.node.mjs
2.48KB -
codecs-strings/dist/index.native.mjs
2.46KB -
sysvars/dist/index.browser.mjs
2.32KB -
sysvars/dist/index.native.mjs
2.31KB -
sysvars/dist/index.node.mjs
2.31KB -
transaction-confirmation/dist/index.browser.m
js
2.3KB -
transaction-confirmation/dist/index.native.mj
s
2.3KB -
transaction-confirmation/dist/index.node.mjs
2.3KB -
rpc-subscriptions-spec/dist/index.browser.mjs
2.04KB -
rpc-subscriptions-spec/dist/index.native.mjs
2.04KB -
rpc-subscriptions-spec/dist/index.node.mjs
2.03KB -
codecs-numbers/dist/index.native.mjs
2.01KB -
codecs-numbers/dist/index.browser.mjs
2.01KB -
codecs-numbers/dist/index.node.mjs
2.01KB -
transactions/dist/index.browser.mjs
1.99KB -
transactions/dist/index.native.mjs
1.99KB -
transactions/dist/index.node.mjs
1.99KB -
react/dist/index.native.mjs
1.98KB -
react/dist/index.browser.mjs
1.98KB -
react/dist/index.node.mjs
1.98KB -
rpc-transport-http/dist/index.browser.mjs
1.91KB -
rpc-transport-http/dist/index.native.mjs
1.91KB -
rpc/dist/index.node.mjs
1.86KB -
keys/dist/index.browser.mjs
1.86KB -
keys/dist/index.native.mjs
1.86KB -
keys/dist/index.node.mjs
1.85KB -
rpc/dist/index.browser.mjs
1.76KB -
rpc/dist/index.native.mjs
1.76KB -
rpc-transport-http/dist/index.node.mjs
1.75KB -
subscribable/dist/index.browser.mjs
1.69KB -
subscribable/dist/index.native.mjs
1.69KB -
subscribable/dist/index.node.mjs
1.69KB -
rpc-types/dist/index.browser.mjs
1.6KB -
rpc-types/dist/index.native.mjs
1.6KB -
rpc-types/dist/index.node.mjs
1.6KB -
rpc-subscriptions-channel-websocket/dist/inde
x.native.mjs
1.27KB -
rpc-subscriptions-channel-websocket/dist/inde
x.browser.mjs
1.26KB -
rpc-subscriptions-channel-websocket/dist/inde
x.node.mjs
1.25KB -
options/dist/index.browser.mjs
1.18KB -
options/dist/index.native.mjs
1.18KB -
options/dist/index.node.mjs
1.17KB -
accounts/dist/index.browser.mjs
1.13KB -
accounts/dist/index.native.mjs
1.12KB -
accounts/dist/index.node.mjs
1.12KB -
rpc-spec-types/dist/index.browser.mjs
964B -
rpc-api/dist/index.browser.mjs
963B -
rpc-api/dist/index.native.mjs
962B -
rpc-spec-types/dist/index.native.mjs
962B -
rpc-spec-types/dist/index.node.mjs
961B -
rpc-api/dist/index.node.mjs
960B -
rpc-subscriptions-api/dist/index.native.mjs
870B -
rpc-subscriptions-api/dist/index.node.mjs
869B -
rpc-subscriptions-api/dist/index.browser.mjs
868B -
rpc-spec/dist/index.browser.mjs
829B -
rpc-spec/dist/index.native.mjs
829B -
rpc-spec/dist/index.node.mjs
828B -
promises/dist/index.browser.mjs
799B -
promises/dist/index.native.mjs
798B -
promises/dist/index.node.mjs
797B -
assertions/dist/index.browser.mjs
790B -
instructions/dist/index.browser.mjs
771B -
instructions/dist/index.native.mjs
770B -
instructions/dist/index.node.mjs
768B -
assertions/dist/index.native.mjs
724B -
fast-stable-stringify/dist/index.browser.mjs
724B -
assertions/dist/index.node.mjs
723B -
fast-stable-stringify/dist/index.native.mjs
723B -
fast-stable-stringify/dist/index.node.mjs
722B -
compat/dist/index.browser.mjs
712B -
compat/dist/index.native.mjs
711B -
compat/dist/index.node.mjs
710B -
programs/dist/index.browser.mjs
329B -
programs/dist/index.native.mjs
327B -
programs/dist/index.node.mjs
325B -
functional/dist/index.browser.mjs
154B -
functional/dist/index.native.mjs
152B -
text-encoding-impl/dist/index.native.mjs
152B -
functional/dist/index.node.mjs
151B -
codecs/dist/index.browser.mjs
137B -
codecs/dist/index.native.mjs
136B -
codecs/dist/index.node.mjs
134B -
ws-impl/dist/index.node.mjs
131B -
text-encoding-impl/dist/index.browser.mjs
122B -
text-encoding-impl/dist/index.node.mjs
119B -
crypto-impl/dist/index.node.mjs
114B -
ws-impl/dist/index.browser.mjs
113B -
crypto-impl/dist/index.browser.mjs
109B -
rpc-parsed-types/dist/index.browser.mjs
66B -
rpc-parsed-types/dist/index.native.mjs
65B -
rpc-parsed-types/dist/index.node.mjs
63B -

No change in files bundle size

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

Comment on lines +385 to +390
// TODO: This can just be `parseTransferSolInstruction(firstInstruction)` when the client is updated
// with the `@solana/web3.js` version that changes the instruction data type to `ReadonlyUint8Array`
const parsedFirstInstruction = parseTransferSolInstruction({
...firstInstruction,
data: firstInstruction.data as unknown as Uint8Array,
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We'll want to create a new build of the Codama JS clients with this change to make this go away.

This was actually where I first hit this issue. I was using an API that returned instruction data as base58, and then trying to convert its output into an IInstruction that I could parse with Codama. Since instruction data is Uint8Array this required me to cast my ReadonlyUint8Array from encoding the API's base58 data to a Uint8Array like this. I figured it makes sense to just widen the type on IInstruction as in this PR

Copy link
Contributor

A preview of the GitHub Pages site based on this PR is now available here:

solana-labs.github.io/solana-web3.js-pr-preview/3632/

Comment on lines +2 to +4
'@solana/transaction-messages': patch
'@solana/instructions': patch
'@solana/errors': patch
Copy link
Collaborator Author

@mcintyre94 mcintyre94 Nov 26, 2024

Choose a reason for hiding this comment

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

This is debatable. I'm widening the type of IInstruction, so any code that creates one of those is unaffected, but any code that treats its data as mutable (relying on the existing narrower type) is affected. In our code nothing relies on it being mutable, but I needed to update some type params. Downstream consumers like Codama clients need to be updated to avoid any code changes for their dependencies. Happy to change this to whatever we think is best.

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.

Added via Giphy

@mcintyre94
Copy link
Collaborator Author

mcintyre94 commented Nov 28, 2024

I've pushed a commit that refactors IInstructionWithData to have an unrestricted data type, and not extend IInstruction. This means that you can define a type like IInstruction & IInstructionWithData<MyParsedInstructionData> which might be useful

I noticed the type felt too restrictive because as-is we can't change Codama to use IInstructionWithData<ReadonlyUint8Array> before merging this PR - since it's tied to Uint8Array. This PR would already loosen it to ReadonlyUint8Array but it might be useful to be able to loosen the type of data further

It also better matches what we do in eg TransactionMessageWithBlockhashLifetime which doesn't extend TransactionMessage

Happy to revert if this is too much of a breaking change to justify though!

Edit: On second thought, I don't think this adds much value, removed it. The change to ReadonlyUint8Array enables what we need and anything wider is speculative and potentially confusing.

@mcintyre94 mcintyre94 force-pushed the iinstruction-with-data-read-only branch from 2580fea to 584c637 Compare November 28, 2024 10:39
Copy link
Collaborator

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

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

Thanks! As we discussed on Slack, let's merge this first and then update the Codama renderer before coordinating the release of the clients.

@mcintyre94 mcintyre94 merged commit 704d8a2 into master Nov 28, 2024
20 checks passed
@mcintyre94 mcintyre94 deleted the iinstruction-with-data-read-only branch November 28, 2024 13:34
@github-actions github-actions bot mentioned this pull request Nov 28, 2024
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.

3 participants