Skip to content

Instantly share code, notes, and snippets.

@dvwright
Created January 28, 2023 02:14
Show Gist options
  • Save dvwright/6d5bb09dae04a1f3fa6660f7368328fb to your computer and use it in GitHub Desktop.
Save dvwright/6d5bb09dae04a1f3fa6660f7368328fb to your computer and use it in GitHub Desktop.
Send Transaction Stellar XLM Testet
// NOTE: Untested
// Usage: node send_transaction_xml_testnet.js (this file)
import fetch from "node-fetch";
import StellarSdk from "stellar-sdk";
(async function main() {
try {
const server = new StellarSdk.Server(
"https://horizon-testnet.stellar.org"
);
const secret =
"S*****************";
const pubKey =
"G*****************";
const destinationWallet =
"G******************";
const account = await server.loadAccount(pubKey);
console.log("Balances for account: " + pubKey);
account.balances.forEach(function (balance) {
console.log(
"Type:",
balance.asset_type,
", Balance:",
balance.balance
);
});
var sourceKeys = StellarSdk.Keypair.fromSecret(secret);
var destinationId =
"G*******************";
var destinationId = destinationWallet;
var transaction;
var memo = "3821719793";
var amount = "10";
// First, check to make sure that the destination account exists.
// You could skip this, but if the account does not exist, you will be charged
// the transaction fee when the transaction fails.
server
.loadAccount(destinationId)
// If the account is not found, surface a nicer error message for logging.
.catch(function (error) {
if (error instanceof StellarSdk.NotFoundError) {
throw new Error("The destination account does not exist!");
} else return error;
})
// If there was no error, load up-to-date information on your account.
.then(function () {
return server.loadAccount(sourceKeys.publicKey());
})
.then(function (sourceAccount) {
// Start building the transaction.
transaction = new StellarSdk.TransactionBuilder(
sourceAccount,
{
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
}
)
.addOperation(
StellarSdk.Operation.payment({
destination: destinationId,
// Because Stellar allows transaction in many currencies, you must
// specify the asset type. The special "native" asset represents Lumens.
asset: StellarSdk.Asset.native(),
amount: amount,
})
)
// A memo allows you to add your own metadata to a transaction. It's
// optional and does not affect how Stellar treats the transaction.
.addMemo(StellarSdk.Memo.text(memo))
// Wait a maximum of three minutes for the transaction
.setTimeout(180)
.build();
// Sign the transaction to prove you are actually the person sending it.
transaction.sign(sourceKeys);
// And finally, send it off to Stellar!
return server.submitTransaction(transaction);
})
.then(function (result) {
console.log("Success! Results:", result);
})
.catch(function (error) {
console.error("Something went wrong!", error);
// If the result is unknown (no response body, timeout etc.) we simply resubmit
// already built transaction:
// server.submitTransaction(transaction);
});
} catch (e) {
console.error("ERROR!", e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment