Now that we understand the CowSdk basics, let's build a complete trading workflow using the unified SDK. We'll create an order in three steps:
- Generate app data (metadata)
- Get a quote (pricing)
- Sign the order (authorization)
Step 1: Generate App Data
run.ts
const appDataDoc = await cowSdk.metadataApi.generateAppDataDoc({
appCode: 'Decentralized CoW',
environment: 'production',
metadata: {
referrer: { address: '0xcA771...' },
quote: { slippageBips: 50 } as latest.Quote,
orderClass: { orderClass: 'market' } as latest.OrderClass
}
});
const { appDataHex } = await cowSdk.metadataApi.getAppDataInfo(appDataDoc);Step 2: Get Quote
run.ts
const { quote } = await cowSdk.orderBook.getQuote({
sellToken: '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d', // wxDAI
buyToken: '0x177127622c4A00F3d409B75571e12cB3c8973d3c', // COW
from: ownerAddress,
receiver: ownerAddress,
sellAmountBeforeFee: '1000000000000000000', // 1 wxDAI
kind: OrderQuoteSideKindSell.SELL,
appData: appDataContent,
appDataHash: appDataHex
});Step 3: Sign Order
run.ts
const order: UnsignedOrder = {
...quote,
sellAmount: '1000000000000000000',
feeAmount: '0',
receiver: ownerAddress,
appData: appDataHex
};
const orderSigningResult = await cowSdk.orderSigning.signOrder(order, chainId, signer);Complete Example
run.ts
export async function run(provider: Web3Provider): Promise<unknown> {
const signer = provider.getSigner();
const adapter = new EthersV5Adapter({ provider, signer });
const chainId = SupportedChainId.GNOSIS_CHAIN;
const cowSdk = new CowSdk({ adapter, chainId, env: 'prod' });
try {
// Step 1: App Data
const appDataDoc = await cowSdk.metadataApi.generateAppDataDoc({...});
const { appDataHex } = await cowSdk.metadataApi.getAppDataInfo(appDataDoc);
// Step 2: Quote
const { quote } = await cowSdk.orderBook.getQuote({...});
// Step 3: Sign
const order = { ...quote, appData: appDataHex, ... };
const signature = await cowSdk.orderSigning.signOrder(order, chainId, signer);
return {
appDataHash: appDataHex,
expectedBuyAmount: quote.buyAmount,
signature: signature.signature.substring(0, 20) + '...',
signingScheme: signature.signingScheme
};
} catch (error) {
return { error: error.message };
}
}Run the Code
To run the code, press the "Run" button in the bottom right panel.
- Accept the wallet connection request
- Press the "Run" button
- Observe the complete workflow execution
Example output:
output.json
{
"appDataHash": "0xe269b...",
"expectedBuyAmount": "400000000000000000000",
"signature": "0x98ac...",
"signingScheme": "eip712"
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import type { Web3Provider } from '@ethersproject/providers';import { CowSdk, SupportedChainId, OrderQuoteSideKindSell, latest } from '@cowprotocol/cow-sdk';import { EthersV5Adapter } from '@cowprotocol/sdk-ethers-v5-adapter';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 signer = provider.getSigner();
const adapter = new EthersV5Adapter({ provider, signer }); const cowSdk = new CowSdk({adapter,
chainId,
env: 'prod'
});
const ownerAddress = await signer.getAddress();
// Trading parameters
const sellToken = '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d'; // wxDAI
const buyToken = '0x177127622c4A00F3d409B75571e12cB3c8973d3c'; // COW
const sellAmount = '1000000000000000000'; // 1 wxDAI
// TODO: Complete workflow using CowSdk modules:
// 1. Generate app data using cowSdk.metadataApi
// 2. Get quote using cowSdk.orderBook
// 3. Sign order using cowSdk.orderSigning
return {};}