Skip to content

Commit a44d059

Browse files
committed
refactor: improve clarity and formatting in Result Callback guide
1 parent e3aee48 commit a44d059

File tree

1 file changed

+48
-67
lines changed

1 file changed

+48
-67
lines changed
Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,95 @@
11
---
22
title: Result Callback Guide
3-
description: Guide to using the iExec result callback to push an off-chain iApp task result directly into a smart contract (price feeds, automation, triggers, proofs, scoring, etc.).
3+
description:
4+
Guide to using the iExec result callback to push an off-chain iApp task result
5+
directly into a smart contract (price feeds, automation, triggers, proofs,
6+
scoring, etc.).
47
---
58

69
# Result Callback
710

8-
This guide explains how to securely push an iExec task result into a smart contract using the callback mechanism.
9-
Oracles are only one of many possible use cases.
10-
11-
## When to Use a Callback
12-
11+
This guide explains how to securely push an iExec task result into a smart
12+
contract using the callback mechanism.
1313
Use a callback when a smart contract should:
1414

15-
- Ingest off-chain computed data (API aggregation, ML inference, analytics) and persist it on-chain
15+
- Ingest off-chain computed data (API aggregation, ML inference, analytics) and
16+
persist it on-chain
1617
- React to an execution outcome (conditional trigger, state transition)
1718
- Store a timestamped record (price feed, score, KPI, proof hash)
1819
- Act as a logic bridge between external systems and on-chain logic
1920

2021
## 🧩 High-Level Flow
2122

2223
1. A requester executes an iApp on iExec.
23-
2. The iApp writes `${IEXEC_OUT}/computed.json` with a `callback-data` field containing ABI‑encoded calldata.
24-
3. The iExec protocol, once the task is completed, invokes the specified callback contract with that data.
24+
2. The iApp writes `${IEXEC_OUT}/computed.json` with a `callback-data` field
25+
containing ABI‑encoded calldata.
26+
3. The iExec protocol, once the task is completed, invokes the specified
27+
callback contract with that data.
2528
4. Your callback smart contract (receiver) ingests the data.
2629

2730
## Step-by-Step Implementation
2831

2932
### Step 1: Write the iApp
3033

31-
The iApp MUST write a file named `computed.json` in the directory pointed to by `IEXEC_OUT`.
32-
Required key: `callback-data` (raw ABI‑encoded bytes you want passed to your contract).
34+
The iApp MUST write a file named `computed.json` in the directory pointed to by
35+
`IEXEC_OUT`.
36+
Required key: `callback-data` (raw ABI‑encoded bytes you want passed to your
37+
contract).
3338

3439
Replaced Web3.js example with ethers v6:
3540

36-
```js
37-
// ethers v6 example producing ABI-encoded callback data
38-
41+
```ts twoslash
3942
import { writeFileSync } from 'node:fs';
4043
import { AbiCoder } from 'ethers';
4144

42-
// Placeholder: replace with real price retrieval / aggregation logic
43-
async function fetchPrice(pair) {
44-
// e.g. query multiple APIs, median, etc.
45-
return 12345.6789;
46-
}
45+
const timestamp = Math.floor(Date.now() / 1000);
46+
const pair = 'BTC-USD';
47+
const scaled = '9';
48+
// ---cut---
4749

4850
async function main() {
49-
const [pair = 'BTC-USD', precision = '9'] = process.argv.slice(2);
50-
51-
const price = await fetchPrice(pair);
52-
const scale = 10n ** BigInt(Number(precision));
53-
const scaled = BigInt(Math.round(price * Number(scale)));
54-
55-
const timestamp = Math.floor(Date.now() / 1000);
51+
// Your business logic here
5652

5753
const abiCoder = new AbiCoder();
5854
const abiPayload = abiCoder.encode(
5955
['uint256', 'string', 'uint256'],
60-
[timestamp, `${pair}-${precision}`, scaled]
56+
[timestamp, `${pair}`, scaled]
6157
);
6258

6359
writeFileSync(
6460
`${process.env.IEXEC_OUT}/computed.json`,
65-
JSON.stringify(
66-
{
67-
'callback-data': abiPayload,
68-
metadata: {
69-
pair,
70-
precision,
71-
timestamp,
72-
}
73-
}
74-
)
61+
JSON.stringify({
62+
'callback-data': abiPayload,
63+
})
7564
);
7665
}
77-
78-
main().catch(() => process.exit(1));
7966
```
8067

8168
### Step 2: Deploy the Callback Contract
8269

83-
The callback contract receives and processes the off-chain result.
84-
It can read the stored `resultsCallback` from the iExec hub (or proxy) to independently verify the task state.
70+
The callback contract receives and processes the off-chain result. Your contract
71+
must implement `receiveResult(bytes32,bytes)`interface from
72+
[ERC1154](https://github.com/iExecBlockchainComputing/iexec-solidity/blob/master/contracts/ERC1154/IERC1154.sol)
8573

8674
```solidity
87-
interface IIexecProxy {
88-
struct Task {
89-
uint256 status;
90-
bytes resultsCallback; // raw callback-data bytes
91-
// ...existing fields...
92-
}
93-
function viewTask(bytes32 _id) external view returns (Task memory);
94-
}
95-
96-
abstract contract IExecCallbackReceiver {
97-
IIexecProxy public immutable iexec;
98-
constructor(address _iexec) { iexec = IIexecProxy(_iexec); }
99-
100-
function _getCallback(bytes32 taskid) internal view returns (bytes memory) {
101-
IIexecProxy.Task memory t = iexec.viewTask(taskid);
102-
require(t.status == 3, "task-not-completed"); // 3 = COMPLETED (example)
103-
return t.resultsCallback;
75+
contract IExecCallbackReceiver {
76+
// ERC1154 - Callback processing
77+
function receiveResult(bytes32 _callID, bytes memory callback) external {
78+
// Parse results
79+
(uint256 timestamp, string memory pairAndPrecision, uint256 scaledValue) =
80+
abi.decode(callback, (uint256, string, uint256));
81+
82+
...business logic...
10483
}
10584
}
10685
```
10786

10887
### Step 3: Run the iApp with Callback
10988

110-
When requesting the execution, set the callback contract address in the deal (or order) parameters.
111-
After completion, the protocol calls your contract passing the `callback-data` bytes.
89+
When requesting the execution, set the callback contract address in the deal (or
90+
order) parameters.
91+
After completion, the protocol calls your contract passing the `callback-data`
92+
bytes.
11293

11394
Checklist:
11495

@@ -118,11 +99,11 @@ Checklist:
11899

119100
## 🔄 Other Use Cases
120101

121-
| Use Case | Description |
122-
|----------|-------------|
123-
| Price oracle | Multi-source API aggregation |
102+
| Use Case | Description |
103+
| -------------------- | ---------------------------------------- |
104+
| Price oracle | Multi-source API aggregation |
124105
| Reputation / scoring | Off-chain ML / analytics pushed on-chain |
125-
| Audit hash | Security scan or verification artifact |
126-
| Automation | Workflow step completion signal |
127-
| Dynamic parameters | Adjust rates / thresholds / quorums |
128-
| Logical bridge | Sync external (IoT / legacy) state |
106+
| Audit hash | Security scan or verification artifact |
107+
| Automation | Workflow step completion signal |
108+
| Dynamic parameters | Adjust rates / thresholds / quorums |
109+
| Logical bridge | Sync external (IoT / legacy) state |

0 commit comments

Comments
 (0)