@tetherto/wdk-protocol-fiat-moonpay 1.0.0-beta.1 β 1.0.0-beta.3
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/.release-retrigger +1 -0
- package/README.md +31 -11
- package/package.json +2 -3
- package/src/moonpay-protocol.js +57 -18
- package/types/src/moonpay-protocol.d.ts +12 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
release retrigger for 1.0.0-beta.2
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Note: This package is in beta. Please test in a dev setup first.
|
|
4
4
|
|
|
5
|
-
A simple way to integrate MoonPay widget for on-ramp and off-ramp services. You can generate signed URLs for the MoonPay widget, get quotes for buying and selling crypto, and read protocol-related data. This
|
|
5
|
+
A simple way to integrate MoonPay widget for on-ramp and off-ramp services. 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
6
|
|
|
7
7
|
## π About WDK
|
|
8
8
|
|
|
@@ -10,8 +10,8 @@ This is part of WDK (Wallet Development Kit). WDK helps you build safe, nonβcu
|
|
|
10
10
|
|
|
11
11
|
## π Features
|
|
12
12
|
|
|
13
|
-
- Generate signed widget URL to buy Crypto (On-ramp)
|
|
14
|
-
- Generate signed widget URL to sell Crypto (Off-ramp)
|
|
13
|
+
- Generate signed or unsigned widget URL to buy Crypto (On-ramp)
|
|
14
|
+
- Generate signed or unsigned widget URL to sell Crypto (Off-ramp)
|
|
15
15
|
- Get quotes (buy and sell)
|
|
16
16
|
- Get supported currencies, countries
|
|
17
17
|
- Get buy/sell transaction details
|
|
@@ -29,10 +29,29 @@ npm install @tetherto/wdk-protocol-fiat-moonpay
|
|
|
29
29
|
```javascript
|
|
30
30
|
import MoonPayProtocol from '@tetherto/wdk-protocol-fiat-moonpay'
|
|
31
31
|
|
|
32
|
+
const signUrl = async (urlForSignature) => {
|
|
33
|
+
const response = await fetch('https://your-backend.example.com/moonpay/sign-url', {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'Content-Type': 'application/json'
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({ urlForSignature })
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
throw new Error(`Failed to sign MoonPay URL: ${response.status} ${response.statusText}`)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { signedUrl } = await response.json()
|
|
46
|
+
|
|
47
|
+
return signedUrl
|
|
48
|
+
}
|
|
49
|
+
|
|
32
50
|
// Initialize protocol
|
|
33
51
|
const moonpay = new MoonPayProtocol(undefined, {
|
|
34
52
|
apiKey: 'YOUR_PUBLISHABLE_API_KEY',
|
|
35
|
-
|
|
53
|
+
signUrl,
|
|
54
|
+
environment: 'sandbox'
|
|
36
55
|
})
|
|
37
56
|
|
|
38
57
|
// Get a buy quote
|
|
@@ -83,9 +102,10 @@ new MoonPayProtocol(account, config)
|
|
|
83
102
|
Parameters:
|
|
84
103
|
- `account` (IWalletAccount | IWalletAccountReadOnly | undefined): The wallet account to use to interact with the protocol
|
|
85
104
|
- `config` (object): The protocol config
|
|
86
|
-
- `apiKey` (string):
|
|
87
|
-
- `
|
|
88
|
-
- `cacheTime` (number, optional): The duration in milliseconds to cache supported currencies
|
|
105
|
+
- `apiKey` (string): Your publishable Moonpay API key.
|
|
106
|
+
- `signUrl` (function, optional): Callback used to sign buy/sell URLs via a trusted provider (e.g., a backend service). If not provided, the protocol returns unsigned URLs.
|
|
107
|
+
- `cacheTime` (number, optional): The duration in milliseconds to cache supported currencies.
|
|
108
|
+
- `environment` ("production" | "sandbox", optional): The environment to use for MoonPay endpoints and widget URLs. Defaults to "production". Use "production" for live transactions and "sandbox" for testing with non-real funds.
|
|
89
109
|
|
|
90
110
|
### Methods
|
|
91
111
|
|
|
@@ -162,12 +182,12 @@ Retrieves a list of supported countries.
|
|
|
162
182
|
- Works with networks and currencies supported by MoonPay.
|
|
163
183
|
- Check MoonPay documentation for the full list of widget parameters, supported cryptocurrencies and regions.
|
|
164
184
|
- The package provides the baseline for MoonPay integration. To fully utilize the power of MoonPay widget, take a look at [MoonPay documentation](https://dev.moonpay.com/docs/ramps-sdk-buy-params) for the full list of parameters.
|
|
165
|
-
- The `
|
|
166
|
-
- It is highly recommended to test the entire buy/sell flow in sandbox environment. Read more [here](https://dev.moonpay.com/docs/faq-sandbox-testing).
|
|
185
|
+
- The `apiKey` can be retrieved through [MoonPay dashboard](https://dashboard.moonpay.com/).
|
|
186
|
+
- It is highly recommended to test the entire buy/sell flow in the `sandbox` environment. Read more [here](https://dev.moonpay.com/docs/faq-sandbox-testing).
|
|
167
187
|
|
|
168
188
|
## π Security Considerations
|
|
169
189
|
|
|
170
|
-
- Keep your `secretKey` safe.
|
|
190
|
+
- Keep your `secretKey` safe on your backend. Expose a backend signing API to clients, and have `signUrl` call that API to retrieve the signed MoonPay URL.
|
|
171
191
|
|
|
172
192
|
## π οΈ Development
|
|
173
193
|
|
|
@@ -207,4 +227,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
|
|
|
207
227
|
|
|
208
228
|
## π Support
|
|
209
229
|
|
|
210
|
-
For support, please open an issue on the GitHub repository.
|
|
230
|
+
For support, please open an issue on the GitHub repository.
|
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.3",
|
|
4
4
|
"description": "A simple package that lets @tetherto/wdk-wallet accounts interact with the MoonPay protocol",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wdk",
|
|
@@ -25,8 +25,7 @@
|
|
|
25
25
|
"test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tetherto/wdk-wallet": "
|
|
29
|
-
"@moonpay/moonpay-node": "0.2.13",
|
|
28
|
+
"@tetherto/wdk-wallet": "1.0.0-beta.14",
|
|
30
29
|
"bare-node-runtime": "^1.1.4",
|
|
31
30
|
"bignumber.js": "^9.3.1"
|
|
32
31
|
},
|
package/src/moonpay-protocol.js
CHANGED
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
|
|
15
15
|
'use strict'
|
|
16
16
|
|
|
17
|
-
import { MoonPay } from '@moonpay/moonpay-node'
|
|
18
17
|
import { FiatProtocol } from '@tetherto/wdk-wallet/protocols'
|
|
19
18
|
import BigNumber from 'bignumber.js'
|
|
20
19
|
|
|
@@ -319,9 +318,10 @@ import BigNumber from 'bignumber.js'
|
|
|
319
318
|
|
|
320
319
|
/**
|
|
321
320
|
* @typedef {Object} MoonPayProtocolConfig
|
|
322
|
-
* @property {string}
|
|
323
|
-
* @property {string}
|
|
321
|
+
* @property {string} apiKey - Your publishable Moonpay API key.
|
|
322
|
+
* @property {(urlForSignature: string) => Promise<string>} [signUrl] - Callback used to sign buy/sell URLs via a trusted provider (e.g., a backend service). If not provided, the protocol returns unsigned URLs.
|
|
324
323
|
* @property {number} [cacheTime] - The duration in milliseconds to cache supported currencies.
|
|
324
|
+
* @property {"production" | "sandbox"} [environment] - The environment to use for MoonPay endpoints and widget URLs. Defaults to "production". Use "production" for live transactions and "sandbox" for testing with non-real funds.
|
|
325
325
|
*/
|
|
326
326
|
|
|
327
327
|
/**
|
|
@@ -357,7 +357,17 @@ function getFiatDecimals (currencyDetail) {
|
|
|
357
357
|
return decimals
|
|
358
358
|
}
|
|
359
359
|
|
|
360
|
-
const
|
|
360
|
+
const MOONPAY_ORIGINS = {
|
|
361
|
+
API: 'https://api.moonpay.com/',
|
|
362
|
+
BUY: {
|
|
363
|
+
production: 'https://buy.moonpay.com/',
|
|
364
|
+
sandbox: 'https://buy-sandbox.moonpay.com'
|
|
365
|
+
},
|
|
366
|
+
SELL: {
|
|
367
|
+
production: 'https://sell.moonpay.com/',
|
|
368
|
+
sandbox: 'https://sell-sandbox.moonpay.com'
|
|
369
|
+
}
|
|
370
|
+
}
|
|
361
371
|
const MOONPAY_CACHE_TIME = 10 * 60 * 1000
|
|
362
372
|
|
|
363
373
|
export default class MoonPayProtocol extends FiatProtocol {
|
|
@@ -384,14 +394,17 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
384
394
|
* @param {IWalletAccount} account - The wallet account to use to interact with the protocol.
|
|
385
395
|
* @param {MoonPayProtocolConfig} config - The MoonPay protocol configuration.
|
|
386
396
|
*/
|
|
387
|
-
constructor (account, {
|
|
397
|
+
constructor (account, { apiKey, signUrl, environment = 'production', cacheTime = MOONPAY_CACHE_TIME }) {
|
|
388
398
|
super(account)
|
|
389
399
|
|
|
390
400
|
/** @private */
|
|
391
|
-
this.
|
|
401
|
+
this._apiKey = apiKey
|
|
392
402
|
|
|
393
403
|
/** @private */
|
|
394
|
-
this.
|
|
404
|
+
this._signUrl = signUrl
|
|
405
|
+
|
|
406
|
+
/** @private */
|
|
407
|
+
this._environment = environment
|
|
395
408
|
|
|
396
409
|
/** @private */
|
|
397
410
|
this._supportedCurrenciesCache = undefined
|
|
@@ -455,11 +468,24 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
455
468
|
params.walletAddress = await this._account.getAddress()
|
|
456
469
|
}
|
|
457
470
|
|
|
458
|
-
const
|
|
459
|
-
|
|
460
|
-
|
|
471
|
+
const url = new URL('/', MOONPAY_ORIGINS.BUY[this._environment])
|
|
472
|
+
|
|
473
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
474
|
+
if (value !== undefined && value !== null) {
|
|
475
|
+
url.searchParams.append(key, value)
|
|
476
|
+
}
|
|
461
477
|
})
|
|
462
478
|
|
|
479
|
+
const urlForSignature = url.toString()
|
|
480
|
+
|
|
481
|
+
if (!this._signUrl) {
|
|
482
|
+
return {
|
|
483
|
+
buyUrl: urlForSignature
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const buyUrl = await this._signUrl(urlForSignature)
|
|
488
|
+
|
|
463
489
|
return {
|
|
464
490
|
buyUrl
|
|
465
491
|
}
|
|
@@ -499,7 +525,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
499
525
|
throw new Error('Either \'cryptoAmount\' or \'fiatAmount\' must be provided')
|
|
500
526
|
}
|
|
501
527
|
|
|
502
|
-
const url = new URL(`v3/currencies/${cryptoAsset}/buy_quote`,
|
|
528
|
+
const url = new URL(`v3/currencies/${cryptoAsset}/buy_quote`, MOONPAY_ORIGINS.API)
|
|
503
529
|
|
|
504
530
|
url.searchParams.append('apiKey', this._apiKey)
|
|
505
531
|
Object.entries(params).forEach(([key, value]) => {
|
|
@@ -557,7 +583,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
557
583
|
.shiftedBy(-1 * cryptoInfo.decimals)
|
|
558
584
|
.toFixed(cryptoInfo.precision, 1)
|
|
559
585
|
|
|
560
|
-
const url = new URL(`v3/currencies/${cryptoAsset}/sell_quote`,
|
|
586
|
+
const url = new URL(`v3/currencies/${cryptoAsset}/sell_quote`, MOONPAY_ORIGINS.API)
|
|
561
587
|
|
|
562
588
|
url.searchParams.append('apiKey', this._apiKey)
|
|
563
589
|
Object.entries(params).forEach(([key, value]) => {
|
|
@@ -631,11 +657,24 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
631
657
|
params.refundWalletAddress = await this._account.getAddress()
|
|
632
658
|
}
|
|
633
659
|
|
|
634
|
-
const
|
|
635
|
-
|
|
636
|
-
|
|
660
|
+
const url = new URL('/', MOONPAY_ORIGINS.SELL[this._environment])
|
|
661
|
+
|
|
662
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
663
|
+
if (value !== undefined && value !== null) {
|
|
664
|
+
url.searchParams.append(key, value)
|
|
665
|
+
}
|
|
637
666
|
})
|
|
638
667
|
|
|
668
|
+
const urlForSignature = url.toString()
|
|
669
|
+
|
|
670
|
+
if (!this._signUrl) {
|
|
671
|
+
return {
|
|
672
|
+
sellUrl: urlForSignature
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const sellUrl = await this._signUrl(urlForSignature)
|
|
677
|
+
|
|
639
678
|
return {
|
|
640
679
|
sellUrl
|
|
641
680
|
}
|
|
@@ -655,7 +694,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
655
694
|
|
|
656
695
|
const path = direction === 'buy' ? `v1/transactions/${txId}` : `v3/sell_transactions/${txId}`
|
|
657
696
|
|
|
658
|
-
const url = new URL(path,
|
|
697
|
+
const url = new URL(path, MOONPAY_ORIGINS.API)
|
|
659
698
|
|
|
660
699
|
url.searchParams.append('apiKey', this._apiKey)
|
|
661
700
|
|
|
@@ -690,7 +729,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
690
729
|
const now = Date.now()
|
|
691
730
|
|
|
692
731
|
if (!this._supportedCurrenciesCache || (now - this._supportedCurrenciesCache.timestamp >= this._cacheThreshold)) {
|
|
693
|
-
const url = new URL('v3/currencies',
|
|
732
|
+
const url = new URL('v3/currencies', MOONPAY_ORIGINS.API)
|
|
694
733
|
url.searchParams.append('apiKey', this._apiKey)
|
|
695
734
|
|
|
696
735
|
const resp = await fetch(url.toString(), {
|
|
@@ -757,7 +796,7 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
757
796
|
* @returns {Promise<MoonPaySupportedCountry[]>} An array of supported countries.
|
|
758
797
|
*/
|
|
759
798
|
async getSupportedCountries () {
|
|
760
|
-
const url = new URL('v3/countries',
|
|
799
|
+
const url = new URL('v3/countries', MOONPAY_ORIGINS.API)
|
|
761
800
|
|
|
762
801
|
url.searchParams.append('apiKey', this._apiKey)
|
|
763
802
|
|
|
@@ -24,10 +24,12 @@ export default class MoonPayProtocol extends FiatProtocol {
|
|
|
24
24
|
*/
|
|
25
25
|
constructor(account: IWalletAccount, config: MoonPayProtocolConfig);
|
|
26
26
|
/** @private */
|
|
27
|
-
private _moonPay;
|
|
28
|
-
/** @private */
|
|
29
27
|
private _apiKey;
|
|
30
28
|
/** @private */
|
|
29
|
+
private _signUrl;
|
|
30
|
+
/** @private */
|
|
31
|
+
private _environment;
|
|
32
|
+
/** @private */
|
|
31
33
|
private _supportedCurrenciesCache;
|
|
32
34
|
/** @private */
|
|
33
35
|
private _cacheThreshold;
|
|
@@ -913,16 +915,20 @@ export type MoonPaySellOptions = SellOptions & {
|
|
|
913
915
|
};
|
|
914
916
|
export type MoonPayProtocolConfig = {
|
|
915
917
|
/**
|
|
916
|
-
* - Your
|
|
918
|
+
* - Your publishable Moonpay API key.
|
|
917
919
|
*/
|
|
918
|
-
|
|
920
|
+
apiKey: string;
|
|
919
921
|
/**
|
|
920
|
-
* -
|
|
922
|
+
* - Callback used to sign buy/sell URLs via a trusted provider (e.g., a backend service). If not provided, the protocol returns unsigned URLs.
|
|
921
923
|
*/
|
|
922
|
-
|
|
924
|
+
signUrl?: (urlForSignature: string) => Promise<string>;
|
|
923
925
|
/**
|
|
924
926
|
* - The duration in milliseconds to cache supported currencies.
|
|
925
927
|
*/
|
|
926
928
|
cacheTime?: number;
|
|
929
|
+
/**
|
|
930
|
+
* - The environment to use for MoonPay endpoints and widget URLs. Defaults to "production". Use "production" for live transactions and "sandbox" for testing with non-real funds.
|
|
931
|
+
*/
|
|
932
|
+
environment?: "production" | "sandbox";
|
|
927
933
|
};
|
|
928
934
|
import { FiatProtocol } from '@tetherto/wdk-wallet/protocols';
|