import {
CowShedSdk,
ICoWShedCall,
SupportedChainId
} from '@cowprotocol/cow-shed'
import {
EthersV6Adapter
} from '@cowprotocol/sdk-ethers-v6-adapter'
import { encodeFunctionData, parseEther, parseUnits } from 'viem'
import { JsonRpcProvider, Wallet } from 'ethers'
// 1. Initialize
const provider = new JsonRpcProvider('https://mainnet.infura.io/v3/YOUR-KEY')
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV6Adapter({ provider, signer: wallet })
const cowShedSdk = new CowShedSdk(adapter)
// 2. Get CoW Shed account
const ownerAddress = await wallet.getAddress()
const cowShedAccount = cowShedSdk.getCowShedAccount(
SupportedChainId.MAINNET,
ownerAddress
)
// 3. Prepare calls
const WETH = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const SETTLEMENT = '0x9008D19f58AAbD9eD0D60971565AA8510560ab41'
const calls: ICoWShedCall[] = [
// Approve WETH
{
target: WETH,
callData: encodeFunctionData({
abi: [{
name: 'approve',
type: 'function',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' }
],
outputs: [{ type: 'bool' }]
}],
functionName: 'approve',
args: [SETTLEMENT, parseEther('1')]
}),
value: 0n,
isDelegateCall: false,
allowFailure: false,
},
// Set pre-signature for order
{
target: SETTLEMENT,
callData: encodeFunctionData({
abi: [{
name: 'setPreSignature',
type: 'function',
inputs: [
{ name: 'orderUid', type: 'bytes' },
{ name: 'signed', type: 'bool' }
],
outputs: []
}],
functionName: 'setPreSignature',
args: [orderUid, true]
}),
value: 0n,
isDelegateCall: false,
allowFailure: false,
},
]
// 4. Sign calls
const preAuthorizedCall = await cowShedSdk.signCalls({
chainId: SupportedChainId.MAINNET,
calls,
})
// 5. Execute (can be done by anyone)
const { signedMulticall, gasLimit } = preAuthorizedCall
const { to, data, value } = signedMulticall
const tx = await wallet.sendTransaction({
to,
data,
value,
gasLimit,
})
const receipt = await tx.wait()
console.log('Pre-authorized calls executed:', receipt.hash)