Skip to main content

Now, you've submitted an order to CoW Protocol. In this tutorial, we will attempt to view the status of the order we just submitted.

Viewing the status

With CoW Protocol, the only way to view the status of an order that has not yet been settled is to query the OrderBookApi. This is because the order is not yet on-chain, and therefore cannot be queried from the blockchain.

Instantiate the SDK

We will start from the basic setup from the quote tutorial after we have instantiated the OrderBookApi.

Query parameters

Now that we have an instantiated OrderBookApi, we can query the status of an order. To do this, we need to know:

  • the orderUid of the order we want to query, for example 0x8464affce2df48b60f6976e51414dbc079e9c30ef64f4c1f78c7abe2c7f96a0c29104bb91ada737a89393c78335e48ff4708727e659523a1
run.ts
import type { Web3Provider } from '@ethersproject/providers';
import { SupportedChainId, OrderBookApi } from '@cowprotocol/cow-sdk';

export async function run(provider: Web3Provider): Promise<unknown> {
  // ...
  const orderUid = `0x8464affce2df48b60f6976e51414dbc079e9c30ef64f4c1f78c7abe2c7f96a0c29104bb91ada737a89393c78335e48ff4708727e659523a1`;
  // ...
}

Querying the order

In this tutorial, we will do two types of queries:

  • getOrder to get general information about the order
  • getTrades that will show us the trades that have been executed against the order
run.ts
import type { Web3Provider } from '@ethersproject/providers';
import { SupportedChainId, OrderBookApi } from '@cowprotocol/cow-sdk';

export async function run(provider: Web3Provider): Promise<unknown> {
  // ...
	try {
		const order = await orderBookApi.getOrder(orderUid);
		const trades = await orderBookApi.getTrades({ orderUid });

		return {
			order,
			trades
		}
	} catch (e) {
		return e;
	}
  // ...
}

Run the code

To run the code, we can press the "Run" button in the bottom right panel (the web container).

A successful query should look something like:

output.json
{
	"order": { ... },
	"trades": [ ... ]
}

The order and trades objects are quite large, so we have omitted them from this example.

Next: Cancelling off-chain

1
2
3
4
5
6
7
8
9
10
11
12
13
import type { Web3Provider } from '@ethersproject/providers';
import { SupportedChainId, OrderBookApi } from '@cowprotocol/cow-sdk';
 
export async function run(provider: Web3Provider): Promise<unknown> {
    const chainId = +(await provider.send('eth_chainId', []));
    if (chainId !== SupportedChainId.GNOSIS_CHAIN) {
        throw new Error(`Please connect to the Gnosis chain. ChainId: ${chainId}`);
    }
 
    const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GNOSIS_CHAIN });
 
}
 
initialising