-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathOracleDemo.cs
More file actions
36 lines (31 loc) · 1.51 KB
/
OracleDemo.cs
File metadata and controls
36 lines (31 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
namespace Neo.SmartContract.Examples
{
[ManifestExtra("Author", "Neo")]
[ManifestExtra("Email", "dev@neo.org")]
[ManifestExtra("Description", "This is an oracle example")]
public class OracleDemo : Framework.SmartContract
{
public static void DoRequest()
{
string url = "https://raw.githubusercontent.com/neo-project/examples/master/csharp/Oracle/example.json"; // the content is { "value": "hello world" }
string filter = "$.value"; // JSONPath format https://github.com/atifaziz/JSONPath
string callback = "callback"; // callback method
object userdata = "userdata"; // arbitrary type
long gasForResponse = Oracle.MinimumResponseFee;
Oracle.Request(url, filter, callback, userdata, gasForResponse);
}
public static void Callback(string url, string userdata, OracleResponseCode code, string result)
{
if (ExecutionEngine.CallingScriptHash != Oracle.Hash) throw new Exception("Unauthorized!");
if (code != OracleResponseCode.Success) throw new Exception("Oracle response failure with code " + (byte)code);
object ret = StdLib.JsonDeserialize(result); // [ "hello world" ]
object[] arr = (object[])ret;
string value = (string)arr[0];
Runtime.Log("userdata: " + userdata);
Runtime.Log("response value: " + value);
}
}
}