Examples / 示例大全
This guide gives concrete examples for the most common Morpheus combinations:
- user contracts calling the Oracle
- passing encrypted parameters
- calling built-in compute functions
- calling built-in functions with encrypted payloads
- calling Oracle with custom functions
- calling Oracle with custom functions plus encrypted params
- reading on-chain pricefeed data
- browser and Node encryption helpers
本指南给出 Morpheus 常见组合的可直接参考示例:
- 用户合约如何调用预言机
- 如何传递加密参数
- 如何调用内置计算函数
- 如何调用“内置函数 + 加密参数”
- 如何调用“预言机 + 自定义函数”
- 如何调用“预言机 + 自定义函数 + 加密参数”
- 如何读取链上 pricefeed
- 浏览器端和 Node 端加密示例
Concrete files also live under:
对应的可复制文件也已经放在:
Who Should Read Which Sections / 不同角色应该看哪几节
dApp user / 普通 dApp 用户
Read these first:
优先看这些:
- Section 3: Browser Encryption Helper
- Section 7: Oracle + Encrypted Token
- Section 8: Oracle + Builtin Provider + Encrypted Params
- Section 10: Compute: Builtin Function + Encrypted Payload
- Section 14: Compute + WASM
- Section 15: Oracle + WASM
- Section 16: Oracle + WASM + Encrypted Params
- Section 22: Quick Combination Matrix
Contract developer / 合约开发者
Read these first:
优先看这些:
- Section 2: Callback Shape
- Section 5: Neo X User Contract: Basic Oracle Request
- Section 6: Neo N3 User Contract: Basic Oracle Request
- Section 17: PriceFeed Read: Neo X
- Section 18: PriceFeed Read: Neo N3
- Section 19: Feed Pair Discovery
- Section 20: Operator Feed Sync Behavior
Operator / 运维与部署人员
Read these first:
优先看这些:
- Section 1: Routing Map
- Section 19: Feed Pair Discovery
- Section 20: Operator Feed Sync Behavior
- Section 21: Preferred Patterns
- Section 22: Quick Combination Matrix
Security reviewer / 安全审计人员
Read these first:
优先看这些:
- Section 1: Routing Map
- Section 2: Callback Shape
- Section 11: Oracle + Custom JS Function
- Section 12: Oracle + Custom JS Function + Encrypted Params
- Section 13: Compute + Custom JS Function
- Section 14: Compute + WASM
- Section 15: Oracle + WASM
Fast Scenario Index / 场景快速索引
If your question is “how do I do X?”, use this shortcut table:
如果你的问题是“我到底该怎么做 X?”,先看这张捷径表:
| Goal / 目标 | Start with / 先看 |
|---|---|
| Trigger a one-off Oracle price callback / 发起一次 Oracle 价格回调 | Section 5, 6, 7 |
| Call a private API with an encrypted token / 用加密 token 调私有 API | Section 3, 7 |
| Keep function name and inputs encrypted / 把函数名和输入一起加密 | Section 3, 10 |
| Run a custom Oracle reduction inside TEE / 在 TEE 里跑自定义 Oracle 逻辑 | Section 11 |
| Run a stronger isolated workload / 使用更强隔离的执行模型 | Section 14, 15, 16 |
| Read on-chain feed state from my contract / 在用户合约里读取 pricefeed | Section 17, 18, 19 |
| Understand automatic feed synchronization / 理解自动 pricefeed 同步 | Section 20 |
| Decide between JS and WASM / 在 JS 与 WASM 之间做选择 | Section 21 |
| Register automation jobs / 注册自动化任务 | Section 23 |
1. Routing Map / 路由映射
The on-chain request only knows
and . The relayer decides which worker route to use from .链上请求只知道
和 。 真正走哪个 worker 路由,是 relayer 根据 决定的。| example | Worker route | Meaning |
|---|---|---|
| Privacy oracle fetch and optional reduction | ||
| Same as above | ||
| Off-chain compute | ||
| Same compute route | ||
| Operator-only feed sync / publish | ||
| Randomness |
Important:
重要:
-
End users should not submit
requests through the Oracle contract. -
Feed synchronization is an internal operator workflow.
-
Users read the synchronized on-chain feed contracts directly.
-
终端用户不应通过 Oracle 合约提交
请求。 -
Feed 同步属于内部运维流程。
-
用户应直接读取链上已同步的 feed 合约数据。
2. Callback Shape / 回调形态
Every successful fulfill writes
bytes that contain UTF-8 JSON:每次成功 fulfill 的
都是 UTF-8 JSON:
Important note:
重要说明:
-
On-chain parsing of full JSON is usually too expensive.
-
The normal pattern is: user contract stores raw callback bytes, and off-chain services parse them.
-
If you want simpler on-chain consumption, design the custom Oracle/compute function to return a very small scalar result.
-
链上直接解析完整 JSON 通常太贵。
-
常见做法是:用户合约先保存原始 callback bytes,链下服务再解析。
-
如果你希望链上更容易消费,就让自定义 Oracle/compute 函数只返回很小的标量值。
3. Browser Encryption Helper / 浏览器端加密辅助
Use the Oracle public key first:
先取 Oracle 公钥:
Encrypt any secret or confidential JSON patch. Recommended format:
.加密任意 secret 或 confidential JSON patch:
4. Node Encryption Helper / Node 端加密辅助
5. Neo X User Contract: Basic Oracle Request / Neo X 用户合约:基础预言机请求
6. Neo N3 User Contract: Basic Oracle Request / Neo N3 用户合约:基础预言机请求
Fee note:
手续费说明:
-
Neo N3 currently consumes prepaid Oracle credit at
per request. -
Neo X reference contracts use
and . -
Neo N3 现在每次请求消耗预存的
Oracle credit。 -
Neo X 参考合约通过
和 支付。
7. Oracle + Encrypted Token / 预言机 + 加密 token
Use this when only the auth token is secret.
当只有认证 token 需要保密时,使用这个组合。
Payload before encryption:
加密前明文:
The caller contract still just passes UTF-8 JSON bytes.
调用方合约仍然只是传 UTF-8 JSON bytes。
8. Oracle + Builtin Provider + Encrypted Params / 内置 Provider + 加密参数
Use this when provider id is public, but some request-specific fields are secret.
当 provider 本身公开,但某些参数需要保密时,使用这个组合。
Plain public payload:
公开部分:
Encrypted JSON patch example:
加密 patch 示例:
9. Compute: Builtin Function / 内置计算函数
From contract, use
:合约侧用
:10. Compute: Builtin Function + Encrypted Payload / 内置计算 + 加密完整载荷
Public payload:
公开部分:
Encrypted JSON patch:
加密 patch:
This is the cleanest pattern when even the function name should stay private.
如果连函数名都要保密,这是最干净的做法。
11. Oracle + Custom JS Function / 预言机 + 自定义 JS 函数
Important:
重要:
-
This path only works if
. -
By default it is disabled.
-
只有
时这条路径才可用。 -
默认是关闭的。
12. Oracle + Custom JS Function + Encrypted Params / 预言机 + 自定义 JS + 加密参数
Public payload:
公开部分:
Encrypted JSON patch:
加密 patch:
13. Compute + Custom JS Function / 计算 + 自定义 JS 函数
14. Compute + WASM / 计算 + WASM
WASM is now the preferred path for stronger isolation.
WASM 现在是更推荐的路径,因为隔离性更强。
Current worker runtime defaults:
当前运行时默认值:
15. Oracle + WASM / 预言机 + WASM
Use this when you want fetch + isolated compute in one trusted step.
当你想把“拉取数据 + 隔离计算”放在同一个 TEE 步骤里时,使用这个组合。
WASM input to the module is:
传给 WASM 模块的输入是:
16. Oracle + WASM + Encrypted Params / 预言机 + WASM + 加密参数
Public payload:
Encrypted JSON patch:
17. PriceFeed Read: Neo X / 读取 Neo X PriceFeed
18. PriceFeed Read: Neo N3 / 读取 Neo N3 PriceFeed
19. Feed Pair Discovery / 枚举 feed 列表
Neo X
Neo N3
20. Operator Feed Sync Behavior / 运维侧 Feed 同步行为
This is an operator-only route. End users should not submit
requests through the Oracle contract.这是运维内部路由,终端用户不应通过 Oracle 合约提交
请求。
This route is used by the relayer / operator automation layer and should not be exposed as a user contract flow.
这个路由属于 relayer / 运维自动化层,不应暴露成用户合约调用路径。
Current sync behavior:
当前同步行为:
- every 60 seconds, scan all configured feed pairs
- compare each pair with the current on-chain stored value for that storage pair
- only pairs whose change is greater than 0.1% are selected for on-chain publication
- if no pair crosses the threshold, no chain transaction is sent
- if multiple pairs cross the threshold, they are grouped into one batch transaction per chain
21. Preferred Patterns / 推荐组合
Recommended for most production private logic / 生产中最推荐
- Oracle + encrypted token
- Oracle + encrypted params
- Compute builtin + encrypted payload
- Oracle + WASM
- Oracle + WASM + encrypted params
Only if you explicitly accept the risk / 仅在明确接受风险时使用
- Oracle + custom JS script
- Compute + custom JS script
Those JS paths require:
这些 JS 路径要求:
22. Quick Combination Matrix / 快速组合矩阵
| Scenario | Key payload fields | |
|---|---|---|
| Oracle + builtin provider | , , | |
| Oracle + encrypted token | , , | |
| Oracle + encrypted params | or , | |
| Oracle + JS function | , optional | |
| Oracle + JS + encrypted params | , | |
| Oracle + WASM | , | |
| Oracle + WASM + encrypted params | , | |
| Compute builtin | , , | |
| Compute builtin + encrypted payload | or | |
| Compute JS script | , , , | |
| Compute WASM | , , | |
| Feed publish (operator-only) | , , | |
| Read feed on-chain | N/A | call on feed contract |
23. Automation Registration / 自动化任务注册
Automation control requests also go through the Oracle gateway.
自动化控制请求也走 Oracle 网关。
Registration request type:
注册类型:
Supported trigger types:
支持的触发器类型:
One-shot example:
一次性任务示例:
Interval example:
周期任务示例:
Note:
说明:
-
if you plan to cancel an interval automation before its first execution, set
in the future -
otherwise the scheduler may legitimately queue the first execution before your cancel request lands
-
如果你打算在第一次执行前取消 interval 自动化,请把
设为未来时间 -
否则调度器可能会在取消请求到达前,先合法地排队第一次执行
Price-threshold example:
价格阈值触发示例:
Fee model:
手续费模型:
-
each automation execution queues a normal Oracle request
-
every queued execution consumes the standard
-
Neo N3 uses prepaid GAS credits already stored in the Oracle contract
-
Neo X uses prepaid fee credits through
-
每次自动化执行都会排队成一个普通 Oracle 请求
-
每次排队执行都会消耗标准
-
Neo N3 直接消耗 Oracle 合约里的预存 GAS credit
-
Neo X 通过
预存手续费
Validated on Neo N3 mainnet:
已在 Neo N3 主网验证:
- one-shot register callback:
- one-shot queued execution callback:
- interval register callback:
- automation cancel callback:
- one-shot job reached Supabase status
- cancelled interval job reached Supabase status
24. Role-Based Playbooks / 按角色的工作流建议
Playbook A: dApp user who just wants a private answer / 只想拿到一个隐私结果的 dApp 用户
Recommended flow:
推荐流程:
- Read the Oracle public key from the on-chain Oracle contract. 先从链上 Oracle 合约读取 Oracle 公钥。
- Encrypt only the secret token if the rest of the request is public. 如果只有 token 保密,就只加密 token。
- Encrypt the whole JSON patch if even the function or parameters are sensitive. 如果连函数名或参数都敏感,就加密整段 JSON patch。
- Prefer Oracle + WASM over Oracle + custom JS. 优先使用 Oracle + WASM,不要优先用 Oracle + 自定义 JS。
- Parse callback bytes off-chain and only store critical booleans / scores on-chain. callback bytes 尽量链下解析,链上只保存关键布尔值 / 分数。
Best matching examples:
最匹配的示例:
- Section 7
- Section 8
- Section 15
- Section 16
Playbook B: contract developer who needs composable callbacks / 需要可组合 callback 的合约开发者
Recommended flow:
推荐流程:
- Keep your callback contract minimal. 让 callback consumer 保持最小化。
- Store raw callback bytes first. 先把原始 callback bytes 存下来。
- Avoid expensive on-chain JSON parsing. 不要在链上解析复杂 JSON。
- If you need simple on-chain branching, design the worker result to be a scalar string / bool. 如果需要链上分支判断,就让 worker 返回标量字符串 / 布尔值。
Best matching examples:
最匹配的示例:
- Section 2
- Section 5
- Section 6
- Section 17
- Section 18
Playbook C: operator who wants stable production usage / 想要稳定生产运行的运维
Recommended flow:
推荐流程:
- Default to builtin providers or WASM. 默认优先 builtin provider 或 WASM。
- Keep unset unless you truly need JS. 除非真的需要 JS,否则保持 不设置。
- Keep feed publication explicit and controlled. feed 发布要明确控制,不要随便暴露触发入口。
- Watch relayer retries and dead letters. 持续看 relayer retry 和 dead letter。
- Tune WASM timeout via env, not code. WASM 超时通过环境变量调,不要改代码。
Best matching examples:
最匹配的示例:
- Section 14
- Section 15
- Section 20
Playbook D: security-first integration / 安全优先集成
Recommended flow:
推荐流程:
- Prefer builtins or WASM. 优先 builtin 或 WASM。
- Avoid custom JS unless isolated and explicitly enabled. 除非明确开启且接受风险,否则避免自定义 JS。
- Use encrypted token for secret headers, encrypted payload for secret logic. secret header 用 ,secret logic 用 / 。
- Keep callbacks allowlisted and contracts minimal. 保持 callback allowlist 严格,consumer 合约保持最小。
- Verify TEE attestation off-chain on every critical flow. 关键流程都做链下 attestation 验证。
Best matching examples:
最匹配的示例:
- Section 7
- Section 10
- Section 14
- Section 15
- Section 16