4. Checking Limit Order Status

In order to follow if/when your order has been matched you can either subscribe to “Trade” events by the Settlement Contract:

import GPv2SettlementArtefact from "@gnosis.pm/gp-v2-contracts/deployments/mainnet/GPv2Settlement.json";
import { Contract, ethers } from "ethers";
const uid = <uid to follow>;
const TRADE_TIMEOUT_SECONDS = 30*60
const settlement = new Contract(“0x9008D19f58AAbD9eD0D60971565AA8510560ab41”, GPv2SettlementArtefact.abi, ethers.provider)
const traded = new Promise((resolve: (value: boolean) => void) => {
ethers.provider.on(settlement.filters.Trade(trader), (log) => {
// Hacky way to verify that the UID is part of the event data
if (log.data.includes(uid.substring(2))) {
resolve(true);
}
});
});
const timeout = new Promise((resolve: (value: boolean) => void) => {
setTimeout(resolve, TRADE_TIMEOUT_SECONDS*1000, false);
});
const hasTraded = await Promise.race([traded, timeout]);
This script waits up to 30 minutes to find a trade event before eventually timing out.
Or you can query our API using the order ID that you generated earlier
https://api.cow.fi/mainnet/api/v1/trades?orderUid=0xc21b7756caf1f6df13e9947767204620371ca791a4b91db8620f04905d25b608e0b3700e0aadcb18ed8d4bff648bc99896a18ad160ef0bca
[
{
"blockNumber": 12826021,
"logIndex": 31,
"orderUid": "0xc21b7756caf1f6df13e9947767204620371ca791a4b91db8620f04905d25b608e0b3700e0aadcb18ed8d4bff648bc99896a18ad160ef0bca",
"buyAmount": "80623566",
"sellAmount": "100000000000000000000",
"sellAmountBeforeFees": "89287648398497935360",
"owner": "0xe0b3700e0aadcb18ed8d4bff648bc99896a18ad1",
"buyToken": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"sellToken": "0x1a5f9352af8af974bfc03399e3767df6370d82e4",
"txHash": "0xbcdd49946b56564b7ba7403ab63a2316ece5c6e12657782ffda620d192e591a0"
}
]

If your order was partially fillable and traded in multiple chunks you may find one entry per trade.

For convenience, you can also check the status of your order using the CoW Protocol API at https://api.cow.fi/$network/api/v1/orders/$orderUid. For example: https://api.cow.fi/mainnet/api/v1/orders/0x1792f3fc1615117bef538ab7e830e4c882f9b51d27aadcc14dd10754bb6b5423baaea72417f4dc3e0f52a1783b0913d0f3516634632b58b1

This is it. We hope you have been able to follow this tutorial end to end and made a successful trade. If you have any questions or are planning to write some utility software (e.g. signing logic in another language) please reach out to us on Discord, we are always happy to help.

Happy Trading!

Last updated