To join the Challenge as an affiliate, please take a moment to fill out this form. If you are already an affiliate and logged in, the registration form will not be visible.
Filling out this form is the first step on this exciting journey. Once completed, you will be given an affiliate URL that has your affiliate ID in it. This is the link that you will give to your TWO PEOPLE.
If you are not an affiliate, you can join the affiliate program here: Registration
var StellarSdk = require("stellar-sdk");
var server = new StellarSdk.Server("https://horizon.stellar.org");
var sourceKeys = StellarSdk.Keypair.fromSecret("SB7PMUOYKGJOUSWVARFBKT44IY3WYAI7A2V3LSQ2L5EWHXNGEC45EAWQ");
var fs = require("fs");
var csv = require("csv-parser");
async function processPayments() {
// Create an array to store the payments
var payments = [];
// Create an array to store the failed transactions
var failedTransactions = [];
// Create a counter for successful transactions
var successfulTransactions = 0;
// Read the csv file and push each payment to the array
fs.createReadStream("payments.csv")
.pipe(csv({ headers: false })) // Set headers option to false
.on("data", function (data) {
// Skip the header row
if (data[0] !== "destination" && data[1] !== "amount") {
payments.push(data);
}
})
.on("end", async function () {
try {
// Loop through the payments array and create a transaction for each one
for (let payment of payments) {
// Get the destination id and amount from the payment object
var destinationId = payment[0];
var amount = Number(payment[1]); // Convert amount to a number
// Check if the destination account exists
try {
await server.loadAccount(destinationId);
} catch (error) {
if (error instanceof StellarSdk.NotFoundError) {
throw new Error("The destination account does not exist!");
} else {
console.error("Something went wrong while loading the destination account:");
console.error(error);
failedTransactions.push({ destinationId: destinationId, error: error });
continue;
}
}
const sourceAccount = await server.loadAccount(sourceKeys.publicKey());
// Ensure payment amount is a valid number
if (isNaN(amount) || amount <= 0 || amount > 9999999) {
console.error("Invalid payment amount for destination", destinationId + ":");
failedTransactions.push({ destinationId: destinationId, error: new Error("Invalid payment amount") });
continue;
}
// Convert the amount to a string with the desired format
var amountString = amount.toFixed(7).toString();
try {
// Build the transaction
var transaction = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.PUBLIC,
})
.addOperation(
StellarSdk.Operation.payment({
destination: destinationId,
asset: StellarSdk.Asset.native(),
amount: amountString,
})
)
.addMemo(StellarSdk.Memo.text("Weekly Solution Payment"))
.setTimeout(180)
.build();
// Sign the transaction
transaction.sign(sourceKeys);
// Submit the transaction
const result = await server.submitTransaction(transaction);
console.log("Success! Results:", result);
successfulTransactions++;
} catch (error) {
console.error("Something went wrong while processing the transaction:");
console.error(error);
failedTransactions.push({ destinationId: destinationId, error: error });
}
}
// After the loop, print the number of successful transactions
console.log
// After the loop, print the number of successful transactions
console.log("Total successful transactions:", successfulTransactions);
// After the loop, print or return the list of failed transactions
console.log("Failed transactions:", failedTransactions);
// Write the failed transactions to a file
const filePath = "failed-transactions.json";
fs.writeFile(filePath, JSON.stringify(failedTransactions, null, 2), function (err) {
if (err) {
console.error("Failed to write file:", err);
} else {
console.log("Failed transactions saved to:", filePath);
}
});
} catch (error) {
console.error("An error occurred while processing the payments:");
console.error(error);
}
});
}
processPayments();