Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(docs): Update How to Oracles #5675

Merged
merged 10 commits into from
Nov 8, 2024
54 changes: 26 additions & 28 deletions docs/docs/how_to/how-to-oracles.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,32 @@ app.listen(5555);
Now, we will add our `getSqrt` method, as expected by the `#[oracle(getSqrt)]` decorator in our Noir code. It maps through the params array and returns their square roots:

```js
server.addMethod("resolve_function_call", async (params) => {
if params.function !== "getSqrt" {
throw Error("Unexpected foreign call")
};
const values = params.inputs[0].Array.map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [{ Array: values }] };
server.addMethod("resolve_foreign_call", async (params) => {
if (params[0].function !== "getSqrt") {
throw Error("Unexpected foreign call")
};
const values = params[0].inputs[0].map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [values] };
});
```

If you're using Typescript, the following types may be helpful in understanding the expected return value and making sure they're easy to follow:

```js
interface SingleForeignCallParam {
Single: string,
}

interface ArrayForeignCallParam {
Array: string[],
}
export type ForeignCallSingle = string;

type ForeignCallParam = SingleForeignCallParam | ArrayForeignCallParam;
export type ForeignCallArray = string[];

interface ForeignCallResult {
values: ForeignCallParam[],
}
export type ForeignCallResult = {
values: (ForeignCallSingle | ForeignCallArray)[];
};
```

::: Multidimensional Arrays
:::Multidimensional Arrays

If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in json as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.
If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in JSON as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the Noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.

:::

Expand Down Expand Up @@ -233,17 +227,21 @@ const client = new JSONRPCClient((jsonRPCRequest) => {

// declaring a function that takes the name of the foreign call (getSqrt) and the inputs
const foreignCallHandler = async (name, input) => {
// notice that the "inputs" parameter contains *all* the inputs
// in this case we make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request(name, [
input[0].map((i) => i.toString("hex")),
]);
return { values: oracleReturn };
const inputs = input[0].map((i) => i.toString("hex"))
// notice that the "inputs" parameter contains *all* the inputs
// in this case we to make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request("resolve_foreign_call", [
{
function: name,
inputs: [inputs]
},
]);
return [oracleReturn.values[0]];
};

// the rest of your NoirJS code
const input = { input: [4, 16] };
const { witness } = await noir.execute(numbers, foreignCallHandler);
const { witness } = await noir.execute(input, foreignCallHandler);
```

:::tip
Expand Down
56 changes: 27 additions & 29 deletions docs/versioned_docs/version-v0.31.0/how_to/how-to-oracles.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ unconstrained fn get_sqrt(number: Field) -> Field {
}
```

In this example, we're wrapping our oracle function in a unconstrained method, and decorating it with `oracle(getSqrt)`. We can then call the unconstrained function as we would call any other function:
In this example, we're wrapping our oracle function in an unconstrained method, and decorating it with `oracle(getSqrt)`. We can then call the unconstrained function as we would call any other function:

```rust
fn main(input: Field) {
Expand Down Expand Up @@ -137,38 +137,32 @@ app.listen(5555);
Now, we will add our `getSqrt` method, as expected by the `#[oracle(getSqrt)]` decorator in our Noir code. It maps through the params array and returns their square roots:

```js
server.addMethod("resolve_function_call", async (params) => {
if params.function !== "getSqrt" {
throw Error("Unexpected foreign call")
};
const values = params.inputs[0].Array.map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [{ Array: values }] };
server.addMethod("resolve_foreign_call", async (params) => {
if (params[0].function !== "getSqrt") {
throw Error("Unexpected foreign call")
};
const values = params[0].inputs[0].map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [values] };
});
```

If you're using Typescript, the following types may be helpful in understanding the expected return value and making sure they're easy to follow:

```js
interface SingleForeignCallParam {
Single: string,
}

interface ArrayForeignCallParam {
Array: string[],
}
export type ForeignCallSingle = string;

type ForeignCallParam = SingleForeignCallParam | ArrayForeignCallParam;
export type ForeignCallArray = string[];

interface ForeignCallResult {
values: ForeignCallParam[],
}
export type ForeignCallResult = {
values: (ForeignCallSingle | ForeignCallArray)[];
};
```

