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 abstractions for converting ScVals to native types. #628

Closed
18 of 22 tasks
Shaptic opened this issue Jun 15, 2023 · 8 comments · Fixed by #630
Closed
18 of 22 tasks

Add abstractions for converting ScVals to native types. #628

Shaptic opened this issue Jun 15, 2023 · 8 comments · Fixed by #630
Assignees

Comments

@Shaptic
Copy link
Contributor

Shaptic commented Jun 15, 2023

Is your feature request related to a problem? Please describe.
Given an arbitrary ScVal type, converting it into a form natively usable from your chosen language is difficult and error prone.

Describe the solution you'd like
Easy conversion methods from ScVals to native types.

Here is the list of types and conversations:

  • scvBool ➡️ boolean
  • scvVoid ➡️ null
  • scvU32 ➡️ number
  • scvI32 ➡️ number
  • scvU64 ➡️ bigint
  • scvI64 ➡️ bigint
  • scvTimepoint ➡️ bigint
  • scvDuration ➡️ bigint
  • scvU128 ➡️ bigint
  • scvI128 ➡️ bigint
  • scvU256 ➡️ bigint
  • scvI256 ➡️ bigint
  • scvBytes ➡️ Buffer (or should it be Uint8Array?)
  • scvString ➡️ string
  • scvVec ➡️ Array<T>
  • scvMap ➡️ Map<ScVal, any>, where each ScVal is converted to its native type if possible
  • scvAddress ➡️ Address

Some conversions are unclear and the value of an abstraction for them is a little unclear:

  • scvSymbol
  • scvContractExecutable
  • scvLedgerKeyContractExecutable
  • scvLedgerKeyNonce
  • scvStatus

Describe alternatives you've considered
Suffering.

Additional context
Original feature request from @piyalbasu here: stellar/js-soroban-client#43.
Discord discussion here: https://discord.com/channels/897514728459468821/1111621289900253245.

@leighmcculloch
Copy link
Member

leighmcculloch commented Jun 16, 2023

  • scvU64 ➡️ ScInt
  • scvI64 ➡️ ScInt
  • scvTimepoint ➡️ bigint
  • scvDuration ➡️ bigint
  • scvU128 ➡️ ScInt
  • scvI128 ➡️ ScInt
  • scvU256 ➡️ ScInt
  • scvI256 ➡️ ScInt

What's the win with ScInt for some greater than 53-bit int's when bigint is available?

@Shaptic
Copy link
Contributor Author

Shaptic commented Jun 16, 2023

@leighmcculloch please refer to #620: it allows easy conversion between number|string|bigint ↔️ ScVals.

I suppose having it go to bigint would be fine, too; then users can new ScInt(...) it if they need the abstraction. Or they can .toBigInt() on the ScInt instance in the same way.

@leighmcculloch
Copy link
Member

That's fine too, however to your point in the doc that linked to this issue, there's so much confusion created by the exposure of non-native types like xdr types, so anything we can do to make them fade away rather than use them or types like them for conversions will probably improve the developer experience. At least to the extent we can do so without compromising on capability.

@Shaptic
Copy link
Contributor Author

Shaptic commented Jun 16, 2023

Yep, you make a good point! bigint is a better "landing pad" for intuition, then they can use the other abstractions as needed

@willemneal
Copy link
Member

One thing to note. Scvmap could be a rust struct, which would be better represented as a js object (TS interface). I had initially gone with Map, but it's a much worse DevX experience than just accessing a field. So perhaps the conversion interface should also allow passing the corresponding ScTypeDef.

As for the bytes I vote Uint8Array as Buffer is not as cross platform.

@Shaptic
Copy link
Contributor Author

Shaptic commented Jun 26, 2023

I moved away from Buffer in 10a69fe, but the migration to do custom type specification definitely needs more thought and work, so I'm going to merge #630 as is to have a decent starting point.

Can you expand a bit on the API and input/output you might like to see, so I can get a better sense of what you're looking for?

@willemneal
Copy link
Member

@Shaptic Shaptic moved this to In Progress in Platform Scrum Jun 30, 2023
@Shaptic
Copy link
Contributor Author

Shaptic commented Jun 30, 2023

I'm marking this as closed by #630 for tracking purposes, but opening a new issue for the remaining work!

@Shaptic Shaptic closed this as completed Jun 30, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in Platform Scrum Jun 30, 2023
@Shaptic Shaptic self-assigned this Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants