Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Recap & Q&A

Welcome back to Day 2! Let’s recap what we learned yesterday and address any questions.

Day 1 Recap

What We Built

Yesterday, we built a market creation workflow:

HTTP Request ──▶ CRE Workflow ──▶ PredictionMarket.sol
(question)       (HTTP Trigger)   (createMarket)

Key Concepts Covered

ConceptWhat We Learned
CRE Mental ModelWorkflows, Triggers, Capabilities, DONs
Project Structureproject.yaml, workflow.yaml, config.json
HTTP TriggerReceiving external HTTP requests
EVM WriteThe two-step pattern (report → writeReport)

The Two-Step Write Pattern

This is the most important pattern from Day 1:

// Step 1: Encode and sign the data
const reportResponse = runtime
  .report({
    encodedPayload: hexToBase64(reportData),
    encoderName: "evm",
    signingAlgo: "ecdsa",
    hashingAlgo: "keccak256",
  })
  .result();

// Step 2: Write to the contract
const writeResult = evmClient
  .writeReport(runtime, {
    receiver: contractAddress,
    report: reportResponse,
    gasConfig: { gasLimit: "500000" },
  })
  .result();

Today’s Agenda

Today we’ll complete the prediction market with:

  1. Log Trigger - React to on-chain events
  2. EVM Read - Read state from smart contracts
  3. HTTP Capability - Call Gemini AI
  4. Complete Flow - Wire everything together

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                      Day 2: Market Settlement                   │
│                                                                 │
│   requestSettlement() ──▶ SettlementRequested Event             │
│                                   │                             │
│                                   ▼                             │
│                           CRE Log Trigger                       │
│                                   │                             │
│                    ┌──────────────┼───────────────────┐         │
│                    ▼              ▼                   ▼         │
│              EVM Read         Gemini AI           EVM Write     │
│           (market data)   (determine outcome)  (settle market)  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Common Questions from Day 1

Q: Why do we need the two-step write pattern?

A: The two-step pattern provides:

  • Security: The report is cryptographically signed by the DON
  • Verification: Your contract can verify the signature came from CRE
  • Consensus: Multiple nodes agree on the data before signing

Q: What happens if my transaction fails?

A: Check:

  1. Your wallet has enough ETH for gas
  2. The contract address is correct
  3. The gas limit is sufficient
  4. The contract function accepts the encoded data

Q: How do I debug workflow issues?

A: Use runtime.log() liberally:

runtime.log(`[DEBUG] Value: ${JSON.stringify(data)}`);

All logs appear in the simulation output.

Q: Can I have multiple triggers in one workflow?

A: Yes! That’s exactly what we’ll do today. A workflow can have up to 10 triggers.

const initWorkflow = (config: Config) => {
  return [
    cre.handler(httpTrigger, onHttpTrigger),
    cre.handler(logTrigger, onLogTrigger),
  ];
};

Quick Environment Check

Before we continue, let’s verify everything is set up:

# Check CRE authentication
cre whoami

# From the prediction-market directory
source .env

export MARKET_ADDRESS=0xYOUR_CONTRACT_ADDRESS

# Verify you have markets created (decoded output)
cast call $MARKET_ADDRESS \
  "getMarket(uint256) returns ((address,uint48,uint48,bool,uint16,uint8,uint256,uint256,string))" \
  0 \
  --rpc-url "https://ethereum-sepolia-rpc.publicnode.com"

Ready for Day 2!

Let’s dive into Log Triggers and build the settlement workflow.