Skip to content

Instantly share code, notes, and snippets.

@loon3
Last active July 8, 2019 18:48
Show Gist options
  • Save loon3/29606b05880b05cf0d64697b5fa6cd16 to your computer and use it in GitHub Desktop.
Save loon3/29606b05880b05cf0d64697b5fa6cd16 to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: application/json');
require 'includes/Client.php';
use JsonRPC\Client;
$cp_server = 'http://public.coindaddy.io:4000/api/';
$cp_user = 'rpc';
$cp_password = '1234';
$client = new Client($cp_server);
$client->authentication($cp_user, $cp_password);
$currentStatus = $client->execute('get_running_info');
$currentBlock = $currentStatus["last_block"]["block_index"];
$startBlock = 583350;
$endBlock = 585350;
$broadcasts = $client->execute('get_broadcasts', array('start_block' => $startBlock, 'end_block' => $endBlock));
$addresses_parsed = array();
for($i=0; $i < count($broadcasts); $i++){
$addresses_parsed[$i] = $broadcasts[$i]["source"];
}
$filters = array(array('field' => 'asset', 'op' => 'IN', 'value' => array("XCP")),array('field' => 'address', 'op' => 'IN', 'value' => $addresses_parsed));
$balances = $client->execute('get_balances', array('filters' => $filters, 'filterop' => "AND"));
$balancesByAddr = array();
for($i=0; $i < count($balances); $i++){
$balancesByAddr[$balances[$i]["address"]] = $balances[$i]["quantity"];
}
$votes = array();
for($i=0; $i < count($broadcasts); $i++){
$broadcastText = $broadcasts[$i]["text"];
$candidateList = array("XCPELECTION2019 1","XCPELECTION2019 2","XCPELECTION2019 3","XCPELECTION2019 4","XCPELECTION2019 5","XCPELECTION2019 6","XCPELECTION2019 8");
for($j=0; $j < count($candidateList); $j++){
if($broadcastText == $candidateList[$j]){
if (!isset($votes[$candidateList[$j]])) {
$votes[$candidateList[$j]] = 0;
}
$value = $broadcasts[$i]["value"] * 100000000;
if($value <= $balancesByAddr[$broadcasts[$i]["source"]]){
$balancesByAddr[$broadcasts[$i]["source"]] -= $value;
$votes[$candidateList[$j]] += $value;
}
}
}
}
foreach ($votes as $key => $value) {
$votes[$key] = $value / 100000000;
}
$jsonarray = array('block' => $currentBlock, 'results' => $votes);
echo json_encode($jsonarray);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment