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:
-
Use the scValToBigInt function:
// ScVal -> BigInt
const bigint = scValToBigInt(scval);
-
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.
-
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.)
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:
Use the
scValToBigIntfunction:Use the
ScInttype:Another problem with
ScIntis that it looks like an XDR type, but it isn't.Use the
XdrLargeInttype. This type should probably not be exported as it seems like an implementation detail?Another problem with
XdrLargeIntis that there's another typeLargeIntwhich serves another purpose.Describe the solution you'd like
Functions on
ScVal. Close proximity toScValso that the functionality is discoverable, and less new concepts (XdrLargeInt,ScInt) are introduced).Expand the existing ScVal functions to accept Number/BigInt:
Add functions for converting from ScVal to Number/BigInt:
Describe alternatives you've considered
Another option would be to focus on using just one of these methods, such as combining
ScIntandXdrLargeIntso all conversions (simple and advanced) are in one type, and removingscValToBigIntsince 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.)