@tetherto/wdk-protocol-fiat-moonpay 1.0.0-beta.3 → 1.0.0-beta.4
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/README.md +4 -2
- package/package.json +3 -2
- package/src/moonpay-protocol.js +37 -5
package/README.md
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Note: This package is in beta. Please test in a dev setup first.
|
|
4
4
|
|
|
5
|
-
A
|
|
5
|
+
A MoonPay on-ramp and off-ramp module for WDK (Wallet Development Kit) by Tether. You can generate signed or unsigned URLs for the MoonPay widget, get quotes for buying and selling crypto, and read protocol-related data. This package can be used in both frontend and backend environments.
|
|
6
|
+
|
|
7
|
+
See the [module documentation](https://docs.wdk.tether.io/sdk/fiat-modules/fiat-moonpay/).
|
|
6
8
|
|
|
7
9
|
## 🔍 About WDK
|
|
8
10
|
|
|
9
|
-
This is part of WDK (Wallet Development Kit). WDK helps you build safe, non‑custody wallets. Read more at https://docs.
|
|
11
|
+
This is part of WDK (Wallet Development Kit). WDK helps you build safe, non‑custody wallets. Read more at https://docs.wdk.tether.io/.
|
|
10
12
|
|
|
11
13
|
## 🌟 Features
|
|
12
14
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tetherto/wdk-protocol-fiat-moonpay",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0-beta.4",
|
|
4
|
+
"description": "MoonPay on-ramp and off-ramp module for WDK (Wallet Development Kit) by Tether, with widget URLs and quotes.",
|
|
5
|
+
"homepage": "https://docs.wdk.tether.io/sdk/fiat-modules/fiat-moonpay/",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"wdk",
|
|
7
8
|
"protocol",
|
package/src/moonpay-protocol.js
CHANGED
|
@@ -357,6 +357,38 @@ function getFiatDecimals (currencyDetail) {
|
|
|
357
357
|
return decimals
|
|
358
358
|
}
|
|
359
359
|
|
|
360
|
+
/**
|
|
361
|
+
* The maximum number of characters of a failed response's body to carry in the error message.
|
|
362
|
+
* @type {number}
|
|
363
|
+
*/
|
|
364
|
+
const MAX_ERROR_DETAIL_LENGTH = 500
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Builds the error thrown when MoonPay rejects a request, carrying the body it
|
|
368
|
+
* answered with.
|
|
369
|
+
*
|
|
370
|
+
* @param {Response} resp - The failed response.
|
|
371
|
+
* @param {string} subject - What was being fetched (e.g. 'MoonPay buy quote').
|
|
372
|
+
* @returns {Promise<Error>} An error whose message names the subject and the response's status
|
|
373
|
+
* code, followed by the reason phrase when the server sent one and by the response body when
|
|
374
|
+
* it is non-empty and readable. A body longer than MAX_ERROR_DETAIL_LENGTH characters is cut
|
|
375
|
+
* to that length and suffixed with an ellipsis.
|
|
376
|
+
*/
|
|
377
|
+
async function fetchErrorFor (resp, subject) {
|
|
378
|
+
const body = (await resp.text().catch(() => '')).trim()
|
|
379
|
+
const codePoints = [...body]
|
|
380
|
+
const detail = codePoints.length > MAX_ERROR_DETAIL_LENGTH
|
|
381
|
+
? `${codePoints.slice(0, MAX_ERROR_DETAIL_LENGTH).join('')}...`
|
|
382
|
+
: body
|
|
383
|
+
|
|
384
|
+
// HTTP/2 carries no reason phrase, so statusText is often empty.
|
|
385
|
+
const status = resp.statusText ? `${resp.status} ${resp.statusText}` : `${resp.status}`
|
|
386
|
+
|
|
387
|
+
return new Error(
|
|
388
|
+
`Failed to fetch ${subject}: ${status}${detail ? ` — ${detail}` : ''}`
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
|
|
360
392
|
const MOONPAY_ORIGINS = {
|
|
361
393
|
API: 'https://api.moonpay.com/',
|
|
362
394
|
BUY: {
|
|
@@ -539,7 +571,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
539
571
|
})
|
|
540
572
|
|
|
541
573
|
if (!resp.ok) {
|
|
542
|
-
throw
|
|
574
|
+
throw await fetchErrorFor(resp, 'MoonPay buy quote')
|
|
543
575
|
}
|
|
544
576
|
|
|
545
577
|
const quote = await resp.json()
|
|
@@ -597,7 +629,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
597
629
|
})
|
|
598
630
|
|
|
599
631
|
if (!resp.ok) {
|
|
600
|
-
throw
|
|
632
|
+
throw await fetchErrorFor(resp, 'MoonPay sell quote')
|
|
601
633
|
}
|
|
602
634
|
|
|
603
635
|
const quote = await resp.json()
|
|
@@ -705,7 +737,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
705
737
|
})
|
|
706
738
|
|
|
707
739
|
if (!resp.ok) {
|
|
708
|
-
throw
|
|
740
|
+
throw await fetchErrorFor(resp, 'MoonPay transaction detail')
|
|
709
741
|
}
|
|
710
742
|
|
|
711
743
|
const moonPayTransaction = await resp.json()
|
|
@@ -737,7 +769,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
737
769
|
})
|
|
738
770
|
|
|
739
771
|
if (!resp.ok) {
|
|
740
|
-
throw
|
|
772
|
+
throw await fetchErrorFor(resp, 'MoonPay supported currencies')
|
|
741
773
|
}
|
|
742
774
|
|
|
743
775
|
const data = await resp.json()
|
|
@@ -807,7 +839,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
807
839
|
})
|
|
808
840
|
|
|
809
841
|
if (!resp.ok) {
|
|
810
|
-
throw
|
|
842
|
+
throw await fetchErrorFor(resp, 'supported countries')
|
|
811
843
|
}
|
|
812
844
|
|
|
813
845
|
const moonPaySupportedCountries = await resp.json()
|