-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Description**: added Cardano chain. **Ticket(s)**: #1 (closed but should be the actual one) and #16. **Contribution Details**: I used the [Cexplorer APIs](https://cexplorer.io/devs) to get the list of pools and staked amount. From Cexplorer, the Nakamoto coefficient is [something like 36](https://cexplorer.io/nakamoto). In the PR, anyway, the value you will get is expected to be higher (the last time I tried was 112) as the single pools are used instead of the [groups](https://cexplorer.io/groups); that is what they used (confirmed by an admin on Discord).
- Loading branch information
1 parent
f8d764e
commit 67b8b7c
Showing
4 changed files
with
121 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package chains | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"net/http" | ||
"sort" | ||
|
||
utils "github.com/xenowits/nakamoto-coefficient-calculator/core/utils" | ||
) | ||
|
||
type CardanoResponse struct { | ||
Label string `json:"label"` | ||
Class string `json:"class"` | ||
Epoch int `json:"epoch"` | ||
Stake float64 `json:"stake"` | ||
} | ||
|
||
func Cardano() (int, error) { | ||
url := "https://www.balanceanalytics.io/api/mavdata.json" | ||
|
||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
log.Println("Error creating request:", err) | ||
return 0, err | ||
} | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println("Error making request:", err) | ||
return 0, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var responseData struct { | ||
ApiData []CardanoResponse `json:"api_data"` | ||
} | ||
err = json.NewDecoder(resp.Body).Decode(&responseData) | ||
if err != nil { | ||
log.Println("Error decoding JSON:", err) | ||
return 0, err | ||
} | ||
|
||
var votingPowers []big.Int | ||
for _, data := range responseData.ApiData { | ||
stakeInt := big.NewInt(int64(data.Stake)) | ||
votingPowers = append(votingPowers, *stakeInt) | ||
} | ||
|
||
// need to sort the powers in descending order since they are in random order | ||
sort.Slice(votingPowers, func(i, j int) bool { | ||
return votingPowers[i].Cmp(&votingPowers[j]) > 0 | ||
}) | ||
|
||
// Calculate total voting power | ||
totalVotingPower := utils.CalculateTotalVotingPowerBigNums(votingPowers) | ||
|
||
// Calculate Nakamoto coefficient | ||
nakamotoCoefficient := utils.CalcNakamotoCoefficientBigNums51(totalVotingPower, votingPowers) | ||
|
||
fmt.Println("The total voting power for Cardano is: ", totalVotingPower) | ||
fmt.Println("The Nakamoto coefficient for Cardano is: ", nakamotoCoefficient) | ||
|
||
// Return Nakamoto coefficient | ||
return nakamotoCoefficient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters