forked from Emmy123222/Stellar-MicroPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.js
More file actions
39 lines (33 loc) · 1.27 KB
/
Copy pathstellar.js
File metadata and controls
39 lines (33 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"use strict";
const { Horizon, Transaction, Networks } = require("stellar-sdk");
const NETWORK = process.env.NEXT_PUBLIC_STELLAR_NETWORK || "testnet"; // Use env var from backend
const HORIZON_URL =
process.env.NEXT_PUBLIC_HORIZON_URL || "https://horizon-testnet.stellar.org";
const NETWORK_PASSPHRASE =
NETWORK === "mainnet" ? Networks.PUBLIC : Networks.TESTNET;
const server = new Horizon.Server(HORIZON_URL);
/**
* Submits a signed Stellar transaction XDR to the Horizon network.
* @param {string} signedXDR - The base64-encoded signed transaction XDR string.
* @returns {Promise<object>} The Horizon transaction submission result.
* @throws {Error} With Horizon result codes if the transaction is rejected.
*/
async function submitTransactionToHorizon(signedXDR) {
const transaction = new Transaction(signedXDR, NETWORK_PASSPHRASE);
try {
const result = await server.submitTransaction(transaction);
return result;
} catch (err) {
const horizonErr = err;
if (horizonErr?.response?.data?.extras?.result_codes) {
const codes = horizonErr.response.data.extras.result_codes;
throw new Error(`Transaction failed: ${JSON.stringify(codes)}`);
}
throw err;
}
}
module.exports = {
submitTransactionToHorizon,
NETWORK_PASSPHRASE,
server,
};