algorand / algorand/js-algorand-sdk

Add bigint support to `microalgosToAlgos()` and `algosToMicroalgos()`

Aperta
#907 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
new-feature-request
Lingua principale
TypeScript
Stelle
297
Fork
214
Merge medio
1h 3m
PR unite (30g)
3

Descrizione

## Problem

The Algod and Indexer clients now use `bigint` for microAlgo amounts in v3. However, the `microalgosToAlgos()` and `algosToMicroalgos()` only support `number`s.

## Solution

### Workaround No. 1

SDK users can workaround this issue for now by converting a `bigint` microAlgo value to a `number` before using `microalgosToAlgos()` or `algosToMicroalgos()`, but this can result in a loss of precision for large Algo values.

### Workaround No. 2

Do not use `microalgosToAlgos()` or `algosToMicroalgos()` and do the conversion calculations manually with `bigint`s.

### Solution for `microalgosToAlgos()`

Accept either a `bigint` or `number` argument. Return a `number` (like before). This solution is backwards compatible and would not require SDK users to change their existing code.

### Solution No. 1 for `algosToMicroalgos()`

Can continue to accept only a `number` argument, but I think it would be good for consistency to also allow a `bigint` argument like proposed solution for `microalgosToAlgos()`. Return a `bigint`, which may be another breaking change.

### Solution No. 2 for `algosToMicroalgos()`

Accept the same argument types as Solution No. 1, but have an optional `returnBigInt` boolean argument that is `false` by default. When `returnBigInt` is `true`, a `bigint` is returned, otherwise a `number` is returned (like before). This would keep backwards compatibility, but is slightly more work to implement and maintain.

## Dependencies

[The `INVALID_MICROALGOS_ERROR_MSG` constant](https://github.com/algorand/js-algorand-sdk/blob/6973ff583b243ddb0632e91f4c0383021430a789/src/convert.ts#L2-L3) will need to be updated to reflect `microalgosToAlgos()` being able to accept integers larger than 2^53 - 1.

## Urgency

Medium.

Web and mobile apps use `microalgosToAlgos()` and `algosToMicroalgos()` frequently to display Algo amounts and to receive an Algo amount from the user respectively.

## Misc.

I think "Solution for `microalgosToAlgos()`" and "Solution No. 2 for `algosToMicroalgos()`" are the best options. I can create a pull request for implementing those solutions.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.