Transferring Coins Guide
Learn how to safely transfer LB Coins between users, to treasuries, and handle common transfer scenarios.
What are Transfers?
Transfers move coins from one wallet to another. This includes peer-to-peer transfers between users and payments to treasuries. All transfers are recorded in the transaction history for complete transparency.
Peer-to-Peer (P2P) Transfers
Users can transfer coins directly to other users:
JavaScript
// Transfer coins to another user
const transferCoins = async (toUserId, amount, reason) => {
// Check sender's balance first
const balanceResponse = await fetch('https://api.loopingbinary.com/api/wallets/me', {
headers: { 'Authorization': 'Bearer ' + token }
});
const { balance } = await balanceResponse.json();
if (balance < amount) {
throw new Error('Insufficient balance');
}
// Perform transfer
const response = await fetch('https://api.loopingbinary.com/api/coins/transfer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify(({
toUserId,
amount,
reason
})
});
if (response.ok) {
const result = await response.json();
console.log(`✓ Transferred $${amount} coins to user #$${toUserId}`);
return result;
} else {
const error = await response.json();
throw new Error(error.message);
}
};
// Example usage
await transferCoins(456, 25, 'Payment for freelance work');Transfer to Treasury
Users transfer coins to treasuries when making purchases:
JavaScript
// Transfer coins to treasury (e.g., course payment)
const payToTreasury = async (treasuryType, amount, reason) => {
const response = await fetch('https://api.loopingbinary.com/api/coins/transfer-to-treasury', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
},
body: JSON.stringify(({
treasuryType, // 'COURSE', 'SUBSCRIPTION', 'REWARD', 'GENERAL'
amount,
reason
})
});
if (response.ok) {
const result = await response.json();
console.log(`✓ Paid $${amount} coins to $${treasuryType} treasury`);
return result;
} else {
const error = await response.json();
throw new Error(error.message);
}
};
// Example: Course purchase
await payToTreasury('COURSE', 50, 'Advanced JavaScript Course');
// Example: Subscription payment
await payToTreasury('SUBSCRIPTION', 30, 'Premium Monthly Subscription');Transfer Validation
Always validate transfers before executing:
JavaScript
// Comprehensive transfer validation
const validateTransfer = async (toUserId, amount) => {
// Check if amount is positive
if (amount <= 0) {
throw new Error('Amount must be positive');
}
// Check minimum transfer amount
const MIN_TRANSFER = 1;
if (amount < MIN_TRANSFER) {
throw new Error(`Minimum transfer amount is $${MIN_TRANSFER} coins`);
}
// Check sender's balance
const balanceResponse = await fetch('https://api.loopingbinary.com/api/wallets/me', {
headers: { 'Authorization': 'Bearer ' + token }
});
const { balance } = await balanceResponse.json();
if (balance < amount) {
throw new Error(`Insufficient balance. You have $${balance} coins`);
}
// Check if recipient exists
const userResponse = await fetch(`https://api.loopingbinary.com/api/admin/users/$${toUserId}`, {
headers: {
'x-api-key': apiKey,
'Authorization': 'Bearer ' + adminToken
}
});
if (!userResponse.ok) {
throw new Error('Recipient user not found');
}
return true;
};
// Use validation before transfer
try {
await validateTransfer(456, 25);
await transferCoins(456, 25, 'Payment');
} catch (error) {
console.error('Transfer validation failed:', error.message);
}Transfer Best Practices
- Always check balance before initiating transfers
- Validate recipient user ID exists
- Use descriptive reasons for all transfers
- Implement confirmation dialogs for large amounts
- Log all transfers for audit purposes
- Handle errors gracefully with user-friendly messages
- Implement rate limiting to prevent abuse
Security Considerations
- Never allow negative transfer amounts
- Implement maximum daily transfer limits
- Require 2FA for transfers above threshold
- Monitor for suspicious transfer patterns
- Store transaction IDs for dispute resolution
- Implement transfer cooldown periods if needed