Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save selvan/7a727a9b2253ee191466499176f10c66 to your computer and use it in GitHub Desktop.
Save selvan/7a727a9b2253ee191466499176f10c66 to your computer and use it in GitHub Desktop.
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);
});
});
@selvan
Copy link
Author

selvan commented Apr 23, 2018

Fix for above issue

  1. op2 didn't have destAccountPair.publicKey() as source
  2. tx also requires destAccountPair as additional signer
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,
                source: destAccountPair.publicKey()
        });
        

        let tb = new StellarSdk.TransactionBuilder(sourceAccount);
        tb.addOperation(op1);  
        tb.addOperation(op2);

        let tx = tb.build();
        tx.sign(sourceAccountPair);
        tx.sign(destAccountPair);
        let _env = tx.toEnvelope().toXDR().toString("base64")
        console.log(_env);
        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