::: Multidimensional Arrays
:::Multidimensional Arrays

If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in json as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.
If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in JSON as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the Noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.

:::

Expand Down Expand Up @@ -233,17 +227,21 @@ const client = new JSONRPCClient((jsonRPCRequest) => {

// declaring a function that takes the name of the foreign call (getSqrt) and the inputs
const foreignCallHandler = async (name, input) => {
// notice that the "inputs" parameter contains *all* the inputs
// in this case we to make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request(name, [
{ Array: input[0].map((i) => i.toString("hex")) },
]);
return [oracleReturn.values[0].Array];
const inputs = input[0].map((i) => i.toString("hex"))
// notice that the "inputs" parameter contains *all* the inputs
// in this case we to make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request("resolve_foreign_call", [
{
function: name,
inputs: [inputs]
},
]);
return [oracleReturn.values[0]];
};

// the rest of your NoirJS code
const input = { input: [4, 16] };
const { witness } = await noir.execute(numbers, foreignCallHandler);
const { witness } = await noir.execute(input, foreignCallHandler);
```

:::tip
Expand Down
54 changes: 26 additions & 28 deletions docs/versioned_docs/version-v0.32.0/how_to/how-to-oracles.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,38 +137,32 @@ app.listen(5555);
Now, we will add our `getSqrt` method, as expected by the `#[oracle(getSqrt)]` decorator in our Noir code. It maps through the params array and returns their square roots:

```js
server.addMethod("resolve_function_call", async (params) => {
if params.function !== "getSqrt" {
throw Error("Unexpected foreign call")
};
const values = params.inputs[0].Array.map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [{ Array: values }] };
server.addMethod("resolve_foreign_call", async (params) => {
if (params[0].function !== "getSqrt") {
throw Error("Unexpected foreign call")
};
const values = params[0].inputs[0].map((field) => {
return `${Math.sqrt(parseInt(field, 16))}`;
});
return { values: [values] };
});
```

If you're using Typescript, the following types may be helpful in understanding the expected return value and making sure they're easy to follow:

```js
interface SingleForeignCallParam {
Single: string,
}

interface ArrayForeignCallParam {
Array: string[],
}
export type ForeignCallSingle = string;

type ForeignCallParam = SingleForeignCallParam | ArrayForeignCallParam;
export type ForeignCallArray = string[];

interface ForeignCallResult {
values: ForeignCallParam[],
}
export type ForeignCallResult = {
values: (ForeignCallSingle | ForeignCallArray)[];
};
```

::: Multidimensional Arrays
:::Multidimensional Arrays

If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in json as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.
If the Oracle function is returning an array containing other arrays, such as `[['1','2],['3','4']]`, you need to provide the values in JSON as flattened values. In the previous example, it would be `['1', '2', '3', '4']`. In the Noir program, the Oracle signature can use a nested type, the flattened values will be automatically converted to the nested type.

:::

Expand Down Expand Up @@ -233,17 +227,21 @@ const client = new JSONRPCClient((jsonRPCRequest) => {

// declaring a function that takes the name of the foreign call (getSqrt) and the inputs
const foreignCallHandler = async (name, input) => {
// notice that the "inputs" parameter contains *all* the inputs
// in this case we make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request(name, [
input[0].map((i) => i.toString("hex")),
]);
return { values: oracleReturn };
const inputs = input[0].map((i) => i.toString("hex"))
// notice that the "inputs" parameter contains *all* the inputs
// in this case we to make the RPC request with the first parameter "numbers", which would be input[0]
const oracleReturn = await client.request("resolve_foreign_call", [
{
function: name,
inputs: [inputs]
},
]);
return [oracleReturn.values[0]];
};

// the rest of your NoirJS code
const input = { input: [4, 16] };
const { witness } = await noir.execute(numbers, foreignCallHandler);
const { witness } = await noir.execute(input, foreignCallHandler);
```

:::tip
Expand Down
Loading