Writing Skills
writing-skills
效率与协作技能,来自 obra/superpowers,查看原文获取完整用途和操作说明。
ditto-workflow【部署与运维】Creates, configures, and deploys on-chain automation workflows using the Ditto Network SDK. Use when the user asks to "create a workflow", "automate on-chain", "schedule transactions", "deploy a workflow", "set up recurring transfers", "swap tokens on a schedule", "automate DeFi", "create a cron job on-chain", "trigger on event", or mentions "Ditto N…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Build and deploy declarative on-chain automation workflows using @ditto/workflow-sdk. Workflows define triggers (cron, event, onchain state) and jobs (batched contract calls) that execute via ZeroDev smart accounts with session keys.
SDK source: github.com/dittonetwork/ditto-workflow-sdk (branch: skill-integration)
Understanding these two roles is critical:
submitWorkflow takes executorAddress (a public 0x... address) — NOT a private key. The session key system grants scoped permissions to this address so the network can execute on behalf of the owner's smart account.
BEFORE writing any workflow code, verify the project setup:
@ditto/workflow-sdk is installed: look for it in package.json.env file exists with required keys (see Environment Setup below)npm install @ditto/workflow-sdkThe .env file MUST contain:
PRIVATE_KEY=0x... # Owner's private key (the user's wallet — used to sign and deploy)
IPFS_SERVICE_URL=https://ipfs-service.dittonetwork.io
Optional (only needed for cancellation):
WORKFLOW_CONTRACT_ADDRESS=0x... # DittoWFRegistry address
The executor address is embedded in the SDK — use getDittoExecutorAddress() from @ditto/workflow-sdk. Do NOT ask the user for an executor address or private key.
CRITICAL:
getDittoExecutorAddress()..env via dotenv.Ask the user for:
If the user is vague, suggest a concrete workflow and confirm before proceeding.
Create a TypeScript file that:
dotenvprivateKeyToAccountWorkflowBuilder and JobBuildersubmitWorkflow, passing the executor's public addressKey pattern: WorkflowBuilder.create() takes an Account (address only, no signing capability). Use addressToEmptyAccount(owner.address) for this. The actual Signer (full private key account from privateKeyToAccount) is passed separately to submitWorkflow for signing session keys and transactions.
Minimal template:
import {
WorkflowBuilder, JobBuilder, ChainId,
submitWorkflow, IpfsStorage, getDittoExecutorAddress
} from '@ditto/workflow-sdk';
import { privateKeyToAccount } from 'viem/accounts';
import { addressToEmptyAccount } from '@zerodev/sdk';
import * as dotenv from 'dotenv';
dotenv.config();
async function main() {
// Owner: full Signer (signs the workflow and session keys)
const owner = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
// Executor address is provided by the SDK — no user configuration needed
const executorAddress = getDittoExecutorAddress();
const storage = new IpfsStorage(process.env.IPFS_SERVICE_URL!);
// WorkflowBuilder gets Account (address only), not Signer
const workflow = WorkflowBuilder.create(addressToEmptyAccount(owner.address))
.addCronTrigger('0 */6 * * *') // Every 6 hours
.setCount(10) // Max 10 executions
.setValidUntil(Date.now() + 30 * 24 * 60 * 60 * 1000) // 30 days
.addJob(
JobBuilder.create('my-job')
.setChainId(ChainId.BASE_SEPOLIA)
.addStep({
target: '0xRecipientAddress',
abi: '', // Empty ABI = raw ETH transfer
args: [],
value: BigInt(1e15) // 0.001 ETH in wei
})
.build()
)
.build();
const { ipfsHash, userOpHashes } = await submitWorkflow(
workflow,
executorAddress, // Public address, not a key
storage,
owner, // Owner signs here
false, // prodContract: false = testnet
process.env.IPFS_SERVICE_URL!,
);
console.log('Deployed! IPFS hash:', ipfsHash);
console.log('Transaction receipts:', userOpHashes);
}
main().catch(console.error);
IMPORTANT: The Ditto SDK uses ZeroDev smart accounts (account abstraction). The smart account address is different from the owner's EOA wallet address. It is deterministically derived from the owner's private key by the ZeroDev kernel.
When submitWorkflow runs, it registers the workflow on-chain from this smart account. The smart account must have ETH on the target chain to pay for gas.
How to find the smart account address: Run the workflow script — if underfunded, the error message will include the smart account address (e.g., AA21 didn't pay prefund). Alternatively, add this before submitWorkflow:
import { signerToEcdsaValidator } from '@zerodev/ecdsa-validator';
import { createKernelAccount } from '@zerodev/sdk';
import { createPublicClient, http } from 'viem';
import { getChainConfig } from '@ditto/workflow-sdk';
const chainConfig = getChainConfig(process.env.IPFS_SERVICE_URL!);
const chain = chainConfig[ChainId.BASE_SEPOLIA]; // use your target chain
const publicClient = createPublicClient({ chain: chain.chain, transport: http(chain.rpcUrl) });
const ecdsaValidator = await signerToEcdsaValidator(publicClient, { signer: owner, entryPoint: { address: '0x0000000071727De22E5E9d8BAf0edAc6f37da032', version: '0.7' } });
const kernelAccount = await createKernelAccount(publicClient, { plugins: { sudo: ecdsaValidator }, entryPoint: { address: '0x0000000071727De22E5E9d8BAf0edAc6f37da032', version: '0.7' } });
console.log('Smart account address (fund this):', kernelAccount.address);
Funding:
CRITICAL: Always recommend testnet first. Only proceed to production chains after the user has verified the workflow works on testnet.
npx ts-node your-workflow-script.ts
Expected output: IPFS hash and transaction receipt(s). The Ditto Network will now automatically execute this workflow according to the triggers. If submission fails, check the Troubleshooting section.
Testnet (use for development):
| Chain | ChainId Enum | ID |
|---|---|---|
| Ethereum Sepolia | ChainId.SEPOLIA | 11155111 |
| Base Sepolia | ChainId.BASE_SEPOLIA | 84532 |
Production:
| Chain | ChainId Enum | ID |
|---|---|---|
| Base | ChainId.BASE | 8453 |
| Arbitrum | ChainId.ARBITRUM | 42161 |
| Polygon | ChainId.POLYGON | 137 |
| Optimism | ChainId.OPTIMISM | 10 |
| Ethereum Mainnet | ChainId.MAINNET | 1 |
Note: ChainId.HOLESKY (17000) exists in the enum but is deprecated and should not be used for new workflows.
CRITICAL: NEVER deploy to production chains (Base, Arbitrum, Polygon, Optimism, Mainnet) without explicit user confirmation. Always default to testnet. When deploying to production, set prodContract: true in submitWorkflow.
.addCronTrigger('*/5 * * * *') // Every 5 minutes (UTC)
.addEventTrigger({
chainId: ChainId.SEPOLIA,
contractAddress: '0xTokenAddress',
signature: 'Transfer(address,address,uint256)',
filter: { from: '0xSpecificSender' } // Optional: filter indexed params
})
import { OnchainConditionOperator } from '@ditto/workflow-sdk';
.addOnchainTrigger({
chainId: ChainId.BASE,
target: '0xOracleAddress',
abi: 'latestAnswer() view returns (int256)',
args: [],
onchainCondition: {
condition: OnchainConditionOperator.GREATER_THAN,
value: 200000000000n // e.g., ETH > $2000 (8 decimals)
}
})
Multiple triggers are AND-ed: all must be satisfied for execution.
OnchainConditionOperator values: EQUAL (0), GREATER_THAN (1), LESS_THAN (2), GREATER_THAN_OR_EQUAL (3), LESS_THAN_OR_EQUAL (4), NOT_EQUAL (5), ONE_OF (6).
Simulation is typically performed by the Ditto Network operators, not by clients. If you need to simulate locally for debugging, use executeFromIpfs with simulate: true — but note this requires an executor account with signing capability (for local testing only).
import { WorkflowContract } from '@ditto/workflow-sdk';
const wfContract = new WorkflowContract(process.env.WORKFLOW_CONTRACT_ADDRESS as `0x${string}`);
await wfContract.cancelWorkflow(ipfsHash, ownerAccount, chainId, process.env.IPFS_SERVICE_URL!);
Use the Ditto Network API (base URL: https://ipfs-service.dittonetwork.io) to monitor deployed workflows. All endpoints use the IPFS hash returned by submitWorkflow. No authentication required.
1. Workflow status — check if the workflow is active, paused, or cancelled:
const ipfsHash = 'QmYourWorkflowHash';
const res = await fetch(`https://ipfs-service.dittonetwork.io/workflow/status/${ipfsHash}`);
const status = await res.json();
console.log('Workflow status:', status);
2. Execution logs (USE THIS to check last executions) — returns the actual execution history with results, timestamps, and transaction details:
const res = await fetch(`https://ipfs-service.dittonetwork.io/workflow/logs/${ipfsHash}?limit=20`);
const logs = await res.json();
console.log('Execution logs:', logs);
This is the primary endpoint for checking whether a workflow has run, when it ran, and whether executions succeeded or failed.
3. Execution reports (advanced — NOT for checking execution history) — these are internal simulation reports sent by all network operator nodes participating in the workflow. Each operator independently simulates the workflow, so you'll see multiple reports per execution (one per node). This is useful for debugging network-level issues but NOT for checking whether your workflow actually executed:
const res = await fetch(`https://ipfs-service.dittonetwork.io/get-reports?ipfsHash=${ipfsHash}&page=1&limit=100`);
const reports = await res.json();
console.log('Node simulation reports:', reports);
IMPORTANT: When the user asks to "check last executions" or "see execution history", always use the execution logs endpoint (/workflow/logs/), NOT the reports endpoint. Reports show per-node simulation data, not actual execution outcomes.
import { dataRef } from '@ditto/workflow-sdk';
const ethPrice = dataRef({
target: '0xChainlinkOracleAddress',
abi: 'latestRoundData() returns (uint80, int256, uint256, uint256, uint80)',
chainId: ChainId.SEPOLIA,
resultIndex: 1, // int256 price is the 2nd return value
});
// Use in a step arg - resolved dynamically at execution time by the network
.addStep({
target: '0xSwapRouter',
abi: 'swap(uint256)',
args: [ethPrice],
})
| Method | Purpose | Example |
|---|---|---|
.setCount(n) | Max total executions | .setCount(100) |
.setInterval(sec) | Min seconds between runs | .setInterval(300) |
.setValidAfter(date) | Start time (Date or ms) | .setValidAfter(Date.now()) |
.setValidUntil(date) | Expiration (Date or ms) | .setValidUntil(Date.now() + 86400000) |
interface Step {
target: string; // Contract address (0x-prefixed)
abi: string; // Function signature, e.g. "transfer(address,uint256)"
// Empty string "" for raw ETH transfer
args: readonly any[]; // Function arguments (can include dataRef strings)
value?: bigint | string; // ETH value in wei
}
async function submitWorkflow(
workflow: Workflow,
executorAddress: `0x${string}`, // Public address of the Ditto Network executor
storage: IWorkflowStorage,
owner: Signer, // Owner signs (from privateKeyToAccount)
prodContract: boolean, // true = mainnet registry, false = testnet
ipfsServiceUrl: string,
usePaymaster?: boolean, // Default: false
switchChain?: (chainId: number) => Promise<void>,
accessToken?: string,
): Promise<{ ipfsHash: string; userOpHashes: UserOperationReceipt[] }>;
async function executeFromIpfs(
ipfsHash: string,
storage: IWorkflowStorage,
executorAccount: Signer, // Executor's Signer — held by network operators only
prodContract: boolean,
ipfsServiceUrl: string,
simulate?: boolean,
usePaymaster?: boolean,
accessToken?: string,
): Promise<{ success: boolean; results: any[] }>;
A workflow can have multiple jobs on different chains:
.addJob(
JobBuilder.create('job-sepolia')
.setChainId(ChainId.SEPOLIA)
.addStep({ /* ... */ })
.build()
)
.addJob(
JobBuilder.create('job-base')
.setChainId(ChainId.BASE)
.addStep({ /* ... */ })
.build()
)
Each job gets its own session key and on-chain registration.
Steps within a single job execute atomically:
JobBuilder.create('weekly-dca')
.setChainId(ChainId.BASE)
.addStep({
target: tokenAddress,
abi: 'approve(address,uint256)',
args: [routerAddress, amount],
})
.addStep({
target: routerAddress,
abi: 'swapExactTokensForETH(uint256,uint256,address[],address,uint256)',
args: [amount, 0, [tokenAddress, wethAddress], owner.address, deadline],
})
.build()
Note: Time-dependent args like deadline are computed at script build time, not execution time. For workflows that may execute later, use generous deadlines or dataRef for on-chain timestamps.
BEFORE calling submitWorkflow, verify:
target address (0x-prefixed, 42 chars)abi is a valid Solidity function signature or empty string for raw ETH transferchainId is from the supported chains listcount is > 0 if setvalidUntil is in the future.env has PRIVATE_KEY and IPFS_SERVICE_URLCause: .env file missing or incomplete.
Solution: Ensure PRIVATE_KEY and IPFS_SERVICE_URL are set. The executor address is provided by the SDK via getDittoExecutorAddress() — do NOT add it to .env.
Cause: setChainId() not called on JobBuilder.
Solution: Add .setChainId(ChainId.BASE_SEPOLIA) before .build().
Cause: No steps added to a job.
Solution: Add at least one .addStep({...}) call.
Cause: setValidUntil was given a past timestamp.
Solution: Use Date.now() + duration_in_ms.
Cause: The ZeroDev smart account doesn't have enough ETH to pay for gas. The smart account address is different from the owner's EOA — it's derived deterministically from the owner's private key. Solution: Send ETH to the smart account address shown in the error on the target chain. See "Step 3: Fund the Smart Account" above. For testnet, use a faucet. For production, 0.005–0.01 ETH is typically enough.
Causes:
Solution: Ensure the owner's smart account is funded on the target chain. Verify contract args are correct.
Cause: IPFS_SERVICE_URL unreachable or invalid.
Solution: Verify the URL is correct and accessible. Default: https://ipfs-service.dittonetwork.io