Last active
June 28, 2024 15:03
-
-
Save irisli/71f04f472475201159e7 to your computer and use it in GitHub Desktop.
Creating a Stellar transaction using JS Stellar SDK
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
// I made this gist in hopes that it is a good tutorial on creating, signing, | |
// and submitting a transaction using JS Stellar SDK. | |
// This gist assumes that you have: | |
// 1. Secret key of a funded account to be the source account | |
// 2. Public key of a funded account as a recipient | |
// 3. Access to JS Stellar SDK (https://github.com/stellar/js-stellar-sdk) | |
// This code can be run in the browser at https://www.stellar.org/developers/ | |
// That site exposes a global StellarSdk object you can use. | |
// To run this code in the Chome, open the console tab in the DevTools. | |
// The hotkey to open the DevTools console is Ctrl+Shift+J or (Cmd+Opt+J on Mac). | |
// Note: run this in incognito mode so the input history is not saved | |
// To use in node, do `npm install stellar-sdk` and uncomment the following line. | |
// var StellarSdk = require('stellar-sdk') | |
// The source account is the account we will be sending from. | |
var sourceSecretKey = 'SCC2GLOCQLDIJPTQB6O2K2GVUXW52IWLSCKE76EMU5ILMED3CYQFHUFC'; | |
var receiverPublicKey = 'GAIRISXKPLOWZBMFRPU5XRGUUX3VMA3ZEWKBM5MSNRU3CHV6P4PYZ74D'; | |
// This is where the fun begins. | |
// Calculate the source account's public key | |
var sourceKeypair = StellarSdk.Keypair.fromSeed(sourceSecretKey); | |
var sourcePublicKey = sourceKeypair.address() | |
// Configure StellarSdk to talk to the horizon instance hosted by Stellar.org | |
// To use the public (live) network, set the hostname to 'horizon.stellar.org' | |
var server = new StellarSdk.Server({ | |
hostname: 'horizon-testnet.stellar.org', | |
secure: true, | |
port: 443 | |
}); | |
// Uncomment the following line to build transactions for the public network. Be | |
// sure to also change the horizon hostname. | |
// StellarSdk.Network.usePublicNetwork(); | |
// Transactions need a valid sequence number inside the transaction to prevent | |
// the transaction from being included in the ledger twice. | |
// We can fetch the current sequence number for the source account by getting | |
// info from Horizon. | |
server.loadAccount(sourcePublicKey) | |
.then(function(account) { | |
var transaction = new StellarSdk.TransactionBuilder(account) | |
// Add a payment operation to the transaction | |
.addOperation(StellarSdk.Operation.payment({ | |
destination: receiverPublicKey, | |
// The term native asset refers to lumens | |
asset: StellarSdk.Asset.native(), | |
// Specify 350.1234567 lumens. Lumens are divisible to seven digits past | |
// the decimal. They are represented in JS Stellar SDK in string format | |
// to avoid errors from the use of the JavaScript Number data structure. | |
amount: '350.1234567', | |
})) | |
// Uncomment to add a memo (https://www.stellar.org/developers/learn/concepts/transactions.html) | |
// .addMemo(StellarSdk.Memo.text('Hello world!')) | |
.build() | |
// Sign this transaction with the secret key | |
transaction.sign(sourceKeypair) | |
// Let's see the XDR (encoded in base64) of the transaction we just built | |
console.log(transaction.toEnvelope().toXDR('base64')) | |
// Submit the transaction to the Horizon server. The Horizon server will then | |
// submit the transaction into the network for us. | |
server.submitTransaction(transaction) | |
.then(function(transactionResult) { | |
console.log('Success!') | |
console.log(transactionResult); | |
}) | |
.catch(function (err){ | |
console.log('error') | |
console.log(err); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment