aeternity / aepp-calldata-js

Aeternity data serialization library
ISC License
3 stars 4 forks source link

Agree on type alias to decode `int` as `Number` instead of `BigInt` #232

Open davidyuk opened 1 year ago

davidyuk commented 1 year ago

Was initially discussed in https://github.com/aeternity/aesophia/issues/351

I propose to decode returned value to Number instead of BigInt if calldata finds a shortint (not sure about name) aliased to int while type resolving.

For example for the contract

contract C =
  type shortint = int
  record rec = { x: shortint }

  entrypoint f(s : shortint) : shortint =
    s * 42

  entrypoint f2() : rec =
    { x = 42 }

ACI would be

[
  {
    "contract": {
      "functions": [
        {
          "arguments": [
            {
              "name": "s",
              "type": "C.shortint"
            }
          ],
          "name": "f",
          "payable": false,
          "returns": "C.shortint",
          "stateful": false
        },
        {
          "arguments": [],
          "name": "f2",
          "payable": false,
          "returns": "C.rec",
          "stateful": false
        }
      ],
      "kind": "contract_main",
      "name": "C",
      "payable": false,
      "typedefs": [
        {
          "name": "shortint",
          "typedef": "int",
          "vars": []
        },
        {
          "name": "rec",
          "typedef": {
            "record": [
              {
                "name": "x",
                "type": "C.shortint"
              }
            ]
          },
          "vars": []
        }
      ]
    }
  }
]

And it looks like ACI preserves enough info to track int alias even in complex structures. Of course, this feature is not urgent.

dincho commented 1 year ago

I'm not sure I get it? You're asking to handle a custom type that's not a buildin into Sophia ?

davidyuk commented 1 year ago

Yep! For js developers' convenience 🙂 I'm not sure how this feature is important. The thing is that BigInt is not very handy. For example, it complains if mixed with numbers (1 * 1n), and it can't be serialized to JSON (JSON.stirngify({ a: 1n })). A developer may need to convert BigInts to Numbers by himself, and I'm proposing to have an agreement instead.