Last active
December 17, 2019 12:32
-
-
Save daverickdunn/d16f7773d9e2d40f4a218a0aee0a5abe to your computer and use it in GitHub Desktop.
Stellar JS SDK make a payment
This file contains hidden or 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
const StellarSdk = require('stellar-sdk'); | |
StellarSdk.Network.useTestNetwork(); // StellarSdk.Network.usePublicNetwork(); | |
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); // const server = new StellarSdk.Server('https://horizon.stellar.org'); | |
// an arbitary list of trusted assets | |
const ASSETS = { | |
'XPORT': new StellarSdk.Asset('XPORT', 'GCC4HAL7SOR7AZBF53SUTVAJT4OHYLBHOUJMM3IAF5YLFF7OVVAWLVFT'), | |
'USDT': new StellarSdk.Asset('USDT', 'GCQTGZQQ5G4PTM2GL7CDIFKUBIPEC52BROAQIAPW53XBRJVN6ZJVTG6V'), | |
'MOBI' : new StellarSdk.Asset('MOBI', 'GA6HCMBLTZS5VYYBCATRBRZ3BZJMAFUDKYYF6AH6MVCMGWMRDNSWJPIH'), | |
'XLM': StellarSdk.Asset.native() | |
} | |
// helper function to get desired asset object | |
const getAsset = (asset) => { | |
if (ASSETS[asset]) return ASSETS[asset] | |
return StellarSdk.Asset.native() | |
} | |
const keypair = StellarSdk.Keypair.fromSecret('S ...') | |
// 'load' the account (really, all it's getting is the next tx sequence number) | |
server.loadAccount(keypair.publicKey()) | |
.then(account => { | |
// new transaction builder (convenience constructor grabs tx sequence number from account object) | |
let builder = new StellarSdk.TransactionBuilder(account); | |
// add an operation, here we're adding a single payment operation. You can add upto 100 operations in one transaction, | |
// that could be 100 payments to 100 different accounts, etc... | |
builder.addOperation( | |
StellarSdk.Operation.payment({ | |
destination: 'G ...', // destination account address | |
asset: getAsset(asset), // see helper function above | |
amount: '10.5' // transaction amount as string | |
}) | |
) | |
// create the transaction XDR | |
let transaction = builder.build(); | |
// sign the XDR | |
transaction.sign(keypair); | |
// submit to the network. this returns a promise (resolves or rejects depending on the outcome of the transaction) | |
server.submitTransaction(transaction); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment