forked from bettybao1209/PriceFeedsService
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OKexProvider.cs
59 lines (51 loc) · 2.85 KB
/
OKexProvider.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Neo;
using Neo.SmartContract;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Native;
using Neo.SmartContract.Framework.Services;
using System;
namespace PriceFeedService
{
[ManifestExtra("Description", "OKex Provider")]
[ContractPermission("0x08148cdc93bf1868e4b8b6c6de31829d1d260531", "updatePriceByProvider")]
public class OKexProvider : SmartContract
{
public const string Prefix_Price_URL = "https://www.okex.com/api/v5/market/candles?bar=1m&limit=1";
public const string Prefix_Price_InstId = "&instId=";
public const string Prefix_Price_Time = "&after=";
// { "code": "0", "msg": "", "data": [["1597026360000","11983.2","11988.5","11980.2","11988.2","26.43284234","316742.81553508"]]}
public const string filter = "$.data[0][4]";
public const long gasForResponse = Oracle.MinimumResponseFee; // TBD
[InitialValue("NWhJATyChXvaBqS9debbk47Uf2X33WtHtL", ContractParameterType.Hash160)]
private static readonly UInt160 Owner = default; // Replace it with your own address
[InitialValue("3105261d9d8231dec6b6b8e46818bf93dc8c1408", ContractParameterType.ByteArray)]
private static readonly UInt160 ProviderRegistry = default;
public static void GetPriceRequest(uint blockIndex, string symbol) // 5830, BTC-USDT
{
ulong timestamp = Ledger.GetBlock(blockIndex).Timestamp;
if (Runtime.CallingScriptHash != ProviderRegistry) throw new Exception("No authorization");
string symbolUrl = Prefix_Price_URL + Prefix_Price_InstId + symbol + Prefix_Price_Time + timestamp;
Oracle.Request(symbolUrl, filter, "getPriceCallback", blockIndex + "#" + symbol, 100000000);
}
public static void GetPriceCallback(string url, string userdata, OracleResponseCode code, string result)
{
if (Runtime.CallingScriptHash != Oracle.Hash) throw new Exception("No authorization");
if (code != OracleResponseCode.Success) throw new Exception("Oracle response failure with code " + (byte)code);
object[] arr = (object[])StdLib.JsonDeserialize(result); // ["11988.2"]
string value = (string)arr[0];
string[] data = StdLib.StringSplit(userdata, "#");
Contract.Call(ProviderRegistry, "updatePriceByProvider", CallFlags.All, new object[] { data[0], data[1], value });
}
public static void Update(ByteString nefFile, string manifest, object data)
{
if (!IsOwner()) throw new Exception("No authorization.");
ContractManagement.Update(nefFile, manifest, data);
}
public static void Destroy()
{
if (!IsOwner()) throw new Exception("No authorization.");
ContractManagement.Destroy();
}
private static bool IsOwner() => Runtime.CheckWitness(Owner);
}
}