import { encodeFunctionData, encodeAbiParameters } from 'viem'
const COMPOSABLE_COW = '0xfdaFc9d1902f4e0b84f65F49f244b32b31013b74'
const HANDLER_ADDRESS = '0xYourDeployedHandlerAddress'
// Encode the handler's static input
const staticInput = encodeAbiParameters(
[
{ type: 'address' }, // sellToken
{ type: 'address' }, // buyToken
{ type: 'uint256' }, // threshold
{ type: 'uint256' }, // maxSellAmount
{ type: 'bytes32' }, // appData
],
[
USDC_ADDRESS,
WETH_ADDRESS,
10000n * 10n ** 6n, // 10,000 USDC threshold
5000n * 10n ** 6n, // Max 5,000 USDC per trade
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
)
// Create the programmatic order via ComposableCoW
const createCalldata = encodeFunctionData({
abi: [{
name: 'create',
type: 'function',
inputs: [
{
name: 'params',
type: 'tuple',
components: [
{ name: 'handler', type: 'address' },
{ name: 'salt', type: 'bytes32' },
{ name: 'staticInput', type: 'bytes' },
],
},
{ name: 'dispatch', type: 'bool' },
],
outputs: [],
}],
functionName: 'create',
args: [
{
handler: HANDLER_ADDRESS,
salt: '0x' + crypto.randomUUID().replace(/-/g, '') + '0'.repeat(32).slice(0, 32),
staticInput,
},
true, // dispatch = true to emit event for Watch Tower
],
})
// Execute from the Safe
const safeTx = await safe.createTransaction({
transactions: [{
to: COMPOSABLE_COW,
value: '0',
data: createCalldata,
}],
})