4

Right now I'm playing around with the test server

Server server = new Server("https://horizon-testnet.stellar.org");

The Stellar getting started guides are very well written, but definitely incomplete. They only cover creating accounts and sending/receiving (with Java examples).

I want to create an Asset and then offer it up for sale on the (test network) Stellar distributed exchange.

My Asset is

Asset testAsset = Asset.createNonNativeAsset("TEST", issuingKeys);

I created the asset, created a trustline to another test account, and then I sent the account 5,000 TEST. It all worked well. I want to write a program that sends sell orders to the distributed exchange.

I can't figure out how to create offers. The correct class seems to be ManageOfferOperation.Builder. It requires parameters Asset, Asset, String, String.

Assuming I didn't create the Asset, this means I'll need to create an Asset object for the Asset I want to trade? Like:

Asset sellTestAsset = Asset.createNonNativeAsset("TEST", ACCOUNT_OBJECT);

But createNonNativeAsset requires an KeyPair object. So, I'll need to find the issuing public ID of whatever asset I want to trade, and make a KeyPair?

KeyPair testIssuer  = KeyPair.fromAccountId("PUBLIC_ID_HERE");

And then add this as a parameter to Asset.createNonNativeAsset.? Going to the ManageOfferOperation constructor:

Builder firstOffer = new ManageOfferOperation.Builder(sellTestAsset, sellTestAsset, "10", "10");

This seems a bit excessive, and I'm still having trouble with ManageOfferOperation.

2 Answers 2

5

It sounds to me like you are unfamiliar with the available methods in the SDK. In addition to what Orbit Lens said, you'll need to create a transaction, sign it, and then submit it to the network.

Because there aren't very many step-by-step examples out there, I'll try to "solve" this for you.

Starting with the Asset. Assuming you correctly issued an asset, you'll need to know the public key of the issuing account.

KeyPair issuer = KeyPair.fromAccountId("GCHANWCZJDCJB4L5KSGTZZV7TA5MCRB2V55GN5NI2EE25SOTICTUIXZ6");

You'll also need to know the asset code. I do not think you should be using createNonNativeAsset. Since you used "TEST" in your code, I'm going to assume your asset was issued as AssetTypeCreditAlphaNum4. Add the KeyPair to an Asset instance:

Asset testAsset = new AssetTypeCreditAlphaNum4("TEST", issuer);

Now, let's build an offer.

 ManageOfferOperation firstOffer = new ManageOfferOperation.Builder(new AssetTypeNative(), testAsset, "10", "1").setSourceAccount(pair).setOfferId(0).build();

This assumes you are trading your asset against XLM, XLM represented as the new AssetTypeNative().

But you're only halfway through at this point. You've created an instance of the ManageOfferOperation, but you haven't created a transaction, signed it, or sent it to the Horizon server.

Transaction:

AccountResponse account = server.accounts().account(pair);

Transaction transaction = new Transaction.Builder(account).addOperation(firstOffer).build();

Sign it, replacing privateKey with the KeyPair object that holds your private key.

  transaction.sign(privateKey);

Finally, let the rest of the Stellar network know about your offer:

SubmitTransactionResponse response = server.submitTransaction(transaction);

You should probably throw that last line into a try/catch.

2

Have you checked the Java SDK docs?

But createNonNativeAsset requires an KeyPair object. So, I'll need to find the issuing public ID of whatever asset I want to trade, and make a KeyPair?

Yes, any custom asset in the Stellar network is described by code and issuer public key. Actually, there is also an asset type (AlphaNum4 or AlphaNum12), but the AssetTypeCreditAlphaNum handles it for you. So you have to create a KeyPair and pass it to the AssetTypeCreditAlphaNum constructor alongside with an asset code. Native XLM asset is represented by AssetTypeNative class.

ManageOfferOperation.Builder has the following semantic:

Builder(Asset selling, Asset buying, java.lang.String amount, java.lang.String price)

  • selling - The asset being sold in this operation
  • buying - The asset being bought in this operation
  • amount - Amount of selling being sold.
  • price - Price of 1 unit of selling in terms of buying.

Your example is incorrect, because both selling an buying refer to the same asset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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