How to cancel pending trades?

How to Cancel a Counterparty DEx Order or Bet via the API

First figure out what needs to be done. We refer to the API documentation and see that create_cancel is available. That requires us to know the order hash. And in order to figure that out, we need to use get_market_orders (counterblock API) to find our order.

Find your order hash

Let’s say we submitted this DEx order and want to cancel it: https://xchain.io/tx/1110714

image

This guy 1HTp81pQMctV2eAYMKZrp35RQ3riBfEoaA is selling PEPECASH in exchange for STARLORDKEK. Let’s find his order by connecting to Counterblock API.

{
	"method": "get_market_orders",
	"params": {"asset1":"PEPECASH","asset2":"STARLORDKEK", "addresses": ["1HTp81pQMctV2eAYMKZrp35RQ3riBfEoaA"]}
	,
	"jsonrpc": "2.0",
	"id": 1
}

There’s only one, good! If there were more, you’d have to figure out which one it is, either by making additional filters or simply looking block_index (block height) of when you issued it.

{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "block_index": 493052,
            "source": "1HTp81pQMctV2eAYMKZrp35RQ3riBfEoaA",
            "block_time": 1509807828,
            "tx_index": 1110714,
            "total": 5,
            "type": "BUY",
            "tx_hash": "a4a96e3f25f0b0156076280975fea5068c2417c6bc5b63ddefdc1cab784b15f5",
            "completion": "0.00%",
            "price": "0.00022222",
            "amount": 2250022500000
        }
    ]
}

Offer Hash: a4a96e3f25f0b0156076280975fea5068c2417c6bc5b63ddefdc1cab784b15f5

If you look at the screenshot, both offer hash and source (of the offer) are actually visible, so you could as well just copy those and skip this step if you’ve visually identified the order.

Cancel a DEx order

Orders are created and canceled using the “default” API, from Counterparty server.

Now we can use create_cancel to cancel this order. As mentioned above, it’s create_cancel(offer_hash,source).

{
	"method": "create_cancel",
	"params": {"offer_hash": "a4a96e3f25f0b0156076280975fea5068c2417c6bc5b63ddefdc1cab784b15f5", "source": "1HTp81pQMctV2eAYMKZrp35RQ3riBfEoaA"}
	,
	"jsonrpc": "2.0",
	"id": 1
}

This returns a result which is a pre-composed Bitcoin transaction with embedded Counterparty DEx transaction (cancellation) order:

{
    "jsonrpc": "2.0",
    "result": "0100000001bf36749cb4996a06fd38c132156db42fb14fc7bd7ff62bdc1807c218fcb0dd64010000001976a914b4942980bcc8bf07d6f1b3939c1545c00ecb6bf688acffffffff0200000000000000002b6a29afd3927dbc58a45d44c127438ba12f4db7c7066e9906b97678cf0571db6fc1b6b18eacadaa86c39e7abad22a00000000001976a914b4942980bcc8bf07d6f1b3939c1545c00ecb6bf688ac00000000",
    "id": 1
}

Sign and Broadcast

Now this transaction has to be signed by 1HTp81pQMctV2eAYMKZrp35RQ3riBfEoaA and then sent (broadcast), which is explained elsewhere (see this).

Other Notes

If you’re not used to JSON-RPC, maybe this will help. You can use CoinDaddy’s public servers to run API examples, but you still need a Bitcoin node where you can sign and broadcast the transaction.