Forked from selvan/Stellar: createAccount + changeTrust in a single Tx
Last active
April 23, 2018 14:47
-
-
Save dbuarque/3f42b406064f2e616b6ad92b5271575e to your computer and use it in GitHub Desktop.
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
var StellarSdk = require('stellar-sdk'); | |
StellarSdk.Network.useTestNetwork(); | |
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org'); | |
var sourceAccountPair = StellarSdk.Keypair.random(); | |
var destAccountPair = StellarSdk.Keypair.random(); | |
var astroDollar1 = new StellarSdk.Asset('AstroDollar1', sourceAccountPair.publicKey()); | |
var request = require('request'); | |
request.get({ | |
url: 'https://horizon-testnet.stellar.org/friendbot', | |
qs: { addr: sourceAccountPair.publicKey() }, | |
json: true | |
}, function(error, response, body) { | |
if (error || response.statusCode !== 200) { | |
console.error('ERROR!', error || body); | |
return; | |
} | |
server.loadAccount(sourceAccountPair.publicKey()).catch(StellarSdk.NotFoundError, function (error) { | |
throw new Error('The destination account does not exist!'); | |
}).then(function(sourceAccount) { | |
let op1 = StellarSdk.Operation.createAccount({ | |
destination: destAccountPair.publicKey(), | |
startingBalance: "10", | |
source: sourceAccountPair.publicKey() | |
}); | |
let op2 = StellarSdk.Operation.changeTrust({ | |
asset: astroDollar1 | |
}); | |
let tb = new StellarSdk.TransactionBuilder(sourceAccount); | |
tb.addOperation(op1); | |
tb.addOperation(op2); | |
let tx = tb.build(); | |
tx.sign(sourceAccountPair); | |
return server.submitTransaction(tx); | |
}).then(function(result) { | |
console.log('Success! Results:', result); | |
}).catch(function(error) { | |
console.log(error.data.extras.result_codes) | |
console.log("+++++++++++++++++++++++++++++++++"); | |
console.log( JSON.stringify(StellarSdk.xdr.TransactionEnvelope.fromXDR(error.data.extras.envelope_xdr, 'base64')) ); | |
console.log("+++++++++++++++++++++++++++++++++"); | |
console.error('Something went wrong in transaction!', error); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment