How to use the @tetherto/wdk-protocol-fiat-moonpay module
This guide covers common use cases for integrating MoonPay fiat on-ramp and off-ramp functionality in your WDK application.
Basic Setup
First, install the module:
npminstall@tetherto/wdk-protocol-fiat-moonpay
Initializing the Module
importMoonPayProtocolfrom'@tetherto/wdk-protocol-fiat-moonpay';// Initialize with a wallet accountconstmoonpay=newMoonPayProtocol(walletAccount,{apiKey:'pk_live_xxxxx',secretKey:'sk_live_xxxxx',});
Generate a widget URL for users to purchase cryptocurrency:
// Buy $100 worth of ETHconstresult=awaitmoonpay.buy({cryptoAsset:'eth',fiatCurrency:'usd',fiatAmount:10000n,// Amount in cents ($100.00)});// Open the MoonPay widgetwindow.open(result.buyUrl,'_blank');
You can also specify the crypto amount instead:
Selling Cryptocurrency (Off-Ramp)
Generate a widget URL for users to sell cryptocurrency:
Getting Price Quotes
Get a quote before initiating a transaction:
Fetching Supported Currencies
Query which cryptocurrencies and fiat currencies are supported:
Checking Transaction Status
Retrieve details about a transaction:
Customizing the Widget
Pass configuration options to customize the MoonPay widget:
Specifying a Custom Recipient Address
By default, the module uses the wallet account's address. You can override this:
For sell operations, you can specify a refund address:
// Buy exactly 0.1 ETH
const result = await moonpay.buy({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 100000000000000000n, // 0.1 ETH in wei
});
// Sell 0.5 ETH
const result = await moonpay.sell({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 500000000000000000n, // 0.5 ETH in wei
});
// Open the MoonPay widget
window.open(result.sellUrl, '_blank');
// Get a buy quote for $100 worth of ETH
const buyQuote = await moonpay.quoteBuy({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
fiatAmount: 10000n, // $100.00 in cents
});
console.log('You will receive:', buyQuote.cryptoAmount);
console.log('Fee:', buyQuote.fee);
console.log('Exchange rate:', buyQuote.rate);
// Get a sell quote for 0.5 ETH
const sellQuote = await moonpay.quoteSell({
cryptoAsset: 'eth',
fiatCurrency: 'usd',
cryptoAmount: 500000000000000000n,
});
console.log('You will receive:', sellQuote.fiatAmount);