Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/content/cre/getting-started/before-you-build-ts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ date: Last Modified
pageId: "getting-started-before-you-build"
sdkLang: "ts"
metadata:
description: "Essential tips before building your own CRE workflows: library compatibility, Solidity integers, working with time, and next steps for production deployment."
description: "Essential tips before building your own CRE workflows: library compatibility, Solidity integers, safe decimal scaling, working with time, and next steps for production deployment."
datePublished: "2026-02-04"
lastModified: "2026-02-04"
---
Expand Down Expand Up @@ -53,6 +53,44 @@ const amount = 10000000000000001n // Stays exactly 10000000000000001
See [Writing Data Onchain](/cre/guides/workflow/using-evm-client/onchain-write/writing-data-onchain#step-2-abi-encode-your-value) for the complete Solidity-to-TypeScript type mapping.
</Aside>

## Safe decimal scaling

Smart contracts use fixed-point integers (e.g., 18 decimals for ERC-20 tokens). When converting between human-readable values and their onchain representations, avoid floating-point arithmetic — it causes **silent precision loss**.

Use <a href="https://viem.sh/docs/utilities/parseUnits" target="_blank">viem's `parseUnits()`</a> to scale up and <a href="https://viem.sh/docs/utilities/formatUnits" target="_blank">`formatUnits()`</a> to scale down. Both operate on strings, so no floating-point math is involved.

### Scaling up (human-readable to onchain)

```typescript
import { parseUnits } from "viem"

// WRONG - floating-point precision loss
const amount = BigInt(0.1 * 1e18)
// Result: 99999999999999984n (off by 16)

// CORRECT - string-based, no precision loss
const amount = parseUnits("0.1", 18)
// Result: 100000000000000000n (exact)
```

### Scaling down (onchain to human-readable)

```typescript
import { formatUnits } from "viem"

// WRONG - precision loss for large bigints
const display = Number(totalSupply) / 1e18

// CORRECT - string-based, no precision loss
const display = formatUnits(totalSupply, 18)
// Returns "1.5" for 1500000000000000000n
```

{/* prettier-ignore */}
<Aside type="tip" title="Works with any decimal count">
Both functions accept any decimal count, not just 18. For example, USDC uses 6 decimals: `parseUnits("100.50", 6)` returns `100500000n`.
</Aside>

## Working with time

If your workflow uses timestamps (e.g., for API authentication or time-based queries), use `runtime.now()` instead of `Date.now()`. This ensures all nodes in the DON use the same timestamp and can reach consensus.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ Viem automatically handles type conversions:
| `string` | `string` |
| `bytes`, `bytes32`, etc. | `Uint8Array` |

{/* prettier-ignore */}
<Aside type="caution" title="Use safe scaling for decimal conversions">
When converting values read from contracts (e.g., token balances, prices) to human-readable formats, use <a href="https://viem.sh/docs/utilities/formatUnits" target="_blank">viem's `formatUnits()`</a> instead of `Number(value) / 1e18`. Floating-point division causes silent precision loss for large `bigint` values. See [Safe decimal scaling](/cre/getting-started/before-you-build-ts#safe-decimal-scaling) for details and examples.
</Aside>

## Complete example with configuration

Here's a full runnable workflow with external configuration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ const amount = 10000000000000001n // Stays exactly 10000000000000001

</Aside>

{/* prettier-ignore */}
<Aside type="caution" title="Use safe scaling for decimal values">
When scaling values to match a token's decimals (e.g., converting `"1.5"` to `1500000000000000000n`), use <a href="https://viem.sh/docs/utilities/parseUnits" target="_blank">viem's `parseUnits()`</a> instead of `BigInt(value * 1e18)`. Floating-point multiplication causes silent precision loss. See [Safe decimal scaling](/cre/getting-started/before-you-build-ts#safe-decimal-scaling) for details and examples.
</Aside>

### Step 3: Generate the signed report

Convert the encoded data to base64 and generate a report:
Expand Down
222 changes: 111 additions & 111 deletions src/content/cre/llms-full-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7486,117 +7486,6 @@ Learn more about how to use CRE capabilities with built-in consensus:

---

# CRE Templates
Source: https://docs.chain.link/cre/templates
Last Updated: 2025-11-21

Templates are workflow examples you can copy and customize for your own projects. They are curated and maintained by Chainlink, offering patterns you can use as references or starting points for your own workflows.


<Aside type="caution" title="Educational Example Disclaimer">
This page includes educational examples to use a Chainlink system, product, or service and is provided to
demonstrate how to interact with Chainlink's systems, products, and services to integrate them into your own. This
template is provided "AS IS" and "AS AVAILABLE" without warranties of any kind, it has not been audited, and it may be
missing key checks or error handling to make the usage of the system, product or service more clear. Do not use the
code in this example in a production environment without completing your own audits and application of best practices.
Neither Chainlink Labs, the Chainlink Foundation, nor Chainlink node operators are responsible for unintended outputs
that are generated due to errors in code.
</Aside>

## Building Blocks and Starter Templates

Templates are organized into two categories:

### 1. Building Blocks

[Building Blocks](https://github.com/smartcontractkit/cre-templates/tree/main/building-blocks) are small, focused examples that teach **one concept at a time**. Each Building Block is self-contained and demonstrates a specific CRE capability or pattern. They feature minimal code, clear configuration, and are runnable locally with `cre workflow simulate`.

**Available Building Blocks:**

1. **`kv-store`** (Key-Value Store with AWS S3)
- Reads a value from an AWS S3 object, increments it, and writes it back
- Demonstrates: SigV4-signed HTTP requests, CRE secrets (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`), and consensus read → single write flow
- Use case: Learn offchain write patterns with secrets and consensus

2. **`read-data-feeds`** (Chainlink Data Feeds)
- Reads `decimals()` and `latestAnswer()` from <a href="https://docs.chain.link/data-feeds" target="_blank" rel="noopener noreferrer">Chainlink Data Feeds</a> on a cron schedule
- Demonstrates: Contract ABIs, Go bindings generation, RPC configuration, and onchain reads
- Use case: Learn how to read onchain data via contract calls

**When to use Building Blocks:**

- You want to learn a specific feature quickly (e.g., secrets + HTTP signing, reading a data feed, cron triggers)
- You need a focused code snippet to copy into your project
- You're exploring a new capability before integrating it into a larger workflow

### 2. Starter Templates

[Starter Templates](https://github.com/smartcontractkit/cre-templates/tree/main/starter-templates) are complete, end-to-end workflows that combine multiple capabilities and mirror real-world use cases. These templates are more comprehensive than Building Blocks and include production-like configuration, optional precompiled smart contracts, and generated bindings. They can be adapted directly into your own projects.

**Available Starter Templates:**

1. **`custom-data-feed`** (Custom Data Feed)
- Periodically fetches offchain data via HTTP and pushes updates onchain
- Demonstrates: Cron scheduling, secrets management, contract bindings, and blockchain writes
- Use case: Build data feeds that combine offchain APIs with onchain smart contracts

2. **`bring-your-own-data`** (BYOD - NAV & PoR)
- End-to-end examples for publishing Net Asset Value (NAV) and Proof of Reserve (PoR) data onchain
- Demonstrates: Complete workflow with demo contracts for publishing institutional data
- Use case: Publish verified financial or reserve data to smart contracts

3. **`multi-chain-token-manager`** (Multi-Chain Token Manager)
- Orchestrates token operations and state across multiple blockchain networks
- Demonstrates: Multi-chain RPC configuration, bindings, and cross-chain coordination patterns
- Use case: Manage token operations across different EVM chains from a single workflow

**When to use Starter Templates:**

- You want a runnable reference architecture that shows production-ready patterns
- You're starting a new project and need a solid foundation
- You want to see how multiple CRE capabilities integrate in a real workflow

### How to access templates

All templates are available on GitHub at [github.com/smartcontractkit/cre-templates](https://github.com/smartcontractkit/cre-templates)

- Browse all Building Blocks and Starter Templates
- Clone or fork templates to customize them for your use case
- View individual READMEs for detailed usage instructions

<Aside type="tip" title="Quick Start: Custom Data Feed via CLI">
The **Custom Data Feed** starter template is also available through `cre init` for a guided, interactive setup
experience. We provide a step-by-step guide: [Running a Demo Workflow](/cre/templates/running-demo-workflow).
</Aside>

## When to use what

Choose the right template based on where you are in your CRE journey:

| Your Situation | Recommended Action |
| ------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
| Just finished the [Getting Started guide](/cre/getting-started/overview) | Run the **Custom Data Feed** template to see a more complex, real-world example |
| Want to learn offchain writes with secrets and consensus | Clone the **`kv-store`** Building Block (AWS S3 example) |
| Need to read Chainlink Data Feeds in your workflow | Clone the **`read-data-feeds`** Building Block (onchain reads with ABIs) |
| Building a custom data feed for your protocol | Fork the **`custom-data-feed`** Starter Template and customize it |
| Publishing NAV or PoR data onchain | Fork the **`bring-your-own-data`** Starter Template (includes demo contracts) |
| Managing tokens across multiple chains | Fork the **`multi-chain-token-manager`** Starter Template (cross-chain patterns) |

## Next steps

Ready to explore templates?

- **[Run the Custom Data Feed Template](/cre/templates/running-demo-workflow)** - Walk through this template with our step-by-step guide (available in Go and TypeScript)

- **[Browse all templates on GitHub](https://github.com/smartcontractkit/cre-templates)** - Explore the full collection of Building Blocks and Starter Templates

<Aside type="note" title="Contributing">
The CRE Templates repository is open source under the MIT license. If you've built a useful workflow pattern and want
to share it with the community, consider contributing.
</Aside>

---

# AI-Powered Prediction Market
Source: https://docs.chain.link/cre/demos/prediction-market
Last Updated: 2025-11-21
Expand Down Expand Up @@ -15995,6 +15884,117 @@ For forwarder contract addresses and chain names, see the [Forwarder Directory](

---

# CRE Templates
Source: https://docs.chain.link/cre/templates
Last Updated: 2025-11-21

Templates are workflow examples you can copy and customize for your own projects. They are curated and maintained by Chainlink, offering patterns you can use as references or starting points for your own workflows.


<Aside type="caution" title="Educational Example Disclaimer">
This page includes educational examples to use a Chainlink system, product, or service and is provided to
demonstrate how to interact with Chainlink's systems, products, and services to integrate them into your own. This
template is provided "AS IS" and "AS AVAILABLE" without warranties of any kind, it has not been audited, and it may be
missing key checks or error handling to make the usage of the system, product or service more clear. Do not use the
code in this example in a production environment without completing your own audits and application of best practices.
Neither Chainlink Labs, the Chainlink Foundation, nor Chainlink node operators are responsible for unintended outputs
that are generated due to errors in code.
</Aside>

## Building Blocks and Starter Templates

Templates are organized into two categories:

### 1. Building Blocks

[Building Blocks](https://github.com/smartcontractkit/cre-templates/tree/main/building-blocks) are small, focused examples that teach **one concept at a time**. Each Building Block is self-contained and demonstrates a specific CRE capability or pattern. They feature minimal code, clear configuration, and are runnable locally with `cre workflow simulate`.

**Available Building Blocks:**

1. **`kv-store`** (Key-Value Store with AWS S3)
- Reads a value from an AWS S3 object, increments it, and writes it back
- Demonstrates: SigV4-signed HTTP requests, CRE secrets (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`), and consensus read → single write flow
- Use case: Learn offchain write patterns with secrets and consensus

2. **`read-data-feeds`** (Chainlink Data Feeds)
- Reads `decimals()` and `latestAnswer()` from <a href="https://docs.chain.link/data-feeds" target="_blank" rel="noopener noreferrer">Chainlink Data Feeds</a> on a cron schedule
- Demonstrates: Contract ABIs, Go bindings generation, RPC configuration, and onchain reads
- Use case: Learn how to read onchain data via contract calls

**When to use Building Blocks:**

- You want to learn a specific feature quickly (e.g., secrets + HTTP signing, reading a data feed, cron triggers)
- You need a focused code snippet to copy into your project
- You're exploring a new capability before integrating it into a larger workflow

### 2. Starter Templates

[Starter Templates](https://github.com/smartcontractkit/cre-templates/tree/main/starter-templates) are complete, end-to-end workflows that combine multiple capabilities and mirror real-world use cases. These templates are more comprehensive than Building Blocks and include production-like configuration, optional precompiled smart contracts, and generated bindings. They can be adapted directly into your own projects.

**Available Starter Templates:**

1. **`custom-data-feed`** (Custom Data Feed)
- Periodically fetches offchain data via HTTP and pushes updates onchain
- Demonstrates: Cron scheduling, secrets management, contract bindings, and blockchain writes
- Use case: Build data feeds that combine offchain APIs with onchain smart contracts

2. **`bring-your-own-data`** (BYOD - NAV & PoR)
- End-to-end examples for publishing Net Asset Value (NAV) and Proof of Reserve (PoR) data onchain
- Demonstrates: Complete workflow with demo contracts for publishing institutional data
- Use case: Publish verified financial or reserve data to smart contracts

3. **`multi-chain-token-manager`** (Multi-Chain Token Manager)
- Orchestrates token operations and state across multiple blockchain networks
- Demonstrates: Multi-chain RPC configuration, bindings, and cross-chain coordination patterns
- Use case: Manage token operations across different EVM chains from a single workflow

**When to use Starter Templates:**

- You want a runnable reference architecture that shows production-ready patterns
- You're starting a new project and need a solid foundation
- You want to see how multiple CRE capabilities integrate in a real workflow

### How to access templates

All templates are available on GitHub at [github.com/smartcontractkit/cre-templates](https://github.com/smartcontractkit/cre-templates)

- Browse all Building Blocks and Starter Templates
- Clone or fork templates to customize them for your use case
- View individual READMEs for detailed usage instructions

<Aside type="tip" title="Quick Start: Custom Data Feed via CLI">
The **Custom Data Feed** starter template is also available through `cre init` for a guided, interactive setup
experience. We provide a step-by-step guide: [Running a Demo Workflow](/cre/templates/running-demo-workflow).
</Aside>

## When to use what

Choose the right template based on where you are in your CRE journey:

| Your Situation | Recommended Action |
| ------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
| Just finished the [Getting Started guide](/cre/getting-started/overview) | Run the **Custom Data Feed** template to see a more complex, real-world example |
| Want to learn offchain writes with secrets and consensus | Clone the **`kv-store`** Building Block (AWS S3 example) |
| Need to read Chainlink Data Feeds in your workflow | Clone the **`read-data-feeds`** Building Block (onchain reads with ABIs) |
| Building a custom data feed for your protocol | Fork the **`custom-data-feed`** Starter Template and customize it |
| Publishing NAV or PoR data onchain | Fork the **`bring-your-own-data`** Starter Template (includes demo contracts) |
| Managing tokens across multiple chains | Fork the **`multi-chain-token-manager`** Starter Template (cross-chain patterns) |

## Next steps

Ready to explore templates?

- **[Run the Custom Data Feed Template](/cre/templates/running-demo-workflow)** - Walk through this template with our step-by-step guide (available in Go and TypeScript)

- **[Browse all templates on GitHub](https://github.com/smartcontractkit/cre-templates)** - Explore the full collection of Building Blocks and Starter Templates

<Aside type="note" title="Contributing">
The CRE Templates repository is open source under the MIT license. If you've built a useful workflow pattern and want
to share it with the community, consider contributing.
</Aside>

---

# Running a Demo Workflow
Source: https://docs.chain.link/cre/templates/running-demo-workflow-go
Last Updated: 2025-11-04
Expand Down
Loading
Loading