Skip to main content
This guide walks you through building a Bitcoin price oracle that fetches the BTC/USD price every minute and writes it on-chain. It demonstrates cron triggers, contract codegen, external API calls, and collection storage.

How it works

  1. Cron trigger fires every minute
  2. CoinGecko API provides the current BTC/USD price
  3. On-chain contract receives the price via a typed contract class
  4. Collection stores the price for historical queries

Prerequisites

Project structure

Step 1: Set up the project

Clone the example repository:

Step 2: Generate contract types

The project includes a PriceOracle.json ABI in src/contracts/. Generate the typed contract class:
This creates a typed PriceOracle class in .compose/generated/ that provides type-safe contract interaction.

Step 3: Understand the task

The bitcoin-oracle.ts task handles everything:

Key Compose features used

  • context.fetch — HTTP requests with built-in retry and backoff
  • evm.wallet — managed wallet with gas sponsorship
  • evm.contracts.PriceOracle — typed contract class generated from ABI JSON via compose codegen
  • collection — persistent document storage for price history

Step 4: Configure the Compose app

The compose.yaml uses a cron trigger to run every minute:

Step 5: Run locally

--fork-chains allows you to run a smart wallet locally. You can also use a private key wallet for your local Compose app. See more here. The task runs immediately and then every minute. You should see logs showing the fetched price and transaction hash.

Step 6: Deploy to Goldsky

Customization

Change the price source

Replace the CoinGecko URL with any API that returns a JSON price:

Use your own contract

  1. Drop your contract’s ABI JSON into src/contracts/MyContract.json
  2. Run goldsky compose codegen to generate the typed class
  3. Use it in your task:

Change the cron schedule

Resources