The Voting Power API from SDS

in #steem5 days ago

Previously, I have been using the SDS API to get the voting power:

https://sds1.steemworld.org/accounts_api/getAccount/justyy

However I found out that within 6 minutes, the voting power difference is 2% which doesn't seem correct. As far as I understand, in 24 hours, the VP is restored 20% so, 6 minutes is 0.08%

image.png

And steemchiller told me I should use the following instead - which returns a real time voting power. The one above doesn't get update until a vote is made.

https://sds0.steemworld.org/accounts_api/getAccountExt/justyy/null/upvote_mana_percent
https://sds0.steemworld.org/accounts_api/getAccountExt/justyy

Therefore, I have updated my functions (with simple retry logics).

PHP

function getVP($id, $retries = 3, $delaySeconds = 1) {
    $url = "https://sds0.steemworld.org/accounts_api/getAccountExt/$id/null/upvote_mana_percent";

    for ($i = 0; $i < $retries; $i++) {
        $ch = curl_init();

        curl_setopt_array($ch, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 5,
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curlError = curl_error($ch);
        curl_close($ch);

        if ($response === false || $httpCode !== 200) {
            error_log("Attempt " . ($i + 1) . ": HTTP Error $httpCode - $curlError");
            sleep($delaySeconds);
            continue;
        }

        $data = json_decode($response, true);

        if (isset($data['result']['upvote_mana_percent'])) {
            return floatval($data['result']['upvote_mana_percent']);
        } else {
            error_log("Attempt " . ($i + 1) . ": Malformed JSON or missing field");
            sleep($delaySeconds);
        }
    }

    return -1; // All retries failed
}

NodeJs

async function getVP(id, retries = 3, delayMs = 1000) {
  const url = `https://sds0.steemworld.org/accounts_api/getAccountExt/${id}/null/upvote_mana_percent`;

  for (let i = 0; i < retries; i++) {
    try {
      const response = await fetch(url);

      if (!response.ok) {
        console.error(`Attempt ${i + 1}: Response status was ${response.status}`);
        await new Promise(resolve => setTimeout(resolve, delayMs));
        continue;
      }

      const data = await response.json();
      const current = parseFloat(data.result.upvote_mana_percent);
      return current;
    } catch (error) {
      console.error(`Attempt ${i + 1}: Fetch error`, error);
      await new Promise(resolve => setTimeout(resolve, delayMs));
    }
  }

  return -1;
}

Steem to the Moon🚀!