geturi

geturi | code​

1
/**
2
* LiquidHarmony uses the geturi action to perform an oracle request
3
*
4
* @param {std::vector<char>} uri - hex conversion of URL syntax to perform an oracle request
5
* @param {Lambda&&} combinator - lambda to return the results of an oracle request
6
*
7
* Example:
8
*
9
* @code
10
* [[eosio::action]] void testrnd(std::vector<char> uri) {
11
* getURI(uri, [&]( auto& results ) {
12
* return results[0].result;
13
* });
14
* }
15
* @endcode
16
*/
17
​
18
TABLE oracleentry { \
19
uint64_t id; \
20
std::vector<char> uri; \
21
std::vector<provider_result> results; \
22
checksum256 hash_key() const { return hashData(uri); } \
23
uint64_t primary_key()const { return id; } \
24
}; \
25
typedef eosio::multi_index<"oracleentry"_n, oracleentry, indexed_by<"byhash"_n, const_mem_fun<oracleentry, checksum256, &oracleentry::hash_key>>> oracleentries_t; \
26
​
27
static std::vector<char> getURI(std::vector<char> uri, Lambda&& combinator){ \
28
checksum256 trxId = transaction_id(); \
29
auto trxIdp = trxId.data(); \
30
std::string trxIdStr(trxIdp, trxIdp + trxId.size()); \
31
auto pubTime = tapos_block_prefix(); \
32
std::string uristr(uri.begin(), uri.end()); \
33
auto s = fc::base64_encode(trxIdStr) + "://" + fc::to_string(pubTime) + "://" + uristr;\
34
std::vector<char> idUri(s.begin(), s.end()); \
35
return _getURI(idUri, combinator); \
36
}\
37
​
38
static std::vector<char> _getURI(std::vector<char> uri, Lambda&& combinator){ \
39
auto _self = name(current_receiver()); \
40
oracleentries_t entries(_self, _self.value); \
41
auto cidx = entries.get_index<"byhash"_n>(); \
42
auto existing = cidx.find(hashData(uri)); \
43
if(existing == cidx.end()){ \
44
SEND_SVC_REQUEST(geturi, uri); \
45
} \
46
else {\
47
auto results = _extractResults(*existing, combinator);\
48
cidx.erase(existing);\
49
return results; \
50
} \
51
return std::vector<char>();\
52
} \
Copied!
Copy link