0

I was trying to make buy offer for a newly created asset (MCoin).

I am getting error:

TypeError: opts.selling.toXDRObject is not a function

The code of node script goes by:

server.loadAccount(distributor.publicKey)
    .then(function(account){
        var transaction = new StellarSdk.TransactionBuilder(account)
                .addOperation(StellarSdk.Operation.manageOffer({
                    selling: 'MCoin',
                    buying: 'XLM',
                    amount : 200,
                    price: 2,
                    offerId: 0,
                }))
                .build();
        transaction.sign(distributorKeyPair);
        //console.log(transaction.toEnvelope().toXDR('base64'));

        server.submitTransaction(transaction)
            .then(function(transactionResult){
                //console.log(JSOn.stringify(transactionResult));
                console.log('\n Success!')
            })
            .catch(function(err){
                console.log('An error has occured');
                console.log(err);
            })

    })
    .catch(function(e){
        console.log(e);
    })
1

I found the answer. I was not representing the Assets that I need to sell properly.

Replacing MCoin with new StellarSdk.Asset('MCoin', 'GCKRPJXPGNHBPMRCXEX4MDH532BN5RLUJJOMVACA3CB7E2LWSTKTB5U2') solved the problem.

Ideally, an asset is Stellar is represented by its code and the issuer account Id.

Final code becomes:

server.loadAccount(distributor.publicKey)
    .then(function(account){
        var transaction = new StellarSdk.TransactionBuilder(account)
                .addOperation(StellarSdk.Operation.manageOffer({
                    selling: new StellarSdk.Asset('MCoin', 'GCKRPJXPGNHBPMRCXEX4MDH532BN5RLUJJOMVACA3CB7E2LWSTKTB5U2'),
                    buying:  StellarSdk.Asset.native(),
                    amount : '200',
                    price: 2,
                    offerId: 0,
                }))
                .build();
        transaction.sign(distributorKeyPair);
        //console.log(transaction.toEnvelope().toXDR('base64'));

        server.submitTransaction(transaction)
            .then(function(transactionResult){
                //console.log(JSOn.stringify(transactionResult));
                console.log('\n Success!')
            })
            .catch(function(err){
                console.log('An error has occured');
                console.log(err);
            })

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

Also note that amount must be string.

| improve this answer | |

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.