1

This is my code:

var sourceSecretKey = 'REDACTED';

var sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecretKey);
var sourcePublicKey = sourceKeypair.publicKey();


var server = new StellarSdk.Server('https://horizon.stellar.org');
StellarSdk.Network.usePublicNetwork();

server.loadAccount(sourcePublicKey)
    .then(function(account) {            
    var transaction = new StellarSdk.TransactionBuilder(account, {                
        fee: StellarSdk.BASE_FEE        
    })
        // Add a manageOffer operation
        .addOperation(StellarSdk.Operation.manageBuyOffer({
        selling: new StellarSdk.Asset.native(),
        buying: new StellarSdk.Asset('ABC', 'GDFBWP6GYJE7PMA4HVLLSPG6346Z67XAME7C2WJIUIA5HGJEJWFWIBPI'),
        amount: '0.001',
        price: '15.6250000',
        offerId: '0'
        }))
        .addMemo(StellarSdk.Memo.text('Hello'))
        .setTimeout(180)
        .build();

    transaction.sign(sourceKeypair);

     console.log(transaction.toEnvelope().toXDR('base64'));    

    server.submitTransaction(transaction)
        .then(function(transactionResult) {

         console.log(JSON.stringify(transactionResult, null, 2));
         console.log('\nSuccess! View the transaction at: ');
         console.log(transactionResult._links.transaction.href);

        })
        .catch(function(err) {
         console.log('An error has occured:');
         console.log(err);                                                                            

        });
    })
    .catch(function(e) {
     console.error(e);

    });

On submit, it shows an error:

TypeError: buyAmount argument must be of type String, represent a positive number and have at most 7 digits after the decimal
at Function.manageBuyOffer (stellar-sdk.js:43384)
at [.addOperation(StellarSdk.Operation.manageBuyOffer({]

I changed manageBuyOffer to manageSellOffer. It goes through, but the order book for the pair is not updated to reflect this offer. I tried doing the same transaction on stellarx.com, and the transaction went through and the order got reflected in the order book.

I tried another transaction, this time switching the pairs. The amount was "1" and the price "0.0540000". I got an error:

Error: Request failed with status code 400
at createError (stellar-sdk.js:27374)
at settle (stellar-sdk.js:46113)
at XMLHttpRequest.handleLoad (stellar-sdk.js:27236)

I am using the JavaScript SDK and running in the browser. I did the same transaction on stellarx.com, and the transaction went through as expected, and the order got reflected in the order book. I wonder where all these errors are coming from.

2

You have to provide buyAmount instead of amount in manageBuyOffer operations.

The 400 error on the testnet may be cause by different reasons: no trustline, not enough funds, invalid signers, etc. Check the extras.result_codes field in the error object itself.

  • It may be a novice's question, but how do I get the {extras.result_codes} to appear in the console.log? What will I add in my code above? – J R Sep 21 at 2:35
  • 1
    Set a breakpoint in your code that handles exceptions and check the error object itself or print the error to console using JSON.stringify(). Like try { horizon.loadAccount('GAB...') } catch(e) { console.log(JSON.stringify(e, null, ' ')) } – Orbit Lens Sep 21 at 10:50
  • This part of the code is the one catching the error and it does not return extras.result_codes: .catch(function(err) { console.log('An error has occured:'); console.log(err); }); – J R Sep 21 at 15:07
  • I did console.log(JSON.stringify(err, null, ' ')); and I can't quite find the extras.result_codes. – J R Sep 22 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.