Handle scSpecTypeVal in Spec.nativeToScVal for raw JS types - #1485
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for converting JS “val”-typed inputs (scSpecTypeVal) into appropriate ScVal variants, with tests and updated reference docs.
Changes:
- Implement
scSpecTypeValhandling inSpec.nativeToScVal(string/address, i128, bool). - Add unit tests covering
nativeToScValandfuncArgsToScValswith val-typed inputs. - Replace
@ts-ignorewith@ts-expect-errorand update docs source links.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/unit/spec/contract_spec.test.ts | Adds tests for scSpecTypeVal conversions and tightens TS suppression directives. |
| src/contract/spec.ts | Implements conversion logic for scSpecTypeVal in nativeToScVal. |
| docs/reference/contracts-client.md | Updates generated “Source” links to match new line numbers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: deb6c38476
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5217f20576
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8de3fc3edb
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7f3e9fda73
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e33b4475c0
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1120c1a8da
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c0e714406
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
9c0e714 to
599d394
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 599d3947e4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 599d3947e4
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Hey @Shadow-MMN, thanks for taking this on! I'd like to see changed before merging though: 1. Most of this already exists in the SDK — let's delegate instead of duplicating. We already have a generic typeless converter: if (value === xdr.ScSpecType.scSpecTypeVal().value) {
return nativeToScVal(val);
}The only thing it doesn't handle is Map — I'd add that to the base helper instead, so the two code paths can't drift apart over time.
It makes the output type depend on the string's content — any strkey that happens to checksum becomes scvAddress, everything else becomes scvString. Address.fromString also accepts B.../L... strkeys, which aren't valid contract inputs. This is why the base helper makes you opt in with {type: 'address'}. If someone wants an address they can just pass an Address object — no ambiguity. Smaller stuff:
The nice thing is your test suite should carry over basically unchanged after the restructure, and that's the hard part. Happy to take another look once it's updated! |
Thank you for the detailed review, will be pushing a change to address them. |
Co-Authored-By: Claude Fable 5 <[email protected]>
…lue specifications
| // The Soroban runtime expects maps to have their keys in sorted | ||
| // order, so let's do that here as part of the conversion to prevent | ||
| // confusing error messages on execution. | ||
| .sort(([key1], [key2]) => (key1 < key2 ? -1 : key1 > key2 ? 1 : 0)) |
There was a problem hiding this comment.
Instead of hand-rolling the sort, can we leverage scvSortedMap here?
What
Spec.prototype.nativeToScValcurrently throws when the target Soroban type isscSpecTypeVal(value 0) and the input is a raw JavaScript primitive (string, number, or boolean). This makes it impossible to pass contract function arguments typed asValwithout manually constructingxdr.ScValobjects.This PR adds handling for
scSpecTypeValby converting JS primitives to their correspondingxdr.ScValtypes:Conversion table (scSpecTypeVal → ScVal)
stringscvAddress(try/catch fallback toscvString)number/bigintScInt(u64 → u128 → u256 → i64 → i128 → i256)booleanscvBoolundefined/nullscvVoidarrayscvVecMapscvMap(sorted){}scvMapwith string keys (sorted)Address/ContractscvAddressUint8Array/BufferscvBytesxdr.ScValTypeError(prevents private field leaks)Why
When a smart contract defines a function parameter as
Val(i.e.,scSpecTypeVal), the type is unconstrained — it accepts any ScVal. The SDK should be able to convert native JS values to the appropriate ScVal without the user having to manually construct XDR objects.This was originally discovered and monkey-patched in the tansu project (see Consulting-Manao/tansu#174) and @chadoh requested the fix be pushed upstream. (Closes #1480)
Testing
Added 9 unit tests covering string, Stellar address, contract ID, number, bigint, boolean, and
funcArgsToScValsintegration. All 2,058 existing tests continue to pass.Files changed
src/contract/spec.ts— AddedscSpecTypeValhandler innativeToScVal()with all conversions above; plain-object guard usesObject.prototype.toString.call+ prototype check to exclude class instancestest/unit/spec/contract_spec.test.ts— Added tests for all conversion pathsSited from Consulting-Manao/tansu#174
Closes #1480