WICG / webmonetization

Proposed Web Monetization standard
https://webmonetization.org
Other
466 stars 152 forks source link

fix: revert ill considered bigint usage for amount #282

Closed sublimator closed 2 years ago

sublimator commented 2 years ago

BigInt Downsides

Need to monkey patch BigInt.prototype.toJSON for JSON.stringify to work

export function extendPrimitives(): void {
  // Extend BigInt prototype for easier JSON stringification
  Object.defineProperty(BigInt.prototype, 'toJSON', {
    value: function (): string {
      return this.toString()
    }
  })
}

All operands must be BigInt in math operations

> BigInt(0) / 5
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions

It is only a Big Integer, not an arbitrary precision, floating point Decimal. This means that when converting to floating point one must use number math. When using BigInt math, fractional amounts will round down to zero.

> const amount = BigInt(1)
> const assetScale = 2
> const assetCode = 'USD'
> console.log(`${assetCode}${amount / BigInt(Math.pow(10, assetScale))}`)
USD0

BigInt Upsides