Proposals Watcher

  1. Scan for new Upgrade proposals once every 30 minutes

Code is in PHP

This code scans for new upgrade proposals once every 30 minutes and outputs the data to a .json file (example - gov_proposals.json)

<?php

$jsonFilePath = '/var/www/html/gov/gov_proposals.json';
$jsonContents = file_get_contents($jsonFilePath);
$existingProposals = json_decode($jsonContents, true);
$final = $existingProposals;

$chains = [ 
    "axelar",
    "acrechain",
    "canto",
    "kujira",
    "mars",
    "meme",
    "migaloo",
    "oraichain",
    "planq",
    "provenance",
    "stride",
    "tenet",
    "terra2",
    "haqq"
];

foreach ($chains as $chain) {

    $api = "https://rest.cosmos.directory/". $chain . "/cosmos/gov/v1beta1/proposals";
    $rpc = "https://rpc.cosmos.directory/". $chain;

    $ch = curl_init($api . '?proposal_status=2');

    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $resp = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($resp, true);

    if($result['code'] === 2)
    {
        $api = "https://rest.cosmos.directory/". $chain . "/cosmos/gov/v1/proposals";
        $rpc = "https://rpc.cosmos.directory/". $chain;
    
        $ch = curl_init($api . '?proposal_status=2');
    
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
        $resp = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($resp, true);
    }

    foreach ($result['proposals'] as $proposal) {
        $pid = $proposal['proposal_id'];
        if($pid === null) $pid = $proposal['id'];
        $type = $proposal['content']['@type'];

            $newProposal = [
                "chain" => ucfirst($chain),
                "pid"   => $pid,
                "rpc"   => $rpc,
                "api"   => $api . '/' . $pid,
            ];

            $exists = false;
            foreach ($existingProposals as $existingProposal) {
                if ($existingProposal['chain'] === $newProposal['chain'] && $existingProposal['pid'] === $newProposal['pid']) {
                    $exists = true;
                    break;
                }
            }

            if (!$exists) {
                $final[] = $newProposal;
            }
    }
}

echo json_encode($final, JSON_UNESCAPED_SLASHES);

?>

Create a .sh file called ``gov_proposals.sh``

#!/bin/bash

/usr/bin/php /var/www/html/gov_proposals.php > /var/www/html/gov_proposals2.json
/usr/bin/cat /var/www/html/gov_proposals2.json > /var/www/html/gov_proposals.json

Setup a cronjob to run the above code once every 30 mins.

*/30 * * * *    /usr/bin/sh /var/www/html/gov_proposals.sh

Now, gov_proposals.json will contain the following:

[{"chain":"Kujira","pid":"447","rpc":"https://rpc.cosmos.directory/kujira","api":"https://rest.cosmos.directory/kujira/cosmos/gov/v1beta1/proposals/447"},{"chain":"Mars","pid":"121","rpc":"https://rpc.cosmos.directory/mars","api":"https://rest.cosmos.directory/mars/cosmos/gov/v1/proposals/121"}]

  1. Update Status & Current Block number once every 5 Minutes

Read the gov_proposals.json file once every 5 minutes, update the status of the proposal, and update current block number of the blockchain.

Code is in PHP.

<?php

// Constants
const JSON_FILE_PATH = '/var/www/html/gov/gov_proposals.json';

// Fetch and decode JSON data
$jsonContents = file_get_contents(JSON_FILE_PATH);
$proposals = json_decode($jsonContents, true);

$voterMapping = [
    "stride" => "stride1lcgzkqstk4jjtphfdfjpw9dd9yfczyzmdjhmhv",
    //... [other mappings]
];

function fetchData($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

$finalData = [];

foreach ($proposals as $proposal) {
    $chain = strtolower($proposal['chain']);

    $voteData = fetchData($proposal['api'] . "/votes" . "/" . $voterMapping[$chain]);
    $voted = ($voteData['vote']['voter'] === $voterMapping[$chain]) ? "Yes" : "No";
    if (isset($voteData['code']) && $voteData['code'] === 3) {
        $voted = "No";
    }

    $proposalData = fetchData($proposal['api']);
    $status = ($proposalData['proposal']['status'] === "PROPOSAL_STATUS_VOTING_PERIOD") ? "Voting period" : "Closed";

    $rpcData = fetchData($proposal['rpc'] . "/status?");
    $chain_id = $rpcData['result']['node_info']['network'] ?? $rpcData['node_info']['network'];

    if ($status === "Voting period") {
        $finalData[] = [
            "chain" => $proposal['chain'],
            "chain_id" => $chain_id,
            "pid" => $proposal['pid'],
            "status" => $status,
            "voted" => $voted
        ];
    }
}

echo json_encode($finalData, JSON_PRETTY_PRINT);

?>

Create a .sh file called ``gov_updates.sh``

#!/bin/bash

/usr/bin/php /var/www/html/gov_updates.php > /var/www/html/gov_updates2.json
/usr/bin/cat /var/www/html/gov_updates2.json > /var/www/html/gov_updates.json

Setup a cronjob to run the above code once every 5 mins.

*/5 * * * *    /usr/bin/sh /var/www/html/gov_updates.sh

Last updated