RootSoft / algorand-dart

Unofficial community SDK to interact with the Algorand network, for Dart & Flutter
MIT License
36 stars 16 forks source link

Parse "total" as int will raise error for a big number in AssetParametersFromJson #20

Closed bofeng closed 2 years ago

bofeng commented 2 years ago

When the asset has a big number in its total field (like USDC, 2^64-1):

{ ..., "total":18446744073709551615,"unit-name":"USDC" ...}

And after dart parse this json with jsonDecode, the "total" field will be treated as "double" not int, because "int" is not big enough to save this number. ((and the value weirdly become "18446744073709552000.0"). Then this json will be passed to the AssetParametersFromJson constructor here:

https://github.com/RootSoft/algorand-dart/blob/75833b3275686f4531c4aeac0db4c81a78ef1515/lib/src/models/assets/asset_parameters_model.g.dart#L13

this line total: json['total'] as int will throw an exception:

Unhandled Exception: type 'double' is not a subtype of type 'int' in type cast

To solve it, I think could either change the type of total in AssetParameters class from "int" to "num", or maybe "BigInt" (probably better) , so total could be initialized as BigInt.from(json['total']) (this will actually change the double 18446744073709552000.0 to 18446744073709551616, so to make it accurate, may need something like total > 18446744073709551615 ? 18446744073709551615 : total).

Also kudos to this repo, I really like it, thank you! great work!!! 👍

RootSoft commented 2 years ago

Hi @bofeng

I will be working on improving the SDK to support bigint rather then num in the coming weeks. In the meantime, if you need the feature urgently, feel free to fork the repo and make the changes locally. The SDK uses json_serializable, so in theory it should be easy to just change int to bigint and rebuild using dart run build_runner build --delete-conflicting-outputs. I will try to add support asap.

Thanks for opening this issue.

bofeng commented 2 years ago

I see, sounds great, thank you very much 👍

developer-mark-io commented 2 years ago

I am glad you opened this issue @bofeng To give my two cents I got around this quickly by changing the asset_parameters_model.dart and asset_parameters_model.g.dart and using num for the total (this is pretty hack but I just needed a quick fix). BigIntcontinued to throw errors unfortunately.

RootSoft commented 2 years ago

As a continuous effort to keep the sdk up to date, the 2.0.0-dev prereleases will be used to update the sdk to add better support for big integers and might result into small breaking changes. 2.0.0-dev1 contains a fix for this issue.

https://pub.dev/packages/algorand_dart/versions/2.0.0-dev.1