-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregen_ecostate_oracle.module
719 lines (583 loc) · 23.5 KB
/
regen_ecostate_oracle.module
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
* @file
* Code for Regen Ecostate Oracle
*/
/*
* Implements hook_menu
*/
function regen_ecostate_oracle_menu() {
// Default dashboard page.
$items['farm/ecostate'] = array(
'title' => 'Ecostate',
'page callback' => 'regen_ecostate_oracle_dashboard_page',
'access arguments' => array('access farm dashboard'),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
// View area contract details.
$items['farm/ecostate/area/%'] = array(
'title' => 'Contract Details',
'page callback' => 'drupal_get_form',
'page arguments' => array('regen_ecostate_oracle_contract_form', 3),
'access arguments' => array('access farm dashboard'),
);
// Select area to instantiate.
$items['farm/ecostate/instantiate/area'] = array(
'title' => 'Link/Instantiate',
'page callback' => 'drupal_get_form',
'page arguments' => array('regen_ecostate_oracle_instantiate_area_form'),
'access arguments' => array('access farm dashboard'),
'type' => MENU_LOCAL_TASK,
);
// Instantiate new contract in area.
$items['farm/ecostate/instantiate/area/%'] = array(
'title' => 'Instantiate',
'page callback' => 'drupal_get_form',
'page arguments' => array('regen_ecostate_oracle_instantiate_contract_form', 4),
'access arguments' => array('access farm dashboard'),
);
// Ecostate dashboard.
$items['farm/ecostate/dashboard'] = array(
'title' => 'Dashboard',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -100,
);
// Oracle configuration form.
$items['admin/config/farm/regen'] = array(
'title' => 'Regen Oracle',
'description' => 'Regen Network config settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('regen_ecostate_oracle_settings_form'),
'access arguments' => array('access farm dashboard'),
);
return $items;
}
/**
* Quick form page callback.
*/
function regen_ecostate_oracle_dashboard_page() {
drupal_set_title(t('Ecostate'));
$output = array();
$output['map'] = farm_map_build('ecostate-dashboard-map', FALSE);
$areas = farm_area_load_areas();
$valid_areas = array();
$contracts = variable_get('regen_ecostate_oracle_area_contracts', array());
foreach($areas as $area) {
if (!empty($contracts[$area->tid])) {
$valid_areas[] = $area;
}
}
$output['contracts'] = array(
'#type' => 'fieldset',
'#title' => 'Contracts',
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$contract_markup = '';
foreach($valid_areas as $area) {
$area_contract_link = l($area->name, 'farm/ecostate/area/'. $area->tid);
$ecostate = regen_ecostate_oracle_get_current_ecostate($area->tid);
$contract_markup = $contract_markup . '<li>'. $area_contract_link .' - Current ecostate: <strong>' . $ecostate. '%</strong></li>';
}
$output['contracts']['created_contracts'] = array(
'#markup' => $contract_markup,
'#prefix' => '<ul>',
'#suffix' => '</ul>'
);
$output['instructions'] = array(
'#type' => 'fieldset',
'#title' => 'Instructions',
);
$output['instructions']['select'] = array(
'#markup' => '<h3>View Ecostate Contracts</h3>' .
'<h5>Select an area on the map and click "Contract Details" OR choose an area from the list above to view the contract associated with that area</h5><br>',
);
$code_link = l('code #152', 'https://regen.wasm.glass/codes/152');
$oracle_address = variable_get('regen_ecostate_oracle_address', '(not configred)');
$contract_requirements_link = l('contract requirements', 'https://github.com/regen-network/testnets/tree/master/kontraua/challenges/phase-5#payout-contract-requirements');
$add_area_link = l('Add an area', 'admin/structure/taxonomy/farm_areas/add', array('query'=> array('destination' => 'farm/ecostate/instantiate/area')));
$instantiate_area_link = l('Instantiate area', 'farm/ecostate/instantiate/area');
$output['instructions']['validator_steps'] = array(
'#markup' => '<h3>Validator Steps</h3><br>'
);
$output['instructions']['link_contract'] = array(
'#markup' => '<h4>1. Link Existing Contract</h4>' .
'<p>Link a custom contract to an area and execute the "update_ecostate" message from the farmOS oracle. This simulates how a 3rd party might make ecostate claims against your contract.</p>' .
'<strong>NOTE: Your contract must be instantiated with the oracle set to the account used by this farmOS server: "'. $oracle_address .'" or else attempts to execute messages on the contract will be denied.</strong>' .
'<ul><li>' . $add_area_link . ' (Create a farmOS "Field" area. Please keep areas clustered in the same region so they remain visible on the map.)</li>' .
'<li>' . $instantiate_area_link .' - Complete the "Link Contract" part of the form.</li></ul></p><br>',
);
$output['instructions']['update_ecostate'] = array(
'#markup' => '<h4>2. Update Ecostate</h4>' .
'<p><ul><li>Select "Contract Details" from an above area.</li>' .
'<li>This page provides a form to update the ecostate value. After execution, the result will be saved in a farmOS Observation Log associated with the area.</li>' .
'<li>This can be repeated until all tokens are released from a contract (or other criteria is met).</li></ul><br>',
);
$init_msg_schema = l('initMsg Schema', 'https://github.com/regen-network/ecodata-payout/blob/master/schema/init_msg.json');
$output['instructions']['instantiate_contract'] = array(
'#markup' => '<h4>Optional: Instantiate Contract</h4>' .
'<p>This module instantiates ' . $code_link . ' by default. This contract implements a subset of features defined in the ' .$contract_requirements_link.'. You may also supply your custom "codeID" <strong>BUT your contract must be compatible with this ' . $init_msg_schema . '</strong>' .
'<ul><li>' . $add_area_link . ' (Create a farmOS "Field" area. Please keep areas clustered in the same region so they remain visible on the map.)</li>' .
'<li>' . $instantiate_area_link .' - This module instantiates code #152 by default. This contract implements a subset of features defined in the ' .$contract_requirements_link.'. You may supply a different code to test your custom contract.</li></ul></p><br>',
);
$kontraua_testnet_link = l('Kontraua Testnet', 'https://github.com/regen-network/testnets');
$cosmwasm_js_link = l('cosmwasm-js', 'https://github.com/CosmWasm/cosmwasm-js');
$testnet_explorer_link = l('explorer', 'https://explorer.regen.vitwit.com');
$output['instructions']['links'] = array(
'#markup' => '<h4>More info:</h4>' .
'<ul><li>Info on the Regen Network '.$kontraua_testnet_link.'</li>'.
'<li>This module uses '.$cosmwasm_js_link.' to communicate with cosmwasm contracts on the testnet.</li>'.
'<li>Live testnet '.$testnet_explorer_link.'</li></ul>',
);
return $output;
}
/*
* Implements hook_form()
*/
function regen_ecostate_oracle_contract_form($form, &$form_state, $area_id) {
// Load area.
$area = taxonomy_term_load($area_id);
$area_details = farm_area_get_details($area_id);
// Load area WKT.
$area_ids = array();
$area_ids[] = $area->tid;
$wkt = farm_area_extract_geoms($area_ids)[0];
// Save the area_id for form_submit.
$form['area_id'] = array(
'#type' => 'hidden',
'#value' => $area_id,
);
// Display area info.
$form['area_details'] = array(
'#type' => 'fieldset',
'#title' => 'farmOS Area Details',
'#collapsible' => TRUE
);
$form['area_details']['area_map'] = array(
'#type' => 'farm_map',
'#map_name' => 'ecostate-contract-map',
'#wkt' => $wkt,
'#edit' => FALSE,
);
$form['area_details']['details'] = array(
'#markup' => '<h3>Area Name:' . $area->name .'</h3><h5>Details:</h5><p>'.$area_details.'</p>'
);
// Load contract associated with area.
$contracts = variable_get('regen_ecostate_oracle_area_contracts', array());
$contract_address = null;
if (!empty($contracts[$area_id])) {
$contract = $contracts[$area_id];
$contract_address = $contract['address'];
$form['contract'] = array(
'#type' => 'fieldset',
'#title' => 'Contract',
);
$form['contract']['info'] = array(
'#markup' => '<h4>Contact Address: <a href="https://regen.wasm.glass/contracts/' . $contract['address'].'">' . $contract['address'] . '</a></h4>' .
'<div id="contract-label">Label: Loading...</div>' .
'<div id="contract-codeId">Code ID: Loading...</div>' .
'<div id="contract-creator">Creator: Loading...</div>' .
'<div id="contract-ecostate">Ecostate: Loading...</div>' .
'<div id="contract-initMsg">Ecostate: Loading...</div>' .
'<div id="contract-state">State: Loading...</div>',
);
$form['contract']['info']['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('regen_ecostate_oracle' => array('contract' => $contract_address)),
);
// Build a query to find observation logs in an area.
$query = farm_quantity_log_query('ratio', 'ecostate', $done = TRUE, $type = 'farm_observation');
$query->addTag('regen_ecostate_oracle_log_query');
$query->join('field_data_field_farm_area', 'ss_fdffa', "ss_fdffa.entity_id = ss_log.id");
$query->where('ss_fdffa.field_farm_area_tid = ' . $area_id);
// Execute the query and gather the log ids.
$result = $query->execute();
$log_ids = array();
foreach ($result as $row) {
if (!empty($row->id)) {
$log_ids[] = $row->id;
}
}
// If there are log IDs, load the logs.
$logs = array();
if (!empty($log_ids)) {
$logs = log_load_multiple($log_ids);
}
// Display logs.
$form['log_fieldset'] = array(
'#type' => 'fieldset',
'#title' => 'Logs',
);
$form['log_fieldset']['logs'] = array();
foreach ($logs as $log) {
// Create a collapsible fieldset for this log.
$form['log_fieldset']['logs'][$log->id] = array(
'#type' => 'fieldset',
'#title' => $log->name,
'#collapsible' => TRUE,
);
// Load ecostate quantity data.
$ecostate = farm_quantity_log_data($log, 'ratio', 'ecostate');
$log_wrapper = entity_metadata_wrapper('log', $log);
$log_data = NULL;
if (!empty($log_wrapper->field_farm_data)) {
$log_data = json_decode($log_wrapper->field_farm_data->value());
}
$tx = isset($log_data->transaction) ? $log_data->transaction : '';
$response = isset($log_data->response) ? $log_data->response : '';
$tx_url = 'https://regen.wasm.glass/transactions/' . $tx;
$output = array(
'#markup' => '<p>Log ID: ' . $log->id .' </p>' .
'<p>Ecostate: ' . $ecostate[0]['value'] . ' %</p>' .
'<p>Transaction: ' . l($tx, $tx_url) . '</p>' .
'<p>Raw Response: </p><pre>' . json_encode($response) . '</pre>',
);
$form['log_fieldset']['logs'][$log->id]['info'] = $output;
}
// Create a new log.
$form['log_fieldset']['logs']['new_log'] = array(
'#type' => 'fieldset',
'#title' => 'Update Ecostate',
'#collapsible' => TRUE,
);
$form['log_fieldset']['logs']['new_log']['new_ecostate'] = array(
'#type' => 'textfield',
'#title' => 'Ecostate value',
'#description' => 'A positive integer, which maintains the percentage of forest cover, without decimals. 56.16% is stored as 5616'
);
$form['log_fieldset']['logs']['new_log']['new_transaction'] = array(
'#type' => 'hidden',
'#default_value' => '',
);
$form['log_fieldset']['logs']['new_log']['status'] = array(
'#type' => 'textfield',
'#title' => 'Status',
'#attributes' => array('readonly' => 'readonly'),
);
$form['log_fieldset']['logs']['new_log']['update_ecostate'] = array(
'#type' => 'button',
'#value' => 'Update Ecostate',
'#id' => 'update-ecostate',
'#attributes' => array('onclick' => 'return (false);'),
);
// Add behavior to update ecostate.
$form['log_fieldset']['logs']['new_log']['update_ecostate']['#attached']['js'] = array(
drupal_get_path('module', 'regen_ecostate_oracle') . '/js/update_ecostate.js',
);
$form['log_fieldset']['logs']['new_log']['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit Log',
'#id' => 'submit-ecostate',
'#attributes' => array(
'class' => array('element-invisible'),
),
);
}
$config = array(
'mnemonic' => variable_get('regen_ecostate_oracle_mnemonic', ''),
'oracle' => variable_get('regen_ecostate_oracle_address', ''),
'httpUrl' => variable_get('regen_ecostate_oracle_http_url', ''),
'networkId' => variable_get('regen_ecostate_oracle_network_id', ''),
'gasPrice' => variable_get('regen_ecostate_oracle_gas_price', ''),
);
drupal_add_js(array(
'regen_ecostate_oracle' => array(
'clientOpts' => $config,
),
), array('type' => 'setting'));
drupal_add_js(drupal_get_path('module', 'regen_ecostate_oracle') . '/cosmwasm/dist/main.js', array('weight'=> -100));
drupal_add_js(drupal_get_path('module', 'regen_ecostate_oracle') . '/js/load_contract.js', array('weight'=> -100));
return $form;
}
/*
* Implements hook_form_submit()
*/
function regen_ecostate_oracle_contract_form_submit($form, &$form_state) {
$area = taxonomy_term_load($form_state['values']['area_id']);
$ecostate = $form_state['values']['new_ecostate'];
$tx = $form_state['values']['new_transaction'];
regen_ecostate_oracle_create_ecostate_observation_log($area, $tx, $ecostate);
}
/*
* Implements hook_form()
*/
function regen_ecostate_oracle_instantiate_area_form($form, &$form_state) {
$areas = farm_area_load_areas();
$valid_areas = array();
$contracts = variable_get('regen_ecostate_oracle_area_contracts', array());
foreach($areas as $area) {
if (empty($contracts[$area->tid])) {
$valid_areas[$area->tid] = $area->name;
}
}
$form['area'] = array(
'#type' => 'select',
'#title' => 'Area',
'#options' => $valid_areas,
'#default_vale' => '',
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/*
* Implements hook_form_submit()
*/
function regen_ecostate_oracle_instantiate_area_form_submit($form, &$form_state) {
$form_state['redirect'] = '/farm/ecostate/instantiate/area/' . $form_state['values']['area'];
}
/*
* Implements hook_form()
*/
function regen_ecostate_oracle_instantiate_contract_form($form, &$form_state, $area_id) {
drupal_set_title('Regen Ecostate');
// Load area.
$area = taxonomy_term_load($area_id);
$area_details = farm_area_get_details($area_id);
// Load area WKT.
$area_ids = array();
$area_ids[] = $area->tid;
$wkt = farm_area_extract_geoms($area_ids)[0];
// Save area_id for form_submit.
$form['area_id'] = array(
'#type' => 'hidden',
'#value' => $area_id,
);
// Display area details.
$form['area_details'] = array(
'#type' => 'fieldset',
'#title' => 'farmOS Area Details',
'#collapsible' => TRUE
);
$form['area_details']['area_map'] = array(
'#type' => 'farm_map',
'#map_name' => 'ecostate-contract-map',
'#wkt' => $wkt,
'#edit' => FALSE,
);
$form['area_details']['details'] = array(
'#markup' => '<h3>Area Name:' . $area->name .'</h3><h5>Details:</h5><p>'.$area_details.'</p>'
);
// Instantiate contract.
$form['instantiate_contract'] = array(
'#type' => 'fieldset',
'#title' => 'Instantiate New Contract',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['instantiate_contract']['instantiate_code_id'] = array(
'#type' => 'textfield',
'#title' => 'Code ID',
'#default_value' => '152',
);
$form['instantiate_contract']['instantiate_beneficiary'] = array(
'#type' => 'textfield',
'#title' => 'Beneficiary',
);
$form['instantiate_contract']['instantiate_total_tokens'] = array(
'#type' => 'textfield',
'#title' => 'Total Tokens',
);
$form['instantiate_contract']['instantiate_ecostate'] = array(
'#type' => 'textfield',
'#title' => 'Initial Ecostate',
'#description' => 'A positive integer, which maintains the percentage of forest cover, without decimals. 56.16% is stored as 5616'
);
$form['instantiate_contract']['instantiate_region'] = array(
'#type' => 'textfield',
'#title' => 'Region',
'#default_value' => $area->name,
'#attributes' => array('readonly' => 'readonly'),
);
$form['instantiate_contract']['instantiate_label'] = array(
'#type' => 'textfield',
'#title' => 'Contract Label',
'#default_value' => 'farmOS: ' . $area->name,
'#attributes' => array('readonly' => 'readonly'),
);
$form['instantiate_contract']['instantiate_address'] = array(
'#type' => 'hidden',
'#default_value' => '',
);
$form['instantiate_contract']['instantiate_tx'] = array(
'#type' => 'hidden',
'#default_value' => '',
);
$form['instantiate_contract']['instantiate_status'] = array(
'#type' => 'textfield',
'#title' => 'Status',
'#default_value' => '',
'#attributes' => array('readonly' => 'readonly'),
);
$form['instantiate_contract']['instantiate_contract'] = array(
'#type' => 'button',
'#value' => 'Instantiate Contract',
'#id' => 'instantiate-contract',
'#attributes' => array('onclick' => 'return (false);'),
);
$form['instantiate_contract']['submit_contract'] = array(
'#type' => 'submit',
'#value' => 'submit contract',
'#id' => 'submit-contract',
'#attributes' => array(
'class' => array('element-invisible'),
),
);
// Add option to link area to existing contract.
$form['link_contract'] = array(
'#type' => 'fieldset',
'#title' => 'Link Existing Contract',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['link_contract']['link_contract_address'] = array(
'#type' => 'textfield',
'#title' => 'Contract Address',
'#id' => 'link-contract-address',
);
$form['link_contract']['link_contract_submit'] = array(
'#type' => 'submit',
'#value' => 'Link contract',
'#submit' => array('regen_ecostate_oracle_instantiate_contract_link_form_submit'),
);
$config = array(
'mnemonic' => variable_get('regen_ecostate_oracle_mnemonic', ''),
'oracle' => variable_get('regen_ecostate_oracle_address', ''),
'httpUrl' => variable_get('regen_ecostate_oracle_http_url', ''),
'networkId' => variable_get('regen_ecostate_oracle_network_id', ''),
'gasPrice' => variable_get('regen_ecostate_oracle_gas_price', ''),
);
drupal_add_js(array(
'regen_ecostate_oracle' => array(
'clientOpts' => $config,
),
), array('type' => 'setting'));
drupal_add_js(drupal_get_path('module', 'regen_ecostate_oracle') . '/cosmwasm/dist/main.js', array('weight' => -100));
drupal_add_js(drupal_get_path('module', 'regen_ecostate_oracle') . '/js/instantiate_contract.js', array('weight'=> -100));
return $form;
}
/*
* Implements hook_form_submit().
*/
function regen_ecostate_oracle_instantiate_contract_form_submit($form, &$form_state) {
$area_id = $form_state['values']['area_id'];
$area = taxonomy_term_load($area_id);
$address = $form_state['values']['instantiate_address'];
$contracts = variable_get('regen_ecostate_oracle_area_contracts', array());
$contracts[$area_id] = array('address'=> $address);
variable_set('regen_ecostate_oracle_area_contracts', $contracts);
$tx = $form_state['values']['instantiate_tx'];
$ecostate = $form_state['values']['instantiate_ecostate'];
regen_ecostate_oracle_create_ecostate_observation_log($area, $tx, $ecostate);
$form_state['redirect'] = '/farm/ecostate/area/' . $area_id;
}
/*
* Implements hook_form_submit().
*/
function regen_ecostate_oracle_instantiate_contract_link_form_submit($form, &$form_state) {
$area_id = $form_state['values']['area_id'];
$address = $form_state['values']['link_contract_address'];
$contracts = variable_get('regen_ecostate_oracle_area_contracts', array());
$contracts[$area_id] = array('address'=> $address);
variable_set('regen_ecostate_oracle_area_contracts', $contracts);
$form_state['redirect'] = '/farm/ecostate/area/' . $area_id;
}
/*
* Helper function to create ecostate observation logs.
*/
function regen_ecostate_oracle_create_ecostate_observation_log($area, $tx, $ecostate) {
$ecostate_percent = substr($ecostate, 0, strlen($ecostate) - 2) . '.' . substr($ecostate, -2);
$measurements = array();
$measurements[] = array(
'measure' => 'ratio',
'value' => $ecostate_percent,
'units' => '%',
'label' => 'ecostate',
);
// Set log name.
$log_name = t('Update ' . $area->name . ' ecostate to ' . $ecostate_percent . '%');
// Create a new farm quantity log.
$log = farm_quantity_log_create('farm_observation', $log_name, REQUEST_TIME, TRUE, array(), $measurements);
// Get the log entity wrapper.
$log_wrapper = entity_metadata_wrapper('log', $log);
// Add the area reference.
$log_wrapper->field_farm_area[] = $area;
$log_wrapper->field_farm_notes->value->set('<a href="https://regen.wasm.glass/transactions/' . $tx . '">' . $tx . '</a>');
$log_wrapper->field_farm_notes->format->set('farm_format');
$tx_data = json_decode($tx);
$log_wrapper->field_farm_data->set(json_encode(array('response'=>$tx, 'transaction'=>$tx_data->transactionHash)));
//$log_wrapper->field_farm_data->format->set('json');
// Save the log (via its wrapper).
$log_wrapper->save();
return $log;
}
/*
* Helper function to get current ecostate value from in an area.
*/
function regen_ecostate_oracle_get_current_ecostate($area_id) {
// Build a query to find a log that references an asset.
$query = farm_quantity_log_query('ratio', 'ecostate', $done = TRUE, $type = 'farm_observation');
// Add a query tag to identify where this came from.
$query->addTag('regen_ecostate_oracle_log_query');
$query->join('field_data_field_farm_area', 'ss_fdffa', "ss_fdffa.entity_id = ss_log.id");
$query->where('ss_fdffa.field_farm_area_tid = ' . $area_id);
$query->range(0,1);
// Execute the query and gather the log ids.
$result = $query->execute();
$log_ids = array();
foreach ($result as $row) {
if (!empty($row->id)) {
$log_ids[] = $row->id;
}
}
$ecostate = 'Not set.';
if (!empty($log_ids)) {
$log = log_load($log_ids[0]);
$ecostate = farm_quantity_log_data($log, 'ratio', 'ecostate')[0]['value'];
}
return $ecostate;
}
/*
* Implements hook_form()
*
* Settings for the regen oracle.
*/
function regen_ecostate_oracle_settings_form($form, &$form_state) {
$form['regen_ecostate_oracle_http_url'] = array(
'#type' => 'textfield',
'#title' => 'Chain URL',
'#description' => 'Blockchain URL',
'#default_value' => variable_get('regen_ecostate_oracle_http_url', ''),
);
$form['regen_ecostate_oracle_network_id'] = array(
'#type' => 'textfield',
'#title' => 'Chain ID',
'#description' => 'Chain ID',
'#default_value' => variable_get('regen_ecostate_oracle_network_id', ''),
);
$form['regen_ecostate_oracle_gas_price'] = array(
'#type' => 'textfield',
'#title' => 'Gas Price',
'#description' => 'Gas Price',
'#default_value' => variable_get('regen_ecostate_oracle_gas_price', ''),
);
$form['regen_ecostate_oracle_address'] = array(
'#type' => 'textfield',
'#title' => 'Oracle Address',
'#description' => 'Public key of Oracle account',
'#default_value' => variable_get('regen_ecostate_oracle_address', ''),
'#maxlength' => 255,
);
$form['regen_ecostate_oracle_mnemonic'] = array(
'#type' => 'textfield',
'#title' => 'Oracle mnemonic',
'#description' => 'Used to generate the private key of oracle to sign txs',
'#default_value' => variable_get('regen_ecostate_oracle_mnemonic', ''),
'#maxlength' => 255,
);
return system_settings_form($form);
}