5. Checking order status
In order to follow if/when your order has been matched you can either subscribe to “Trade” events by the Settlement Contract:
1
import GPv2SettlementArtefact from "@gnosis.pm/gp-v2-contracts/deployments/mainnet/GPv2Settlement.json";
2
import { Contract, ethers } from "ethers";
3
4
const uid = <uid to follow>;
5
const TRADE_TIMEOUT_SECONDS = 30*60
6
7
const settlement = new Contract(0x9008D19f58AAbD9eD0D60971565AA8510560ab41, GPv2SettlementArtefact.abi, ethers.provider)
8
const traded = new Promise((resolve: (value: boolean) => void) => {
9
ethers.provider.on(settlement.filters.Trade(trader), (log) => {
10
// Hacky way to verify that the UID is part of the event data
11
if (log.data.includes(uid.substring(2))) {
12
resolve(true);
13
}
14
});
15
});
16
17
const timeout = new Promise((resolve: (value: boolean) => void) => {
18
setTimeout(resolve, TRADE_TIMEOUT_SECONDS*1000, false);
19
});
20
21
const hasTraded = await Promise.race([traded, timeout]);
Copied!
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
1
[
2
{
3
"blockNumber": 12826021,
4
"logIndex": 31,
5
"orderUid": "0xc21b7756caf1f6df13e9947767204620371ca791a4b91db8620f04905d25b608e0b3700e0aadcb18ed8d4bff648bc99896a18ad160ef0bca",
6
"buyAmount": "80623566",
7
"sellAmount": "100000000000000000000",
8
"sellAmountBeforeFees": "89287648398497935360",
9
"owner": "0xe0b3700e0aadcb18ed8d4bff648bc99896a18ad1",
10
"buyToken": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
11
"sellToken": "0x1a5f9352af8af974bfc03399e3767df6370d82e4",
12
"txHash": "0xbcdd49946b56564b7ba7403ab63a2316ece5c6e12657782ffda620d192e591a0"
13
}
14
]
Copied!
If your order was partially fillable and traded in multiple chunks you may find one entry per trade.
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!
Copy link