Manual BTC Sell-ing on the Counterparty Distributed Exchange (DEx) using counterparty-client


#1

NOTE:The market command was removed from counterparty-client after this article was published, so the approach described in this post cannot be used verbatim (because market was removed, one would have to obtain DEx market information elsewhere (there are several community sites that offer it; you can also use the API), and do the rest with counterparty-client).
Certain wallets and applications that utilize the Counterparty API continue to retain the full ability to trade BTC on the Counterparty Distributed Exchange, whereas counterparty-client is now missing the ability to obtain the market information.

Introduction

Because BTC Pay was removed from Counterwallet, this article explains how to use counterparty-client to trade BTC on the Counterparty DEx.

This article is aimed at power users who feel comfortable with the counterparty CLI and intermediate-to-advanced bitcoin operations.

If you’re new to Counterparty and/or Bitcoin, at least try this on testnet until you’re sufficiently familiar and comfortable with all the steps. Regular users who want to purchase assets for bitcoin (BTC) can use one of traditional (centralized) exchanges or Shapeshift.io to buy XCP or other Counterparty assets.

All counterparty client commands used in this article can be found here. In the CLI you can get simple help by running “counterparty-cli --help” and “counterparty-cli --help” to get help for the particular command (replace <command> with one of commands, or actions, from the output of counterparty-client --help; for example, counterparty-client send --help would show help for the send action).

NOTE

  1. While BTC Pay is still available in Counterwallet (new BTC Sell orders were stopped as of Nov 5, 2014), if you use the same address in CLI close your Counterwallet wallet when making BTC Pay from CLI just so that you do not accidentally make BTC payment from both counterparty-client and Counterwallet.

  2. It is strongly recommended to try these commands on the testnet before you use “real” coins. Manual trading using the CLI is prone to human errors. Please be very careful when computing totals and interpreting command outputs.

How to find offers and place an order on the DEx

This part assumes you already have a funded address in Bitcoin-Core with addrindex patch.

  1. Display market offers of SOME asset denominated in anything (although you’d be looking for BTC-denominated SOME, you may find attractive offers in XCP, so it’s okay to display multiple denominators).

counterparty-client market --give-asset=SOME

Output may look similar to this:

Open Orders
+----------------+------------+--------------+---------------+------------------+------------------+-----------+------------------------------------------------------------------+
| Give Quantity | Give Asset | Price | Price Assets | Required BTC Fee | Provided BTC Fee | Time Left | Tx Hash |
+----------------+------------+--------------+---------------+------------------+------------------+-----------+------------------------------------------------------------------+
| 20.0 | SOME | 0.001 | BTC/SOME ask | 0.0012 | 0.0001 | 94 | 9LONGSTRING0 |

Let’s see: someone’s offering 20 SOME in exchange for BTC at the price of 0.001 BTC per SOME (we’ll ignore DEx fees in this how to). This particular order expires in 94 blocks.

  1. That offer is good for us but we want to order 10 SOME (not all 20):
counterparty-client order --source=mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns --get-quantity=10 --get-asset=SOME --give-quantity=0.01 --give-asset=BTC --expiration=100 --fee-fraction-provided=0.009

In Counterwallet the default expiration for BTC Sell was (before it was disabled) 2000 blocks and fee provided is 1% of the order value. Here we show an example with different values (100 blocks, 0.9%).

We’re buying 10 units of SOME for 0.01 BTC (therefore 10 SOME * 0.001 BTC/SOME = 0.01 BTC order size) using funds from our Bitcoin/Counterparty address mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns.

How to manually complete BTC Pay using counterparty-client

If you want to trade with your Counterwallet address, you need to import its private key to Bitcoin-Core with addrindex. In Counterwallet get your private key for the address you want to use and then In Bitcoin Core go to Help > Debug Window. In debug console import your private key:

importprivkey <YOUR-PRIVATE-KEY> CW2 rescan

CW2 is just a label that we chose for this address (to remind us this is our second Counterwallet address, for example). If you have a space in the label, surround it with double quotes (e.g. “Counterwallet Address #2”). There’s rescan option to importprivkey is time consuming. After that command completes, you will not see non-BTC assets in Bitcoin-Qt, but you can use counterparty-client wallet to check.

If you do not want to use your Counterwallet address, then you can send some BTC to your Bitcoin Core address and avoid the need to import private key.

This counterparty-client command would be executed in Windows console (not in Bitcoin-Core debug console).

counterparty-client wallet

If everything is okay, you will see your imported address and BTC and Counterparty assets it contains.

If your order has been matched, check ID for the order match:

counterparty-client pending --address <YOUR-ADDRESS>

Obvoiusly we’d provide mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns as our address because that’s what we used to place our order. In return we’d get Order Match ID which is required for BTC Pay. Let’s assume that Order Match ID was 092f15d36786136c4d868c33356ec3c9b5a0c77de54ed0e96a8dbdd8af160c23.

In output of the pending action we can see Matched Order ID and also Time Left. Below that we see that we have 18 blocks (figure in the last column) to make BTC Pay. If we want you can wait a few more blocks for additional confirmations on the blockchain, although for small orders that may be considered unnecessary.

---------------------------------------------------+-----------+
| Matched Order ID                                 | Time Left |
---------------------------------------------------+-----------+
| 092f15d36786136c4d868c33356ec3c9b5a0c77de54ed0e96a8dbdd8af160c23|18| 
| ---------------------------------------------------+-----------+

Now execute BTC Pay (again, you can do this within 20 blocks from the moment of matching - see Time Left above). That’s why you need to have your setup ready before your BTC Sell order is matched.

counterparty-client btcpay --source=mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns --order-match-id=092f15d36786136c4d868c33356ec3c9b5a0c77de54ed0e96a8dbdd8af160c23

Again, we use our source address and Order Match ID we obtained in the previous step. We need to have enough BTC at the paying address.

After 3 confirmations BTC Pay should be complete and 20 SOME should be visible in the output of counterparty-client wallet for your source address.

"Dual" access to the same address

Notice that if you imported your private key from a Counterwallet address, this address is still accessible from there as well.

If is better to not use this approach, although it does allow convenient BTC (from Counterwallet) and non-BTC (in counterparty-client) operations on the DEx.


How does the counterparty decentralized exchange work?
Keep getting this bug when trying to deposit btc to multisig address
How to import private key from Counterwallet to another wallet?
I'm having issues with Counterwallet. Are there alternative Counterparty (XCP) wallets?
Worried about "Unable to decode output address"
Need a good BTC-XCP exchange
User Experience with BTC on DEX
Sent BTC Pay after order had expired