-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const TransactionModel = require('../models/Transaction'); // Assuming you have a Transaction model | ||
const { sendPayment } = require('../api/stellar'); // Import Stellar API integration | ||
|
||
// Function to create a new transaction | ||
async function createTransaction(userId, destinationId, amount) { | ||
try { | ||
const transaction = new TransactionModel({ | ||
userId, | ||
destinationId, | ||
amount, | ||
status: 'pending', | ||
createdAt: new Date(), | ||
}); | ||
await transaction.save(); | ||
return transaction; | ||
} catch (error) { | ||
console.error('Error creating transaction:', error); | ||
throw new Error('Transaction creation failed'); | ||
} | ||
} | ||
|
||
// Function to execute a transaction | ||
async function executeTransaction(transactionId, sourceSecret) { | ||
try { | ||
const transaction = await TransactionModel.findById(transactionId); | ||
if (!transaction) { | ||
throw new Error('Transaction not found'); | ||
} | ||
if (transaction.status !== 'pending') { | ||
throw new Error('Transaction already executed or canceled'); | ||
} | ||
|
||
const result = await sendPayment(sourceSecret, transaction.destinationId, transaction.amount); | ||
transaction.status = 'completed'; | ||
transaction.transactionHash = result.hash; // Assuming the result contains a hash | ||
await transaction.save(); | ||
|
||
return transaction; | ||
} catch (error) { | ||
console.error('Error executing transaction:', error); | ||
throw new Error('Transaction execution failed'); | ||
} | ||
} | ||
|
||
// Function to get transaction history for a user | ||
async function getUser TransactionHistory(userId) { | ||
try { | ||
const transactions = await TransactionModel.find({ userId }).sort({ createdAt: -1 }); | ||
return transactions; | ||
} catch (error) { | ||
console.error('Error fetching user transaction history:', error); | ||
throw new Error('Unable to fetch transaction history'); | ||
} | ||
} | ||
|
||
module.exports = { createTransaction, executeTransaction, getUser TransactionHistory }; |