const executeOnRamp = async (userId, walletAddress, amount) => {
try {
// 1. Create wallet account if needed
let wallet = await findWalletAccount(userId, 'POLYGON', 'USDC');
if (!wallet) {
wallet = await createWalletAccount(userId, walletAddress, 'USDC', 'POLYGON');
}
// 2. Get quotation
const quote = await fetch('/api/v2/quotations', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
fromCurrency: 'COP',
toCurrency: 'USDC',
amount: amount,
amountIsToCurrency: false,
cashInMethod: 'PSE',
cashOutMethod: 'POLYGON'
})
}).then(r => r.json());
console.log(`You will receive: ${quote.toAmount} USDC`);
// 3. Create ramp
const ramp = await fetch('/api/v2/ramps', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
quotationId: quote.id,
userId: userId,
accountId: wallet.id,
externalId: `onramp-${Date.now()}`
})
}).then(r => r.json());
// 4. Redirect to payment
return {
rampId: ramp.id,
paymentUrl: ramp.paymentInfo[0].url,
expectedAmount: quote.toAmount
};
} catch (error) {
console.error('On-ramp failed:', error);
throw error;
}
};
// Usage
const result = await executeOnRamp('user-id', '0x742d35...', 100000);
window.location.href = result.paymentUrl;