-
Notifications
You must be signed in to change notification settings - Fork 10
/
fio.oracle.cpp
615 lines (488 loc) · 27.6 KB
/
fio.oracle.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/** Fio Oracle implementation file
* Description:
* @author Casey Gardiner
* @modifedby
* @file fio.oracle.cpp
* @license FIO Foundation ( https://github.com/fioprotocol/fio/blob/master/LICENSE )
*/
#include <eosiolib/asset.hpp>
#include "fio.oracle.hpp"
#include <fio.fee/fio.fee.hpp>
#include <fio.address/fio.address.hpp>
#include <fio.common/fiotime.hpp>
#include <fio.common/fio.common.hpp>
#include <fio.common/fioerror.hpp>
namespace fioio {
class [[eosio::contract("FIOOracle")]] FIOOracle : public eosio::contract {
private:
oracleledger_table receipts;
oraclevoters_table voters;
oracles_table oracles;
fionames_table fionames;
domains_table domains;
eosiosystem::producers_table producers;
eosio_names_table accountmap;
fiofee_table fiofees;
config appConfig;
public:
using contract::contract;
FIOOracle(name s, name code, datastream<const char *> ds) :
contract(s, code, ds),
receipts(_self, _self.value),
voters(_self, _self.value),
domains(AddressContract, AddressContract.value),
oracles(_self, _self.value),
producers(SYSTEMACCOUNT, SYSTEMACCOUNT.value),
accountmap(AddressContract, AddressContract.value),
fiofees(FeeContract, FeeContract.value),
fionames(AddressContract, AddressContract.value) {
configs_singleton configsSingleton(FeeContract, FeeContract.value);
appConfig = configsSingleton.get_or_default(config());
}
[[eosio::action]]
void wraptokens(int64_t &amount, string &chain_code, string &public_address, int64_t &max_oracle_fee,
int64_t &max_fee, string &tpid, name &actor) {
require_auth(actor);
fio_400_assert(validateTPIDFormat(tpid), "tpid", tpid,
"TPID must be empty or valid FIO address",
ErrorPubKeyValid);
fio_400_assert(max_fee >= 0, "max_fee", to_string(max_fee), "Invalid fee value",
ErrorMaxFeeInvalid);
fio_400_assert(validatePubAddressFormat(public_address), "public_address", public_address,
"Invalid public address", ErrorInvalidFioNameFormat);
fio_400_assert(validateChainNameFormat(chain_code), "chain_code", chain_code, "Invalid chain code format",
ErrorInvalidFioNameFormat);
fio_400_assert(max_oracle_fee >= 0, "max_oracle_fee", to_string(max_oracle_fee), "Invalid oracle fee value",
ErrorMaxFeeInvalid);
uint8_t oracle_size = std::distance(oracles.cbegin(), oracles.cend());
fio_400_assert(3 <= oracle_size, "actor", actor.to_string(), "Not enough registered oracles.",
ErrorMaxFeeInvalid);
//force uppercase chain code
std::transform(chain_code.begin(), chain_code.end(),chain_code.begin(), ::toupper);
//max amount?
fio_400_assert(amount >= 0, "amount", to_string(amount), "Invalid amount",
ErrorMaxFeeInvalid);
const uint32_t present_time = now();
//Oracle fee is transferred from actor account to all registered oracles in even amount.
auto idx = oracles.begin();
fio_400_assert(idx != oracles.end(), "max_oracle_fee", to_string(max_oracle_fee), "No Oracles registered or fees set",
ErrorMaxFeeInvalid);
int index = 0;
vector<uint64_t> totalfees;
uint64_t feeFinal;
while( idx != oracles.end() ){
if(idx->fees.size() == 0){ break; }
uint64_t tempfee = idx->fees[1].fee_amount; //1 is token in fee vector
totalfees.push_back(tempfee);
idx++;
}
fio_400_assert(totalfees.size() == oracle_size, "max_oracle_fee", to_string(max_oracle_fee), "All registered oracles have not set fees",
ErrorMaxFeeInvalid);
// median fee / oracle_info.size = fee paid
sort(totalfees.begin(), totalfees.end());
if (oracle_size % 2 == 0) {
feeFinal = (totalfees[oracle_size / 2 - 1] + totalfees[oracle_size / 2]) / 2;
} else {
feeFinal = totalfees[oracle_size / 2];
}
idx = oracles.begin();
uint64_t feeTotal = feeFinal * oracle_size;
fio_400_assert(max_oracle_fee >= feeTotal, "max_oracle_fee", to_string(max_oracle_fee), "Invalid oracle fee value",
ErrorMaxFeeInvalid);
while( idx != oracles.end() ){
action(permission_level{get_self(), "active"_n},
TokenContract, "transfer"_n,
make_tuple(actor, name{idx->actor}, asset(feeFinal, FIOSYMBOL), string("Token Wrapping Oracle Fee"))
).send();
//update voting power for receiver
action(permission_level{SYSTEMACCOUNT, "active"_n},
SYSTEMACCOUNT, "updatepower"_n,
make_tuple(name{idx->actor}, true)
).send();
idx++;
}
//Copy information to receipt table
receipts.emplace(actor, [&](struct oracleledger &p) {
p.id = receipts.available_primary_key();
p.actor = actor.value;
p.chaincode = chain_code;
p.pubaddress = public_address;
p.amount = amount;
p.timestamp = present_time;
});
//Tokens are transferred to fio.oracle for escrow.
action(permission_level{get_self(), "active"_n},
TokenContract, "transfer"_n,
make_tuple(actor, FIOORACLEContract, asset(amount, FIOSYMBOL), string("Token Wrapping"))
).send();
//Chain wrap_fio_token fee is collected.
const uint128_t endpoint_hash = string_to_uint128_hash(WRAP_FIO_TOKENS_ENDPOINT);
auto fees_by_endpoint = fiofees.get_index<"byendpoint"_n>();
auto fee_iter = fees_by_endpoint.find(endpoint_hash);
const uint64_t fee_type = fee_iter->type;
const int64_t wrap_amount = fee_iter->suf_amount;
fio_400_assert(fee_iter != fees_by_endpoint.end(), "endpoint_name", WRAP_FIO_TOKENS_ENDPOINT,
"FIO fee not found for endpoint", ErrorNoEndpoint);
uint64_t fee_amount = fee_iter->suf_amount;
fio_400_assert(max_fee >= (int64_t)fee_amount, "max_fee", to_string(max_fee), "Fee exceeds supplied maximum.",
ErrorMaxFeeExceeded);
fio_fees(actor, asset(wrap_amount, FIOSYMBOL), WRAP_FIO_TOKENS_ENDPOINT);
process_rewards(tpid, wrap_amount,get_self(), actor);
//RAM of signer is increased (512) more?
action(
permission_level{SYSTEMACCOUNT, "active"_n},
"eosio"_n,
"incram"_n,
std::make_tuple(actor, WRAPTOKENRAM)
).send();
const string response_string = string("{\"status\": \"OK\",\"oracle_fee_collected\":\"") +
to_string(feeTotal) + string("\",\"fee_collected\":") +
to_string(fee_amount) + string("}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransaction);
send_response(response_string.c_str());
}
[[eosio::action]]
void unwraptokens(int64_t &amount, string &obt_id, string &fio_address, name &actor) {
require_auth(actor);
//max amount would go here
fio_400_assert(amount > 0, "amount", to_string(amount), "Invalid amount",
ErrorMaxFeeInvalid);
fio_400_assert(obt_id.size() > 0 && obt_id.size() <= 128, "obt_id", obt_id,
"Invalid obt_id",
ErrorContentLimit);
FioAddress fa;
getFioAddressStruct(fio_address, fa);
fio_400_assert(validateFioNameFormat(fa), "fio_address", fa.fioaddress, "Invalid FIO Address",
ErrorDomainAlreadyRegistered);
uint8_t oracle_size = std::distance(oracles.cbegin(), oracles.cend());
fio_400_assert(3 <= oracle_size, "actor", actor.to_string(), "Not enough registered oracles.",
ErrorMaxFeeInvalid);
auto oraclesearch = oracles.find(actor.value);
fio_400_assert(oraclesearch != oracles.end(), "actor", actor.to_string(),
"Not a registered Oracle", ErrorPubAddressExist);
const uint128_t nameHash = string_to_uint128_hash(fio_address);
auto namesbyname = fionames.get_index<"byname"_n>();
auto fioname_iter = namesbyname.find(nameHash);
const uint128_t idHash = string_to_uint128_hash(obt_id);
auto votesbyid = voters.get_index<"byidhash"_n>();
auto voters_iter = votesbyid.find(idHash);
fio_404_assert(fioname_iter != namesbyname.end(), "FIO Address not found", ErrorFioNameNotRegistered);
const uint64_t recAcct = fioname_iter->owner_account;
vector<name> tempvoters;
//if found, search for actor in table
if (voters_iter != votesbyid.end()) {
tempvoters = voters_iter->voters;
fio_400_assert(voters_iter->amount == amount, "amount", std::to_string(amount),
"Token amount mismatch.", ErrorPubAddressExist);
auto it = std::find(tempvoters.begin(), tempvoters.end(), actor);
fio_400_assert(it == tempvoters.end(), "actor", actor.to_string(),
"Oracle has already voted.", ErrorPubAddressExist);
tempvoters.push_back(actor);
votesbyid.modify(voters_iter, actor, [&](auto &p) {
p.voters = tempvoters;
});
} else {
tempvoters.push_back(actor);
uint64_t currenttime = now();
voters.emplace(actor, [&](struct oracle_votes &p) {
p.id = voters.available_primary_key();
p.idhash = idHash;
p.voters = tempvoters;
p.obt_id = obt_id;
p.fio_address = fio_address;
p.amount = amount;
p.timestamp = currenttime;
});
}
voters_iter = votesbyid.find(idHash); // 1 oracle hack?
//verify obt and address match other entries
uint8_t size = tempvoters.size();
// if entries vs. number of regoracles meet consensus.
if (oracle_size == size && !voters_iter->isComplete) {
votesbyid.modify(voters_iter, actor, [&](auto &p) {
p.isComplete = true;
});
//Tokens are transferred to fio.wrapping.
action(permission_level{get_self(), "active"_n},
TokenContract, "transfer"_n,
make_tuple(FIOORACLEContract, recAcct, asset(amount, FIOSYMBOL), string("Token Unwrapping"))
).send();
}
const string response_string = string("{\"status\": \"OK\"}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransaction);
send_response(response_string.c_str());
}
[[eosio::action]]
void regoracle(name oracle_actor, name &actor) {
require_auth(SYSTEMACCOUNT);
const bool accountExists = is_account(oracle_actor);
auto other = accountmap.find(oracle_actor.value);
fio_400_assert(other != accountmap.end(), "oracle_actor", oracle_actor.to_string(),
"Account is not bound on the fio chain",
ErrorPubAddressExist);
fio_400_assert(accountExists, "oracle_actor", oracle_actor.to_string(),
"Account does not yet exist on the fio chain",
ErrorPubAddressExist);
auto prodbyowner = producers.get_index<"byowner"_n>();
auto proditer = prodbyowner.find(oracle_actor.value);
fio_400_assert(proditer != prodbyowner.end(), "oracle_actor", oracle_actor.to_string(),
"Oracle not active producer", ErrorNoFioAddressProducer);
std::vector <oraclefees> tempVec;
oracles.emplace(actor, [&](struct oracles &p) {
p.actor = oracle_actor.value;
p.fees = tempVec;
});
const string response_string = string("{\"status\": \"OK\"}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransactionTooLarge);
send_response(response_string.c_str());
}
[[eosio::action]]
void unregoracle(name oracle_actor) {
require_auth(SYSTEMACCOUNT);
auto oraclesearch = oracles.find(oracle_actor.value);
fio_400_assert(oraclesearch != oracles.end(), "oracle_actor", oracle_actor.to_string(),
"Oracle is not registered", ErrorPubAddressExist);
oracles.erase(oraclesearch);
const string response_string = string("{\"status\": \"OK\"}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransactionTooLarge);
send_response(response_string.c_str());
}
[[eosio::action]]
void setoraclefee(int64_t &wrap_fio_domain, int64_t &wrap_fio_tokens, name &actor) {
require_auth(actor);
//add check for < 0 on both fees
auto oraclesearch = oracles.find(actor.value);
fio_400_assert(oraclesearch != oracles.end(), "actor", actor.to_string(),
"Oracle is not registered", ErrorPubAddressExist);
fio_400_assert(wrap_fio_domain >= 0, "wrap_fio_domain", to_string(wrap_fio_domain), "Invalid fee value",
ErrorMaxFeeInvalid);
fio_400_assert(wrap_fio_tokens >= 0, "wrap_fio_tokens", to_string(wrap_fio_tokens), "Invalid fee value",
ErrorMaxFeeInvalid);
//search if fee is already set.
std::vector <oraclefees> fees = oraclesearch->fees;
for (int it = 0; it < fees.size(); it++) {
if (fees[it].fee_name == "wrap_fio_domain") {
fees[it].fee_amount = wrap_fio_domain;
} else if (fees[it].fee_name == "wrap_fio_tokens") {
fees[it].fee_amount = wrap_fio_tokens;
}
}
if (fees.size() == 0) {
oraclefees domain = {"wrap_fio_domain", static_cast<uint64_t>(wrap_fio_domain)};
oraclefees tokens = {"wrap_fio_tokens", static_cast<uint64_t>(wrap_fio_tokens)};
fees.push_back(domain);
fees.push_back(tokens);
}
oracles.modify(oraclesearch, actor, [&](auto &p) {
p.fees = fees;
});
const string response_string = string("{\"status\": \"OK\"}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransactionTooLarge);
send_response(response_string.c_str());
}
[[eosio::action]]
void wrapdomain(string &fio_domain, string &chain_code, string &public_address, int64_t &max_oracle_fee,
int64_t &max_fee, string &tpid, name &actor) {
require_auth(actor);
fio_400_assert(validateTPIDFormat(tpid), "tpid", tpid,
"TPID must be empty or valid FIO address",
ErrorPubKeyValid);
fio_400_assert(max_fee >= 0, "max_fee", to_string(max_fee), "Invalid fee value",
ErrorMaxFeeInvalid);
fio_400_assert(validatePubAddressFormat(public_address), "public_address", public_address,
"Invalid public address", ErrorInvalidFioNameFormat);
fio_400_assert(validateChainNameFormat(chain_code), "chain_code", chain_code, "Invalid chain code format",
ErrorInvalidFioNameFormat);
fio_400_assert(max_oracle_fee >= 0, "max_oracle_fee", to_string(max_oracle_fee), "Invalid oracle fee value",
ErrorMaxFeeInvalid);
uint8_t oracle_size = std::distance(oracles.cbegin(), oracles.cend());
fio_400_assert(3 <= oracle_size, "actor", actor.to_string(), "Not enough registered oracles.",
ErrorMaxFeeInvalid);
//force uppercase chain code
std::transform(chain_code.begin(), chain_code.end(),chain_code.begin(), ::toupper);
FioAddress fa;
getFioAddressStruct(fio_domain, fa);
uint128_t domainHash = string_to_uint128_hash(fio_domain);
fio_400_assert(fa.domainOnly, "fio_domain", fio_domain, "Invalid FIO domain",
ErrorInvalidFioNameFormat);
auto domainsbyname = domains.get_index<"byname"_n>();
auto domains_iter = domainsbyname.find(domainHash);
fio_400_assert(domains_iter != domainsbyname.end(), "fio_domain", fio_domain,
"FIO Domain not registered",
ErrorDomainNotRegistered);
fio_400_assert(domains_iter->account == actor.value, "fio_domain", fio_domain,
"Actor and domain owner mismatch.",
ErrorDomainNotRegistered);
//Oracle fee is transferred from actor account to all registered oracles in even amount.
auto idx = oracles.begin();
fio_400_assert(idx != oracles.end(), "max_oracle_fee", to_string(max_oracle_fee), "No Oracles registered or fees set",
ErrorMaxFeeInvalid);
int index = 0;
vector<uint64_t> totalfees;
uint64_t feeFinal;
while( idx != oracles.end() ){
if(idx->fees.size() == 0){ break; }
uint64_t tempfee = idx->fees[0].fee_amount; //0 is domain in fee vector
totalfees.push_back(tempfee);
idx++;
}
fio_400_assert(totalfees.size() == oracle_size, "max_oracle_fee", to_string(max_oracle_fee), "All registered oracles have not set fees",
ErrorMaxFeeInvalid);
// median fee / oracle_info.size = fee paid
sort(totalfees.begin(), totalfees.end());
if (oracle_size % 2 == 0) {
feeFinal = (totalfees[oracle_size / 2 - 1] + totalfees[oracle_size / 2]) / 2;
} else {
feeFinal = totalfees[oracle_size / 2];
}
idx = oracles.begin();
uint64_t feeTotal = feeFinal * oracle_size;
fio_400_assert(max_oracle_fee >= feeTotal, "max_oracle_fee", to_string(max_oracle_fee), "Invalid oracle fee value",
ErrorMaxFeeInvalid);
while( idx != oracles.end() ){
action(permission_level{get_self(), "active"_n},
TokenContract, "transfer"_n,
make_tuple(actor, name{idx->actor}, asset(feeFinal, FIOSYMBOL), string("Token Wrapping Oracle Fee"))
).send();
action(permission_level{SYSTEMACCOUNT, "active"_n},
SYSTEMACCOUNT, "updatepower"_n,
make_tuple(name{idx->actor}, true)
).send();
idx++;
}
const uint32_t present_time = now();
//Copy information to receipt table
receipts.emplace(actor, [&](struct oracleledger &p) {
p.id = receipts.available_primary_key();
p.actor = actor.value;
p.chaincode = chain_code;
p.pubaddress = public_address;
p.nftname = fio_domain;
p.timestamp = present_time;
});
//Transfer Domain to escrow
bool isTransferToEscrow = true;
action(
permission_level{FIOORACLEContract, "active"_n},
AddressContract,
"xferescrow"_n,
std::make_tuple(fio_domain, nullptr, isTransferToEscrow, actor)
).send();
//Chain wrap_fio_token fee is collected.
const uint128_t endpoint_hash = string_to_uint128_hash(WRAP_FIO_DOMAIN_ENDPOINT);
auto fees_by_endpoint = fiofees.get_index<"byendpoint"_n>();
auto fee_iter = fees_by_endpoint.find(endpoint_hash);
const uint64_t fee_type = fee_iter->type;
const int64_t wrap_amount = fee_iter->suf_amount;
fio_400_assert(fee_iter != fees_by_endpoint.end(), "endpoint_name", WRAP_FIO_DOMAIN_ENDPOINT,
"FIO fee not found for endpoint", ErrorNoEndpoint);
uint64_t fee_amount = fee_iter->suf_amount;
fio_400_assert(max_fee >= (int64_t)fee_amount, "max_fee", to_string(max_fee), "Fee exceeds supplied maximum.",
ErrorMaxFeeExceeded);
fio_fees(actor, asset(wrap_amount, FIOSYMBOL), WRAP_FIO_TOKENS_ENDPOINT);
process_rewards(tpid, wrap_amount,get_self(), actor);
//RAM of signer is increased (512) more?
action(
permission_level{SYSTEMACCOUNT, "active"_n},
"eosio"_n,
"incram"_n,
std::make_tuple(actor, WRAPTOKENRAM)
).send();
const string response_string = string("{\"status\": \"OK\",\"oracle_fee_collected\":\"") +
to_string(feeTotal) + string("\",\"fee_collected\":") +
to_string(fee_amount) + string("}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransaction);
send_response(response_string.c_str());
}
[[eosio::action]]
void unwrapdomain(string &fio_domain, string &obt_id, string &fio_address, name &actor) {
require_auth(actor);
FioAddress fa;
getFioAddressStruct(fio_domain, fa);
fio_400_assert(fa.domainOnly, "fio_domain", fa.fioaddress, "Invalid FIO domain",
ErrorInvalidFioNameFormat);
const uint128_t domainHash = string_to_uint128_hash(fio_domain.c_str());
auto domainsbyname = domains.get_index<"byname"_n>();
auto domains_iter = domainsbyname.find(domainHash);
name nm = name("fio.oracle");
fio_400_assert(obt_id.size() > 0 && obt_id.size() <= 128, "obt_id", obt_id,
"Invalid obt_id",
ErrorContentLimit);
fio_400_assert(domains_iter != domainsbyname.end(), "fio_domain", fio_domain,
"FIO domain not found", ErrorDomainNotRegistered);
fio_400_assert(domains_iter->account == nm.value, "fio_domain", fio_domain,
"FIO domain not owned by Oracle contract.", ErrorDomainNotRegistered);
uint8_t oracle_size = std::distance(oracles.cbegin(), oracles.cend());
fio_400_assert(3 <= oracle_size, "actor", actor.to_string(), "Not enough registered oracles.",
ErrorMaxFeeInvalid);
auto oraclesearch = oracles.find(actor.value);
fio_400_assert(oraclesearch != oracles.end(), "actor", actor.to_string(),
"Not a registered Oracle", ErrorPubAddressExist);
const uint128_t nameHash = string_to_uint128_hash(fio_address);
auto namesbyname = fionames.get_index<"byname"_n>();
auto fioname_iter = namesbyname.find(nameHash);
const uint128_t idHash = string_to_uint128_hash(obt_id);
auto votesbyid = voters.get_index<"byidhash"_n>();
auto voters_iter = votesbyid.find(idHash);
fio_404_assert(fioname_iter != namesbyname.end(), "FIO Address not found", ErrorFioNameNotRegistered);
const uint64_t recAcct = fioname_iter->owner_account;
vector<name> tempvoters;
//if found, search for actor in table
if (voters_iter != votesbyid.end()) {
tempvoters = voters_iter->voters;
auto it = std::find(tempvoters.begin(), tempvoters.end(), actor);
fio_400_assert(it == tempvoters.end(), "actor", actor.to_string(),
"Oracle has already voted.", ErrorPubAddressExist);
fio_400_assert(fio_domain == voters_iter->nftname, "fio_domain", fio_domain,
"Domain name mismatch.", ErrorPubAddressExist);
tempvoters.push_back(actor);
votesbyid.modify(voters_iter, actor, [&](auto &p) {
p.voters = tempvoters;
});
} else {
tempvoters.push_back(actor);
uint64_t currenttime = now();
voters.emplace(actor, [&](struct oracle_votes &p) {
p.id = voters.available_primary_key();
p.idhash = idHash;
p.voters = tempvoters;
p.obt_id = obt_id;
p.fio_address = fio_address;
p.nftname = fio_domain;
p.timestamp = currenttime;
});
}
voters_iter = votesbyid.find(idHash);
//verify obt and address match other entries
uint8_t size = tempvoters.size();
// if entries vs. number of regoracles meet consensus.
if (oracle_size == size && !voters_iter->isComplete) {
votesbyid.modify(voters_iter, actor, [&](auto &p) {
p.isComplete = true;
});
//transfer ownership to new owner
auto owner = accountmap.find(recAcct);
bool isTransferToEscrow = false;
action(
permission_level{FIOORACLEContract, "active"_n},
AddressContract,
"xferescrow"_n,
std::make_tuple(fio_domain, owner->clientkey, isTransferToEscrow, actor)
).send();
}
const string response_string = string("{\"status\": \"OK\"}");
fio_400_assert(transaction_size() <= MAX_TRX_SIZE, "transaction_size", std::to_string(transaction_size()),
"Transaction is too large", ErrorTransaction);
send_response(response_string.c_str());
}
};
EOSIO_DISPATCH(FIOOracle, (wraptokens)(unwraptokens)(regoracle)(unregoracle)
(setoraclefee)(wrapdomain)(unwrapdomain)
)
}