Buying and Selling Your Custom Token on the Stellar Testnet Server: Adventures in Cryptocurrency Programming Part 2

Step 1: Create a Buyer Account

This is the second in a series of blogposts. If you've not read the first one, then do so now, because before you can experiment with buying and selling on the Stellar network, you'll need to have created a custom asset (aka token).

The first blogpost will also tell you how to create accounts in its own Step 1 and that's what you need to do now. Once you've created this account, which will be your third in total, and recorded its public and private keys (as you did with the first two), we'll be ready to move on. Note: Place a heading above the two keys of Buyer Account in your text file.

Step 2: Trusting the Custom Token

In order for the Buyer Account to trust the custom token you created, turn to the first post once again and follow Step 3 to establish trust between the Buying Account and the Custom Token (note: use the Issuing Account public key beneath the token code not the Distributor Account one).

Step 3: Create a Sell Offer

Now the fun starts and involves a bit of role-play, because we're going to play the parts of both seller and buyer. Imagine yourself the seller first of all and create a Sell Offer:
  1. Select the Build Transaction tab.
  2. Enter your Distributor Account public key in the source account field.
  3. Fetch a new Transaction sequence number.
  4. Select a transaction type in the dropdown box of Manage Sell Offer.
  5. Select Alphanumeric 4 in the Selling field and enter your token code and Issuing Account public key.
  6. In the Buying field select Native.
  7. Specify the Amount you want to sell.
  8. Specify the Price you want to achieve expressed per custom token in the buying currency, which in this case is Lumens (XLM), the native Stellar currency.
  9. Enter 0 in the Offer ID field. (This is because we're not updating an offer, we're creating a new one.)
  10. Press Sign in Transactor button at the bottom of the page.

Step 4: Signing the Transaction

Repeat Step 4 from the first post using the Secret Key from the Distributor Account to sign the transaction and then submit it.

Step 5: Viewing the Selling Offer

Now you've created a selling offer it can be viewed in one of two ways:
  1. By visiting the following URL https://horizon-testnet.stellar.org/accounts/{DISTRIBUTOR ACCOUNT PUBLIC KEY} and scrolling down to the balances.
  2. Or under the Explore Endpoints tab by selecting Offers > Offers for Account and entering your Distributor Account public key in the Account ID field.



Step 6: Create a Buy Offer

Change hats, you're going to be the buyer now. 
  1. Select the Build Transaction tab.
  2. Enter your Buyer Account public key in the source account field.
  3. Fetch a new Transaction sequence number.
  4. Select a transaction type in the dropdown box of Manage Buyer Offer.
  5. Select Native in the Selling field.
  6. Select Alphanumeric 4 in the Buying field and enter your custom token code and Issuing Account public key.
  7. Specify the Amount you want to buy.
  8. Specify the maximum Price you want to spend per custom token in the currency you're selling, which in this case is Lumens (XLM), the native Stellar currency. (Note: It must be the same or higher than the price you set as the seller from the Distributor Account.)
  9. Enter in the Offer ID field. (This is because once again we're not updating an offer, we're creating a new one.)
  10. Press Sign in Transactor button at the bottom of the page.
  11. Sign the transaction as we did in Step 4 using your Buyer Account secret key, and submit the transaction. 


Step 7: Viewing the Trade

Having created compatible buying and selling offers, the trade takes place within an instant, closing the buying and selling offers, and we can now inspect the trade and see the result.
  1. Under the Explore Endpoints tab select Trades > Trades for Account and enter your Buyer Account (or Distributor Account) public key in the Account ID field and submit.
  2. Visit the URL https://horizon-testnet.stellar.org/accounts/{BUYER ACCOUNT PUBLIC KEY}  to see how your balances have been updated.
The price the buyer pays will always be the seller's offer price. If the buyer offered less, the trade won't be compatible and the buyer's offer will sit around waiting for the next compatible one.



What's next?

Next time, I'm planning to look at the revising of offers using the laboratory interface vs the Node.js API.


Comments