@tetherto/wdk-protocol-fiat-moonpay 1.0.0-beta.5 → 1.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tetherto/wdk-protocol-fiat-moonpay",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.7",
|
|
4
4
|
"description": "MoonPay on-ramp and off-ramp module for WDK (Wallet Development Kit) by Tether, with widget URLs and quotes.",
|
|
5
5
|
"homepage": "https://docs.wdk.tether.io/sdk/fiat-modules/fiat-moonpay/",
|
|
6
6
|
"keywords": [
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@tetherto/wdk-wallet": "1.0.0-beta.14",
|
|
30
|
-
"bare-node-runtime": "^1.
|
|
30
|
+
"bare-node-runtime": "^1.5.2",
|
|
31
31
|
"bignumber.js": "^9.3.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
package/src/moonpay-protocol.js
CHANGED
|
@@ -402,6 +402,25 @@ const MOONPAY_ORIGINS = {
|
|
|
402
402
|
}
|
|
403
403
|
const MOONPAY_CACHE_TIME = 10 * 60 * 1000
|
|
404
404
|
|
|
405
|
+
/**
|
|
406
|
+
* The most decimals, or digits of precision, a MoonPay currency record may carry.
|
|
407
|
+
*/
|
|
408
|
+
const MAX_CURRENCY_DIGITS = 36
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Checks a decimals or precision value from a MoonPay currency record before it scales an amount.
|
|
412
|
+
*
|
|
413
|
+
* @param {string} code - The currency's code.
|
|
414
|
+
* @param {string} field - The record field the value comes from.
|
|
415
|
+
* @param {unknown} value - The value.
|
|
416
|
+
* @throws {Error} If the value is not an integer between 0 and 36.
|
|
417
|
+
*/
|
|
418
|
+
function assertCurrencyDigits (code, field, value) {
|
|
419
|
+
if (!Number.isInteger(value) || value < 0 || value > MAX_CURRENCY_DIGITS) {
|
|
420
|
+
throw new Error(`MoonPay returned an invalid ${field} for ${code}: ${value}`)
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
405
424
|
export default class MoonPayProtocol extends FiatProtocol {
|
|
406
425
|
/**
|
|
407
426
|
* Creates a new interface to interact with the MoonPay protocol without binding it to a wallet account.
|
|
@@ -455,6 +474,12 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
455
474
|
if (!cryptoInfo || !fiatInfo) {
|
|
456
475
|
throw new Error('Cannot find info for cryptoAsset and fiatCurrency')
|
|
457
476
|
}
|
|
477
|
+
|
|
478
|
+
assertCurrencyDigits(cryptoInfo.code, 'decimals', cryptoInfo.decimals)
|
|
479
|
+
assertCurrencyDigits(cryptoInfo.code, 'precision', cryptoInfo.precision)
|
|
480
|
+
assertCurrencyDigits(fiatInfo.code, 'decimals', getFiatDecimals(fiatInfo))
|
|
481
|
+
assertCurrencyDigits(fiatInfo.code, 'precision', fiatInfo.precision)
|
|
482
|
+
|
|
458
483
|
return { cryptoInfo, fiatInfo }
|
|
459
484
|
}
|
|
460
485
|
|
|
@@ -463,6 +488,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
463
488
|
* @override
|
|
464
489
|
* @param {MoonPayBuyOptions} options - The options for the purchase.
|
|
465
490
|
* @returns {Promise<BuyResult>} The URL for the user to complete the purchase.
|
|
491
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
466
492
|
*/
|
|
467
493
|
async buy (options) {
|
|
468
494
|
const { cryptoAsset, fiatCurrency, recipient, config } = options
|
|
@@ -528,6 +554,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
528
554
|
* @override
|
|
529
555
|
* @param {MoonPayQuoteBuyOptions} options - The options for the quote.
|
|
530
556
|
* @returns {Promise<MoonPayBuyQuote>} A quote for the transaction.
|
|
557
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
531
558
|
*/
|
|
532
559
|
async quoteBuy (options) {
|
|
533
560
|
const { cryptoAsset, fiatCurrency, config } = options
|
|
@@ -594,6 +621,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
594
621
|
* @override
|
|
595
622
|
* @param {MoonPayQuoteSellOptions} options - The options for the quote.
|
|
596
623
|
* @returns {Promise<MoonPaySellQuote>} A quote for the transaction.
|
|
624
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
597
625
|
*/
|
|
598
626
|
async quoteSell (options) {
|
|
599
627
|
const { cryptoAsset, fiatCurrency, cryptoAmount, config } = options
|
|
@@ -652,6 +680,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
652
680
|
* @override
|
|
653
681
|
* @param {MoonPaySellOptions} options - The options for the sale.
|
|
654
682
|
* @returns {Promise<SellResult>} The URL for the user to complete the sale.
|
|
683
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
655
684
|
*/
|
|
656
685
|
async sell (options) {
|
|
657
686
|
const { cryptoAsset, fiatCurrency, refundAddress, config } = options
|
|
@@ -43,6 +43,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
43
43
|
* @override
|
|
44
44
|
* @param {MoonPayBuyOptions} options - The options for the purchase.
|
|
45
45
|
* @returns {Promise<BuyResult>} The URL for the user to complete the purchase.
|
|
46
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
46
47
|
*/
|
|
47
48
|
override buy(options: MoonPayBuyOptions): Promise<BuyResult>;
|
|
48
49
|
/**
|
|
@@ -50,6 +51,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
50
51
|
* @override
|
|
51
52
|
* @param {MoonPayQuoteBuyOptions} options - The options for the quote.
|
|
52
53
|
* @returns {Promise<MoonPayBuyQuote>} A quote for the transaction.
|
|
54
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
53
55
|
*/
|
|
54
56
|
override quoteBuy(options: MoonPayQuoteBuyOptions): Promise<MoonPayBuyQuote>;
|
|
55
57
|
/**
|
|
@@ -57,6 +59,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
57
59
|
* @override
|
|
58
60
|
* @param {MoonPayQuoteSellOptions} options - The options for the quote.
|
|
59
61
|
* @returns {Promise<MoonPaySellQuote>} A quote for the transaction.
|
|
62
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
60
63
|
*/
|
|
61
64
|
override quoteSell(options: MoonPayQuoteSellOptions): Promise<MoonPaySellQuote>;
|
|
62
65
|
/**
|
|
@@ -64,6 +67,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
64
67
|
* @override
|
|
65
68
|
* @param {MoonPaySellOptions} options - The options for the sale.
|
|
66
69
|
* @returns {Promise<SellResult>} The URL for the user to complete the sale.
|
|
70
|
+
* @throws {Error} If MoonPay's record for the crypto asset or the fiat currency has a decimals or precision that is not an integer between 0 and 36.
|
|
67
71
|
*/
|
|
68
72
|
override sell(options: MoonPaySellOptions): Promise<SellResult>;
|
|
69
73
|
/**
|