Skip to content

Commit 1308e06

Browse files
committed
update docs and rename functions
1 parent cd39a75 commit 1308e06

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ await oracle.wait(taskId);
136136

137137
When we return from `wait` without any errors, we are sure that the request has been completed.
138138

139-
### Reading Results
139+
### Reading the Best Response
140140

141141
To read the best (i.e. highest score) response to a request, we have the `read` function:
142142

@@ -145,13 +145,41 @@ const response = await oracle.read(taskId);
145145
const { output, metadata } = response;
146146
```
147147

148-
Internally, this handles t
148+
This function also handles downloading the actual values from Arweave, if the responder nodes have used it to save from gas. Note that you must have passed in the `ArweaveStorage` instance to the `Oracle` constructor for this to work.
149149

150-
TODO: describe parsing
150+
### Reading All sResponses
151+
152+
If you are interested in reading all generations, you can use:
153+
154+
```ts
155+
const responses = await oracle.getResponses(taskId);
156+
```
157+
158+
This returns you an array of raw responses. You can fetch the actual values from Arweave using the `processResponse` function:
159+
160+
```ts
161+
for (const responseRaw of responses) {
162+
const response = await oracle.processResponse(responseRaw);
163+
// console.log ...
164+
}
165+
```
151166

152167
### Reading Validations
153168

154-
TODO: describe validations
169+
If you are interested in reading all validations, you can use:
170+
171+
```ts
172+
const validations = await oracle.getValidations(taskId);
173+
```
174+
175+
This returns you an array of raw responses. You can fetch the actual values from Arweave using the `processResponse` function:
176+
177+
```ts
178+
for (const validationRaw of validations) {
179+
const validation = await oracle.processValidation(validationRaw);
180+
// console.log ...
181+
}
182+
```
155183

156184
## Examples
157185

examples/view.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ async function main() {
88

99
// task id from command line
1010
const taskId = BigInt(process.argv[2] ?? 1);
11+
console.log("Viewing task:", taskId);
1112

1213
console.log("\nGenerations:");
13-
const responses = await oracle.readResponses(taskId);
14+
const responses = await oracle.getResponses(taskId);
1415
for (const responseRaw of responses) {
1516
const response = await oracle.processResponse(responseRaw);
1617
console.log(

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dria-oracle-sdk",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "An on-chain AI oracle SDK for Dria",
55
"license": "MIT",
66
"author": "FirstBatch Team <[email protected]>",

src/client.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
4444

4545
// defaults
4646
public taskParameters: TaskParameters = { difficulty: 2, numGenerations: 2, numValidations: 2 };
47-
public protocol = "oracle-js-sdk/0.1.0";
47+
public protocol = "dria-oracle-sdk/0.x.x";
4848

4949
constructor(
5050
readonly client: {
@@ -83,9 +83,24 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
8383

8484
/**
8585
* Change the underlying default protocol.
86+
*
87+
* The protocol is a string that should fit a `bytes32` type in Solidity. It is used
88+
* to identify the source of the request, and can be used within event filters.
89+
*
90+
* It should have to format `name/version`, e.g. `dria-oracle-sdk/0.x.x`.
8691
* @param protocol protocol name
8792
*/
8893
withProtocol(protocol: string) {
94+
// ensure `/` appears once
95+
if (protocol.split("/").length !== 2) {
96+
throw new Error("Invalid protocol format.");
97+
}
98+
99+
// ensure it fits bytes32
100+
if (Buffer.from(protocol).length > 32) {
101+
throw new Error("Protocol string is too long.");
102+
}
103+
89104
this.protocol = protocol;
90105
return this;
91106
}
@@ -225,8 +240,7 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
225240
* @returns true if the task is completed, or `taskId` is 0
226241
*/
227242
async isCompleted(taskId: bigint | number): Promise<boolean> {
228-
// 0 is always accepted
229-
// TODO: explain why
243+
// 0 is always accepted; because `history_id: 0` may be used for chat messages
230244
if (BigInt(taskId) === 0n) {
231245
return true;
232246
}
@@ -252,9 +266,9 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
252266
}
253267

254268
/**
255-
* Reads the request of a task.
269+
* Returns the validations of all generation responses for a task.
256270
* @param taskId task id
257-
* @returns task validations
271+
* @returns array of task validations
258272
*/
259273
async getValidations(taskId: bigint | number): Promise<readonly Prettify<TaskValidation>[]> {
260274
if (this.coordinator === undefined) {
@@ -264,12 +278,11 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
264278
}
265279

266280
/**
267-
* Read the validations of a task.
281+
* Returns the generation responses for a task.
268282
* @param taskId task id
269-
* @param idx index of the response, if not provided, the best & completed response is returned
270-
* @returns task response
283+
* @returns array of task responses
271284
*/
272-
async readResponses(taskId: bigint | number): Promise<readonly Prettify<TaskResponse>[]> {
285+
async getResponses(taskId: bigint | number): Promise<readonly Prettify<TaskResponse>[]> {
273286
if (this.coordinator === undefined) {
274287
throw new Error("SDK not initialized.");
275288
}
@@ -306,6 +319,7 @@ export class Oracle<T extends Transport, C extends Chain, K = unknown> {
306319
try {
307320
return {
308321
...validation,
322+
// here we assume the type like this, because validators are trusted
309323
metadata: JSON.parse(metadata) as TaskValidationScores[],
310324
};
311325
} catch (err) {

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// main
1+
// sdk
22
export { Oracle } from "./client";
33
export type { OracleModels } from "./types";
44

55
// storage
66
export type { DecentralizedStorage } from "./storage";
7-
export { ArweaveStorage, type ArweaveWallet } from "./storage/arweave";
7+
export { ArweaveStorage, type ArweaveWallet } from "./storage/";
88

99
// utilities
1010
export { contractBytesToStringWithStorage, stringToContractBytesWithStorage } from "./utils";

0 commit comments

Comments
 (0)