Buying Bitcoin
The SDK also provides access to a Fiat on-ramp to purchase Bitcoin using Moonpay as a provider. It will generate a Bitcoin address and prepare a URL using the specified provider. The user then needs to open the URL and proceed with the provider flow to buy Bitcoin. Once the buy is completed, the provider will transfer the purchased amount to the Bitcoin address.
Checking the limits API docs
Fetch the current onchain limits to check the minimum and maximum allowed to purchase.
let current_limits = sdk.fetch_onchain_limits().await?;
info!("Minimum amount: {} sats", current_limits.receive.min_sat);
info!("Maximum amount: {} sats", current_limits.receive.max_sat);
if let currentLimits = try? sdk.fetchOnchainLimits() {
print("Minimum amount, in sats: \(currentLimits.receive.minSat)")
print("Maximum amount, in sats: \(currentLimits.receive.maxSat)")
}
try {
val currentLimits = sdk.fetchOnchainLimits()
// Log.v("Breez", "Minimum amount, in sats: ${currentLimits.receive.minSat}")
// Log.v("Breez", "Maximum amount, in sats: ${currentLimits.receive.maxSat}")
} catch (e: Exception) {
// handle error
}
try {
const currentLimits = await sdk.fetchOnchainLimits()
console.log(`Minimum amount, in sats: ${currentLimits.receive.minSat}`)
console.log(`Maximum amount, in sats: ${currentLimits.receive.maxSat}`)
} catch (err) {
console.error(err)
}
try {
const currentLimits = await fetchOnchainLimits()
console.log(`Minimum amount, in sats: ${currentLimits.receive.minSat}`)
console.log(`Maximum amount, in sats: ${currentLimits.receive.maxSat}`)
} catch (err) {
console.error(err)
}
OnchainPaymentLimitsResponse currentLimits = await breezSDKLiquid.instance!.fetchOnchainLimits();
print("Minimum amount: ${currentLimits.receive.minSat} sats");
print("Maximum amount: ${currentLimits.receive.maxSat} sats");
try:
current_limits = sdk.fetch_onchain_limits()
logging.debug(f"Minimum amount, in sats {current_limits.receive.min_sat}")
logging.debug(f"Maximum amount, in sats {current_limits.receive.max_sat}")
return current_limits
except Exception as error:
logging.error(error)
raise
if currentLimits, err := sdk.FetchOnchainLimits(); err == nil {
log.Printf("Minimum amount, in sats: %v", currentLimits.Receive.MinSat)
log.Printf("Maximum amount, in sats: %v", currentLimits.Receive.MaxSat)
}
try
{
var currentLimits = sdk.FetchOnchainLimits();
Console.WriteLine($"Minimum amount, in sats: {currentLimits.receive.minSat}");
Console.WriteLine($"Maximum amount, in sats: {currentLimits.receive.maxSat}");
}
catch (Exception)
{
// Handle error
}
Preparing to buy, checking fees API docs
Using the current onchain limits, select a provider and amount to purchase.
let prepare_response = sdk
.prepare_buy_bitcoin(PrepareBuyBitcoinRequest {
provider: BuyBitcoinProvider::Moonpay,
amount_sat: current_limits.receive.min_sat,
})
.await?;
// Check the fees are acceptable before proceeding
let receive_fees_sat = prepare_response.fees_sat;
info!("Fees: {} sats", receive_fees_sat);
let req = PrepareBuyBitcoinRequest(
provider: .moonpay,
amountSat: currentLimits.receive.minSat)
if let prepareResponse = try? sdk.prepareBuyBitcoin(req: req) {
// Check the fees are acceptable before proceeding
let receiveFeesSat = prepareResponse.feesSat;
print("Fees: \(receiveFeesSat) sats")
}
try {
val req = PrepareBuyBitcoinRequest(BuyBitcoinProvider.MOONPAY, currentLimits.receive.minSat)
val prepareResponse = sdk.prepareBuyBitcoin(req)
// Check the fees are acceptable before proceeding
val receiveFeesSat = prepareResponse.feesSat;
// Log.v("Breez", "Fees: ${receiveFeesSat} sats")
} catch (e: Exception) {
// Handle error
}
try {
const prepareRes = await sdk.prepareBuyBitcoin({
provider: 'moonpay',
amountSat: currentLimits.receive.minSat
})
// Check the fees are acceptable before proceeding
const receiveFeesSat = prepareRes.feesSat
console.log(`Fees: ${receiveFeesSat} sats`)
} catch (err) {
console.error(err)
}
try {
const prepareRes = await prepareBuyBitcoin({
provider: BuyBitcoinProvider.MOONPAY,
amountSat: currentLimits.receive.minSat
})
// Check the fees are acceptable before proceeding
const receiveFeesSat = prepareRes.feesSat
console.log(`Fees: ${receiveFeesSat} sats`)
} catch (err) {
console.error(err)
}
PrepareBuyBitcoinRequest req =
PrepareBuyBitcoinRequest(provider: BuyBitcoinProvider.moonpay, amountSat: currentLimits.receive.minSat);
PrepareBuyBitcoinResponse prepareRes = await breezSDKLiquid.instance!.prepareBuyBitcoin(req: req);
// Check the fees are acceptable before proceeding
BigInt receiveFeesSat = prepareRes.feesSat;
print("Fees: $receiveFeesSat sats");
try:
req = PrepareBuyBitcoinRequest(
provider=BuyBitcoinProvider.MOONPAY,
amount_sat=current_limits.receive.min_sat
)
prepare_response = sdk.prepare_buy_bitcoin(req)
# Check the fees are acceptable before proceeding
receive_fees_sat = prepare_response.fees_sat
logging.debug(f"Fees: {receive_fees_sat} sats")
return prepare_response
except Exception as error:
logging.error(error)
raise
req := breez_sdk_liquid.PrepareBuyBitcoinRequest{
Provider: breez_sdk_liquid.BuyBitcoinProviderMoonpay,
AmountSat: currentLimits.Receive.MinSat,
}
// Check the fees are acceptable before proceeding
if prepareResponse, err := sdk.PrepareBuyBitcoin(req); err == nil {
receiveFeesSat := prepareResponse.FeesSat
log.Printf("Fees: %v sats", receiveFeesSat)
}
try
{
var req = new PrepareBuyBitcoinRequest(BuyBitcoinProvider.Moonpay, currentLimits.receive.minSat);
var prepareResponse = sdk.PrepareBuyBitcoin(req);
// Check the fees are acceptable before proceeding
var receiveFeesSat = prepareResponse.feesSat;
Console.WriteLine($"Fees: {receiveFeesSat} sats");
}
catch (Exception)
{
// Handle error
}
Generate the URL API docs
Generate a URL to the provider with a Bitcoin address to receive the purchase to. You can also pass in an optional redirect URL here that the provider redirects to after a successful purchase.
let url = sdk.buy_bitcoin(BuyBitcoinRequest {
prepare_response,
redirect_url: None,
})
.await?;
let req = BuyBitcoinRequest(prepareResponse: prepareResponse)
let url = try? sdk.buyBitcoin(req: req)
try {
val req = BuyBitcoinRequest(prepareResponse)
val url = sdk.buyBitcoin(req)
} catch (e: Exception) {
// Handle error
}
try {
const url = await sdk.buyBitcoin({
prepareResponse
})
} catch (err) {
console.error(err)
}
try {
const url = await buyBitcoin({
prepareResponse
})
} catch (err) {
console.error(err)
}
BuyBitcoinRequest req = BuyBitcoinRequest(prepareResponse: prepareResponse);
String url = await breezSDKLiquid.instance!.buyBitcoin(req: req);
try:
req = BuyBitcoinRequest(prepare_response=prepare_response)
url = sdk.buy_bitcoin(req)
except Exception as error:
logging.error(error)
raise
req := breez_sdk_liquid.BuyBitcoinRequest{
PrepareResponse: prepareResponse,
}
if url, err := sdk.BuyBitcoin(req); err == nil {
log.Printf("Url: %v", url)
}
try
{
var req = new BuyBitcoinRequest(prepareResponse);
var url = sdk.BuyBitcoin(req);
}
catch (Exception)
{
// Handle error
}