Skip to content

Simplify how to convert from ScVal types into BigInts/Numbers #722

@leighmcculloch

Description

@leighmcculloch

Is your feature request related to a problem? Please describe.

The APIs for working with XDR ScVal number types are reasonably confusing and non-obvious. There are also multiple ways to do the same thing.

For example there are three ways to get at / work with number values:

  1. Use the scValToBigInt function:

    // ScVal -> BigInt
    const bigint = scValToBigInt(scval);
  2. Use the ScInt type:

    // ScVal -> BigInt
    const bigint = ScInt.fromScVal(scval);
    // BigInt -> ScVal
    const scval = new ScInt(bigint).toI128();

    Another problem with ScInt is that it looks like an XDR type, but it isn't.

  3. Use the XdrLargeInt type. This type should probably not be exported as it seems like an implementation detail?

    // ScVal -> BigInt
    const bigint = new XdrLargeInt(scIntType, [scval.value().lo(), scval.value().hi()]).toBigInt();

    Another problem with XdrLargeInt is that there's another type LargeInt which serves another purpose.

Describe the solution you'd like

Functions on ScVal. Close proximity to ScVal so that the functionality is discoverable, and less new concepts (XdrLargeInt, ScInt) are introduced).

Expand the existing ScVal functions to accept Number/BigInt:

-xdr.ScVal.scvI128(value: xdr.Int128Parts): xdr.ScVal
+xdr.ScVal.scvI128(value: xdr.Int128Parts | BigInt): xdr.ScVal

Add functions for converting from ScVal to Number/BigInt:

const bigint = scval.toBigInt();
const number = scval.toNumber(); // with check

Describe alternatives you've considered

Another option would be to focus on using just one of these methods, such as combining ScInt and XdrLargeInt so all conversions (simple and advanced) are in one type, and removing scValToBigInt since it's superfluous.

Additional context

Thinking about this after seeing the following question in Discord:

Historically we've tried to pull people away from the XDR, and hide the XDR where possible behind nicer APIs. With Soroban folks need to embrace the XDR more than they did with classic operations. Also, in this case the APIs we've pulled people from to are still relatively confusing, and we can introduce less concepts by pushing people back to the XDR type and adding functions to simplify interaction on the XDR type.

(I am more interested in solving the problem than with the solution I suggest above, and definitely defer to others more qualified for better solutions.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions